Fullscreen Windows

Restoring Display Settings After Crashes

When using exclusive fullscreen, how can I ensure the display settings are properly restored if my game crashes?

Abstract art representing computer programming

When SDL needs to make system-level changes, it generally does a good job of reverting those settings to their original values when our program ends - even when it crashes.

However, it’s not perfect, so we can add additional steps to minimise the risk of our program leaving the system in an altered state that our players will find frustrating.

This is especially true when using more impactful system-level changes, like exclusive fullscreen mode. We want to do as much as possible to prevent the player's monitor from being in the wrong resolution after our program terminates.

Using RAII for Safety

The most reliable approach is to use RAII (Resource Acquisition Is Initialization) to automatically restore display settings:

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

class DisplayModeGuard {
 public:
  DisplayModeGuard(SDL_Window* Window)
  : Window{Window} {
    // Store the initial display mode
    int DisplayIndex{
      SDL_GetWindowDisplayIndex(Window)};
    if (SDL_GetCurrentDisplayMode(
      DisplayIndex, &OriginalMode) != 0) {
      throw std::runtime_error{SDL_GetError()};
    }
  }

  ~DisplayModeGuard() {
    // Restore original display mode when
    // object is destroyed
    if (Window) {
      SDL_SetWindowDisplayMode(
        Window, &OriginalMode);  
    }
  }

 private:
  SDL_Window* Window;
  SDL_DisplayMode OriginalMode;
};

// Usage example
int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* Window{SDL_CreateWindow(
    "Game",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    1920, 1080,
    SDL_WINDOW_FULLSCREEN
  )};

  // Create guard to restore display settings
  // even if we crash
  DisplayModeGuard Guard{Window};  

  // Game loop here...

  SDL_Quit();
  return 0;
}

System-Level Fallback

For additional safety, consider creating a small helper application that can restore display settings:

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

// Save current display settings to a file
void SaveDisplaySettings() {
  SDL_DisplayMode Mode;
  SDL_GetWindowDisplayMode(Window, &Mode);

  std::ofstream File{
    "display_backup.dat",
    std::ios::binary
  };
  File.write(reinterpret_cast<char*>(&Mode),
    sizeof(Mode));
}

// Restore display settings from file
void RestoreDisplaySettings() {
  SDL_DisplayMode Mode;

  std::ifstream File{"display_backup.dat",
    std::ios::binary};
  File.read(reinterpret_cast<char*>(&Mode),
    sizeof(Mode));

  SDL_SetWindowDisplayMode(Window, &Mode);
}

The key is to implement multiple layers of protection since exclusive fullscreen directly modifies system settings. The RAII approach handles normal program flow and most crash scenarios, while the helper application provides a manual recovery option if needed.

This Question is from the Lesson:

Fullscreen Windows

Learn how to create and manage fullscreen windows in SDL, including desktop and exclusive fullscreen modes.

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

This Question is from the Lesson:

Fullscreen Windows

Learn how to create and manage fullscreen windows in SDL, including desktop and exclusive fullscreen modes.

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:

  • 71 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