Thx 4 your help.
I now tried to implement a transparent version which inherits from sf::RenderWindow (it's not perfect as some methods are not virtual, but I didn't want to forward each method call...).
Maybe this is also interesting for SFML because other people might have the same problem.
Another non perfect thing is that I need to use Terminate to make the thread exit, as I don't have any other method to exit the WaitEvent loop.
Here is the code (I ommited things like includes and include guards):
//never cast up to sf::Renderwindow (some non-virtual functions are hidden)!
class ThreadedEventLoopRenderWindow : public sf::RenderWindow
{
public:
ThreadedEventLoopRenderWindow(){}
~ThreadedEventLoopRenderWindow();
void Create(sf::VideoMode mode, const std::string& title, unsigned long style = sf::Style::Default, const sf::ContextSettings& settings = sf::ContextSettings());
void Close();
bool PollEvent(sf::Event &event);
private:
std::unique_ptr<sf::Thread> eventThread_;
sf::Mutex queueMutex_;
std::queue<sf::Event> pendingEvents_;
};
//.cpp file
ThreadedEventLoopRenderWindow::~ThreadedEventLoopRenderWindow()
{
if(eventThread_)
{
sf::Lock lock(queueMutex_);
eventThread_->Terminate();
}
}
void ThreadedEventLoopRenderWindow::Create(sf::VideoMode mode, const std::string& title, unsigned long style /*= sf::Style::Default*/, const sf::ContextSettings& settings /*= sf::ContextSettings()*/)
{
sf::Mutex createMutex;
eventThread_.reset(new sf::Thread([&]()
{
{
sf::Lock createLock(createMutex);
sf::RenderWindow::Create(mode, title, style, settings);
SetActive(false);
}
sf::Event evt;
while(true)
{
bool ok = WaitEvent(evt);
if(ok)
{
sf::Lock lock(queueMutex_);
pendingEvents_.push(evt);
}
else
{
cerr << "error occurred while waiting for event" << endl;
}
}
}));
eventThread_->Launch();
sf::Sleep(100); //hack to make sure that the window is created (sfml does not have a wait condition afaik)
sf::Lock createLock2(createMutex);
}
bool ThreadedEventLoopRenderWindow::PollEvent(sf::Event &event)
{
sf::Lock lock(queueMutex_);
if(pendingEvents_.empty())
{
return false;
}
event = pendingEvents_.front();
pendingEvents_.pop();
return true;
}
void ThreadedEventLoopRenderWindow::Close()
{
if(eventThread_)
{
sf::Lock lock(queueMutex_);
eventThread_->Terminate();
}
sf::RenderWindow::Close();
}