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:
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:
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.
Learn how to control display brightness and gamma correction using SDL's window management functions