Skip to content

Commit

Permalink
Platform: Move emscripten port of Sdl2Application to SDL2
Browse files Browse the repository at this point in the history
Signed-off-by: Squareys <squareys@googlemail.com>
  • Loading branch information
Squareys committed Oct 4, 2017
1 parent 628314b commit b2e7091
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
27 changes: 1 addition & 26 deletions src/Magnum/Platform/Sdl2Application.cpp
Expand Up @@ -100,8 +100,6 @@ 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) {
Int major, minor;
Expand Down Expand Up @@ -238,23 +236,12 @@ 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;
}

Expand All @@ -269,21 +256,13 @@ 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 {
Expand All @@ -310,12 +289,8 @@ 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();
}

Expand Down Expand Up @@ -526,7 +501,7 @@ void Sdl2Application::textInputEvent(TextInputEvent&) {}
void Sdl2Application::textEditingEvent(TextEditingEvent&) {}

Sdl2Application::Configuration::Configuration():
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
#ifndef CORRADE_TARGET_IOS
_title("Magnum SDL2 Application"),
#endif
#ifdef CORRADE_TARGET_EMSCRIPTEN
Expand Down
22 changes: 9 additions & 13 deletions src/Magnum/Platform/Sdl2Application.h
Expand Up @@ -415,15 +415,13 @@ class Sdl2Application {
*/
void mainLoopIteration();

#ifndef CORRADE_TARGET_EMSCRIPTEN
/**
* @brief Underlying window handle
*
* Use in case you need to call SDL functionality directly.
* @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
Expand Down Expand Up @@ -726,12 +724,10 @@ class Sdl2Application {
typedef Containers::EnumSet<Flag> 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<Platform::Context> _context;
Expand Down Expand Up @@ -830,12 +826,11 @@ class Sdl2Application::Configuration {
/*implicit*/ Configuration();
~Configuration();

#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
#ifndef CORRADE_TARGET_IOS
/**
* @brief Window title
*
* @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten"
* and @ref CORRADE_TARGET_IOS "iOS".
* @note Not available in @ref CORRADE_TARGET_IOS "iOS".
*/
std::string title() const { return _title; }
#endif
Expand All @@ -845,12 +840,13 @@ class Sdl2Application::Configuration {
* @return Reference to self (for method chaining)
*
* Default is `"Magnum SDL2 Application"`.
* @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
* @note In @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`.
*/
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
#ifndef CORRADE_TARGET_IOS
Configuration& setTitle(std::string title) {
_title = std::move(title);
return *this;
Expand Down Expand Up @@ -975,7 +971,7 @@ class Sdl2Application::Configuration {
#endif

private:
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
#ifndef CORRADE_TARGET_IOS
std::string _title;
#endif
Vector2i _size;
Expand Down

0 comments on commit b2e7091

Please sign in to comment.