I've been trying to create an SMFL program which can update graphics even while the window is being resized.
I created a thread which runs a function that collects events, and initially creates the render window.
The event gathering function runs properly, and the program seems to respond to events as it's supposed to; I can't get anything to display, however. Even a simple Clear(sf::Color) command does not display.
Please note that I have already checked this in a debugger, and can confirm the the draw commands are executed, they just don't do anything to the screen.
Update:
I tried adding a Clear(sf::Color::White); command inside the thread function, and it worked. However, I cannot update graphics from within the thread function, as that would be highly impractical and inconvenient.
Here's the code:
System.cpp
#include "system.h"
Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR) :
vm(rVM),
vtitle(rSTR),
win(0),
gInst(*this)
{
}
void Sys::System::System::run()
{
for(;;)
{
eventQueueMutex.Lock();
while (!eventQueue.empty())
{
if (gInst.menu.onEvent(eventQueue.front()) == 0) {}
if (eventQueue.front().Type == sf::Event::Closed)
return;
eventQueue.pop();
}
eventQueueMutex.Unlock();
win->Clear();
gInst.menu.draw();
win->Display();
}
}
void Sys::System::start()
{
// Initialize system
System s(sf::VideoMode(1024, 600, 32), "Test 2");
// Load standard resources
s.mainFont.LoadFromMemory(Sys::EmbeddedFiles::getStandardFontPointer(), Sys::EmbeddedFiles::getStandardFontSize(), 18);
// Start event gatherer thread (this also creates the render window)
sf::Thread thread(&threadEventFunction, &s);
thread.Launch();
for(;;)
{
s.gathererSignalMutex.Lock();
if (s.win) break;
s.gathererSignalMutex.Unlock();
}
// No need to unlock the mutex here (it never gets used again)
// Attempting to access the render window before this point will
// result in a segmentation violation
// Change graphics settings (win now points to heap space)
s.win->SetFramerateLimit(120);
// Create GUI menus and menu entries
s.uiInit();
// Start running the editor program
s.run();
// Wait for the gatherer thread to end
s.win->Close();
thread.Wait();
}
Ui_input.cpp
#include "system.h"
void Sys::System::threadEventFunction(void* arg)
{
System::System& rS = *static_cast<System::System*>(arg);
rS.gathererSignalMutex.Lock();
rS.win = new sf::RenderWindow(rS.vm, rS.vtitle);
rS.gathererSignalMutex.Unlock();
sf::RenderWindow& rWin = *rS.win;
sf::Event event;
while (rWin.IsOpened())
{
while (rWin.GetEvent(event))
{
rS.eventQueueMutex.Lock();
rS.eventQueue.push(event);
rS.eventQueueMutex.Unlock();
}
sf::Sleep(0.01f);
}
}