What should I do with enemy after it is killed? (C++ and SDL)

Started by
7 comments, last by yurian 16 years ago
Hi, i need some help here, I'm an extreme beginner. I'm making a game with SDL, and in it you shoot enemies, and when you shoot them enough times they die. My program can recognize when the enemy dies and I have it so that it only blits the enemy if they are alive, so that once they die their surface isn't being blitted anymore and it looks like they're gone. of course, even though you can't see them, the surfaces are still there, and they're causing my game to lag. How do I actually get rid of the surface completely when they get killed? Someone mentioned freesurface but i didnt understand how and where to use it. I have all my surfaces and rectangles being declared at the start of my .cpp file. I will also need to use this same surface on all the other levels of the game. What do I do with the surface when it isnt being used? Thanks!
Advertisement
what data structure are you using to represent your enemies?
SDL_Surface *soldier1

for the surface, and then

SDL_Rect *soldier1_info

for the rectangle, if thats what you mean by data structure.
Remember that you probably don't need a separate surface for each enemy if you are using the same graphics for both.
i load the same bmp to both surfaces... but i could have 2 enemies on one screen using one surface?
yes, i don't use SDL so i dont know what command you are using for blitting or whatever, but theres no reason to have the same data loaded twice.
Quote:i load the same bmp to both surfaces... but i could have 2 enemies on one screen using one surface?

Yes. How you go about doing it is up to you (global surfaces, pointers, arrays, whatever) but when you blit a surface (I'm assuming you have a separate function or class to do this. If not, you should) you should include an argument that specifies which surface to blit.

If you haven't already, i strongly recommend you check out this site.

To answer your question about SDL_FreeSurface, what it essentially does is erases a surface from memory. It's commonly used to clean up when a game is ending, or when a surface is no longer needed (for instance, once it's been copied to another surface). In your specific case, you really shouldn't be saving the same surface multiple times when you can save it once and use it multiple times, but it would be used when an enemy dies to erase that enemies' surface.
I found where your issue is

if (es1right == 1 && soldier1 != SDL_LoadBMP("graphics/esright.bmp") && es1dani == 10 && es1ducking == false && es1active == true)

Every time you run an if statement like this it loads graphics/esright.bmp and it just remains in memory, my question is why you're doing this among a whole bunch of other questions, so all I'm going to say is check your e-mail
Do you mean destructors?
And delete?

This topic is closed to new replies.

Advertisement