From ac0d6987adb7d7d0187b03a40d3014274dab8e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 26 May 2019 23:45:07 +0200 Subject: [PATCH] Platform: actually properly destroy the WebGL context after. And expose it to the users. --- src/Magnum/Platform/EmscriptenApplication.cpp | 14 ++++++++------ src/Magnum/Platform/EmscriptenApplication.h | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index 79721e11a..def76f07b 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -208,7 +208,11 @@ EmscriptenApplication::EmscriptenApplication(const Arguments& arguments, NoCreat } EmscriptenApplication::~EmscriptenApplication() { - emscripten_webgl_make_context_current(0); + #ifdef MAGNUM_TARGET_GL + _context.reset(); + + emscripten_webgl_destroy_context(_glContext); + #endif } void EmscriptenApplication::create() { @@ -335,9 +339,8 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration, const const Vector2i scaledCanvasSize = canvasSize*_dpiScaling*_devicePixelRatio; emscripten_set_canvas_element_size("#canvas", scaledCanvasSize.x(), scaledCanvasSize.y()); - /* Create surface and context */ - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = - emscripten_webgl_create_context("#canvas", &attrs); + /* Create WebGL context */ + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attrs); if(!context) { /* When context creation fails, `context` is a negative integer matching EMSCRIPTEN_RESULT_* defines */ @@ -347,8 +350,7 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration, const } /* Make the context current */ - CORRADE_INTERNAL_ASSERT_OUTPUT( - emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS); + CORRADE_INTERNAL_ASSERT_OUTPUT(emscripten_webgl_make_context_current(_glContext = context) == EMSCRIPTEN_RESULT_SUCCESS); setupCallbacks(!!(configuration.windowFlags() & Configuration::WindowFlag::Resizable)); diff --git a/src/Magnum/Platform/EmscriptenApplication.h b/src/Magnum/Platform/EmscriptenApplication.h index 1f7246d87..635d81f5d 100644 --- a/src/Magnum/Platform/EmscriptenApplication.h +++ b/src/Magnum/Platform/EmscriptenApplication.h @@ -49,6 +49,7 @@ struct EmscriptenKeyboardEvent; struct EmscriptenMouseEvent; struct EmscriptenWheelEvent; struct EmscriptenUiEvent; +typedef int EMSCRIPTEN_WEBGL_CONTEXT_HANDLE; #endif namespace Magnum { namespace Platform { @@ -309,6 +310,16 @@ class EmscriptenApplication { */ void exit(int exitCode = 0); + #ifdef MAGNUM_TARGET_GL + /** + * @brief Underlying WebGL context + * + * Use in case you need to call Emscripten functionality directly. + * Returns @cpp 0 @ce in case the context was not created yet. + */ + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE glContext() { return _glContext; } + #endif + protected: /* Nobody will need to have (and delete) EmscriptenApplication*, thus this is faster than public pure virtual destructor */ @@ -574,6 +585,7 @@ class EmscriptenApplication { Flags _flags; #ifdef MAGNUM_TARGET_GL + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE _glContext{}; Containers::Pointer _context; #endif