#include "SDL/SDL.h" #include "SDL/SDL_image.h" #include class Sprite { int x, y; bool redraw; SDL_Surface *surface; public: void SetPos( int newX, int newY ) { x = newX; newY = newY; redraw = true; } bool SetMaterial( std::string ); void Draw( SDL_Surface* ); void FreeSurface(); Sprite(); Sprite( std::string ); ~Sprite(); }; Sprite::Sprite() {}; Sprite::Sprite( std::string filename ) { SetMaterial( filename ); }; Sprite::~Sprite() { FreeSurface(); } bool Sprite::SetMaterial( std::string filename ) { //Temporary storage for the image that's loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = IMG_Load( filename.c_str() ); //If nothing went wrong in loading the image... if( loadedImage != NULL ) { //Create an optimized image, with alpha if it has it. if( loadedImage->flags & SDL_SRCALPHA ) optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); else optimizedImage = SDL_DisplayFormat( loadedImage ); // Free the old image. SDL_FreeSurface( loadedImage ); surface = optimizedImage; return true; } // Something went wrong. return false; } void Sprite::Draw( SDL_Surface *destination ) { //Make a temporary rectangle to hold the offsets. SDL_Rect offset; //Give the offsets to the rectangle. offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( surface, NULL, destination, &offset ); } void Sprite::FreeSurface() { SDL_FreeSurface( surface ); }