Page 1 of 1

Loading Images in SDL and Scope

Posted: Wed Mar 04, 2009 11:02 pm
by Maevik
So I'm working my way through Lazy Foo's SDL tutorial and I found something in the code that suprised me. Hopefully someone can clear this up for me.

In his tutorials he usually creates global variables for images, which is fine, but since I am still learning I'd like to stick to OO design as much as possible. He loads all the surfaces with images using a function like:

Code: Select all

bool load_files()
{
    //Load the image
    image = load_image( "x.png" );
	background = load_image( "background.png" );
	message = load_image( "dude.png");
	dots = load_image( "preview.png" );
    
    //If there was an error in loading the image
    if( image == NULL )
    {
        return false;    
    }
    
    //If everything loaded fine
    return true;    
}
So if I wanted to make something similar in OO fasion, creating the variables in the functions in which I will use them, should I just load them after making them instead of creating a seperate function to handle it?

I realize I wouldn't have the function to test whether everything loaded ok, but I guess I could make a function like:

Code: Select all

bool didItLoad(SDL_Surface* tester)
{
     if( tester == NULL)
     {
          return false;
     }
     return true;
}

Re: Loading Images in SDL and Scope

Posted: Thu Mar 05, 2009 3:59 am
by programmerinprogress
I tend to make all of my SDL_Surfaces* private to the class they're used in, then I use accessor functions to change what the SDL_Surface Pointers point to.

The only exception to this rule, is I keep the main screen surface global(but theres nothing that says you can't stick the screen into a class, and write some accessor functions)

Heres an example of how I would implement an image loading function, loading a spritesheet into an object with an SDL_Surface*

Code: Select all

// sprite.h
class Sprite
{
 private:
  SDL_Surface* Image; 
 public:
  void Load_Sprite(const char* Filename); 
  Sprite(){Image = NULL ;}
};
//sprite.cpp
void Sprite::Load_Sprite(const char* Filename)
{
  if(Image != NULL)
  { 
   SDL_FreeSurface(Image); 
   Image = NULL;
  }
 Image = Load_Image(Filename); // I'm just using the Load_Image function from lazy foo,
 //this is just implied so I don't have to type it out  :P
// I implement it as a library function, so it's global, but you don't have to write it again and again 
// you could make it a class method, but whatever :-)
}


I'm not an OO expert, but my main aim is to keep everything managed and encapsulated, I try and prevent un needed access (when you thnk about it, do you really need other objects to access an SDL_Surface*? if you do all of your drawing within the object, then I don't see any point)

Hope this helped

Re: Loading Images in SDL and Scope

Posted: Thu Mar 05, 2009 5:52 am
by Ginto8
Well, I can't say I necessarily have good style, but I hold all the SDL_Surface*'s in my Window class, and then my Sprite class (which is for all images, not just sprites) has another SDL_Surface which I allign with the correct surface in the Window class. I probably explained it horribly, but that's how I do it. For the Window class I am currently using a map, but I will be switching to a map-like linked list soon. ;)

Re: Loading Images in SDL and Scope

Posted: Thu Mar 05, 2009 6:40 am
by ismetteren
I put all my images into a seperate class.

Then when i wont an image, i just calls:

Sprites.getImage(path);

the class will then check if the images already is loaded into memory, or if it will have to do it.

Re: Loading Images in SDL and Scope

Posted: Thu Mar 05, 2009 4:16 pm
by Maevik
Thanks guys, all of this was really helpful.

I think I'm gonna have screen be a global for a few programs, then start putting that into an object also.

Re: Loading Images in SDL and Scope

Posted: Thu Mar 05, 2009 5:02 pm
by MarauderIIC
I usually have a Renderer class that has a vector of textures and a separate Screen* for the main screen.