How to limit game loop fps?

335 views Asked by At

I'm trying to limit my gameloop fps using SFML but it's not working correctly.

if I divide 60/1000 shouldn't the result be the time of each frame to get 60 FPS?

I put in 60 but I only get 33 does anyone know where I made a mistake?

Here is my header

class Game 
{
    //..
    sf::Clock deltaTimeClock;
    float deltaTime;

    const float frameTime = 60.0f/ 1000.0f;

    bool aSecondPassed();
    float currentFPS = 0;
    sf::Clock second;

//..
}

My implementation


    //...
    
    void Game::updateDeltaTime()
    {
        this->deltaTime = (this->deltaTimeClock.restart().asSeconds());
    
    }
    
    //...
    
    void Game::run()
    {
        while (this->window->isOpen())
        {
        this->showFPS();
        this->tick();
        this->render();
            
        while (deltaTime < this->frameTime)
        {
            this->deltaTime = this->deltaTimeClock.getElapsedTime().asSeconds();
    
        }
    
        this->updateDeltaTime();
    }
}

1

There are 1 answers

0
David Rocha On

Getting a stable 60 fps by updating the loop condition to

this->deltaTimeClock.getElapsedTime().asSeconds() < this->frameTime

But I will study more on the subject, maybe there is a better way to implement this. Thank you all for your help.