Movement Without Framerate Limit C++ SFML

前端 未结 2 1339
别跟我提以往
别跟我提以往 2021-01-01 02:29

I\'m working an making a game in SFML right now, but I am getting stuck on movement without a framerate limit. Right now the only way I have figured out how to get consisten

2条回答
  •  死守一世寂寞
    2021-01-01 03:22

    @Waty's answer is right, but you might want to use fixed time step.

    Take a look at the SFML Game development book source code. Here's the interesting snippet:

    const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
    
    // ...
    
    sf::Clock clock;
    sf::Time timeSinceLastUpdate = sf::Time::Zero;
    while (mWindow.isOpen())
    {
        sf::Time elapsedTime = clock.restart();
        timeSinceLastUpdate += elapsedTime;
        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;
    
            processEvents();
            update(TimePerFrame);
    
        }
    
        updateStatistics(elapsedTime);
        render();
    }
    

提交回复
热议问题