Display Mode Safety Timer

How can we implement a system that reverts to the previous display mode after a timer if the user doesn't confirm the changes?

Here's how to implement a safety timer for display mode changes:

#include <SDL.h>
#include <iostream>

class DisplayModeGuard {
  SDL_Window* Window;
  SDL_DisplayMode PreviousMode;
  Uint32 StartTime;
  Uint32 TimeoutMs;
  bool Confirmed{false};

public:
  DisplayModeGuard(
    SDL_Window* Window,
    Uint32 TimeoutMs
  ) : Window{Window}, TimeoutMs{TimeoutMs} {
    // Store current mode before changes
    SDL_GetWindowDisplayMode(
      Window, &PreviousMode);
    StartTime = SDL_GetTicks();
  }

  void Confirm() { Confirmed = true; }

  bool HasExpired() const {
    return !Confirmed && (SDL_GetTicks() -
      StartTime >= TimeoutMs);
  }

  void RevertIfNeeded() {
    if (HasExpired()) {
      SDL_SetWindowDisplayMode(
        Window, &PreviousMode);
    }
  }

  ~DisplayModeGuard() { RevertIfNeeded(); }
};

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* Window{SDL_CreateWindow(
    "Display Mode Test",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600, 0
  )};

  // Create guard with 15 second timeout
  DisplayModeGuard Guard{Window, 15000};

  // Try new display mode
  SDL_DisplayMode NewMode{
    0, 1920, 1080, 60, nullptr};
  SDL_SetWindowDisplayMode(Window, &NewMode);
  SDL_SetWindowFullscreen(
    Window, SDL_WINDOW_FULLSCREEN);

  std::cout << "Press Enter to confirm new display"
    " mode or wait 15 seconds to revert\n";

  // Simple event loop
  bool Running{true};
  while (Running) {
    SDL_Event Event;
    while (SDL_PollEvent(&Event)) {
      if (Event.type == SDL_KEYDOWN &&
        Event.key.keysym.sym == SDLK_RETURN) {
        Guard.Confirm();
        Running = false;
      } else if (Event.type == SDL_QUIT) {
        Running = false;
      }
    }

    if (Guard.HasExpired()) { Running = false; }

    SDL_Delay(16);
  }

  SDL_DestroyWindow(Window);
  SDL_Quit();
  return 0;
}

This implementation uses RAII (Resource Acquisition Is Initialization) to ensure the display mode reverts if not confirmed. The DisplayModeGuard class:

  • Stores the previous display mode when constructed
  • Tracks time since the change was made
  • Automatically reverts if not confirmed within the timeout period
  • Uses RAII to guarantee cleanup even if the program crashes

You could expand this system by adding a visual countdown or handling additional user input types.

Display Modes

Learn how to manage screen resolutions and refresh rates in SDL games using display modes.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Handling Unsupported Display Modes
What happens if a player has a monitor that doesn't support the display mode our game wants to use?
Scaling Retro Games
If I'm making a retro-style game that uses a low resolution, how can I make it look good on modern high-resolution displays?
Display Settings and Game Restart
Why do some games require a restart after changing display settings while others can change them instantly?
Saving Fullscreen Preferences
How can I save the player's preferred fullscreen mode and restore it the next time they launch the game?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant