[C++] Where to load global images/sprites?

Started by
6 comments, last by boogyman19946 13 years, 2 months ago
Hello, first post on this community.

I've started learning more and more about C++ in college and decided to finally sit down and try to put some of these concepts together from what I ahve learned in class. My question is this, when creating a basic ship Class (for the player of an arcade game) who owns the sprite? I don't really want each ship to own a copy of the same sprite as that seems redundatn. Instead I think I would like to make some of the resources global so only one sprite needs to be loaded into memory.

How are shared resources loaded in C++ usually? I'm using SFML which allows me to create sprites.
The big question, if many ships share the same sprite how do they access the loaded sprite? Do I make the sprite a constant global variable in the definition of the Ship class?
I've included the SFML library inside my ship header file so I can use functions from that library. Now should all resources used by the Ship class also be loaded in the definition of the Ship? If so how is this best done. I'm used to using Blitzmax and C++ gives the programmer many many options on how to do things.
Advertisement
I would suggest implementing an asset manager. Simply create a singleton instance of this asset manager in your main game space. Then you can load sprites, animations, tile sets and so forth into the asset manager, store them as named objects themselves. Then from your ship objects you can simply use a reference to the single asset manager. From there just return you sprites by name, frame number or however you wish to organize your content. I can whip up a little example for you if you need a little more help with the concept. Shoot me an E-Mail and let me know.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

hey Ryan! congrats on your first post and learning C++... I agree with Dan's solution (which is certainly the right way to go), but I would suggest that you stick to your intuition... why not just load the sprite globally (not as a constant in the class but in your whole application) and then have each Ship just store a pointer to it? it's quick and easy (and also very bad) but now you have the opportunity to learn why it's the wrong way. remember, a singleton is a kind of global variable, like a monkey dressed up in a suit... Good luck!

hey Ryan! congrats on your first post and learning C++... I agree with Dan's solution (which is certainly the right way to go), but I would suggest that you stick to your intuition... why not just load the sprite globally (not as a constant in the class but in your whole application) and then have each Ship just store a pointer to it? it's quick and easy (and also very bad) but now you have the opportunity to learn why it's the wrong way. remember, a singleton is a kind of global variable, like a monkey dressed up in a suit... Good luck!




Thanks, I asked Dan to send me an example. Now just to get my code strait. Considering my options here, how do I define a global variable? Where does it get defined inside the class or externally? I'm unsure of the syntax as they have not covered them in my classes too extensively. I understand that globals are usually in bad form, but this is a small project which will probably not have reusable objects.


'sdaq' said:

hey Ryan! congrats on your first post and learning C++… I agree with Dan's solution (which is certainly the right way to go), but I would suggest that you stick to your intuition… why not just load the sprite globally (not as a constant in the class but in your whole application) and then have each Ship just store a pointer to it? it's quick and easy (and also very bad) but now you have the opportunity to learn why it's the wrong way. remember, a singleton is a kind of global variable, like a monkey dressed up in a suit… Good luck!




Thanks, I asked Dan to send me an example. Now just to get my code strait. Considering my options here, how do I define a global variable? Where does it get defined inside the class or externally? I'm unsure of the syntax as they have not covered them in my classes too extensively. I understand that globals are usually in bad form, but this is a small project which will probably not have reusable objects.




you might find this thread interesting: http://www.gamedev.n...anager-classes/

a global variable is probably the easiest variable to declare. it can be as simple as this:


std::map <std::String, Sprite *> spriteMap;

int main (int argc, char ** argv)
{
// manipulate global spriteMap here
return 0;
}


hope that helps!

I would suggest implementing an asset manager. Simply create a singleton instance of this asset manager in your main game space. Then you can load sprites, animations, tile sets and so forth into the asset manager, store them as named objects themselves. Then from your ship objects you can simply use a reference to the single asset manager. From there just return you sprites by name, frame number or however you wish to organize your content. I can whip up a little example for you if you need a little more help with the concept. Shoot me an E-Mail and let me know.


Can I just say that, while I don't find singletons bad when used correctly, don't use the singleton design pattern while you are learning. There are plenty of articles on google for why not to, but if you are learning C++ the best answer is to not form bad habits.

edit: here's a quote I found that kind of explains my opinion on the matter, "The apprentice uses it without thinking. The journeyman avoids it without thinking. The master uses it thoughtfully."
Thanks for all the help guys. This has given quite a bit to put into practice.
I'm going to implement these concepts the next time I get a break in my University schedule.
Couldn't the resource possibly be declared as a static variable for the class? That way all classes that need access to the resources, do have it, and those that don't need access, don't have it.

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement