From fa66b879656933e8c00ba5990cce0baf09726a9b Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 04:16:11 +0100 Subject: [PATCH 1/6] cmp_resolutions: compare by numbers directly instead by comparing it with strings --- src/sdl/i_video.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 598b420fb..962b69da2 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -1531,17 +1531,12 @@ static const struct { static const int num_canonicals = sizeof(canonicals)/sizeof(*canonicals); // [FG] sort resolutions by width first and height second -static int cmp_resolutions (const void *a, const void *b) +static int cmp_resolutions(const void *a, const void *b) { - const char *const *sa = (const char *const *) a; - const char *const *sb = (const char *const *) b; + const video_mode_t *ma = (const video_mode_t *) a; + const video_mode_t *mb = (const video_mode_t *) b; - int wa, wb, ha, hb; - - if (sscanf(*sa, "%dx%d", &wa, &ha) != 2) wa = ha = 0; - if (sscanf(*sb, "%dx%d", &wb, &hb) != 2) wb = hb = 0; - - return (wa == wb) ? hb - ha : wb - wa; + return (ma->w == mb->w) ? mb->h - ma->h : mb->w - ma->w; } static void I_AppendResolution(SDL_DisplayMode *mode, int *list_size) From d6bbb2c1b3004d37aaa2f3e3c0d6f024e7901dea Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 04:22:29 +0100 Subject: [PATCH 2/6] use static buffer for windowedModes names no need to worry about memory management here :chaosleep: --- src/sdl/i_video.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 962b69da2..56a2d1eb5 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -90,14 +90,14 @@ static void I_FillScreenResolutionsList(boolean force); typedef struct { - const char *name; + char name[10]; INT32 w; INT32 h; } video_mode_t; -static video_mode_t windowedModes[MAXWINMODES]; +static video_mode_t windowedModes[MAXWINMODES] = {{"320x200", 320, 200}}; // make sure the default mode exists static const char *fallback_resolution_name = "Fallback"; -static int vid_nummodes = 0; +static int vid_nummodes = 1; rendermode_t rendermode = render_none; @@ -1542,15 +1542,15 @@ static int cmp_resolutions(const void *a, const void *b) static void I_AppendResolution(SDL_DisplayMode *mode, int *list_size) { int i; - char mode_name[256]; - - snprintf(mode_name, sizeof(mode_name), "%dx%d", mode->w, mode->h); for (i = 0; i < *list_size; i++) - if (!strcmp(mode_name, windowedModes[i].name)) + if (windowedModes[i].w == mode->w + && windowedModes[i].h == mode->h) return; - windowedModes[*list_size].name = strdup(mode_name); + snprintf(windowedModes[*list_size].name, + sizeof(windowedModes[*list_size].name), + "%dx%d", mode->w, mode->h); windowedModes[*list_size].w = mode->w; windowedModes[*list_size].h = mode->h; @@ -1567,9 +1567,9 @@ static void I_FillScreenResolutionsList(boolean force) int currentDisplayIndex = -1; static int oldDisplayIndex = -1; SDL_DisplayMode mode; - int i, list_size; + int i = 0, list_size = 0; int count = 0; - char desired_resolution[256]; + char custom_resolution[10]; currentDisplayIndex = SDL_GetWindowDisplayIndex(window); @@ -1618,7 +1618,7 @@ static void I_FillScreenResolutionsList(boolean force) I_AppendResolution(&mode, &list_size); } - windowedModes[list_size].name = NULL; + windowedModes[list_size].name[0] = '\0'; } INT32 custom_w = cv_scr_width.value; @@ -1644,10 +1644,12 @@ static void I_FillScreenResolutionsList(boolean force) { if (list_size < MAXWINMODES) { - snprintf(desired_resolution, sizeof(desired_resolution), "%dx%d", custom_w, custom_h); + snprintf(custom_resolution, sizeof(custom_resolution), "%dx%d", custom_w, custom_h); // [FG] if the desired resolution not in the list, append it - windowedModes[list_size].name = strdup(desired_resolution); + snprintf(windowedModes[list_size].name, + sizeof(windowedModes[list_size].name), + "%dx%d", custom_w, custom_h); windowedModes[list_size].w = custom_w; windowedModes[list_size].h = custom_h; list_size++; @@ -1660,7 +1662,7 @@ static void I_FillScreenResolutionsList(boolean force) // [FG] sort the list SDL_qsort(windowedModes, list_size, sizeof(*windowedModes), cmp_resolutions); - windowedModes[list_size].name = NULL; + windowedModes[list_size].name[0] = '\0'; vid_nummodes = list_size; // be sure to update the video menu @@ -2104,12 +2106,6 @@ void I_StartupGraphics(void) // Fury: we do window initialization after GL setup to allow // SDL_GL_LoadLibrary to work well on Windows - // make sure the default mode exists - windowedModes[0].name = "320x200"; - windowedModes[0].w = 320; - windowedModes[0].h = 200; - vid_nummodes = 1; - // Create window VID_SetMode(VID_GetModeForSize(BASEVIDWIDTH, BASEVIDHEIGHT)); From 6fd14f0a803d56ab54e397277138f4eddc310997 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 04:24:28 +0100 Subject: [PATCH 3/6] M_VideoModeMenu: remove old video mode dupe check --- src/m_menu.c | 62 ++++++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 112c481a4..813db7459 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -9416,7 +9416,7 @@ static modedesc_t modedescs[MAXMODEDESCS]; void M_VideoModeMenu(INT32 choice) { - INT32 i, j, vdup, nummodes, width, height; + INT32 i, nummodes, width, height; const char *desc; (void)choice; @@ -9430,57 +9430,30 @@ void M_VideoModeMenu(INT32 choice) nummodes = VID_NumModes(); // DOS does not skip mode 0, because mode 0 is ALWAYS present - i = 0; - for (; i < nummodes && vidm_nummodes < MAXMODEDESCS; i++) + for (i = 0; i < nummodes && vidm_nummodes < MAXMODEDESCS; i++) { desc = VID_GetModeName(i); - if (desc) - { - vdup = 0; - // when a resolution exists both under VGA and VESA, keep the - // VESA mode, which is always a higher modenum - for (j = 0; j < vidm_nummodes; j++) - { - if (fastcmp(modedescs[j].desc, desc)) - { - // mode(0): 320x200 is always standard VGA, not vesa - if (modedescs[j].modenum) - { - modedescs[j].modenum = i; - vdup = 1; + if (!desc) + continue; - if (i == vid.modenum) - vidm_selected = j; - } - else - vdup = 1; + modedescs[vidm_nummodes].modenum = i; + modedescs[vidm_nummodes].desc = desc; - break; - } - } + if (i == vid.modenum) + vidm_selected = vidm_nummodes; - if (!vdup) - { - modedescs[vidm_nummodes].modenum = i; - modedescs[vidm_nummodes].desc = desc; + // Pull out the width and height + sscanf(desc, "%u%*c%u", &width, &height); - if (i == vid.modenum) - vidm_selected = vidm_nummodes; + // Show resolutions above desktop res as red. + if (I_CheckAboveDesktopRes(width, height)) + modedescs[vidm_nummodes].goodratio = 2; + // Show multiples of 320x200 as green. + else if (SCR_IsAspectCorrect(width, height)) + modedescs[vidm_nummodes].goodratio = 1; - // Pull out the width and height - sscanf(desc, "%u%*c%u", &width, &height); - - // Show multiples of 320x200 as green. - if (SCR_IsAspectCorrect(width, height)) - modedescs[vidm_nummodes].goodratio = 1; - - if (I_CheckAboveDesktopRes(width, height)) - modedescs[vidm_nummodes].goodratio = 2; - - vidm_nummodes++; - } - } + vidm_nummodes++; } vidm_column_size = (vidm_nummodes+2) / 3; @@ -9697,7 +9670,6 @@ static void M_DrawVideoMode(void) va("Wait %d second%s", testtime, (testtime > 1) ? "s" : "")); M_CentreText(OP_VideoModeDef.y + 158, "or press ESC to return"); - } else { From d074f375cad6fabd335562a75344fc51b391659f Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 04:24:49 +0100 Subject: [PATCH 4/6] M_VideoModeMenu: make sure red marked modes take precedence --- src/m_menu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/m_menu.c b/src/m_menu.c index 813db7459..4c6c5b97c 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -9644,7 +9644,7 @@ static void M_DrawVideoMode(void) else { const UINT8 goodratio = modedescs[i].goodratio; - const INT32 goodflag = ((goodratio == 1) ? recommendedflags : (goodratio == 2) ? warningflags : 0); + const INT32 goodflag = ((goodratio == 2) ? warningflags : (goodratio == 1) ? recommendedflags : 0); V_DrawString(row, col, goodflag|MENUCAPS, modedescs[i].desc); } From b5bb2b42bfad9d52a669baddf2e69ef9a4e02c8e Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 04:46:21 +0100 Subject: [PATCH 5/6] I_FillScreenResolutionsList: shrink list if it is too large we check against MAXMODEDESCS, which is the maximum of modes the videomode can list just in case we really get a display with a fuckton of resolutions, which most likely wont ever be the case though this just removes the smallest modes from the list (accounting for the canonicals), to keep things simple --- src/m_menu.c | 3 --- src/m_menu.h | 3 +++ src/sdl/i_video.c | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 4c6c5b97c..86553c7e9 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -9409,9 +9409,6 @@ static void M_ResetControls(INT32 choice) // =============== //added : 30-01-98: -#define MAXCOLUMNMODES 12 //max modes displayed in one column -#define MAXMODEDESCS (MAXCOLUMNMODES*3) - static modedesc_t modedescs[MAXMODEDESCS]; void M_VideoModeMenu(INT32 choice) diff --git a/src/m_menu.h b/src/m_menu.h index 2e7cedbbb..5bc957541 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -221,6 +221,9 @@ extern menu_t *currentMenu; extern menu_t MainDef; extern menu_t SP_LoadDef; + +#define MAXCOLUMNMODES 12 //max modes displayed in one column +#define MAXMODEDESCS (MAXCOLUMNMODES*3) extern menu_t OP_VideoModeDef; void M_VideoModeMenu(INT32 choice); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 56a2d1eb5..905770951 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -1557,6 +1557,18 @@ static void I_AppendResolution(SDL_DisplayMode *mode, int *list_size) (*list_size)++; } +// small helper to check if a video mode is canonical +static boolean IsModeCanonical(int w, int h) +{ + for (int i = 0; i < num_canonicals; i++) + { + if (canonicals[i].w == w && canonicals[i].h == h) + return true; + } + + return false; +} + // // I_FillScreenResolutionsList // Get all the supported screen resolutions @@ -1603,10 +1615,6 @@ static void I_FillScreenResolutionsList(boolean force) // make sure the canonical resolutions are always available if (i > count - 1) { - // no hard-coded resolutions for mode-changing fullscreen - //if (exclusive_fullscreen) - //continue; - mode.w = canonicals[i - count].w; mode.h = canonicals[i - count].h; } @@ -1662,6 +1670,30 @@ static void I_FillScreenResolutionsList(boolean force) // [FG] sort the list SDL_qsort(windowedModes, list_size, sizeof(*windowedModes), cmp_resolutions); + // if we have more video modes than we can list + // try to shrink the list, lowest resolutions first + // whilst keeping the canonicals + if (list_size > MAXMODEDESCS) + { + // list is sorted from largest to smallest, so start at the end + for (i = list_size - 1; i >= 0 && list_size > MAXMODEDESCS; i--) + { + if (IsModeCanonical(windowedModes[i].w, windowedModes[i].h)) + continue; + + // Should we like, instead prioritize checking for non aspect correct resolutions to remove? + + // we gotta shift everything + // due to canonicals we cant just shrink the list + for (int k = i; k < list_size - 1; k++) + { + windowedModes[k] = windowedModes[k + 1]; + } + + list_size--; + } + } + windowedModes[list_size].name[0] = '\0'; vid_nummodes = list_size; From c312380944605193cd5ff9ff8e047b5564384723 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 18 Nov 2025 05:08:46 +0100 Subject: [PATCH 6/6] check if our desired screen res already is in the videomode list before deciding to rebuild the whole list when changing the resolution cvars --- src/sdl/i_video.c | 77 ++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 905770951..5a38114bc 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -1569,6 +1569,29 @@ static boolean IsModeCanonical(int w, int h) return false; } +// check if our desired resolution already is in the videomode list +static int IsCustomResolutionInList(int list_size) +{ + INT32 custom_w = cv_scr_width.value; + INT32 custom_h = cv_scr_height.value; + + // invalid resolution + // dont even attempt to add it + if (custom_w < BASEVIDWIDTH || custom_w > MAXVIDWIDTH + || custom_h < BASEVIDHEIGHT || custom_h > MAXVIDHEIGHT) + return -1; + + for (int i = 0; i < list_size; i++) + { + if (windowedModes[i].w == custom_w && windowedModes[i].h == custom_h) + { + return 1; + } + } + + return 0; +} + // // I_FillScreenResolutionsList // Get all the supported screen resolutions @@ -1629,43 +1652,28 @@ static void I_FillScreenResolutionsList(boolean force) windowedModes[list_size].name[0] = '\0'; } - INT32 custom_w = cv_scr_width.value; - INT32 custom_h = cv_scr_height.value; + const int customresinlist = IsCustomResolutionInList(list_size); - // make sure those are valid - if ((custom_w >= BASEVIDWIDTH && custom_h >= BASEVIDHEIGHT) && - (custom_w <= MAXVIDWIDTH && custom_h <= MAXVIDHEIGHT)) + if (customresinlist == 0) { - boolean needcustom = true; - - for (i = 0; i < list_size; i++) - { - if (windowedModes[i].w == custom_w && windowedModes[i].h == custom_h) - { - needcustom = false; - break; - } - } - // did not find mode from list, make custom resolution if the values somewhat make sense - if (needcustom) + if (list_size < MAXWINMODES) { - if (list_size < MAXWINMODES) - { - snprintf(custom_resolution, sizeof(custom_resolution), "%dx%d", custom_w, custom_h); + snprintf(custom_resolution, sizeof(custom_resolution), "%dx%d", cv_scr_width.value, cv_scr_height.value); - // [FG] if the desired resolution not in the list, append it - snprintf(windowedModes[list_size].name, - sizeof(windowedModes[list_size].name), - "%dx%d", custom_w, custom_h); - windowedModes[list_size].w = custom_w; - windowedModes[list_size].h = custom_h; - list_size++; - } - else - CONS_Alert(CONS_ERROR, "Could not set custom resolution!\n"); + // [FG] if the desired resolution not in the list, append it + snprintf(windowedModes[list_size].name, + sizeof(windowedModes[list_size].name), + "%dx%d", cv_scr_width.value, cv_scr_height.value); + windowedModes[list_size].w = cv_scr_width.value; + windowedModes[list_size].h = cv_scr_height.value; + list_size++; } + else + CONS_Alert(CONS_ERROR, "Could not set custom resolution!\n"); } + else if (customresinlist == -1) + CONS_Alert(CONS_ERROR, "Could not set custom resolution!\n"); // [FG] sort the list SDL_qsort(windowedModes, list_size, sizeof(*windowedModes), cmp_resolutions); @@ -1706,7 +1714,6 @@ static void I_FillScreenResolutionsList(boolean force) // return number of fullscreen + X11 modes INT32 VID_NumModes(void) { - //return MAXWINMODES; return vid_nummodes; } @@ -1740,12 +1747,8 @@ INT32 VID_GetModeForSize(INT32 w, INT32 h) void VID_RefreshModeList(void) { - INT32 custom_w = cv_scr_width.value; - INT32 custom_h = cv_scr_height.value; - - // make sure those are valid - if ((custom_w >= BASEVIDWIDTH && custom_h >= BASEVIDHEIGHT) && - (custom_w <= MAXVIDWIDTH && custom_h <= MAXVIDHEIGHT)) + // check if we even need to rebuild the list + if (IsCustomResolutionInList(vid_nummodes) == 0) I_FillScreenResolutionsList(true); }