Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fixed tickrate gameloop
- // Loops through Input and Update at fixed intervals, renders when possible
- // Game updated at fixed times, FPS can vary, and will not affect game speed
- void Engine::mainloop()
- {
- sf::Clock clock;
- double previous = clock.getElapsedTime().asSeconds();
- double metricsCounter = previous;
- double accumulator = 0.0;
- uint16_t tickCounter = 0;
- uint16_t frameCounter = 0;
- while (_isRunning)
- {
- double current = clock.getElapsedTime().asSeconds();
- // calculate time since last frame
- double elapsed = current - previous;
- metricsCounter += elapsed;
- // set previous frame time as now (ready to render a now frame)
- previous = current;
- // add cycle duration to the accumulator.
- // this is the update time + render time from previous cycle
- accumulator += elapsed;
- // when 1 second has passed, reset metrics and timer
- if (metricsCounter >= 1.0)
- {
- #ifdef _DEBUG
- // if we're debugging, set window title as the metrics display
- std::string title =
- " FPS: " + std::to_string(frameCounter) +
- " TickRate: " + std::to_string(tickCounter) +
- " Duration: " + std::to_string(clock.getElapsedTime().asSeconds());
- _window.setTitle(title);
- #endif
- metricsCounter = 0.0;
- frameCounter = tickCounter = 0;
- }
- // while accumulator larger than current game tickrate duration, do update
- while (accumulator >= GAMETICK_DURATION_MS)
- {
- processInput();
- update(GAMETICK_DURATION_MS);
- tickCounter++;
- accumulator -= GAMETICK_DURATION_MS;
- }
- // Rendering section. Render a frame, then increment frame to FPS counter
- render();
- frameCounter++;
- }
- _window.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement