Saving Fullscreen Preferences

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

Here's a comprehensive solution for managing fullscreen preferences:

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

enum class FullscreenMode {
  Windowed,
  Fullscreen,
  BorderlessFullscreen
};

struct WindowSettings {
  FullscreenMode Mode{FullscreenMode::Windowed};
  int Width{800};
  int Height{600};
};

void SaveSettings(
  const WindowSettings& Settings) {
  std::ofstream File{"window_settings.cfg"};
  if (File) {
    File << static_cast<int>(Settings.Mode)
      << "\n"
      << Settings.Width << "\n"
      << Settings.Height;
  }
}

WindowSettings LoadSettings() {
  WindowSettings Settings;
  std::ifstream File{"window_settings.cfg"};
  if (File) {
    int Mode;
    File >> Mode;
    Settings.Mode = static_cast<FullscreenMode>(
      Mode);
    File >> Settings.Width >> Settings.Height;
  }
  return Settings;
}

Uint32 GetSDLFullscreenFlags(FullscreenMode Mode) {
  switch (Mode) {
  case FullscreenMode::Fullscreen:
    return SDL_WINDOW_FULLSCREEN;
  case FullscreenMode::BorderlessFullscreen:
    return SDL_WINDOW_FULLSCREEN_DESKTOP;
  default:
    return 0;
  }
}

bool ApplyWindowSettings(
  SDL_Window* Window,
  const WindowSettings& Settings) {
  // Store current display mode for fallback
  SDL_DisplayMode CurrentMode;
  SDL_GetWindowDisplayMode(
    Window, &CurrentMode);

  // Apply new size if windowed
  if (Settings.Mode ==
    FullscreenMode::Windowed) {
    SDL_SetWindowSize(
      Window,
      Settings.Width,
      Settings.Height
    );
  }

  // Apply fullscreen mode
  if (SDL_SetWindowFullscreen(
    Window,
    GetSDLFullscreenFlags(Settings.Mode)) < 0) {
    // Revert on failure
    SDL_SetWindowFullscreen(Window, 0);
    SDL_SetWindowSize(
      Window, CurrentMode.w, CurrentMode.h);
    return false;
  }

  return true;
}

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

  // Load saved settings
  WindowSettings Settings{LoadSettings()};

  // Create initial window
  SDL_Window* Window{
    SDL_CreateWindow(
      "Game",
      SDL_WINDOWPOS_UNDEFINED,
      SDL_WINDOWPOS_UNDEFINED,
      Settings.Width,
      Settings.Height,
      GetSDLFullscreenFlags(Settings.Mode)
    )};

  // Game loop would go here...

  // Save settings before exit
  SaveSettings(Settings);

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

This implementation:

  • Supports three fullscreen modes: windowed, exclusive fullscreen, and borderless fullscreen
  • Saves and loads preferences from a configuration file
  • Includes safety checks and fallback options
  • Properly handles window sizing for different modes
  • Uses type-safe enums for fullscreen modes

For better error handling, you might want to add:

  • Validation of loaded settings
  • User notifications for setting application failures
  • Multiple fallback options for various failure scenarios
  • Settings migration for handling older config file versions

Remember to handle the window size appropriately when switching between modes - borderless fullscreen will use the desktop resolution, while exclusive fullscreen uses the configured resolution.

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 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?
Display Settings and Game Restart
Why do some games require a restart after changing display settings while others can change them instantly?
Or Ask your Own Question
Purchase the course to ask your own questions