Hello Community,
I’m facing "strange" (in my opinion) issue – infamous white square problem, in which I can’t find its source.
I’ m well aware of official tutorial (
http://www.sfml-dev.org/tutorials/2.3/graphics-sprite.php#the-white-square-problem) – used it couple of times, but this time I just don’t see what might be causing an error.
Also, I’ve searched forum for similar issue but as it seems each case is different and uique in its own way…
Describing my issue in couple words: I’m trying to create title manager – class that’s storing titles inside std::map container, so I could access them when necessary. The issue is, when I’m creating “plain” Title object – it’s drawn without problem. Once I store it inside container and try to access it, all I’m receiving is white square.
I’m a little bit stuck at this moment, so any suggestion would be very appreciated.
Minimal code provided below:
#include <SFML/Graphics.hpp>
enum class LocalTitleType {
Grass
};
class LocalTitle {
public:
static const int WIDTH = 64;
static const int HEIGHT = 64;
void setTexture( sf::Texture &texture ) {
title.setTexture( &texture );
}
sf::RectangleShape getTitle( void ) {
return title;
}
void create( void ) {
title.setSize( sf::Vector2f( WIDTH, HEIGHT ) );
}
private:
sf::RectangleShape title;
};
class LocalTitleManager {
public:
void create( void ) {
createGrassTitle();
}
LocalTitle& getTitleRef( LocalTitleType titleType ) {
return titles.at( titleType );
}
private:
std::map< LocalTitleType, LocalTitle > titles;
void createGrassTitle( void ) {
sf::Texture texture;
texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
LocalTitle title;
title.create();
title.setTexture( texture );
titles[LocalTitleType::Grass] = title;
}
};
int main() {
LocalTitleManager manager;
manager.create();
sf::RenderWindow window( sf::VideoMode( 200, 200 ), "Title Manager Test" );
while ( window.isOpen() ) {
sf::Event event;
while ( window.pollEvent( event ) ) {
if ( event.type == sf::Event::Closed )
window.close();
}// while
window.clear();
LocalTitle title = manager.getTitleRef( LocalTitleType::Grass );
window.draw( title.getTitle() ); /** here's the issue */
/** white square problem is not existent using below:
sf::Texture texture;
texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
LocalTitle localTitle;
localTitle.create();
localTitle.setTexture( texture );
window.draw( localTitle.getTitle() );
*/
window.display();
}// while
return EXIT_SUCCESS;
}
I know that issue lies inside “LocalTitleManager” Class, in this method precisely:
void createGrassTitle( void ) {
sf::Texture texture;
texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
LocalTitle title;
title.create();
title.setTexture( texture );
titles[LocalTitleType::Grass] = title;
}
...but according to my knowledge, once I’ve created desired texture, attach it to LocalTitle class and finally store it in Container, it should work.
What am I missing?
Thank You for You’re help in advance