1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Have you tried SDL_GetError() to catch the possible SDL errors? Maybe there was a problem while initializing some of the SDL \$\endgroup\$
    – Pins
    Apr 21, 2017 at 9:39
  • \$\begingroup\$ Thank you! I printed SDL_GetError() and got Couldn't open player.png. I'd assumed the wrong working directory for the images! \$\endgroup\$
    – Rich. T.
    Apr 21, 2017 at 11:15

2 Answers 2

3
\$\begingroup\$

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()?).

No, that's wrong. Pointers are not magic, they don't change value by themselves anymore than other objects do. If drawTexture stores a non-null SDL_Texture * inside the vector, then it will still be there later, so your code works.

Note, however, that IMG_LoadTexture returns NULL if it encounters an error, and you don't check for it, so you might store an actual null pointer in textureList from the beginning.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for this. I misunderstood, and it turns out there was an error with the creation of the texture. \$\endgroup\$
    – Rich. T.
    Apr 21, 2017 at 11:16
-1
\$\begingroup\$

Note to future me...

The problem had nothing to do with the use of pointers to textures (as Quentin stated above), but was instead due to SDL_Texture not being able to find the image.

I changed the working directory in the build settings and everything works!

Thanks everyone for your help -- I knew it must have been due to a mistake on my part!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ As Quentin mentioned it would be also helpful for your future development to check if the creation of the textures was successful by checking the returned pointer of the IMG_Loadtexture() so you will avoid crashes and possible errors. \$\endgroup\$
    – Pins
    Apr 21, 2017 at 14:41
  • \$\begingroup\$ "Note, however, that IMG_LoadTexture returns NULL if it encounters an error," Not finding the image is an error and NULL was being added to the vector because of this. Quentin's answer is completely correct. \$\endgroup\$
    – Pharap
    Apr 23, 2017 at 18:36
  • \$\begingroup\$ Great, thanks. I've marked Quentin's answer as correct. \$\endgroup\$
    – Rich. T.
    Apr 24, 2017 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .