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

Author Topic: setFrameLimit causes absurd CPU usage  (Read 2170 times)

0 Members and 1 Guest are viewing this topic.

StriderPulse599

  • Newbie
  • *
  • Posts: 16
    • View Profile
setFrameLimit causes absurd CPU usage
« on: November 02, 2023, 11:18:15 pm »
Any window capped at 60 FPS suddenly gets 5-10% extra CPU usage. Everything works fine uncapped/capped at 120+ no matter if it's simple rectangle or a medium-sized game prototype.

Example:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Encore");
    window.setFramerateLimit(60);

    sf::RectangleShape rectangle;
    rectangle.setSize(sf::Vector2f(100, 100));
    rectangle.setPosition(0, 0);
    rectangle.setFillColor(sf::Color(100, 250, 50));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
                return 0;
            }
        }
       
        window.clear();
        window.draw(rectangle);
        window.display();
    }

    return 0;
}
« Last Edit: November 02, 2023, 11:21:59 pm by StriderPulse599 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11045
    • View Profile
    • development blog
    • Email
Re: setFrameLimit causes absurd CPU usage
« Reply #1 on: November 03, 2023, 12:07:56 am »
How do you "measure" CPU usage?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

StriderPulse599

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: setFrameLimit causes absurd CPU usage
« Reply #2 on: November 03, 2023, 12:33:11 am »
How do you "measure" CPU usage?
From Task Manager, Resource Monitor also shows a huge difference in CPU usage.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11045
    • View Profile
    • development blog
    • Email
Re: setFrameLimit causes absurd CPU usage
« Reply #3 on: November 03, 2023, 07:58:00 am »
Do you enforce VSync in your global GPU driver settings?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

StriderPulse599

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: setFrameLimit causes absurd CPU usage
« Reply #4 on: November 03, 2023, 11:37:12 am »
Do you enforce VSync in your global GPU driver settings?

Yup, I did and turning it off solved the problem. Is there any way to detect that and still limit the game to 60 FPS, or would this work too:


int main()
{

    sf::Vector2f resolution = { 1920, 1080 };

    sf::RenderWindow window(sf::VideoMode(resolution.x, resolution.y), "ChronoTest", sf::Style::Close);

    using clock = std::chrono::steady_clock;
    using frameLimit = std::chrono::duration<int, std::ratio<1, 60>>;

    while (window.isOpen())
    {
        auto frameStart = clock::now();

        // Game Logic
       
        if (clock::now() - frameStart < frameLimit(1)) std::this_thread::sleep_until(frameStart + frameLimit(1));
       
    }
   
    return 0;
}
« Last Edit: November 03, 2023, 11:56:36 am by StriderPulse599 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11045
    • View Profile
    • development blog
    • Email
Re: setFrameLimit causes absurd CPU usage
« Reply #5 on: November 07, 2023, 09:55:33 am »
Yeah, mixing VSync and setFramerateLimit is not a good idea.

No, there isn't really a way to check it from SFML, beyond just measuring yourself.
At the end of the day, if a user decides to set VSync globally, it's their decision, but they also need to live with the consequences of odd behavior.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything