@@ -107,37 +107,6 @@ void CalculatePreferredWindowSize(int &width, int &height)
107
107
height = mode.h * width / mode.w ;
108
108
}
109
109
}
110
-
111
- SDL_DisplayMode GetNearestDisplayMode (Size preferredSize)
112
- {
113
- SDL_DisplayMode nearestDisplayMode;
114
- if (SDL_GetWindowDisplayMode (ghMainWnd, &nearestDisplayMode) != 0 )
115
- ErrSdl ();
116
-
117
- const int displayIndex = SDL_GetWindowDisplayIndex (ghMainWnd);
118
- const int modeCount = SDL_GetNumDisplayModes (displayIndex);
119
- for (int modeIndex = 0 ; modeIndex < modeCount; modeIndex++) {
120
- SDL_DisplayMode displayMode;
121
- if (SDL_GetDisplayMode (displayIndex, modeIndex, &displayMode) != 0 )
122
- continue ;
123
-
124
- const int diffHeight = std::abs (nearestDisplayMode.h - preferredSize.height ) - std::abs (displayMode.h - preferredSize.height );
125
- const int diffWidth = std::abs (nearestDisplayMode.w - preferredSize.width ) - std::abs (displayMode.w - preferredSize.width );
126
- if (diffHeight < 0 )
127
- continue ;
128
- if (diffHeight == 0 && diffWidth < 0 )
129
- continue ;
130
- nearestDisplayMode = displayMode;
131
- }
132
-
133
- LogVerbose (" Nearest display mode to {}x{} is {}x{} {}bpp {}Hz" ,
134
- preferredSize.width , preferredSize.height ,
135
- nearestDisplayMode.w , nearestDisplayMode.h ,
136
- SDL_BITSPERPIXEL (nearestDisplayMode.format ),
137
- nearestDisplayMode.refresh_rate );
138
-
139
- return nearestDisplayMode;
140
- }
141
110
#endif
142
111
143
112
void CalculateUIRectangle ()
@@ -299,6 +268,59 @@ const auto OptionChangeHandlerVSync = (GetOptions().Graphics.frameRateControl.Se
299
268
300
269
} // namespace
301
270
271
+ #if SDL_VERSION_ATLEAST(2, 0, 0)
272
+ SDL_DisplayMode GetNearestDisplayMode (Size preferredSize, SDL_PixelFormatEnum preferredPixelFormat)
273
+ {
274
+ SDL_DisplayMode nearestDisplayMode;
275
+ if (SDL_GetWindowDisplayMode (ghMainWnd, &nearestDisplayMode) != 0 )
276
+ ErrSdl ();
277
+
278
+ const int displayIndex = SDL_GetWindowDisplayIndex (ghMainWnd);
279
+ const int modeCount = SDL_GetNumDisplayModes (displayIndex);
280
+
281
+ // First, find the best mode among the modes with the requested pixel format.
282
+ SDL_PixelFormatEnum bestPixelFormat = SDL_PIXELFORMAT_UNKNOWN;
283
+ for (int modeIndex = 0 ; modeIndex < modeCount; modeIndex++) {
284
+ SDL_DisplayMode displayMode;
285
+ if (SDL_GetDisplayMode (displayIndex, modeIndex, &displayMode) != 0 )
286
+ continue ;
287
+ const int diffHeight = std::abs (nearestDisplayMode.h - preferredSize.height ) - std::abs (displayMode.h - preferredSize.height );
288
+ const int diffWidth = std::abs (nearestDisplayMode.w - preferredSize.width ) - std::abs (displayMode.w - preferredSize.width );
289
+ if (diffHeight < 0 )
290
+ continue ;
291
+ if (diffHeight == 0 && diffWidth < 0 )
292
+ continue ;
293
+ if (preferredPixelFormat == SDL_PIXELFORMAT_UNKNOWN
294
+ || displayMode.format == preferredPixelFormat) {
295
+ nearestDisplayMode = displayMode;
296
+ }
297
+ }
298
+ if (preferredPixelFormat != SDL_PIXELFORMAT_UNKNOWN && bestPixelFormat == SDL_PIXELFORMAT_UNKNOWN) {
299
+ // If no mode with the preferred pixel format was found, allow any pixel format:
300
+ for (int modeIndex = 0 ; modeIndex < modeCount; modeIndex++) {
301
+ SDL_DisplayMode displayMode;
302
+ if (SDL_GetDisplayMode (displayIndex, modeIndex, &displayMode) != 0 )
303
+ continue ;
304
+ const int diffHeight = std::abs (nearestDisplayMode.h - preferredSize.height ) - std::abs (displayMode.h - preferredSize.height );
305
+ const int diffWidth = std::abs (nearestDisplayMode.w - preferredSize.width ) - std::abs (displayMode.w - preferredSize.width );
306
+ if (diffHeight < 0 )
307
+ continue ;
308
+ if (diffHeight == 0 && diffWidth < 0 )
309
+ continue ;
310
+ nearestDisplayMode = displayMode;
311
+ }
312
+ }
313
+
314
+ LogVerbose (" Nearest display mode to {}x{} is {}x{} {}bpp {}Hz" ,
315
+ preferredSize.width , preferredSize.height ,
316
+ nearestDisplayMode.w , nearestDisplayMode.h ,
317
+ SDL_BITSPERPIXEL (nearestDisplayMode.format ),
318
+ nearestDisplayMode.refresh_rate );
319
+
320
+ return nearestDisplayMode;
321
+ }
322
+ #endif
323
+
302
324
void AdjustToScreenGeometry (Size windowSize)
303
325
{
304
326
gnScreenWidth = windowSize.width ;
@@ -455,6 +477,13 @@ bool SpawnWindow(const char *lpWindowName)
455
477
456
478
ghMainWnd = SDL_CreateWindow (lpWindowName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowSize.width , windowSize.height , flags);
457
479
480
+ #if defined(DEVILUTIONX_DISPLAY_PIXELFORMAT)
481
+ SDL_DisplayMode nearestDisplayMode = GetNearestDisplayMode (windowSize, DEVILUTIONX_DISPLAY_PIXELFORMAT);
482
+ if (SDL_SetWindowDisplayMode (ghMainWnd, &nearestDisplayMode) != 0 ) {
483
+ ErrSdl ();
484
+ }
485
+ #endif
486
+
458
487
// Note: https://github.com/libsdl-org/SDL/issues/962
459
488
// This is a solution to a problem related to SDL mouse grab.
460
489
// See https://github.com/diasurgical/devilutionX/issues/4251
0 commit comments