Skip to content

Commit

Permalink
emscripten: Return an error for webgl context limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daft-Freak committed Apr 6, 2022
1 parent cb6d810 commit d00f373
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/video/emscripten/SDL_emscriptenopengles.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ Emscripten_GLES_CreateContext(_THIS, SDL_Window * window)
attribs.majorVersion = 2; /* WebGL 2.0 ~= GLES 3.0 */

window_data = (SDL_WindowData *) window->driverdata;

if (window_data->gl_context) {
SDL_SetError("Cannot create multiple webgl contexts per window");
return NULL;
}

context = emscripten_webgl_create_context(window_data->canvas_id, &attribs);

if (context < 0) {
Expand All @@ -106,13 +112,26 @@ Emscripten_GLES_CreateContext(_THIS, SDL_Window * window)
return NULL;
}

window_data->gl_context = (SDL_GLContext)context;

return (SDL_GLContext)context;
}

void
Emscripten_GLES_DeleteContext(_THIS, SDL_GLContext context)
{
SDL_Window *window;

/* remove the context from its window */
for (window = _this->windows; window != NULL; window = window->next) {
SDL_WindowData *window_data;
window_data = (SDL_WindowData *) window->driverdata;

if (window_data->gl_context == context) {
window_data->gl_context = NULL;
}
}

emscripten_webgl_destroy_context((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context);
}

Expand All @@ -129,7 +148,16 @@ Emscripten_GLES_SwapWindow(_THIS, SDL_Window * window)
int
Emscripten_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{
/* ignores window, as it isn't possible to reuse contexts across canvases */
/* it isn't possible to reuse contexts across canvases */
if (window && context) {
SDL_WindowData *window_data;
window_data = (SDL_WindowData *) window->driverdata;

if (context != window_data->gl_context) {
return SDL_SetError("Cannot make context current to another window");
}
}

if (emscripten_webgl_make_context_current((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context) != EMSCRIPTEN_RESULT_SUCCESS) {
return SDL_SetError("Unable to make context current");
}
Expand Down
2 changes: 2 additions & 0 deletions src/video/emscripten/SDL_emscriptenvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ typedef struct SDL_WindowData
SDL_Window *window;
SDL_Surface *surface;

SDL_GLContext gl_context;

char *canvas_id;

float pixel_ratio;
Expand Down

0 comments on commit d00f373

Please sign in to comment.