SDL: Removed display mode flags

From 14338ab459b43754ddfa89748b1b5289d98f2c05 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 31 Jan 2023 23:35:56 -0800
Subject: [PATCH] Removed display mode flags

They weren't really adding any value and added complexity to the API
---
 include/SDL3/SDL_video.h             | 11 -----
 src/video/SDL_sysvideo.h             |  1 +
 src/video/SDL_video.c                | 67 +++++-----------------------
 src/video/cocoa/SDL_cocoamodes.m     |  2 +-
 src/video/cocoa/SDL_cocoaopengl.m    |  2 +-
 src/video/n3ds/SDL_n3dsvideo.c       |  5 +--
 src/video/wayland/SDL_waylandvideo.c |  5 +--
 src/video/x11/SDL_x11messagebox.c    |  5 +--
 src/video/x11/SDL_x11modes.c         |  5 +--
 9 files changed, 22 insertions(+), 81 deletions(-)

diff --git a/include/SDL3/SDL_video.h b/include/SDL3/SDL_video.h
index fa1c73d6c457..6a8002276dc4 100644
--- a/include/SDL3/SDL_video.h
+++ b/include/SDL3/SDL_video.h
@@ -43,16 +43,6 @@ extern "C" {
 typedef Uint32 SDL_DisplayID;
 typedef Uint32 SDL_WindowID;
 
-/**
- *  \brief The flags on a display mode
- */
-typedef enum
-{
-    SDL_DISPLAYMODE_DESKTOP         = 0x00000001,   /**< The display uses this as the desktop mode */
-    SDL_DISPLAYMODE_CURRENT         = 0x00000002,   /**< The display is currently using this mode */
-
-} SDL_DisplayModeFlags;
-
 /**
  *  \brief  The structure that defines a display mode
  *
@@ -65,7 +55,6 @@ typedef enum
 typedef struct
 {
     SDL_DisplayID displayID;    /**< the display this mode is associated with */
-    Uint32 flags;               /**< whether this mode is the current mode or a desktop mode */
     Uint32 format;              /**< pixel format */
     int pixel_w;                /**< width in pixels (used for creating back buffers) */
     int pixel_h;                /**< height in pixels (used for creating back buffers) */
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 11f82a236115..0c78cdf56af7 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -154,6 +154,7 @@ struct SDL_VideoDisplay
     int num_fullscreen_modes;
     SDL_DisplayMode *fullscreen_modes;
     SDL_DisplayMode desktop_mode;
+    const SDL_DisplayMode *current_mode;
     SDL_DisplayOrientation orientation;
 
     SDL_Window *fullscreen_window;
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 73665a5f9f2d..03c578333c4f 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -649,7 +649,7 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
         }
 
         new_display->desktop_mode.displayID = id;
-        new_display->desktop_mode.flags = (SDL_DISPLAYMODE_DESKTOP | SDL_DISPLAYMODE_CURRENT);
+        new_display->current_mode = &new_display->desktop_mode;
         SDL_FinalizeDisplayMode(&new_display->desktop_mode);
 
         if (send_event) {
@@ -782,7 +782,6 @@ const char *SDL_GetDisplayName(SDL_DisplayID displayID)
 int SDL_GetDisplayBounds(SDL_DisplayID displayID, SDL_Rect *rect)
 {
     SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
-    const SDL_DisplayMode *mode;
 
     CHECK_DISPLAY_MAGIC(display, -1);
 
@@ -804,14 +803,8 @@ int SDL_GetDisplayBounds(SDL_DisplayID displayID, SDL_Rect *rect)
         SDL_GetDisplayBounds(_this->displays[SDL_GetDisplayIndex(displayID) - 1].id, rect);
         rect->x += rect->w;
     }
-    mode = SDL_GetCurrentDisplayMode(displayID);
-    if (mode) {
-        rect->w = mode->screen_w;
-        rect->h = mode->screen_h;
-    } else {
-        rect->w = 0;
-        rect->h = 0;
-    }
+    rect->w = display->current_mode->screen_w;
+    rect->h = display->current_mode->screen_h;
     return 0;
 }
 
@@ -929,22 +922,11 @@ SDL_bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_Displ
     new_mode.displayID = display->id;
     SDL_FinalizeDisplayMode(&new_mode);
 
-    if ((new_mode.flags & SDL_DISPLAYMODE_CURRENT) != 0) {
-        display->desktop_mode.flags &= ~SDL_DISPLAYMODE_CURRENT;
-        for (i = 0; i < display->num_fullscreen_modes; ++i) {
-            display->fullscreen_modes[i].flags &= ~SDL_DISPLAYMODE_CURRENT;
-        }
-    }
-
     /* Make sure we don't already have the mode in the list */
     modes = display->fullscreen_modes;
     nmodes = display->num_fullscreen_modes;
     for (i = 0; i < nmodes; ++i) {
         if (cmpmodes(&new_mode, &modes[i]) == 0) {
-            /* If the new mode is current, make sure we save that */
-            if ((new_mode.flags & SDL_DISPLAYMODE_CURRENT) != 0) {
-                SDL_memcpy(&modes[i], &new_mode, sizeof(modes[i]));
-            }
             return SDL_FALSE;
         }
     }
@@ -979,6 +961,7 @@ void SDL_ResetFullscreenDisplayModes(SDL_VideoDisplay *display)
     display->fullscreen_modes = NULL;
     display->num_fullscreen_modes = 0;
     display->max_fullscreen_modes = 0;
+    display->current_mode = &display->desktop_mode;
 }
 
 const SDL_DisplayMode **SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID, int *count)
@@ -1077,7 +1060,6 @@ void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode
 {
     SDL_memcpy(&display->desktop_mode, mode, sizeof(*mode));
     display->desktop_mode.displayID = display->id;
-    display->desktop_mode.flags = (SDL_DISPLAYMODE_DESKTOP | SDL_DISPLAYMODE_CURRENT);
     SDL_FinalizeDisplayMode(&display->desktop_mode);
 }
 
@@ -1092,48 +1074,21 @@ const SDL_DisplayMode *SDL_GetDesktopDisplayMode(SDL_DisplayID displayID)
 
 void SDL_SetCurrentDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
 {
-    int i;
-
-    if (mode == &display->desktop_mode) {
-        display->desktop_mode.flags |= SDL_DISPLAYMODE_CURRENT;
-    } else {
-        display->desktop_mode.flags &= ~SDL_DISPLAYMODE_CURRENT;
-    }
-
-    for (i = 0; i < display->num_fullscreen_modes; ++i) {
-        if (mode == &display->fullscreen_modes[i]) {
-            display->fullscreen_modes[i].flags |= SDL_DISPLAYMODE_CURRENT;
-        } else {
-            display->fullscreen_modes[i].flags &= ~SDL_DISPLAYMODE_CURRENT;
-        }
-    }
-    SDL_assert((mode->flags & SDL_DISPLAYMODE_CURRENT) != 0);
+    display->current_mode = mode;
 }
 
 const SDL_DisplayMode *SDL_GetCurrentDisplayMode(SDL_DisplayID displayID)
 {
     SDL_VideoDisplay *display = SDL_GetVideoDisplay(displayID);
-    const SDL_DisplayMode **modes;
-    const SDL_DisplayMode *current_mode = NULL;
 
     CHECK_DISPLAY_MAGIC(display, NULL);
 
-    modes = SDL_GetFullscreenDisplayModes(displayID, NULL);
-    if (modes) {
-        int i;
-
-        for (i = 0; modes[i]; ++i) {
-            if ((modes[i]->flags & SDL_DISPLAYMODE_CURRENT) != 0) {
-                current_mode = modes[i];
-                break;
-            }
-        }
-        SDL_free(modes);
-    }
-    if (current_mode == NULL) {
-        current_mode = &display->desktop_mode;
+    /* Make sure our mode list is updated */
+    if (display->num_fullscreen_modes == 0 && _this->GetDisplayModes) {
+        _this->GetDisplayModes(_this, display);
     }
-    return current_mode;
+
+    return display->current_mode;
 }
 
 static int SDL_SetDisplayModeForDisplay(SDL_VideoDisplay *display, SDL_DisplayMode *mode)
@@ -1142,7 +1097,7 @@ static int SDL_SetDisplayModeForDisplay(SDL_VideoDisplay *display, SDL_DisplayMo
         mode = &display->desktop_mode;
     }
 
-    if ((mode->flags & SDL_DISPLAYMODE_CURRENT) != 0) {
+    if (mode == display->current_mode) {
         return 0;
     }
 
diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m
index d597fbc79134..35289e05b09d 100644
--- a/src/video/cocoa/SDL_cocoamodes.m
+++ b/src/video/cocoa/SDL_cocoamodes.m
@@ -622,7 +622,7 @@ void Cocoa_QuitModes(_THIS)
         SDL_VideoDisplay *display = &_this->displays[i];
         SDL_DisplayModeData *mode;
 
-        if (SDL_GetCurrentDisplayMode(display->id) != SDL_GetDesktopDisplayMode(display->id)) {
+        if (display->current_mode->driverdata != display->desktop_mode.driverdata) {
             Cocoa_SetDisplayMode(_this, display, &display->desktop_mode);
         }
 
diff --git a/src/video/cocoa/SDL_cocoaopengl.m b/src/video/cocoa/SDL_cocoaopengl.m
index 937102d3ae7c..e4b68c9c5a12 100644
--- a/src/video/cocoa/SDL_cocoaopengl.m
+++ b/src/video/cocoa/SDL_cocoaopengl.m
@@ -301,7 +301,7 @@ SDL_GLContext Cocoa_GL_CreateContext(_THIS, SDL_Window *window)
         attr[i++] = profile;
 
         attr[i++] = NSOpenGLPFAColorSize;
-        attr[i++] = 32;
+        attr[i++] = SDL_BYTESPERPIXEL(display->current_mode->format) * 8;
 
         attr[i++] = NSOpenGLPFADepthSize;
         attr[i++] = _this->gl_config.depth_size;
diff --git a/src/video/n3ds/SDL_n3dsvideo.c b/src/video/n3ds/SDL_n3dsvideo.c
index a58635c9f9c7..6026a8501bfd 100644
--- a/src/video/n3ds/SDL_n3dsvideo.c
+++ b/src/video/n3ds/SDL_n3dsvideo.c
@@ -139,7 +139,6 @@ static void N3DS_VideoQuit(_THIS)
 static int N3DS_GetDisplayBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
 {
     SDL_DisplayData *driver_data = display->driverdata;
-    const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display->id);
 
     if (driver_data == NULL) {
         return -1;
@@ -147,8 +146,8 @@ static int N3DS_GetDisplayBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rec
 
     rect->x = 0;
     rect->y = (driver_data->screen == GFX_TOP) ? 0 : GSP_SCREEN_WIDTH;
-    rect->w = mode->screen_w;
-    rect->h = mode->screen_h;
+    rect->w = display->current_mode->screen_w;
+    rect->h = display->current_mode->screen_h;
     return 0;
 }
 
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index e0bdb344e0a3..e1bf8c8a5afe 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -972,11 +972,10 @@ int Wayland_VideoInit(_THIS)
 static int Wayland_GetDisplayBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
 {
     SDL_DisplayData *driverdata = display->driverdata;
-    const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display->id);
     rect->x = driverdata->x;
     rect->y = driverdata->y;
-    rect->w = mode->screen_w;
-    rect->h = mode->screen_h;
+    rect->w = display->current_mode->screen_w;
+    rect->h = display->current_mode->screen_h;
     return 0;
 }
 
diff --git a/src/video/x11/SDL_x11messagebox.c b/src/video/x11/SDL_x11messagebox.c
index d7825deee550..78fad7dd0584 100644
--- a/src/video/x11/SDL_x11messagebox.c
+++ b/src/video/x11/SDL_x11messagebox.c
@@ -470,9 +470,8 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
         if ((dev) && (dev->displays) && (dev->num_displays > 0)) {
             const SDL_VideoDisplay *dpy = &dev->displays[0];
             const SDL_DisplayData *dpydata = dpy->driverdata;
-            const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(dpy->id);
-            x = dpydata->x + ((mode->pixel_w - data->dialog_width) / 2);
-            y = dpydata->y + ((mode->pixel_h - data->dialog_height) / 3);
+            x = dpydata->x + ((dpy->current_mode->pixel_w - data->dialog_width) / 2);
+            y = dpydata->y + ((dpy->current_mode->pixel_h - data->dialog_height) / 3);
         } else { /* oh well. This will misposition on a multi-head setup. Init first next time. */
             x = (DisplayWidth(display, data->screen) - data->dialog_width) / 2;
             y = (DisplayHeight(display, data->screen) - data->dialog_height) / 3;
diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c
index d52be85047ce..da58c19fd83d 100644
--- a/src/video/x11/SDL_x11modes.c
+++ b/src/video/x11/SDL_x11modes.c
@@ -816,12 +816,11 @@ void X11_QuitModes(_THIS)
 int X11_GetDisplayBounds(_THIS, SDL_VideoDisplay *sdl_display, SDL_Rect *rect)
 {
     SDL_DisplayData *data = sdl_display->driverdata;
-    const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(sdl_display->id);
 
     rect->x = data->x;
     rect->y = data->y;
-    rect->w = mode->screen_w;
-    rect->h = mode->screen_h;
+    rect->w = sdl_display->current_mode->screen_w;
+    rect->h = sdl_display->current_mode->screen_h;
     return 0;
 }