1
\$\begingroup\$

I am currently working on loading tmx levels into my game, all is going well and i have layers being loaded textures being loaded and all the data being loaded such as level name, tile sizes etc.

The issue

Loading 1 texture for tiles is perfectly ok, but loading more than 1 the texture seems to get removed from memory (only white squares are displayed) I'm not going to include all my code as there's lot's of it but i will include a couple of snippets.

//code inside load function.
Texture texture;
texture.loadFromFile(src);
Texture *t = addTexture(texture);
loadTileSheet(twidth, theight, t);

Texture* Level::addTexture(Texture texture){
    Level::textures.push_back(texture);
    return & textures[textures.size() - 1];
}

I am then outputting all the information to the console of the tileset this is as follows: mem adress 1 is the texture creation and mem adress 2 is the adress in the vector.

Tileset Information: nametileset2 | src G:/C++/Workspace/test/res/tileset2.png | tWidth: 32 | tHeight32 | MemAdress 1: 0x85d028 | MemAdress 2: 0xa81848

currently this works, loading in a second texture outputs this and breaks all the first set of textures.

Tileset Information: nametileset2 | src G:/C++/Workspace/test/res/tileset2.png | tWidth: 32 | tHeight32 | MemAdress 1: 0x85d028 | MemAdress 2: 0x82522b0

I'm pretty sure this is something simple that i have over looked, and I'm pretty sure the first texture is somehow being removed from memory when the second one is created somehow.

Thanks in advance:

Jordan

\$\endgroup\$
  • \$\begingroup\$ I can't spot the problem from that code, but I suggest an improvement. The method addTexture(Texture texture) gets a Texture as a copy, and then it will be copied again to the container (on push_back()). I would change the container (supposing it's a vector) to std::vector<Texture*> and rethink how to create and delete textures from there. \$\endgroup\$ – MadEqua Jul 9 '16 at 15:00
  • \$\begingroup\$ It was initially a pointer to a texture in the vector, but i removed it for working out what was wrong to see if it was being removed that way but it made no difference, forgot to change it all back before i uploaded it, in a way kind of glad you can't see anything think it shows I'm not being completly stupid :) \$\endgroup\$ – jordy2254 Jul 9 '16 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.