Jack Wilsdon
Jack Wilsdon

Reputation: 7025

Loading images using custom function fails in SDL

My script is failing to run, as it opens a window then closes it. It fails at file loading. The script works on my laptop but not my desktop PC, and all of the neccecary files are there. It's to do with this function;

This is my function load_image();

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormatAlpha( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;

}

And I call it like this;

sprite = load_image( "sprites.png" );

if( sprite == NULL )
{
    return false;
}

The only problem is, it always returns false, EVEN THOUGH the file is in there. The problem is, this code doesn't return false on my laptop!

Upvotes: 0

Views: 906

Answers (1)

Alink
Alink

Reputation: 394

All seems fine, try to use IMG_GetError() to diagnose this. Here's code from the SDL doc showing how to:

// load sample.png into image
SDL_Surface *image;
image=IMG_Load("sample.png");
if(!image) {
    printf("IMG_Load: %s\n", IMG_GetError());
    // handle error
}

Upvotes: 2

Related Questions