I made a small change in the code, now the output is even more odd.. First of all, a mutex protect the sf::Text variable from data races, and now in the 'main' function i wait for some time before changing the string with setString:
class foo
{
sf::Font font;
sf::Text text;
std::thread th;
sf::RenderWindow window;
std::mutex mt;
public:
foo()
{
text.setCharacterSize(12);
text.setStyle( sf::Text::Regular );
text.setColor( sf::Color::Green );
text.setPosition( sf::Vector2f( 0, 0 ) );
//Run the thread
std::thread myth( &foo::loop, this );
th = std::move( myth );
}
~foo()
{
th.join();
}
void loop()
{
window.create(sf::VideoMode(300, 100), "My window");
if( !font.loadFromFile("consola.ttf") )
{
std::cerr<<"FONT LOAD ERROR\n";
std::terminate();
}
text.setFont( font );
while (window.isOpen())
{
window.clear(sf::Color::Black);
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
glFlush();
mt.lock();
window.draw( text );
window.display();
mt.unlock();
std::this_thread::sleep_for( std::chrono::milliseconds{ 50 } );
}
}
void setstring( const std::string& str )
{
std::lock_guard< std::mutex > lock( mt );
text.setString( str.c_str() );
}
};
int main()
{
foo myfoo;
std::this_thread::sleep_for( std::chrono::milliseconds{ 200 } );
//Change the string.
myfoo.setstring("THIS IS A TEST");
std::this_thread::sleep_for( std::chrono::milliseconds{ 2500 } );
myfoo.setstring("DO NOT WORK ");
std::this_thread::sleep_for( std::chrono::milliseconds{ 2500 } );
return 0;
}
At first the instance of foo is created and the thread runs, after 200ms a call to sestring set a new string in the sf::Text variable, then the app wait 2500ms, in this time something should be displayed but only dark is visible, 'THIS IS A TEST' is not even printed. after 2500ms the second string is printed, but without the 'K'
"DO NOT WOR ".
This is pretty strange to me, if i remove the first 200ms waiting point, the first string is properly printed, but the second one is still without the 'K'.
The glFlush call is right before the 'draw' and 'display' call.
Any idea on what may be wrong here?