Brightness and Gamma

Save and Restore Brightness Settings

How can I save the player's preferred brightness settings and restore them the next time they launch the game?

Abstract art representing computer programming

Let's create a system to manage persistent brightness settings. We'll use a simple file-based approach, but you could adapt this to use your game's existing save system:

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

class BrightnessSettings {
  SDL_Window* Window;
  std::string SettingsPath;
  float DefaultBrightness{1.0f};

  struct Settings {
    float Brightness{1.0f};
    float GammaRed{1.0f};
    float GammaGreen{1.0f};
    float GammaBlue{1.0f};
  } Current;

public:
  BrightnessSettings(
    SDL_Window* W,const std::string& SavePath
  ) : Window{W},
      SettingsPath{SavePath + "/display.cfg"} {
    LoadSettings();
    ApplySettings();
  }

  void SaveSettings() {
    try {
      // Ensure directory exists
      std::filesystem::create_directories(
        std::filesystem::path{SettingsPath}.
        parent_path());

      std::ofstream File{
        SettingsPath, std::ios::binary};
      if (!File) {
        std::cout <<
          "Failed to save settings\n";
        return;
      }

      File.write(
        reinterpret_cast<char*>(&Current),
        sizeof(Settings));

      std::cout << "Settings saved\n";
    }
    catch (const std::exception& E) {
      std::cout << "Error saving settings: "
        << E.what() << '\n';
    }
  }

  void SetBrightness(float NewBrightness) {
    Current.Brightness = NewBrightness;
    SDL_SetWindowBrightness(
      Window, NewBrightness);
  }

  void SetGamma(float Red, float Green,
                float Blue) {
    Current.GammaRed = Red;
    Current.GammaGreen = Green;
    Current.GammaBlue = Blue;

    Uint16 RedRamp[256];
    Uint16 GreenRamp[256];
    Uint16 BlueRamp[256];

    SDL_CalculateGammaRamp(Red, RedRamp);
    SDL_CalculateGammaRamp(Green, GreenRamp);
    SDL_CalculateGammaRamp(Blue, BlueRamp);

    SDL_SetWindowGammaRamp(
      Window, RedRamp, GreenRamp, BlueRamp);
  }

private:
  void LoadSettings() {
    try {
      std::ifstream File{
        SettingsPath, std::ios::binary};
      if (!File) {
        std::cout << "Using default settings\n";
        return;
      }

      File.read(
        reinterpret_cast<char*>(&Current),
        sizeof(Settings));

      std::cout << "Settings loaded\n";
    }
    catch (const std::exception& E) {
      std::cout << "Error loading settings: "
        << E.what() << '\n';
      Current = Settings{}; // Use defaults
    }
  }

  void ApplySettings() {
    SetBrightness(Current.Brightness);
    SetGamma(Current.GammaRed,
             Current.GammaGreen,
             Current.GammaBlue);
  }
};

int main() {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* Window{
    SDL_CreateWindow("Settings Demo",
                     SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED,
                     800, 600,
                     SDL_WINDOW_SHOWN)};

  BrightnessSettings Settings{
    Window, "./saves"};

  // Simulate user changing settings
  Settings.SetBrightness(1.5f);
  Settings.SetGamma(1.1f, 1.0f, 0.9f);

  // Save changes
  Settings.SaveSettings();

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

This system:

  • Saves both brightness and gamma settings
  • Creates the save directory if needed
  • Handles loading/saving errors gracefully
  • Automatically applies saved settings on startup
  • Uses binary file format for efficiency

Consider adding:

  • Settings validation before saving
  • Version number in the save file
  • Configuration for save file location
  • Integration with your game's existing save system

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

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