Skip to content

Commit 66e235a

Browse files
handle fullscreen mode change properly
This better replicates the old behavior of SDL2's fullscreen style. SDL3 defaults to a fullscreen-window style unless you specifically set a fullscreen display mode. Note that gr_activate() must default to true in order to avoid a weird sequence of window mode changes on startup when using fullscreen.
1 parent e1e993f commit 66e235a

6 files changed

Lines changed: 52 additions & 18 deletions

File tree

code/globalincs/windebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
# include "globalincs/mspdb_callstack.h"
4141
#endif
4242

43-
extern void gr_activate(int active);
43+
extern void gr_activate(bool active);
4444

4545
#ifndef _ASSERT
4646
#ifndef _DEBUG

code/graphics/2d.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,10 +2116,9 @@ bool gr_init(std::unique_ptr<os::GraphicsOperations>&& graphicsOps, int d_mode,
21162116
return true;
21172117
}
21182118

2119-
int gr_activated = 0;
2120-
void gr_activate(int active)
2119+
static bool gr_activated = true; // start activated
2120+
void gr_activate(bool active)
21212121
{
2122-
21232122
if (gr_activated == active) {
21242123
return;
21252124
}
@@ -3317,4 +3316,4 @@ bool gr_lua_context_active() {
33173316
}
33183317

33193318
return gr_lua_screen.active;
3320-
}
3319+
}

code/graphics/2d.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ extern int gr_get_dynamic_font_lines(int number_default_lines);
10631063
extern io::mouse::Cursor* Web_cursor;
10641064

10651065
// Called by OS when application gets/looses focus
1066-
extern void gr_activate(int active);
1066+
extern void gr_activate(bool active);
10671067

10681068
#define GR_CALL(x) (x)
10691069

code/osapi/dialogs.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ namespace os
233233
boxData.title = "Error!";
234234
boxData.window = getDialogParent();
235235

236-
gr_activate(0);
236+
gr_activate(false);
237237

238238
int buttonId;
239239
if ( !SDL_ShowMessageBox(&boxData, &buttonId) )
@@ -255,7 +255,7 @@ namespace os
255255
break;
256256
}
257257

258-
gr_activate(1);
258+
gr_activate(true);
259259
}
260260

261261
void Error(const char * filename, int line, const char * format, ...)
@@ -317,7 +317,7 @@ namespace os
317317
boxData.title = "Error!";
318318
boxData.window = getDialogParent();
319319

320-
gr_activate(0);
320+
gr_activate(false);
321321

322322
int buttonId;
323323
if ( !SDL_ShowMessageBox(&boxData, &buttonId) )
@@ -335,7 +335,7 @@ namespace os
335335
Int3();
336336
break;
337337
}
338-
gr_activate(1);
338+
gr_activate(true);
339339
}
340340

341341
// Actual implementation of the warning function. Used by the various warning functions
@@ -385,7 +385,7 @@ namespace os
385385
boxData.title = "Warning!";
386386
boxData.window = getDialogParent();
387387

388-
gr_activate(0);
388+
gr_activate(false);
389389

390390
int buttonId;
391391
if ( !SDL_ShowMessageBox(&boxData, &buttonId) )
@@ -407,7 +407,7 @@ namespace os
407407
break;
408408
}
409409

410-
gr_activate(1);
410+
gr_activate(true);
411411
}
412412

413413

@@ -487,11 +487,11 @@ namespace os
487487
SCP_string boxMessage = truncateLines(boxMsgStream, Messagebox_lines);
488488
boxMessage += "\n[ This info is in the clipboard so you can paste it somewhere now ]\n";
489489

490-
gr_activate(0);
490+
gr_activate(false);
491491

492492
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Information", boxMessage.c_str(), getDialogParent());
493493

494-
gr_activate(1);
494+
gr_activate(true);
495495
}
496496

497497
void Message(MessageType type, const char* message, const char* title)

code/osapi/osapi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,15 @@ void debug_int3(const char *file, int line)
638638
{
639639
mprintf(("Int3(): From %s at line %d\n", file, line));
640640

641-
gr_activate(0);
641+
gr_activate(false);
642642

643643
mprintf(("%s\n", dump_stacktrace().c_str()));
644644

645645
#ifndef NDEBUG
646646
SDL_TriggerBreakpoint();
647647
#endif
648648

649-
gr_activate(1);
649+
gr_activate(true);
650650
}
651651

652652
namespace os

freespace2/SDLGraphicsOperations.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,26 @@ class SDLWindowViewPort: public os::Viewport {
148148
SDL_SetWindowFullscreen(_window, false);
149149
SDL_SetWindowBordered(_window, false);
150150
break;
151-
case os::ViewportState::Fullscreen:
151+
case os::ViewportState::Fullscreen: {
152+
SDL_DisplayMode target;
153+
int width, height;
154+
155+
if (SDL_GetWindowSizeInPixels(_window, &width, &height)) {
156+
if (SDL_GetClosestFullscreenDisplayMode(SDL_GetDisplayForWindow(_window),
157+
width, height, 0.0f, true, &target))
158+
{
159+
SDL_SetWindowFullscreenMode(_window, &target);
160+
}
161+
}
162+
163+
// NOTE: This can be buggy if the mode failed to set since FSO
164+
// doesn't account for a difference between assumed window size
165+
// and actual window size. This can present as screen anomalies
166+
// such as distortion, mirroring, or flickering.
152167
SDL_SetWindowFullscreen(_window, true);
168+
153169
break;
170+
}
154171
default:
155172
UNREACHABLE("Invalid window state!");
156173
break;
@@ -229,7 +246,10 @@ std::unique_ptr<os::Viewport> SDLGraphicsOperations::createViewport(const os::Vi
229246
windowflags |= SDL_WINDOW_BORDERLESS;
230247
}
231248
if (props.flags[os::ViewPortFlags::Fullscreen]) {
232-
windowflags |= SDL_WINDOW_FULLSCREEN;
249+
// don't set window flag here since we need to alter the display mode
250+
// first and that can only be done after the window is created
251+
//
252+
// windowflags |= SDL_WINDOW_FULLSCREEN;
233253
}
234254
if (props.flags[os::ViewPortFlags::Resizeable]) {
235255
windowflags |= SDL_WINDOW_RESIZABLE;
@@ -273,6 +293,21 @@ std::unique_ptr<os::Viewport> SDLGraphicsOperations::createViewport(const os::Vi
273293
return nullptr;
274294
}
275295

296+
// switch to fullscreen if we should
297+
if (props.flags[os::ViewPortFlags::Fullscreen]) {
298+
SDL_DisplayMode target;
299+
300+
if (SDL_GetClosestFullscreenDisplayMode(props.display, width, height, 0.0f, true, &target)) {
301+
SDL_SetWindowFullscreenMode(window, &target);
302+
}
303+
304+
// NOTE: This can be buggy if the mode failed to set since FSO doesn't
305+
// account for a difference between assumed window size and actual window
306+
// size. This can present as screen anomalies such as distortion, mirroring,
307+
// or flickering.
308+
SDL_SetWindowFullscreen(window, true);
309+
}
310+
276311
SDL_SetWindowPosition(window, x, y);
277312
SDL_RaiseWindow(window);
278313

0 commit comments

Comments
 (0)