/** * Recreates the surface with a new size. * Old contents will not be altered, and may be * cropped to fit the new size. * @param width Width in pixels. * @param height Height in pixels. */ void Surface::resize(int width, int height) { // Set up new surface Uint8 bpp = _surface->format->BitsPerPixel; int pitch = GetPitch(bpp, width); void *alignedBuffer = NewAligned(bpp, width, height); SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(alignedBuffer, width, height, bpp, pitch, 0, 0, 0, 0); if (surface == 0) { throw Exception(SDL_GetError()); } // Copy old contents SDL_SetColorKey(surface, SDL_SRCCOLORKEY, 0); SDL_SetColors(surface, getPalette(), 0, 255); SDL_BlitSurface(_surface, 0, surface, 0); // Delete old surface DeleteAligned(_alignedBuffer); SDL_FreeSurface(_surface); _alignedBuffer = alignedBuffer; _surface = surface; _clear.w = getWidth(); _clear.h = getHeight(); }
/** * Loads the contents of an image file of a * known format into the surface. * @param filename Filename of the image. */ void Surface::loadImage(const std::string &filename) { // Destroy current surface (will be replaced) DeleteAligned(_alignedBuffer); SDL_FreeSurface(_surface); _alignedBuffer = 0; _surface = 0; // SDL only takes UTF-8 filenames // so here's an ugly hack to match this ugly reasoning std::string utf8 = Language::wstrToUtf8(Language::fsToWstr(filename)); // Load file _surface = IMG_Load(utf8.c_str()); if (!_surface) { std::string err = filename + ":" + IMG_GetError(); throw Exception(err); } }
/** * Deletes the surface from memory. */ Surface::~Surface() { DeleteAligned(_alignedBuffer); SDL_FreeSurface(_surface); }