void IMainGame::run() { if (!init()) return; const float DESIRED_FPS = 60.0f; // FPS the game is designed to run at const int MAX_PHYSICS_STEPS = 1; // Max number of physics steps per frame const float MS_PER_SECOND = 1000; // Number of milliseconds in a second const float DESIRED_FRAMETIME = MS_PER_SECOND / DESIRED_FPS; // The desired frame time per frame const float MAX_DELTA_TIME = 3.0f; // Maximum size of deltaTime FpsLimiter limiter; limiter.setMaxFPS(DESIRED_FPS); m_isRunning = true; float previousTicks = SDL_GetTicks(); while (m_isRunning) { limiter.begin(); // Calculate the frameTime in milliseconds float newTicks = SDL_GetTicks(); float frameTime = newTicks - previousTicks; previousTicks = newTicks; // Store newTicks in previousTicks so we can use it next frame // Get the total delta time float totalDeltaTime = frameTime / DESIRED_FRAMETIME; inputManager.update(); int i = 0; while (totalDeltaTime > 0.0f && i < MAX_PHYSICS_STEPS && m_isRunning) { // The deltaTime should be the the smaller of the totalDeltaTime and MAX_DELTA_TIME float deltaTime = std::min(totalDeltaTime, MAX_DELTA_TIME); // Update all physics here and pass in deltaTime update(deltaTime); if (m_isRunning) { draw(); m_fps = limiter.end(); m_window.swapBuffers(); } // Since we just took a step that is length deltaTime, subtract from totalDeltaTime totalDeltaTime -= deltaTime; // Increment our frame counter so we can limit steps to MAX_PHYSICS_STEPS i++; } } }
void IMainGame::run() { if (!init()) return; FpsLimiter limiter; limiter.setMaxFPS(60.0f); // Game loop m_isRunning = true; while (m_isRunning) { limiter.begin(); inputManager.update(); // Call the custom update and draw method update(); if (m_isRunning) { draw(); m_fps = limiter.end(); m_window.swapBuffer(); } } }