SFML community forums

Help => General => Topic started by: ormenbjorn on December 31, 2016, 11:47:12 pm

Title: Events in seperate thread
Post by: ormenbjorn on December 31, 2016, 11:47:12 pm
So, i want to have static bools like "key_left_wasPressed" in a class, update them in main and do the game stuff in another thread.
right now my code looks something like this:
RenderWindow window;
volatile bool willUpdateEvents = false;
void game(){
    //in a loop:
        willUpdateEvents = true;
        while(willUpdateEvents){}
        //(do game stuff)
        //(draw stuff)
}
int main(){
    //(setup window)
    window.setActive(false);    

    Thread gameThread(game);
    gameThread.launch();
    while(Game::gameState != Game::Exit){
        if(willUpdateEvents){
            GameEvents::update(window);
            willUpdateEvents = false;
        }
    }
    gameThread.wait();
}

//Update function in GameEvents class:
void update(RenderWindow &window){
    everything = false;
    while(window.pollEvent(event)){
        //(set the event bools)
    }
}
 

The problem is, it's super slow, i can't close the window and it still freezes when i'm dragging it.

And i can't just do the game stuff that should happen at events in the event loop because i have like 8 rooms in the game so far and i don't want to have half of the logic in an event loop.
Title: Re: Events in seperate thread
Post by: DarkRoku12 on January 01, 2017, 03:25:44 am
I got a very good implementation of rendering in another thread with SFML few months ago.

I used std::atomic, and discovered that events MUST be handled in the main thread.
So you will need to draw in the new thread, i think this is because the SFML implementation on Windows.

Title: Re: Events in seperate thread
Post by: K.F on January 01, 2017, 05:07:36 am
I used std::atomic, and discovered that events MUST be handled in the main thread.
So you will need to draw in the new thread, i think this is because the SFML implementation on Windows.

That's an OpenGL limitation if I remember correctly. Vulkan gives more leeway if you want multithreaded drawing.
Title: Re: Events in seperate thread
Post by: Laurent on January 01, 2017, 01:59:29 pm
Quote
I used std::atomic, and discovered that events MUST be handled in the main thread.
Don't discover, read the doc ;)

http://www.sfml-dev.org/tutorials/2.4/window-window.php#things-to-know-about-windows
Title: Re: Events in seperate thread
Post by: ormenbjorn on January 01, 2017, 03:31:40 pm
Okay, isn't that what i'm doing?
btw, events like KeyPressed do work.
it's just really slow, the window doesn't close and it freezes when i'm dragging it.
Title: Re: Events in seperate thread
Post by: ormenbjorn on January 01, 2017, 04:01:08 pm
Ohhh, now i got why it doens't close and why it freezes.
The while(willUpdateEvents){} is blocking because it won't continue until the event loop in GameEvents::update() is done.
But if i put willUpdateEvents = false; before GameEvents::update();, the event bools are all false in the game loop..