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

Author Topic: What is wrong with my self made frame rate limit code?  (Read 4532 times)

0 Members and 1 Guest are viewing this topic.

maltman

  • Newbie
  • *
  • Posts: 5
    • View Profile
What is wrong with my self made frame rate limit code?
« on: December 20, 2019, 08:05:49 am »
Using window.setFramerateLimit(60), I get consistent FPS between 58-62, but I wanted to do things myself to be sure I understand how it works, so I made this code here: (notice the code in the loop function at the bottom.)


class SystemClock
        {
        public:
                void establishFrameTime()
                {
                        frametime = (double)frameClock.restart().asMicroseconds()/e6;

                        if (tempavgFrameTime == 0.0)
                        {
                                tempavgFrameTime = frametime;
                        }
                        else
                        {
                                tempavgFrameTime = (tempavgFrameTime + frametime) / 2.0;
                        }
                        accumulatedFrameTime += frametime;
                        if (accumulatedFrameTime >= 1.0)
                        {
                                avgFrameTime = tempavgFrameTime;
                                FPS = 1 / avgFrameTime;
                                accumulatedFrameTime = 0.0;
                                tempavgFrameTime = 0.0;
                        }
                }

                double const fixedTimeStep{ 1.0 / 60.0 };
                double const e6{ 1000000 };
                sf::Clock tickClock;
                sf::Clock frameClock;
                sf::Clock timeClock;

                // time data
                double FPS{ 0.0 };
                double frametime{ 0.0 };
                double avgFrameTime{ 0.0 };

        private:
                double tempavgFrameTime{ 0.0 };
                double accumulatedFrameTime{ 0.0 };
        };


        void loop()
        {
                while (systemWindow.window.isOpen())
                {
                        systemClock.establishFrameTime();
                        if (systemClock.frametime < systemClock.fixedTimeStep)
                        {
                                sf::Int64 remainder{ (sf::Int64)((systemClock.fixedTimeStep - systemClock.frametime) * systemClock.e6)};
                                sf::sleep(sf::microseconds(remainder));
                        }
                        update();
                        render();
                }
        }
 


When I run this, the framerate fluctuates between 80-100 sometimes spiking to 160. Why are my frames running too fast, am I not using sf::sleep correctly? Or is it my IDE im running visual studio in debug mode(but then why would there be a difference between this and window.setframerateLimit()

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What is wrong with my self made frame rate limit code?
« Reply #1 on: January 06, 2020, 10:02:19 am »
You're doing more than just limiting your framerate.
I don't really have the time right now to debug your code, but you can take a look at how SFML implements setFramerateLimit, in case you're looking for a simple solution: https://github.com/SFML/SFML/blob/master/src/SFML/Window/Window.cpp#L222
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything