Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Events in seperate thread  (Read 3104 times)

0 Members and 1 Guest are viewing this topic.

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Events in seperate thread
« 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.
« Last Edit: December 31, 2016, 11:51:40 pm by ormenbjorn »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Events in seperate thread
« Reply #1 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.

I would like a spanish/latin community...
Problems building for Android? Look here

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Events in seperate thread
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Events in seperate thread
« Reply #3 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
Laurent Gomila - SFML developer

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Events in seperate thread
« Reply #4 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.
« Last Edit: January 01, 2017, 03:43:39 pm by ormenbjorn »

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Events in seperate thread
« Reply #5 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..