Sorry if you're being annoyed by my frequent posts (I kinda get confused from SDL tutorials) but I'm posting again... this time it's with game design.
Well, I'm trying to design a class called class Enemy . For class enemy I want to figure out a way to make an unlimited amount of enemies in a class without a surface interfering with another in SDL_FreeSurface(); ?
For Example:
1 2 3 4
class Enemy
{
SDL_Surface* EnemySprite;
}Enemy1, Enemy2;
And
1 2 3 4
void OnQuit()
{
SDL_FreeSurface(EnemySprite);
}
^Would This interfere with one another or can I do this
I don't know SDL, but it would make sense to me to create the texture then pass a reference to the Enemy ctor when creating enemies. As to the unlimited enemies, you can push them into a std::vector.
How would I use vectors??? I heard of them once, but never bothered with them as I thought they were useless. I'm starting to rethink their usefulness.... thanks in advance.
I'm not sure how you are using the grid, but if you want to have a grid represent say tiles in a tile map you should be able to calculate the grid cell based on the character position and the character size. You may need to elaborate on what you want to do exactly.
So, "enemies" is the object, and enemies.push_back(Enemy()); would add another "Enemy"?
enemies is a vector of Enemy objects (and is an object of type vector itself) And yes, that adds a new Enemy to the vector. A vector is basically just a re-sizable array so you can add as many enemies as you want.
But when I am trying to use SDL_FreeSurface(); How would I clear the surface to avoid memory leaks without it interfering? Do you have any pseudo code that I could use as a reference?
Each enemy should not own their own surface. It will waste tons of memory because you will have the same image loaded in dozens of different surfaces. It also is a waste of CPU time because loading surfaces is expensive.
You should use a resource manager.
The idea is, you have one central class which owns all surfaces. Other classes like your enemy class will simply get the desired surface from the manager. That way all enemies can share the same surface. Also, because enemies don't own the surface, they don't have to clean anything up (only the resource manager has to do cleanup).
1) Have a std::map to keep track of your images. Use a string key, where the string is the filename of the image. ie: std::map< std::string, SDL_Surface* > yourmap;
2) Have a function that takes a filename as a paramter and returns an SDL_Surface*. That function can be called by enemies and whatever other parts of your program need an image. Basically they call this function to load a surface rather than actually loading the surface.
3) This function will take the filename, and check the map. If that filename is already in the map, just return the image that is already loaded. Otherwise, load the image, put it in the map, then return it.
4) You probably only need to unload images at shutdown. Unless you have a TON of image files and you don't want them all loaded at once.
The idea is files are loaded as needed, and you never have to load the same file more than once.
Thanks Dish! Would you happen to know how to make a projectile system? If so, do you have some pseudo code for one? Also, wouldn't it be easier to put SDL_FreeSurface(); into the destructor of a class making it easier to handle with the vector? Or is the std::map better?