SDL Game Design Help

closed account (N36fSL3A)
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

1
2
3
4
5
void OnQuit()
{
   Enemy1::SDL_FreeSurface(EnemySprite);
   Enemy2::SDL_FreeSurface(EnemySprite);
}


================================================================================

Is there a cleaner way to end SDL than clearing a surface in one function with subfunctions for example I can do this:
1
2
3
4
5
6
7
8
9
void FreeEnemySprites()
{
   SDL_FreeSurface(EnemySprite);
   SDL_FreeSurface(EnemyWeapon);
}
void OnQuit()
{
   void FreeEnemySprites();
}


================================================================================

I'm also trying to make a grid for a map that doesn't snap me to a fixed coordinate. For example I can go between two coordinates. Like:

1
2
3
4
5
#include "main.h"


void show_playerPosition(x,y); 


1
2
Output:
Player position is x(1.5), y(2.0)
Last edited on by Fredbill30
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.
closed account (N36fSL3A)
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.
Vectors are definitely not useless, I wouldn't consider anything in the STL useless. http://www.cplusplus.com/reference/stl/vector/

You can create a vector of enemies like
std::vector<Enemy> enemies;
and add to the vector with enemies.push_back(Enemy());
closed account (N36fSL3A)
You can make an object vector??? And do you know any way to work on my problem about maps?

EDIT: So, "enemies" is the object, and enemies.push_back(Enemy()); would add another "Enemy"?
Last edited on by Fredbill30
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.
I wouldn't consider anything in the STL useless


std::stack is pretty useless....
closed account (N36fSL3A)
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).
closed account (N36fSL3A)
Any good designs for Resource Managers?
I often do something like this:


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.

It's also very easy to implement.
closed account (N36fSL3A)
But SDL_Surface* is a pointer to a struct... so would this be more appropiate?:

1
2
3
4
void myFunction(char* filename)
{
  SDL_Surface* filename;
}


or

1
2
3
4
void myFunction(std::string filename)
{
  SDL_Surface* filename;
}



or can you not use variable values for pointer names? If you can, how would you do it?
Last edited on by Fredbill30
No that does not work. That just makes a variable named 'filename'. It does not take the name from your string variable with the same name.

What you want is a map:

1
2
3
4
5
std::map< std::string, SDL_Surface*> mymap;

//...

SDL_Surface* x = mymap["filename.bmp"];
closed account (N36fSL3A)
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?
Last edited on by Fredbill30
Topic archived. No new replies allowed.