The Enemy class inherits from the Object class and uses the Object class’s constructor to load the image…
Object::Object(SDL_Surface *imageFile,int x, int y, int w, int h)
{
image = imageFile;
}
Since I want the level class to handle loading and removing images, I’ve left the destructor empty for both the Object and Enemy classes…
The level class initializes SDL_Surface for the enemy image and loads the image into it…
class Level : public StateManager
{
private:
SDL_Surface *enemyImage;
}
Level::Level(int levelNo)
{
enemyImage = loadImage("image/enemy.png");
}
Then, in the level update function, the enemy image is passed to the Enemy object when its pushed onto the enemy vector…
enemy.push_back(Enemy(enemyImage, 640, 200, 32, 32));
So, my question is if when the image loaded from the Level object is passed to an Enemy object, is it creating a new instance of it in memory, or is it pointing to the one loaded in the level?
All you’re doing is copying the pointer to the
SDL_Surfaceobject; the object itself, and thus the image data, is not reproduced. You should be careful about management of shared resources. In particular, you should ensure that you only callSDL_FreeSurface()once for eachSDL_Surfaceyou load.