Loading Images in SDL and Scope

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Loading Images in SDL and Scope

Post 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;
}
My love is like a Haddoken, it's downright fierce!
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Loading Images in SDL and Scope

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Loading Images in SDL and Scope

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Loading Images in SDL and Scope

Post 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.
Image ImageImage Image
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: Loading Images in SDL and Scope

Post 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.
My love is like a Haddoken, it's downright fierce!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Loading Images in SDL and Scope

Post by MarauderIIC »

I usually have a Renderer class that has a vector of textures and a separate Screen* for the main screen.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply