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

Author Topic: [SOLVED] White square problem using vector<Texture>  (Read 2281 times)

0 Members and 1 Guest are viewing this topic.

ondrej008

  • Newbie
  • *
  • Posts: 8
    • View Profile
[SOLVED] White square problem using vector<Texture>
« on: September 27, 2017, 12:33:20 pm »
Hi guys,
I've attempted to make a TextureManager class, which loads and stores the textures in a std::vector, however for some reason drawing the textures makes a white square.

Here's the code:

TextureManager
(click to show/hide)

Tile
(click to show/hide)

I use tile.draw(window); and it just makes white squares.
Does the vector change the memory location or something?

ondrej008

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [SOLVED] White square problem using vector<Texture>
« Reply #1 on: September 27, 2017, 12:34:25 pm »
I'm an idiot, the texture manager was in the wrong scope so the textures got deleted.

fallahn

  • Hero Member
  • *****
  • Posts: 502
  • Buns.
    • View Profile
    • Trederia
Re: [SOLVED] White square problem using vector<Texture>
« Reply #2 on: September 27, 2017, 02:01:04 pm »
I think it's worth noting that if you're using a vector of textures a call to push_back() may cause it to resize. When this happens the existing textures will be moved around in memory (probably using some fairly heavy copy operations due to the way sf::Texture is implemented) and invalidate existing pointers, returning you to your white square problem. Take a look at how the resource manager is implemented in the sfml game development book, it has served me well for many years :)

https://github.com/SFML/SFML-Game-Development-Book/blob/master/02_Resources/Include/Book/ResourceHolder.hpp

 

anything