Skip to main content
gamedev.net
🔒 Locked

SDL_Surface question

Started by ForD Mar 15, 2007 at 6:15 AM 6 replies 1.5k views
Original Post
ForD
ForD
In my SDL program, I delcare some surfaces at the beginning: SDL_Surface *background = NULL; SDL_Surface *foo = NULL; SDL_Surface *screen = NULL; Then later, I have a fuction that puts images on these surfaces:

bool load_files()
{
    //Load the background image
    background = load_image( "background.png" );

    //If the background didn't load
    if( background == NULL )
    {
        return false;
    }

    //Load the stick figure
    foo = load_image( "foo.png" );

    //If the stick figure didn't load
    if( foo == NULL )
    {
        return false;
    }

    return true;
}
Right now, I'm in the processes of rewriting the above function so I don't have to do an if statment for each and every statement.

bool load_files2(std::string filename, "SDL_Surface as parameter here")
{
    //Load the background image
    "SDL_Surface here" = load_image( filename );

    //If the background didn't load
    if( background == NULL )
    {
        return false;
    }

    return true;
}
My problem is this, how can I declare that the second parameter will be an SDL_Surface?
Rakshasa
Rakshasa
bool load_files2(std::string filename, SDL_Surface *surf){    //Load the background image    surf = load_image( filename );    //If the background didn't load    if( surf == NULL )    {        return false;    }    return true;}


I think... XD Not enough coffee yet.
Doesn't matter how much you lose as long as you make sure the winner loses enough blood.
Abraham
Abraham

You need to pass a pointer to the pointer of the surface otherwise you're manipulating the wrong thing.

bool load_files2(std::string filename, SDL_Surface **surf){    //Load the background image    *surf = load_image( filename );    //If the background didn't load    if( surf == NULL )    {        return false;    }    return true;}

It's all about the Tea.

Abraham
Jack Sotac
Jack Sotac
Quote:
Original post by Abraham
    if( surf == NULL )
should be
     if(*surf == NULL )

Milk does the body good :)
0xa0000000
ForD
ForD
Thanks guys, I have one more problem related to this, when I use the function ins int main(): load_files2("background.png", *background) I get a compiler error that says "cannot convert." Any ideas on what I'm doing wrong?
rip-off
rip-off
To pass an SDL_Surface pointer to that function, you'll need to obtain its address using the "&" operator:

SDL_Surface *someSurface;load_files2("somefile.bmp",&someSurface);


However, instead of using SDL_Surface ** you can use references instead. References to pointers are a bit ugly, but with use of typedefs they can be manageable ( we can pass the string by reference too ):
typedef SDL_Surface *SDL_SurfacePtr;typedef SDL_SurfacePtr &SDL_SurfaceRef;bool load_file( const std::string &filename, SDL_SurfaceRef surface ){    assert( surface == NULL );    surface = SDL_LoadBMP( filename.c_str() );    return ( surface != NULL );}
Chad Smith
Chad Smith
I also noticed you had load_files2. Just go ahead and use function overloading and call it load_files.


Chad.

ForD
ForD
Wellit was just an update version of load_files, which was going to replace it anyway. So now that it compiles and works just fine, I deleted load_files.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.