UPDATE:
I still need help determining how MAX_UPDATES works. I want my game to be designed in such a way that if the user can't run the game at full speed, it slows down so that no updates are missed.. can someone help me understand that part of the code? Also, how can I calculate an accurate realtime fps?
double frametime = 1000000./60, remainingTime = 0; const unsigned int MAX_UPDATES = 10; void Game::gameLoop(sf::RenderWindow &window) { remainingTime += clock.restart().asMicroseconds(); unsigned int updates = 0; while (remainingTime >= frametime) { remainingTime -= frametime; if (updates++ < MAX_UPDATES) { update(); } } render(window); } Hi I'm learning how to create a 2d platformer in C++ using the SFML2 library. My current goal is limiting the game's framerate. SFML2 has a window.setFramerateLimit(unsigned int limit) function for the window however I have found it to be imprecise due to it using sf::sleep. Therefore I'm attempting to implement my own solution. I want my game to used a fixed timestep and be framerate dependent as in the future when I implement attacks I want them to be based on frame data.
Since the game loop wont always execute exactly when a frame is supposed to happen, I create a delta variable that will account for the extra time spent starting the frame to start the next frame earlier by that same amount.
Does this all look right?
double frametime = 1000000./60, delta = 0; void Game::gameLoop(sf::RenderWindow &window) { t = clock.getElapsedTime().asMicroseconds(); if (t + delta >= frametime) { clock.restart(); delta = t + delta - frametime; update(); } render(window); } edit: double shouldn't have f suffix doh
double frametime = 1000000.f/60why are you casting as floats? if you use a double, it should bedouble frametime = 1000000.0/60.0... \$\endgroup\$