SFML community forums
Help => General => Topic started by: Kerachi on November 26, 2017, 04:12:43 pm
-
I have a Wall Class, and I store them in a list:
std::list<Wall> walls;
But when I add a wall like:
texture.loadFromFile("wall.png");
walls.push_back(Wall(texture, 500.f, 490.f, 4)); //arguments: texture, position X, postion Y, direction 4 = down
Then it's works, but the texture shown like it would be a nullptr. (So it's a white rectangle)
I draw it the walls this way:
for (Wall const &wal : walls)
wal.draw(window);
But if I add another wall to the list, then the first one is appear fine, but the second one is white, like:
(http://www.kepfeltoltes.eu/images/hdd1/2017/11/26/105wall.png)
And if I add different textured walls like:
texture.loadFromFile("wall.png");
walls.push_back(Wall(texture, 500.f, 490.f, 4));
walls.push_back(Wall(texture, 563.f, 490.f, 4));
walls.push_back(Wall(texture, 626.f, 490.f, 4));
walls.push_back(Wall(texture, 500.f, 600.f, 4));
walls.push_back(Wall(texture, 563.f, 600.f, 4));
walls.push_back(Wall(texture, 626.f, 600.f, 4));
texture.loadFromFile("wall2.png");
walls.push_back(Wall(texture, 720.f, 540.f, 0));
then 6. (the one before texture changes) will be corrupted:
(http://www.kepfeltoltes.eu/images/hdd1/2017/11/26/804walls.png)
I spend about 6 days of free time to figure out what could be the problem, and so far I managed to identify that the poblem is with my list, but I don't know what.
Because, if I create walls without adding to the a list, and draw them, then everything works, as intended.
(Of course I tried vectors , arrays, and so on, but I thing it would be reasonable to store them in a list)
The entire source code could be found at my GitHub: https://github.com/Kerachi/Kerachi (https://github.com/Kerachi/Kerachi)
Any idea?
-
Hello, I think I can help (if you haven't figured it out yet).
It looks like you made some basic mistakes. Have you read the tutorials and stuff? You really shouldn't spend 6 days on such a problem, it's usually just a small mistake :)
First, understand how sf::Texture works. You are loading a texture and then copying it into each object. I don't think you should ever copy a texture like that.
A sf::Texture lives in the GPU memory and there it only ever needs one copy that it can draw in different places. I don't know why copying only doesn't work for the walls (shouldn't it? Still kind of a noob myself... It might be your drawing code), but even if it did, it wouldn't make sense.
I compiled your code and for me all 7 rectangles are white, so it even depends on the system/compiler, which is weird. But saving the texture as a reference in the Wall class (I added 3 '&'s) and making a second sf::Texture for the other wall in main() fixes it.
I really don't know why your version didn't work, though.
Also, if I replace the std::list of walls with a std::vector it works exactly the same. I don't think you should use lists if you can replace it that easily with a vector. But maybe you have some bigger plans where a list makes sense.
-
Thanks, but I figured out a while ago, thanks to the irc help, I know it's doesn't make sense to create a texture for every single Wall, but still, I couldn't figure out why it isn't working.
"First, understand how sf::Texture works." I don't know how they works, but if I have time I will read the sfml source code, and I hope I will know :)
"I compiled your code and for me all 7 rectangles are white" those are sf::Sprite-s >:( ... :)
and they white because they got a nullptr as texture (I think).
"Also, if I replace the std::list of walls with a std::vector" good idea, I did, but I come from C# and Java, so that should explain why I used list :)
Shortly thanks, those are good tips, I would have been glad, if I got your answer about 8-10 days earlier :D