[SDL] Lazy foo's tutorial 02

Jonathan Dearborn grimfang4 at gmail.com
Wed Apr 21 06:47:20 PDT 2010


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 D


On 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100421/511d929c/attachment.htm>


More information about the SDL mailing list