From 86bf20a4cf2f6a2fb980b3b35f1d18a3801476a9 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 29 Nov 2022 15:02:11 -0500
Subject: [PATCH] Implemented gamma/brightness APIs removed from SDL3.
---
src/sdl2_compat.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 0907be9..ab290a5 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -730,6 +730,88 @@ SDL_RWFromFP(FILE *fp, SDL_bool autoclose)
}
#endif
+
+/* All gamma stuff was removed from SDL3 because it affects the whole system
+ in intrusive ways, and often didn't work on various platforms. These all
+ just return failure now. */
+
+DECLSPEC int SDLCALL
+SDL_SetWindowBrightness(SDL_Window *window, float brightness)
+{
+ return SDL3_Unsupported();
+}
+
+DECLSPEC float SDLCALL
+SDL_GetWindowBrightness(SDL_Window *window)
+{
+ return 1.0f;
+}
+
+DECLSPEC int SDLCALL
+SDL_SetWindowGammaRamp(SDL_Window *window, const Uint16 *r, const Uint16 *g, const Uint16 *b)
+{
+ return SDL3_Unsupported();
+}
+
+DECLSPEC void SDLCALL
+SDL_CalculateGammaRamp(float gamma, Uint16 *ramp)
+{
+ int i;
+
+ /* Input validation */
+ if (gamma < 0.0f) {
+ SDL_InvalidParamError("gamma");
+ return;
+ }
+ if (ramp == NULL) {
+ SDL_InvalidParamError("ramp");
+ return;
+ }
+
+ /* 0.0 gamma is all black */
+ if (gamma == 0.0f) {
+ SDL_memset(ramp, 0, 256 * sizeof(Uint16));
+ return;
+ } else if (gamma == 1.0f) {
+ /* 1.0 gamma is identity */
+ for (i = 0; i < 256; ++i) {
+ ramp[i] = (i << 8) | i;
+ }
+ return;
+ } else {
+ /* Calculate a real gamma ramp */
+ int value;
+ gamma = 1.0f / gamma;
+ for (i = 0; i < 256; ++i) {
+ value =
+ (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
+ if (value > 65535) {
+ value = 65535;
+ }
+ ramp[i] = (Uint16) value;
+ }
+ }
+}
+
+DECLSPEC int SDLCALL
+SDL_GetWindowGammaRamp(SDL_Window *window, Uint16 *red, Uint16 *blue, Uint16 *green)
+{
+ Uint16 *buf = red ? red : (green ? green : blue);
+ if (buf) {
+ SDL_CalculateGammaRamp(1.0f, buf);
+ if (red && (red != buf)) {
+ SDL_memcpy(red, buf, 256 * sizeof (Uint16));
+ }
+ if (green && (green != buf)) {
+ SDL_memcpy(green, buf, 256 * sizeof (Uint16));
+ }
+ if (blue && (blue != buf)) {
+ SDL_memcpy(blue, buf, 256 * sizeof (Uint16));
+ }
+ }
+ return 0;
+}
+
#ifdef __cplusplus
}
#endif