I have setup a small shooter game as a tutorial for myself in SDL. I have a struct of a projectile
struct projectile
{
SDL_Surface* surface;
int x;
int y;
};
And I put that into a vector.
vector<projectile> shot;
projectile one_shot;
And when I press space I create a new projectile and add it to the vector and then they re blitted when they re rendered.
This works fine, but I m in seemingly random cases getting a "program has stopped working" error.
So I m wondering what is the proper way to free the surfaces.
- Do I free them all afterwards?
- Do I free each individual shot when it exits the screen?
- Or some other choice?
UPDATE:
I have found where it crashes when I quit, when I have fired a few shots and they have all exited the screen. I have tried replacing the code that adds the surface to the vector with the "proper way to duplicate" as seen in this example, and it still behaves in the same way.
This is how I free the surface.
if(shot.at(i).y < 0 - shot.at(i).surface->h)
{
SDL_FreeSurface(shot.at(i).surface);
shot.erase(shot.begin() + i);
}
Anyone have an idea or some sample code I can look at to figure this out.