SFML community forums

Help => Window => Topic started by: tymofey on September 19, 2011, 07:51:15 pm

Title: Window and multiple threads
Post by: tymofey on September 19, 2011, 07:51:15 pm
I have a thread one where i create the window:
Code: [Select]
sf::Window* wnd = new sf::Window(sf::VideoMode(400, 300), "some title", sf::Style::Close);
Then i pass the wnd ponter to two other threads.
In thread two i query window for events:
Code: [Select]
sf::Event event;
while (true)
    while(wnd->PollEvent(event))
    {
         //....
    }

And in thread three I draw in it:
Code: [Select]
while (true)
{
    //...
    wnd->Display();
}


The window is of course protected by proper locking.

This causes lots of
Quote
Failed to activate window's context
messages to the console. Is what i am trying to do even possible?
Title: Window and multiple threads
Post by: Laurent on September 19, 2011, 08:35:47 pm
You must first deactivate (SetActive(false)) the window in the thread where it is active (->the thread where you created it), before activating it in another thread.
Title: Window and multiple threads
Post by: tymofey on September 19, 2011, 08:42:39 pm
Would doing that really often (like every frame) introduce huge overhead?
Title: Window and multiple threads
Post by: Laurent on September 19, 2011, 09:25:23 pm
Yes. But why would you need to draw to the same window from multiple threads?
Title: Window and multiple threads
Post by: tymofey on September 19, 2011, 10:15:50 pm
i dont. i want to draw in one thread and poll events in another. but it appears that i still need the window to be active to poll events from it.
Title: Window and multiple threads
Post by: Laurent on September 19, 2011, 10:19:58 pm
Nop. SetActive activates the OpenGL context, which is necessary only for drawing operations.
Title: Window and multiple threads
Post by: tymofey on September 19, 2011, 10:47:10 pm
then appearently i am doing something really wrong as the window does not seem to generate any events this way
Title: Window and multiple threads
Post by: Laurent on September 19, 2011, 11:02:32 pm
Some OSes (like Windows) require events to be polled from the thread that created the window.
Title: Window and multiple threads
Post by: tymofey on September 19, 2011, 11:07:17 pm
thanks, that explains everything. i believe this should be mentioned in the docs.
Title: Window and multiple threads
Post by: Laurent on September 20, 2011, 07:49:32 am
Quote
i believe this should be mentioned in the docs

I agree ;)