SFML community forums

Help => Window => Topic started by: JinLisek on August 29, 2018, 07:09:25 pm

Title: "Empty" Window is slow
Post by: JinLisek on August 29, 2018, 07:09:25 pm
Hello,

I created a new Window, with nothing drawn on it. The problem is that, when I move the window it moves sluggishly.
Problem occurs on Linux (Archlinux/Manjaro), my device has 2 graphics cards: integrated (intel hd 4000) and dedicated (NVidia GT650m).

I tested the same code on another device, also Linux Manjaro. I think that device has only 1 integrated card though. My program worked there and when I moved the window it was snappy and responsive.

EDIT: Also, note that other windows (for example Firefox) on my system work just fine. But when I run my program, moving them is slow too.

Code below, don't mind class name - it was only a test.

main.cpp:
int main()
{
    KeyboardInput keyInput {};
    keyInput.beginWaitingOnInput();

    return 0;
}
 

KeyboardInput.cpp:
#include "SFML/Window/Keyboard.hpp"
#include "SFML/Window.hpp"

void getUserInput()
{
    sf::Window window(sf::VideoMode(800, 600), "Window Title");

    while(window.isOpen()) //to be moved elsewhere
    {
        sf::Event event;

        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        window.display();
    }

    while(true) //to be used later
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
            Game::gameState = GameState::Quit;
            break;
        }
    }
}

KeyboardInput::~KeyboardInput()
{
    if(inputThread.joinable())
        inputThread.join();
}

void KeyboardInput::beginWaitingOnInput()
{
    inputThread = std::thread{getUserInput};
}
 


What could be a problem here?
Title: Re: "Empty" Window is slow
Post by: eXpl0it3r on August 29, 2018, 09:00:08 pm
You're rendering as fast as possible, so you're at least using up a whole CPU core, which might well cause the window manager to have some troubles.
Title: Re: "Empty" Window is slow
Post by: JinLisek on August 30, 2018, 07:04:30 am
But on another device (which is probably less powerful) the programs runs without problems. How do I render slower? Do I limit frame rates or yield thread and wait for X (what is X then?)? Also, note that I create a window in a new thread, not main.

The code is also shown in tutorials, so I really don't think it's fault of wrong code. Probably environmental issues, but I'd like to know how to possibly fix them.
Title: Re: "Empty" Window is slow
Post by: verdog on August 30, 2018, 03:50:13 pm
Try calling window.setFramerateLimit(60) or window.setVerticalSyncEnabled(true).

But not both!!
Title: Re: "Empty" Window is slow
Post by: Hapax on August 31, 2018, 11:18:24 pm
It's also a good idea to create and process the window's events from the main thread.
(think how bored your main thread must be!)
Title: Re: "Empty" Window is slow
Post by: JinLisek on September 04, 2018, 08:08:59 am
Try calling window.setFramerateLimit(60) or window.setVerticalSyncEnabled(true).

But not both!!

I tried both of suggested improvements (separately) and both of them work - now when I move the window it no longer lags.

So... Why is that on one PC it's needed to work properly and on another PC it's not needed? Is it because of drivers (I think I've messed up updating drivers on the first one). Or something else?

Shouldn't the user (instead of developer) choose whether to use frame limiting and/or vertical sync?

It's also a good idea to create and process the window's events from the main thread.
(think how bored your main thread must be!)

I tried moving window to main thread, but it didn't change anything (no improvement whatsoever). :(
Title: Re: "Empty" Window is slow
Post by: FRex on September 04, 2018, 08:30:08 am
It depends and might be something really niche like drivers, integrated or not, settings, (on Linux the DE or which drivers flavor you use maybe?), etc.

E.g. with (and that's the default) 'optimal power' (which is a great descriptive name BTW ::)) in 'Power Management' in NVidia CP my Windows laptop limited my SFML/CUDA app to 60 FPS on battery despite not using those two functions in my program (so it ran at 200-400+ when plugged in).

Input, window creation (and sometimes GL) also interact differently with threads on different OSes so it's best to keep all Window stuff in one/main thread (or something.. I never messed with those things all at once), see threading question in (might be obsolete info by now?): http://fabiensanglard.net/doom3/interviews.php and this section of Window tutorial: https://www.sfml-dev.org/tutorials/2.5/window-window.php#things-to-know-about-windows

Title: Re: "Empty" Window is slow
Post by: eXpl0it3r on September 04, 2018, 08:31:32 am
A user can force VSync in the driver settings by default, while a user can equally not force it, so you can get different behavior on different PCs.

Shouldn't the user (instead of developer) choose whether to use frame limiting and/or vertical sync?
Sure, that's why you should provide options in your options menu to set VSync or limit the framerate. ;)
Title: Re: "Empty" Window is slow
Post by: JinLisek on September 06, 2018, 10:18:06 am
Oh, that makes sense. Thank you. :)