diff --git a/modules/FindMagnum.cmake b/modules/FindMagnum.cmake index 43997ff48..ef58d203c 100644 --- a/modules/FindMagnum.cmake +++ b/modules/FindMagnum.cmake @@ -534,10 +534,6 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) find_package(SDL2) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES SDL2::SDL2) - if(CORRADE_TARGET_EMSCRIPTEN) - # TODO: give me INTERFACE_LINK_OPTIONS or something, please - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_SDL=2") - endif() # (Windowless) GLX application dependencies elseif(_component STREQUAL GlxApplication OR _component STREQUAL WindowlessGlxApplication) diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 190409485..f626117f1 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -100,6 +100,7 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, configuration.isSRGBCapable()); #endif + /** @todo Remove when Emscripten has proper SDL2 support */ #ifndef CORRADE_TARGET_EMSCRIPTEN /* Set context version, if user-specified */ if(configuration.version() != Version::None) { @@ -146,7 +147,6 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); #endif } - #endif /* Create window. Hide it by default so we don't have distracting window blinking in case we have to destroy it again right away */ @@ -238,12 +238,23 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { SDL_GL_GetDrawableSize(_window, &drawableSize.x(), &drawableSize.y()); glViewport(0, 0, drawableSize.x(), drawableSize.y()); #endif + #else + /* Emscripten-specific initialization */ + if(!(_glContext = SDL_SetVideoMode(configuration.size().x(), configuration.size().y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF))) { + Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create context:" << SDL_GetError(); + return false; + } + #endif /* Destroy everything also when the Magnum context creation fails */ if(!_context->tryCreate()) { + #ifndef CORRADE_TARGET_EMSCRIPTEN SDL_GL_DeleteContext(_glContext); SDL_DestroyWindow(_window); _window = nullptr; + #else + SDL_FreeSurface(_glContext); + #endif return false; } @@ -258,13 +269,21 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) { } Vector2i Sdl2Application::windowSize() { + #ifndef CORRADE_TARGET_EMSCRIPTEN Vector2i size; SDL_GetWindowSize(_window, &size.x(), &size.y()); return size; + #else + return {_glContext->w, _glContext->h}; + #endif } void Sdl2Application::swapBuffers() { + #ifndef CORRADE_TARGET_EMSCRIPTEN SDL_GL_SwapWindow(_window); + #else + SDL_Flip(_glContext); + #endif } Int Sdl2Application::swapInterval() const { @@ -291,8 +310,12 @@ bool Sdl2Application::setSwapInterval(const Int interval) { Sdl2Application::~Sdl2Application() { _context.reset(); + #ifndef CORRADE_TARGET_EMSCRIPTEN SDL_GL_DeleteContext(_glContext); SDL_DestroyWindow(_window); + #else + SDL_FreeSurface(_glContext); + #endif SDL_Quit(); } @@ -503,7 +526,7 @@ void Sdl2Application::textInputEvent(TextInputEvent&) {} void Sdl2Application::textEditingEvent(TextEditingEvent&) {} Sdl2Application::Configuration::Configuration(): - #ifndef CORRADE_TARGET_IOS + #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) _title("Magnum SDL2 Application"), #endif #ifdef CORRADE_TARGET_EMSCRIPTEN diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 0b3f25b35..ea6b04444 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -227,10 +227,6 @@ file contains event listeners which print loading status on the page. The status displayed in the remaining two `<div>`s, if they are available. The CSS file contains rudimentary style to avoid eye bleeding. -The document `<title>` can be overriden by calling -@ref Configuration::setTitle(), but that of course happens only after the app -fully loads. - The application redirects all output (thus also @ref Corrade::Utility::Debug "Debug", @ref Corrade::Utility::Warning "Warning" and @ref Corrade::Utility::Error "Error") to JavaScript console. It's possible to pass command-line arguments to `main()` @@ -419,6 +415,7 @@ class Sdl2Application { */ void mainLoopIteration(); + #ifndef CORRADE_TARGET_EMSCRIPTEN /** * @brief Underlying window handle * @@ -426,6 +423,7 @@ class Sdl2Application { * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". */ SDL_Window* window() { return _window; } + #endif protected: /* Nobody will need to have (and delete) Sdl2Application*, thus this is @@ -728,10 +726,12 @@ class Sdl2Application { typedef Containers::EnumSet Flags; CORRADE_ENUMSET_FRIEND_OPERATORS(Flags) + #ifndef CORRADE_TARGET_EMSCRIPTEN SDL_Window* _window; SDL_GLContext _glContext; - #ifndef CORRADE_TARGET_EMSCRIPTEN UnsignedInt _minimalLoopPeriod; + #else + SDL_Surface* _glContext; #endif std::unique_ptr _context; @@ -830,11 +830,12 @@ class Sdl2Application::Configuration { /*implicit*/ Configuration(); ~Configuration(); - #ifndef CORRADE_TARGET_IOS + #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) /** * @brief Window title * - * @note Not available in @ref CORRADE_TARGET_IOS "iOS". + * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten" + * and @ref CORRADE_TARGET_IOS "iOS". */ std::string title() const { return _title; } #endif @@ -844,13 +845,12 @@ class Sdl2Application::Configuration { * @return Reference to self (for method chaining) * * Default is `"Magnum SDL2 Application"`. - * @note In @ref CORRADE_TARGET_IOS "iOS" this function does nothing and - * is included only for compatibility. You need to set the title + * @note In @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten" and + * @ref CORRADE_TARGET_IOS "iOS" this function does nothing and is + * included only for compatibility. You need to set the title * separately in platform-specific configuration file. - * In @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten" this title will - * be used to set `document.title`. */ - #ifndef CORRADE_TARGET_IOS + #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) Configuration& setTitle(std::string title) { _title = std::move(title); return *this; @@ -975,7 +975,7 @@ class Sdl2Application::Configuration { #endif private: - #ifndef CORRADE_TARGET_IOS + #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) std::string _title; #endif Vector2i _size;