Brightness and Gamma

Create a Brightness Settings Slider

How do I implement a brightness slider in my game's settings menu?

Abstract art representing computer programming

Let's implement a reusable brightness control system that integrates with a settings menu:

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

class BrightnessControl {
  SDL_Window* Window;
  float CurrentBrightness{1.0f};
  float MinBrightness{0.5f};
  float MaxBrightness{2.0f};

public:
  BrightnessControl(SDL_Window* W) : Window{W} {
    // Store initial brightness
    CurrentBrightness = SDL_GetWindowBrightness(
      Window);
  }

  // Convert 0-100 slider value to brightness
  void SetFromSlider(int SliderValue) {
    float Normalized{SliderValue / 100.0f};
    float NewBrightness{
      MinBrightness +
      (MaxBrightness - MinBrightness) *
      Normalized
    };

    SetBrightness(NewBrightness);
  }

  // Convert current brightness to 0-100 slider value
  int GetSliderValue() const {
    float Normalized{
      (CurrentBrightness - MinBrightness) /
      (MaxBrightness - MinBrightness)
    };

    return static_cast<int>(Normalized *
      100.0f);
  }

  void SetBrightness(float NewBrightness) {
    // Clamp to valid range
    if (NewBrightness < MinBrightness) {
      NewBrightness = MinBrightness;
    }
    if (NewBrightness > MaxBrightness) {
      NewBrightness = MaxBrightness;
    }

    if (SDL_SetWindowBrightness(
      Window, NewBrightness) < 0) {
      std::cout << "Brightness error: "
        << SDL_GetError() << '\n';
      return;
    }

    CurrentBrightness = NewBrightness;
  }

  float GetBrightness() const {
    return CurrentBrightness;
  }
};

// Example usage in a settings menu
int main() {
  SDL_Init(SDL_INIT_VIDEO);

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

  BrightnessControl Control{Window};

  // Simulate user moving slider to 75%
  Control.SetFromSlider(75);
  std::cout << "Brightness set to: "
    << Control.GetBrightness() << '\n';

  // Get slider position for UI
  std::cout << "Slider position: "
    << Control.GetSliderValue() << '\n';

  SDL_DestroyWindow(Window);
  SDL_Quit();
  return 0;
}
Brightness set to: 1.75
Slider position: 75

This implementation:

  • Maps slider values (0-100) to brightness values
  • Handles error checking
  • Provides easy integration with UI systems
  • Maintains reasonable brightness limits
  • Can retrieve current values for UI updates

Consider adding a preview feature that temporarily applies brightness changes while the user adjusts the slider, only saving the setting when they click "Apply" or "Save".

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