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

Author Topic: Handle events in different thread  (Read 8363 times)

0 Members and 1 Guest are viewing this topic.

Flawe

  • Newbie
  • *
  • Posts: 24
    • View Profile
Handle events in different thread
« on: July 27, 2010, 12:33:54 am »
I'm having some trouble handling events in a second thread. I do the rendering and the logic on the main thread but I spawn off a second thread for the event loop. This loop basically calls GetEvent() on the render window but it never seems to catch any events.

I have the window activated on the main thread and deactivated on the event thread, does that matter? Do I need something specific set up for the input to work on this second thread?

Thanks for any help

Flawe

  • Newbie
  • *
  • Posts: 24
    • View Profile
Handle events in different thread
« Reply #1 on: July 27, 2010, 01:14:36 am »
So I read that you can't handle the input on a different thread. Fair enough, what I do is this:

Code: [Select]

void main() {
Thread mainThread( &GameManager::Init, GetGameManager() );

sf::Event evt;
bool running = true;
while( running ) {
if( renderWindow == NULL ) {
ThreadSleep( PTimeMS( 25 ) );
continue;
}

while( renderWindow->GetEvent( evt ) ) {
if( evt.Type == sf::Event::Closed ) {
GetGameManager()->StopGame();
running = false;
}
}

ThreadSleep( PTimeMS( 10 ) );
}

mainThread.join();
}


renderWindow is an extern which is set up in GameManager::Init. Everything works render wise and "mainThread" executes as expected. Only it seems that GetEvent never returns true as all input actions are ignored.

Flawe

  • Newbie
  • *
  • Posts: 24
    • View Profile
Handle events in different thread
« Reply #2 on: July 27, 2010, 02:05:19 am »
Ok my bad, I actually created the renderWindow on a different thread. So in conclusion: create the window on the main thread, handle events on main thread, have party on many other threads.

I'll get my coat now, thanks for your time!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handle events in different thread
« Reply #3 on: July 27, 2010, 08:17:47 am »
I think you just need to handle events in the same thread that created the window, but this can be another thread than the main one.
Laurent Gomila - SFML developer

 

anything