Hi all,
First post, new to SFML. Something is puzzling me about texture/sprite management and setup.
I'm managing my textures and sprites by loading all textures into a std::vector<sf::Texture>, and all sprites into a std::vector<sf::Sprite>.
The following code doesn't work:
1 this->tileTexures.push_back(sf::Texture());
2 this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
3
4 this->tileSprites.push_back(sf::Sprite());
5 this->tileSprites[0].setTexture(this->tileTexures[0]);
6
7 this->tileTexures.push_back(sf::Texture());
8 this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
9
10 this->tileSprites.push_back(sf::Sprite());
11 this->tileSprites[1].setTexture(this->tileTexures[1]);
When the code above runs, this->tileSprites[1] draws correctly, but this->tileSprites[0] has no texture.
I've followed this through with the debugger and observed the following:
Up to line 5 everything is fine. Once I call line 7, and call .push_back on the texture vector, the texture property of tileSprites[0] gets all messed up. Why is this?
If i change the order of this code however it works fine. The following code works a treat:
1 this->tileTexures.push_back(sf::Texture());
2 this->tileTexures.push_back(sf::Texture());
3
4 this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
5 this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
6
7 this->tileSprites.push_back(sf::Sprite());
8 this->tileSprites.push_back(sf::Sprite());
9
10 this->tileSprites[0].setTexture(this->tileTexures[0]);
11 this->tileSprites[1].setTexture(this->tileTexures[1]);
Same code, different order.
What's going on here? I've never run into this issue before where the order of my interactions with vectors seems to be altering memory somewhere.
Hopefully I've explained well enough, and thanks in advance for any input.