Skip to content

Commit e001f7e

Browse files
author
ubkp
authored
Add ToggleFullscreen() and required changes for PLATFORM_WEB (raysan5#3634)
1 parent 9bdc217 commit e001f7e

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

src/platforms/rcore_web.c

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
//----------------------------------------------------------------------------------
7474
typedef struct {
7575
GLFWwindow *handle; // GLFW window handle (graphic device)
76+
bool ourFullscreen; // Internal var to filter our handling of fullscreen vs the user handling of fullscreen
7677
} PlatformData;
7778

7879
//----------------------------------------------------------------------------------
@@ -140,6 +141,37 @@ bool WindowShouldClose(void)
140141
// Toggle fullscreen mode
141142
void ToggleFullscreen(void)
142143
{
144+
platform.ourFullscreen = true;
145+
bool enterFullscreen = false;
146+
147+
const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
148+
if (wasFullscreen)
149+
{
150+
EM_ASM(document.exitFullscreen(););
151+
152+
if (CORE.Window.flags & FLAG_FULLSCREEN_MODE) enterFullscreen = false;
153+
else enterFullscreen = true;
154+
155+
CORE.Window.fullscreen = false;
156+
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
157+
CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
158+
}
159+
else enterFullscreen = true;
160+
161+
if (enterFullscreen)
162+
{
163+
// NOTE: The setTimeouts handle the browser mode change delay
164+
EM_ASM(
165+
setTimeout(function()
166+
{
167+
Module.requestFullscreen(false, false);
168+
}, 100);
169+
);
170+
CORE.Window.fullscreen = true;
171+
CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
172+
}
173+
174+
// NOTE: Old notes below:
143175
/*
144176
EM_ASM
145177
(
@@ -204,40 +236,44 @@ void ToggleFullscreen(void)
204236
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
205237
}
206238
*/
207-
208-
CORE.Window.fullscreen = !CORE.Window.fullscreen; // Toggle fullscreen flag
209239
}
210240

211241
// Toggle borderless windowed mode
212242
void ToggleBorderlessWindowed(void)
213243
{
244+
platform.ourFullscreen = true;
245+
bool enterBorderless = false;
246+
214247
const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
215248
if (wasFullscreen)
216249
{
217250
EM_ASM(document.exitFullscreen(););
218251

252+
if (CORE.Window.flags & FLAG_BORDERLESS_WINDOWED_MODE) enterBorderless = false;
253+
else enterBorderless = true;
254+
219255
CORE.Window.fullscreen = false;
220256
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
257+
CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
221258
}
259+
else enterBorderless = true;
222260

223-
if (!IsWindowState(FLAG_BORDERLESS_WINDOWED_MODE))
261+
if (enterBorderless)
224262
{
225263
// NOTE: 1. The setTimeouts handle the browser mode change delay
226-
// 2. The style unset handles the possibility of a width="100%" like on the default shell.html file
264+
// 2. The style unset handles the possibility of a width="value%" like on the default shell.html file
227265
EM_ASM(
228266
setTimeout(function()
229267
{
230-
Module.requestFullscreen(true, true);
268+
Module.requestFullscreen(false, true);
231269
setTimeout(function()
232270
{
233271
canvas.style.width="unset";
234272
}, 100);
235273
}, 100);
236274
);
237-
238275
CORE.Window.flags |= FLAG_BORDERLESS_WINDOWED_MODE;
239276
}
240-
else CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
241277
}
242278

243279
// Set window state: maximized, if resizable
@@ -277,9 +313,9 @@ void SetWindowState(unsigned int flags)
277313
}
278314

279315
// State change: FLAG_FULLSCREEN_MODE
280-
if ((flags & FLAG_FULLSCREEN_MODE) > 0)
316+
if ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) != (flags & FLAG_FULLSCREEN_MODE))
281317
{
282-
TRACELOG(LOG_WARNING, "SetWindowState(FLAG_FULLSCREEN_MODE) not available yet on target platform");
318+
ToggleFullscreen(); // NOTE: Window state flag updated inside function
283319
}
284320

285321
// State change: FLAG_WINDOW_RESIZABLE
@@ -384,9 +420,9 @@ void ClearWindowState(unsigned int flags)
384420
}
385421

386422
// State change: FLAG_FULLSCREEN_MODE
387-
if ((flags & FLAG_FULLSCREEN_MODE) > 0)
423+
if (((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0) && ((flags & FLAG_FULLSCREEN_MODE) > 0))
388424
{
389-
TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_FULLSCREEN_MODE) not available yet on target platform");
425+
ToggleFullscreen(); // NOTE: Window state flag updated inside function
390426
}
391427

392428
// State change: FLAG_WINDOW_RESIZABLE
@@ -1015,6 +1051,9 @@ int InitPlatform(void)
10151051
CORE.Window.display.width = CORE.Window.screen.width;
10161052
CORE.Window.display.height = CORE.Window.screen.height;
10171053

1054+
// Init fullscreen toggle required var:
1055+
platform.ourFullscreen = false;
1056+
10181057
if (CORE.Window.fullscreen)
10191058
{
10201059
// remember center for switchinging from fullscreen to window
@@ -1148,7 +1187,7 @@ int InitPlatform(void)
11481187
// Initialize input events callbacks
11491188
//----------------------------------------------------------------------------
11501189
// Setup callback functions for the DOM events
1151-
emscripten_set_fullscreenchange_callback("#canvas", NULL, 1, EmscriptenFullscreenChangeCallback);
1190+
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenFullscreenChangeCallback);
11521191

11531192
// WARNING: Below resize code was breaking fullscreen mode for sample games and examples, it needs review
11541193
// Check fullscreen change events(note this is done on the window since most browsers don't support this on #canvas)
@@ -1412,7 +1451,19 @@ static void CursorEnterCallback(GLFWwindow *window, int enter)
14121451
// Register fullscreen change events
14131452
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *event, void *userData)
14141453
{
1415-
// TODO: Implement EmscriptenFullscreenChangeCallback()?
1454+
// NOTE: 1. Reset the fullscreen flags if the user left fullscreen manually by pressing the Escape key
1455+
// 2. Which is a necessary safeguard because that case will bypass the toggles CORE.Window.flags resets
1456+
if (platform.ourFullscreen) platform.ourFullscreen = false;
1457+
else
1458+
{
1459+
const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
1460+
if (!wasFullscreen)
1461+
{
1462+
CORE.Window.fullscreen = false;
1463+
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
1464+
CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
1465+
}
1466+
}
14161467

14171468
return 1; // The event was consumed by the callback handler
14181469
}

0 commit comments

Comments
 (0)