I'm attempting to (re)learn C++ by making a top-down shooter game using SDL. In my game, I have created a Graphics
class that handles all of the drawing, textures, etc.
The game is in its very early stages, and at the moment the Graphics
class has a textureList
, which is simply a vector of pointers to SDL textures:
vector<SDL_Texture*> textureList;
The Graphics
class has an int addTexture(const char* filename)
method, which is used by a GameObject
to add textures to the textureList
vector, and return the vector ID back to the GameObject, so that it can access its texture when needed:
int Graphics::addTexture(const char* filename) {
SDL_Texture* tx = IMG_LoadTexture(this->renderer, filename);
this->textureList.push_back(tx);
return (textureList.size() - 1);
}
The addTexture
method is called by a GameObject
like this:
GameObject player(10, 10, g.addTexture("player.png"));
Later on, a GameObject
will call an updateGraphics()
method on the Graphics
class, which uses the stored ID to draw a texture:
void Graphics::drawTexture(int id, int x, int y, int w, int h, int angle) {
SDL_Texture* tx = this->textureList.at(id);
SDL_Point* centre = NULL;
SDL_RendererFlip flip = SDL_FLIP_NONE;
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderCopyEx(this->renderer, tx, NULL, &rect, angle, centre, flip);
}
From what I can tell, the SDL_Texture*
taken from textureList
and stored in tx
is NULL
, and I've read that this is because textureList
is storing pointers to objects that become NULL
once the objects are destroyed (after the call to addTexture()
?).
I've also read about storing deep copies of the textures in the vector, but I'm unsure how to do that, as I only seem to have access to pointers to textures.
Any help or advice greatly appreciated - especially if I'm just going about this the wrong way!
Thanks, Rich.
SDL_GetError()
and gotCouldn't open player.png
. I'd assumed the wrong working directory for the images! \$\endgroup\$