Brightness and Gamma

Control Individual Window Brightness

Why does changing window brightness affect the whole display? Is there a way to only change my game's brightness?

Abstract art representing computer programming

SDL's brightness functions affect the entire display because they work by modifying the display's gamma lookup tables, which are hardware-level settings. To adjust brightness only for your game window, you'll need to implement the effect in your rendering pipeline instead. Here's how:

Using SDL_Renderer

If you're using SDL's renderer, you can modify the color of everything you render:

#include <SDL.h>

class BrightnessController {
  SDL_Renderer* Renderer;
  float Brightness{1.0f};

public:
  BrightnessController(SDL_Renderer* R) :
    Renderer{R} {}

  void SetBrightness(float NewBrightness) {
    Brightness = NewBrightness;
    // Convert brightness to SDL's 0-255 range
    Uint8 Value = static_cast<Uint8>(
      Brightness * 255);
    SDL_SetRenderDrawColor(
      Renderer, Value, Value, Value, 255
    );

    // Set blend mode to modulate
    // (multiply) colors
    SDL_SetRenderDrawBlendMode(
      Renderer, SDL_BLENDMODE_MOD
    );
  }

  void Apply() {
    // Create fullscreen overlay with
    // current brightness
    SDL_Rect FullScreen;
    SDL_RenderGetViewport(
      Renderer, &FullScreen);
    SDL_RenderFillRect(Renderer, &FullScreen);
  }
};

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
    )};

  SDL_Renderer* Renderer{
    SDL_CreateRenderer(
      Window, -1, SDL_RENDERER_ACCELERATED
    )};

  BrightnessController Controller{Renderer};

  // Your game loop:
  bool Running{true};
  while (Running) {
    // Clear screen
    SDL_RenderClear(Renderer);

    // Draw your game content here

    // Apply brightness effect
    Controller.SetBrightness(0.5f);
    // 50% brightness
    Controller.Apply();

    // Present the frame
    SDL_RenderPresent(Renderer);
  }

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

This approach has several advantages:

  • Only affects your game window
  • Works consistently across all platforms
  • Can be animated smoothly
  • Can be combined with other effects
  • No system permission issues

However, it does require more processing power than using the display's gamma tables, so test performance on your target platforms.

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