Skip to content

Commit 9c04b1d

Browse files
committed
REVIEWED: Store canvas name id at platform initialization
Useful to support multiple canvases running different wasm instances in same webpage
1 parent b465b4e commit 9c04b1d

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/platforms/rcore_web.c

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ typedef struct {
7676
bool ourFullscreen; // Internal var to filter our handling of fullscreen vs the user handling of fullscreen
7777
int unmaximizedWidth; // Internal var to store the unmaximized window (canvas) width
7878
int unmaximizedHeight; // Internal var to store the unmaximized window (canvas) height
79+
80+
char canvasId[64]; // Keep current canvas id where wasm app is running
7981
} PlatformData;
8082

8183
//----------------------------------------------------------------------------------
@@ -142,7 +144,11 @@ static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPoin
142144
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
143145
static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData);
144146

145-
static const char *GetCanvasId(void);
147+
// JS: Set the canvas id provided by the module configuration
148+
EM_JS(void, SetCanvasIdJs, (char *out, int outSize), {
149+
var canvasId = "#" + Module.canvas.id;
150+
stringToUTF8(canvasId, out, outSize);
151+
});
146152

147153
//----------------------------------------------------------------------------------
148154
// Module Functions Declaration
@@ -233,7 +239,7 @@ void ToggleFullscreen(void)
233239
// This option does not seem to work at all:
234240
// emscripten_request_pointerlock() and emscripten_request_fullscreen() are affected by web security,
235241
// the user must click once on the canvas to hide the pointer or transition to full screen
236-
//emscripten_request_fullscreen("#canvas", false);
242+
//emscripten_request_fullscreen(platform.canvasId, false);
237243
238244
// Option 2: Request fullscreen for the canvas element with strategy
239245
// This option does not seem to work at all
@@ -245,7 +251,7 @@ void ToggleFullscreen(void)
245251
// .canvasResizedCallback = EmscriptenWindowResizedCallback,
246252
// .canvasResizedCallbackUserData = NULL
247253
// };
248-
//emscripten_request_fullscreen_strategy("#canvas", EM_FALSE, &strategy);
254+
//emscripten_request_fullscreen_strategy(platform.canvasId, EM_FALSE, &strategy);
249255
250256
// Option 3: Request fullscreen for the canvas element with strategy
251257
// It works as expected but only inside the browser (client area)
@@ -256,10 +262,10 @@ void ToggleFullscreen(void)
256262
.canvasResizedCallback = EmscriptenWindowResizedCallback,
257263
.canvasResizedCallbackUserData = NULL
258264
};
259-
emscripten_enter_soft_fullscreen("#canvas", &strategy);
265+
emscripten_enter_soft_fullscreen(platform.canvasId, &strategy);
260266
261267
int width, height;
262-
emscripten_get_canvas_element_size("#canvas", &width, &height);
268+
emscripten_get_canvas_element_size(platform.canvasId, &width, &height);
263269
TRACELOG(LOG_WARNING, "Emscripten: Enter fullscreen: Canvas size: %i x %i", width, height);
264270
265271
CORE.Window.fullscreen = true; // Toggle fullscreen flag
@@ -271,7 +277,7 @@ void ToggleFullscreen(void)
271277
//emscripten_exit_soft_fullscreen();
272278
273279
int width, height;
274-
emscripten_get_canvas_element_size("#canvas", &width, &height);
280+
emscripten_get_canvas_element_size(platform.canvasId, &width, &height);
275281
TRACELOG(LOG_WARNING, "Emscripten: Exit fullscreen: Canvas size: %i x %i", width, height);
276282
277283
CORE.Window.fullscreen = false; // Toggle fullscreen flag
@@ -866,7 +872,7 @@ void EnableCursor(void)
866872
// Disables cursor (lock cursor)
867873
void DisableCursor(void)
868874
{
869-
emscripten_request_pointerlock(GetCanvasId(), 1);
875+
emscripten_request_pointerlock(platform.canvasId, 1);
870876

871877
// Set cursor position in the middle
872878
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
@@ -1097,6 +1103,8 @@ void PollInputEvents(void)
10971103
// Initialize platform: graphics, inputs and more
10981104
int InitPlatform(void)
10991105
{
1106+
SetCanvasIdJs(platform.canvasId, 64); // Get the current canvas id
1107+
11001108
glfwSetErrorCallback(ErrorCallback);
11011109

11021110
// Initialize GLFW internal global state
@@ -1347,30 +1355,30 @@ int InitPlatform(void)
13471355
//----------------------------------------------------------------------------
13481356
// Setup window events callbacks
13491357
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenFullscreenChangeCallback);
1350-
emscripten_set_blur_callback(GetCanvasId(), platform.handle, 1, EmscriptenFocusCallback);
1351-
emscripten_set_focus_callback(GetCanvasId(), platform.handle, 1, EmscriptenFocusCallback);
1358+
emscripten_set_blur_callback(platform.canvasId, platform.handle, 1, EmscriptenFocusCallback);
1359+
emscripten_set_focus_callback(platform.canvasId, platform.handle, 1, EmscriptenFocusCallback);
13521360
emscripten_set_visibilitychange_callback(NULL, 1, EmscriptenVisibilityChangeCallback);
13531361

13541362
// WARNING: Below resize code was breaking fullscreen mode for sample games and examples, it needs review
1355-
// Check fullscreen change events(note this is done on the window since most browsers don't support this on #canvas)
1363+
// Check fullscreen change events(note this is done on the window since most browsers don't support this on canvas)
13561364
// emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
1357-
// Check Resize event (note this is done on the window since most browsers don't support this on #canvas)
1365+
// Check Resize event (note this is done on the window since most browsers don't support this on canvas)
13581366
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
13591367

13601368
// Trigger resize callback to force initial size
13611369
EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
13621370

13631371
// Setup input events
13641372
// NOTE: Keyboard callbacks only used to consume some events, libglfw.js takes care of the actual input
1365-
//emscripten_set_keypress_callback(GetCanvasId(), NULL, 1, EmscriptenKeyboardCallback); // WRNING: Breaks input
1366-
//emscripten_set_keydown_callback(GetCanvasId(), NULL, 1, EmscriptenKeyboardCallback);
1367-
emscripten_set_click_callback(GetCanvasId(), NULL, 1, EmscriptenMouseCallback);
1373+
//emscripten_set_keypress_callback(platform.canvasId, NULL, 1, EmscriptenKeyboardCallback); // WRNING: Breaks input
1374+
//emscripten_set_keydown_callback(platform.canvasId, NULL, 1, EmscriptenKeyboardCallback);
1375+
emscripten_set_click_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
13681376
emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenPointerlockCallback);
1369-
emscripten_set_mousemove_callback(GetCanvasId(), NULL, 1, EmscriptenMouseMoveCallback);
1370-
emscripten_set_touchstart_callback(GetCanvasId(), NULL, 1, EmscriptenTouchCallback);
1371-
emscripten_set_touchend_callback(GetCanvasId(), NULL, 1, EmscriptenTouchCallback);
1372-
emscripten_set_touchmove_callback(GetCanvasId(), NULL, 1, EmscriptenTouchCallback);
1373-
emscripten_set_touchcancel_callback(GetCanvasId(), NULL, 1, EmscriptenTouchCallback);
1377+
emscripten_set_mousemove_callback(platform.canvasId, NULL, 1, EmscriptenMouseMoveCallback);
1378+
emscripten_set_touchstart_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1379+
emscripten_set_touchend_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1380+
emscripten_set_touchmove_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1381+
emscripten_set_touchcancel_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
13741382
emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback);
13751383
emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback);
13761384
//----------------------------------------------------------------------------
@@ -1691,7 +1699,7 @@ static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent
16911699
// NOTE: emscripten_get_canvas_element_size() returns canvas.width and canvas.height but
16921700
// we are looking for actual CSS size: canvas.style.width and canvas.style.height
16931701
// EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
1694-
emscripten_get_element_css_size(GetCanvasId(), &canvasWidth, &canvasHeight);
1702+
emscripten_get_element_css_size(platform.canvasId, &canvasWidth, &canvasHeight);
16951703

16961704
for (int i = 0; (i < CORE.Input.Touch.pointCount) && (i < MAX_TOUCH_POINTS); i++)
16971705
{
@@ -1802,7 +1810,7 @@ static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent *
18021810
if (height < (int)CORE.Window.screenMin.height) height = CORE.Window.screenMin.height;
18031811
else if ((height > (int)CORE.Window.screenMax.height) && (CORE.Window.screenMax.height > 0)) height = CORE.Window.screenMax.height;
18041812

1805-
emscripten_set_canvas_element_size(GetCanvasId(), width, height);
1813+
emscripten_set_canvas_element_size(platform.canvasId, width, height);
18061814

18071815
SetupViewport(width, height); // Reset viewport and projection matrix for new size
18081816

@@ -1845,21 +1853,4 @@ static EM_BOOL EmscriptenVisibilityChangeCallback(int eventType, const Emscripte
18451853
}
18461854
//-------------------------------------------------------------------------------------------------------
18471855

1848-
// JS: Get the canvas id provided by the module configuration
1849-
EM_JS(char *, GetCanvasIdJs, (), {
1850-
var canvasId = "#" + Module.canvas.id;
1851-
var lengthBytes = lengthBytesUTF8(canvasId) + 1;
1852-
var stringOnWasmHeap = _malloc(lengthBytes);
1853-
stringToUTF8(canvasId, stringOnWasmHeap, lengthBytes);
1854-
return stringOnWasmHeap;
1855-
});
1856-
1857-
// Get canvas id (using embedded JS function)
1858-
static const char *GetCanvasId(void)
1859-
{
1860-
static char *canvasId = NULL;
1861-
if (canvasId == NULL) canvasId = GetCanvasIdJs();
1862-
return canvasId;
1863-
}
1864-
18651856
// EOF

0 commit comments

Comments
 (0)