Tick Rate and Time Deltas

Handling Large Time Deltas

How can we handle very large time deltas that might occur if the game is paused or minimized?

Abstract art representing computer programming

Handling large time deltas is crucial for maintaining game stability and preventing unexpected behavior when a game is paused, minimized, or experiences a sudden lag spike. Here are some strategies to deal with this issue:

Capping the Time Delta

The simplest approach is to cap the time delta to a maximum value. This prevents the game from trying to catch up all at once if there's a long pause:

#include <SDL.h>
#include <algorithm>

// Maximum of 100ms
const float MAX_DELTA_TIME = 0.1f;

void updateGame(float deltaTime) {
  // Game update logic here
}

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  Uint64 previousTime = SDL_GetTicks64();

  while (true) {
    Uint64 currentTime = SDL_GetTicks64();
    float deltaTime = (currentTime -
      previousTime) / 1000.0f;

    // Cap the delta time
    deltaTime = std::min(deltaTime,
                         MAX_DELTA_TIME);

    updateGame(deltaTime);

    previousTime = currentTime;
  }

  SDL_Quit();
  return 0;
}

Accumulating Time and Updating in Fixed Steps

Another approach is to accumulate time and update the game in fixed time steps. This method is particularly useful for physics simulations:

#include <SDL.h>

// 60 updates per second
const float FIXED_TIME_STEP = 0.016f;

void updateGame(float deltaTime) {
  // Game update logic here
}

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  Uint64 previousTime = SDL_GetTicks64();
  float accumulatedTime = 0.0f;

  while (true) {
    Uint64 currentTime = SDL_GetTicks64();
    float deltaTime = (currentTime -
      previousTime) / 1000.0f;
    accumulatedTime += deltaTime;

    while (accumulatedTime >= FIXED_TIME_STEP) {
      updateGame(FIXED_TIME_STEP);
      accumulatedTime -= FIXED_TIME_STEP;
    }

    previousTime = currentTime;
  }

  SDL_Quit();
  return 0;
}

This approach ensures that the game logic always updates with a consistent time step, regardless of how much real time has passed.

Pausing Game Logic

When the game is explicitly paused or minimized, you might want to skip updating game logic entirely:

#include <SDL.h>

bool isPaused = false;

void updateGame(float deltaTime) {
  // Game update logic here
}

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  Uint64 previousTime = SDL_GetTicks64();

  while (true) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_WINDOWEVENT) {
        if (event.window.event ==
          SDL_WINDOWEVENT_MINIMIZED) {
          isPaused = true;
        } else if (event.window.event ==
          SDL_WINDOWEVENT_RESTORED) {
          isPaused = false;
          // Reset the previous time
          previousTime = SDL_GetTicks64();
        }
      }
    }

    if (!isPaused) {
      Uint64 currentTime = SDL_GetTicks64();
      float deltaTime = (currentTime -
        previousTime) / 1000.0f;
      updateGame(deltaTime);
      previousTime = currentTime;
    }
  }

  SDL_Quit();
  return 0;
}

By implementing these strategies, you can ensure that your game handles large time deltas gracefully, maintaining a smooth and consistent experience for players even when unexpected pauses or slowdowns occur.

This Question is from the Lesson:

Tick Rate and Time Deltas

Learn how to create smooth, time-aware game loops that behave consistently across different hardware configurations

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Tick Rate and Time Deltas

Learn how to create smooth, time-aware game loops that behave consistently across different hardware configurations

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved