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

Author Topic: Window does not react while app's main loop is in a thread  (Read 2855 times)

0 Members and 1 Guest are viewing this topic.

Polpopolitan

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Window does not react while app's main loop is in a thread
« on: September 08, 2012, 10:57:43 am »
Hey There,

I've got some issue with the program i'm currently coding. No error at all, but the window doesn't react to any event. Here's the concerned code :

main.cpp
#include <iostream> // mostly for debug
#include <cstdlib> // for atoi()
#include <SFGUI/SFGUI.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

#include "ServerWindow.hpp"
#include "constants.hpp"

int main(int argc, char* argv[]) {

        #ifdef DEBUG
                std::cout << "DEBUG : Pour desactiver les messages de debug, retirer la definition de DEBUG et la remplacer par #define RELEASE dans constants.hpp" << std::endl << std::endl;
                std::cout << "DEBUG : Initialisation de l'interface graphique." << std::endl;
                sf::Clock debugClock;
        #endif // DEBUG
               
        ServerWindow window;
        window.initialize();

        #ifdef DEBUG
                std::cout << "DEBUG : Interface graphice initialisee avec succes en " << debugClock.getElapsedTime().asSeconds() << " secondes." << std::endl;
        #endif // DEBUG

        window.setActive(false); // cause we use threads
        sf::Thread guiThread(&ServerWindow::go, &window);
        guiThread.launch();

        return EXIT_SUCCESS;
}

ServerWindow's go method (ServerWindow inherits sf::RenderWindow)
void ServerWindow::go() {

        setActive(true);
        o_running = true;
        this->resetGLStates(); // else we wouldn't see anything
        this->setFramerateLimit(60); // because it's enough

        o_clock.restart();

        // main loop
        while(this->isOpen()) {

                // event handling
                while(this->pollEvent(o_event)) {

                        // sfgui event handling
                        o_desktop.HandleEvent(o_event);

                        // closing
                        if(o_event.type == sf::Event::Closed) {

                                o_running = false;
                                this->close();
                        }
                }

                o_desktop.Update(o_clock.restart().asSeconds());

                this->clear(sf::Color::Black);
                // bad but necessary
                o_sfgui.Display(*(const_cast<ServerWindow*>(this)));
                this->display();
        }
}

As you can see, I use SFGUI but I don't think it's related to the problem, since I can't even close the window, which is a regular sfml event.

Thanks in advance !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window does not react while app's main loop is in a thread
« Reply #1 on: September 08, 2012, 11:04:59 am »
What's your OS?

Have you read the corresponding tutorial carefully? There are a few important things to know about windows and threads.
Laurent Gomila - SFML developer

Polpopolitan

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Window does not react while app's main loop is in a thread
« Reply #2 on: September 08, 2012, 11:16:27 am »
I am on a Windows 7 Starter 32 bits.
Well, I don't think I missed anything on the tutorial, but i'm going to read it again anyway.

Polpopolitan

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Window does not react while app's main loop is in a thread
« Reply #3 on: September 08, 2012, 11:47:51 am »
I'm definitly sure I didn't miss anything on the tutorial, and my application is still frozen  ???

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Window does not react while app's main loop is in a thread
« Reply #4 on: September 08, 2012, 01:22:46 pm »
He means this warning from the tutorial: Opening and managing a SFML window

Quote
Events must be polled in the window's thread
This is an important limitation of most OSes: the event loop (more precisely, the pollEvent or waitEvent function) must be called in the same thread that created the window. This means that if you want to create a dedicated thread for event handling, you'll have to make sure that the window is created in this thread too. If you really want to split things between threads, it is more convient to keep event handling in the main thread and move the rest (rendering, physics, logic, ...) to a separate thread instead. This configuration will also be compatible with the other limitation described below.

Polpopolitan

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Window does not react while app's main loop is in a thread
« Reply #5 on: September 08, 2012, 01:50:16 pm »
Oh I see, thanks i'll try that immediately  :)