SFML community forums

Help => Window => Topic started by: Quit on September 15, 2016, 06:12:34 pm

Title: Program "freezes" when mouse cursor doesn't move over the RenderWindow
Post by: Quit on September 15, 2016, 06:12:34 pm
Hello

i've encountered a strange problem and I'm not sure how to tackle it. At the moment my program consists of a renderwindow and a console(debug build). The problem is that when the render has "no events to capture" (i move mouse outside of the window region, minimize the window, or simply leave the cursor within the window but motionless) the class responsible for graphics (the one that has the renderwindow as its member) just holds execution. The messages it would print to the console won't appear until the moment i move the mouse within the render window. The messages generated by a separate class appear in the console normally. What can be the cause of this behavior? I would like my program to continue execution regardless if the window has focus or not/ if a user sits in front of the pc or not. The code that represents the program structure (and partially recreates the problem - partially because in here both classes stop "working" when the i don't move the mouse/move it outside of the window) is below

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class Window
{
public:
    Window(): timer(2), window(sf::VideoMode(800, 600), "SFML window"){}
    timeFunc()
    {
        sf::Time measure = clock.getElapsedTime();
        if (measure.asSeconds() >= timer)
        {
            std::cout<< "window func" << std::endl;
            clock.restart();
        }

    }
    sf::RenderWindow window;
private:
    int timer;
    sf::Clock clock;
};

class Terminal
{
public:
    Terminal(): timer(2){}
    timeFunc()
    {
        sf::Time measure = clock.getElapsedTime();
        if (measure.asSeconds() >= timer)
        {
            std::cout<< "terminal func" << std::endl;
            clock.restart();
        }
    }
private:
    int timer;
    sf::Clock clock;
};

class base
{
public:
    base(){running = true;}
    Window window;
    Terminal terminal;
    bool running;
};

int main()
{
    base a;
    while(a.running)
    {
        sf::Event event;
        while(a.window.window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                a.running = false;

            a.window.timeFunc();
            a.terminal.timeFunc();

            a.window.window.clear();
            a.window.window.display();
        }
    }

    return 0;
}
Title: Re: Program "freezes" when mouse cursor doesn't move over the RenderWindow
Post by: eXpl0it3r on September 15, 2016, 06:16:18 pm
Your rendering part is inside the event loop. The event loop only gets executed when there are events to be processed (e.g. when you move your mouse over the window). As such your renderings only update when events are triggered.

Move your rendering part (clear, draw, display) into the main loop as seen in the official tutorials. :)
Title: Re: Program "freezes" when mouse cursor doesn't move over the RenderWindow
Post by: Quit on September 15, 2016, 06:25:56 pm
The moment i clicked "post" i came to the same conclusion and have made a biggest facepalm the world has seen yet =) I wanted to delete the post but it wouldn't let me... At least I know now why there should always be a separation between getting input and updating the program state =)
One way or another, thank you for a quick reply, we can always count on you guys =) Keep up the awesome work!