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

Author Topic: "Empty" Window is slow  (Read 5213 times)

0 Members and 1 Guest are viewing this topic.

JinLisek

  • Newbie
  • *
  • Posts: 8
    • View Profile
"Empty" Window is slow
« 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?
« Last Edit: August 29, 2018, 08:30:58 pm by JinLisek »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: "Empty" Window is slow
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JinLisek

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: "Empty" Window is slow
« Reply #2 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.
« Last Edit: August 30, 2018, 07:18:11 am by JinLisek »

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: "Empty" Window is slow
« Reply #3 on: August 30, 2018, 03:50:13 pm »
Try calling window.setFramerateLimit(60) or window.setVerticalSyncEnabled(true).

But not both!!

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: "Empty" Window is slow
« Reply #4 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!)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JinLisek

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: "Empty" Window is slow
« Reply #5 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). :(

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: "Empty" Window is slow
« Reply #6 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

« Last Edit: September 04, 2018, 08:31:50 am by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: "Empty" Window is slow
« Reply #7 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JinLisek

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: "Empty" Window is slow
« Reply #8 on: September 06, 2018, 10:18:06 am »
Oh, that makes sense. Thank you. :)