SFML community forums

Help => Window => Topic started by: NipIsTrue on August 19, 2019, 05:54:57 pm

Title: RenderWindow::pollEvent() appears to not be working
Post by: NipIsTrue on August 19, 2019, 05:54:57 pm
The window.pollEvent() function in my game loop appears to not be handling any events, and the created window won't respond to anything. Here is the code for my game loop:
void Application::run()
        {
                sf::Context context;
                window.setFramerateLimit(60);

                while (window.isOpen())
                {
                        sf::Event e;
                        std::cout  << "loop" << std::endl;
                        while (window.pollEvent(e))
                        {
                                std::cout << "event" << std::endl;
                                handleEvent(e);
                        }

                        update();

                        render();
                }
        }
 

The print statement never prints "event", but "loop" prints repeatedly, so I know the loop is running. The result is that the window won't respond if I try to resize it or close it, and eventually windows marks it as "not responding". I am using the snapshot from    13-May-2019 19:49 from the download page.
Title: Re: RenderWindow::pollEvent() appears to not be working
Post by: eXpl0it3r on August 21, 2019, 08:24:09 am
Are you running this in a separate thread?
Title: Re: RenderWindow::pollEvent() appears to not be working
Post by: NipIsTrue on August 21, 2019, 08:00:44 pm
Yes I  am, this is what the code for launching that thread looks like:

app.getWindow().setActive(false);
auto t = std::async(std::launch::async, &yee::Application::run, &app);
t.get();
 
Title: Re: RenderWindow::pollEvent() appears to not be working
Post by: Hapax on August 21, 2019, 08:35:46 pm
It doesn't look like the window is created by the thread this is polling the events i.e. being created in Application::run()

Quote
Events must be polled in the window's thread

This is an important limitation of most operating systems: 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 convenient to keep event handling in the main thread and move the rest (rendering, physics, logic, ...) to a separate thread instead.
source: https://www.sfml-dev.org/tutorials/2.5/window-window.php#things-to-know-about-windows
Title: Re: RenderWindow::pollEvent() appears to not be working
Post by: NipIsTrue on August 21, 2019, 10:29:20 pm
Thank you, that fixed it!