Lazy foo's tutorial 02

I would like to make the following code either part of a class or available to be used by a class, this code is part of lazy foo’s tutorial #2, any ideas?

Code:

SDL_Surface load_image( std::string filename )
{
SDL_Surface
load_image( 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 = SDL_LoadBMP( filename.c_str() );

//If nothing went wrong in loading the image

if( loadedImage != NULL )

{

    //Create an optimized image

    optimizedImage = SDL_DisplayFormat( loadedImage );

    //Free the old image

    SDL_FreeSurface( loadedImage );

}

//Return the optimized image

return optimizedImage;

}

How much class design have you done before? Something like this comes to my
mind:
class Sprite
{
private:
SDL_Surface* image;
public:
float x, y;
Sprite()
: image(NULL), x(0), y(0)
{}
Sprite(SDL_Surface* image)
: image(image), x(0), y(0)
{}

bool load_image(const string& filename)
{
SDL_Surface* temp = SDL_LoadBMP(filename.c_str());
if(temp == NULL)
return false;
SDL_FreeSurface(image);
image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
return (image != NULL);
}

void draw(SDL_Surface* screen)
{
SDL_Rect dest = {x, y, 0, 0};
SDL_BlitSurface(image, NULL, screen, &dest);
}
};

Jonny DOn Tue, Apr 20, 2010 at 9:20 AM, cndr.mike <cndr.backup at yahoo.com> wrote:

I would like to make the following code either part of a class or
available to be used by a class, this code is part of lazy foo’s tutorial
#2, any ideas?

Code:

SDL_Surface load_image( std::string filename )
{
SDL_Surface
load_image( 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 = SDL_LoadBMP( filename.c_str() );

//If nothing went wrong in loading the image

if( loadedImage != NULL )

{

    //Create an optimized image

    optimizedImage = SDL_DisplayFormat( loadedImage );

    //Free the old image

    SDL_FreeSurface( loadedImage );

}

//Return the optimized image

return optimizedImage;

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org