Saving Display Preferences

How can I save the player's preferred fullscreen mode and restore it the next time they launch the game?

Saving and restoring player preferences is an essential feature for any game. Here's how to implement a robust system for handling fullscreen preferences:

Basic Implementation

Here's a basic framework that we can use to save and load display preferences:

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

struct DisplayPreferences {
  enum class Mode {
    Windowed,
    DesktopFullscreen,
    ExclusiveFullscreen
  };

  Mode FullscreenMode{Mode::Windowed};
  int WindowWidth{1920};
  int WindowHeight{1080};
  int DisplayIndex{0};
};

void SavePreferences(
  const DisplayPreferences& Prefs) {
  std::ofstream File{
    "display_prefs.dat", std::ios::binary};
  File.write(
    reinterpret_cast<const char*>(&Prefs),
    sizeof(DisplayPreferences));
}

DisplayPreferences LoadPreferences() {
  DisplayPreferences Prefs;
  std::ifstream File{
    "display_prefs.dat", std::ios::binary};

  if (File) {
    File.read(reinterpret_cast<char*>(&Prefs),
              sizeof(DisplayPreferences));
  }

  return Prefs;
}

void ApplyPreferences(
  SDL_Window* Window,
  const DisplayPreferences& Prefs
) {
  // First set window size
  SDL_SetWindowSize(
    Window,
    Prefs.WindowWidth,
    Prefs.WindowHeight);

  // Then apply fullscreen mode
  switch (Prefs.FullscreenMode) {
  case DisplayPreferences::Mode::Windowed:
    SDL_SetWindowFullscreen(Window, 0);
    break;
  case
  DisplayPreferences::Mode::DesktopFullscreen:
    SDL_SetWindowFullscreen(
      Window, SDL_WINDOW_FULLSCREEN_DESKTOP);
    break;
  case
  DisplayPreferences::Mode::ExclusiveFullscreen:
    SDL_SetWindowFullscreen(
      Window, SDL_WINDOW_FULLSCREEN);
    break;
  }
}

Using JSON for Better Flexibility

For a more maintainable solution, consider using JSON to store preferences:

#include <SDL.h>
#include <fstream>
#include <nlohmann/json.hpp>

void SavePreferencesJson(
  const DisplayPreferences& Prefs
) {
  nlohmann::json Json;
  Json["fullscreen_mode"] =
    static_cast<int>(Prefs.FullscreenMode);
  Json["window_width"] =
    Prefs.WindowWidth;
  Json["window_height"] =
    Prefs.WindowHeight;
  Json["display_index"] =
    Prefs.DisplayIndex;

  std::ofstream File{"display_prefs.json"};
  
  // Pretty print with 2 space indent
  File << Json.dump(2);
}

The JSON approach makes the preferences file human-readable and easier to modify or debug. However, you'll need to include a JSON library in your project. We cover the popular nlohmann::json library here:

Using JSON in Modern C++

A practical guide to working with the JSON data format in C++ using the popular nlohmann::json library.

Fullscreen Windows

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

Questions & Answers

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

Desktop vs Exclusive Fullscreen
Why would we want to use desktop fullscreen mode if exclusive fullscreen has better performance?
Managing Different Aspect Ratios
What happens if a player has a monitor with a different aspect ratio than our game's resolution?
Smooth Fullscreen Transitions
How can I smoothly transition between windowed and fullscreen modes without the screen going black?
Using Bitwise AND with Window Flags
What's the purpose of the bitwise AND operator (&) when checking window flags? Why can't we use == instead?
Restoring Display Settings After Crashes
When using exclusive fullscreen, how can I ensure the display settings are properly restored if my game crashes?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant