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

Author Topic: RenderWindow::pollEvent() appears to not be working  (Read 3027 times)

0 Members and 2 Guests are viewing this topic.

NipIsTrue

  • Newbie
  • *
  • Posts: 15
    • View Profile
RenderWindow::pollEvent() appears to not be working
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: RenderWindow::pollEvent() appears to not be working
« Reply #1 on: August 21, 2019, 08:24:09 am »
Are you running this in a separate thread?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NipIsTrue

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: RenderWindow::pollEvent() appears to not be working
« Reply #2 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();
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: RenderWindow::pollEvent() appears to not be working
« Reply #3 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

NipIsTrue

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: RenderWindow::pollEvent() appears to not be working
« Reply #4 on: August 21, 2019, 10:29:20 pm »
Thank you, that fixed it!