Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A noob can't handle lists  (Read 1303 times)

0 Members and 1 Guest are viewing this topic.

Kerachi

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
A noob can't handle lists
« 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:

Code: [Select]
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:

Code: [Select]
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:


And if I add different textured walls like:
Code: [Select]
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:



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

Any idea?

Exilef

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: A noob can't handle lists
« Reply #1 on: November 30, 2017, 02:27:17 pm »
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.

Kerachi

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: A noob can't handle lists
« Reply #2 on: December 01, 2017, 05:50:35 pm »
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