|
|
|
|
@ -303,12 +303,12 @@ bool Sdl2Application::tryCreate(const Configuration& configuration, const GLConf
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, glConfiguration.isSRGBCapable()); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/** @todo Remove when Emscripten has proper SDL2 support */ |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
/* Scale window based on DPI */ |
|
|
|
|
_dpiScaling = dpiScaling(configuration); |
|
|
|
|
const Vector2i scaledWindowSize = configuration.size()*_dpiScaling; |
|
|
|
|
|
|
|
|
|
/** @todo Remove when Emscripten has proper SDL2 support */ |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
/* Set context version, if user-specified */ |
|
|
|
|
if(glConfiguration.version() != GL::Version::None) { |
|
|
|
|
Int major, minor; |
|
|
|
|
@ -446,9 +446,42 @@ bool Sdl2Application::tryCreate(const Configuration& configuration, const GLConf
|
|
|
|
|
SDL_GL_GetDrawableSize(_window, &drawableSize.x(), &drawableSize.y()); |
|
|
|
|
glViewport(0, 0, drawableSize.x(), drawableSize.y()); |
|
|
|
|
#endif |
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
/* Emscripten-specific initialization */ |
|
|
|
|
if(!(_glContext = SDL_SetVideoMode(scaledWindowSize.x(), scaledWindowSize.y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF))) { |
|
|
|
|
#else |
|
|
|
|
/* Get CSS canvas size. This is used later to detect canvas resizes and
|
|
|
|
|
fire viewport events, because Emscripten doesn't do that. Related info: |
|
|
|
|
https://github.com/kripken/emscripten/issues/1731 */
|
|
|
|
|
/** @todo don't hardcode "module" here, make it configurable from outside */ |
|
|
|
|
{ |
|
|
|
|
Vector2d canvasSize; |
|
|
|
|
emscripten_get_element_css_size("module", &canvasSize.x(), &canvasSize.y()); |
|
|
|
|
_lastKnownCanvasSize = Vector2i{canvasSize}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* By default Emscripten creates a 300x150 canvas. That's so freaking
|
|
|
|
|
random I'm getting mad. Use the real (CSS pixels) canvas size instead, |
|
|
|
|
if the size is not hardcoded from the configuration. This is then |
|
|
|
|
multiplied by the DPI scaling. */ |
|
|
|
|
Vector2i windowSize; |
|
|
|
|
if(!configuration.size().isZero()) { |
|
|
|
|
windowSize = configuration.size(); |
|
|
|
|
} else { |
|
|
|
|
windowSize = _lastKnownCanvasSize; |
|
|
|
|
Debug{_verboseLog ? Debug::output() : nullptr} << "Platform::Sdl2Application::tryCreate(): autodetected canvas size" << windowSize; |
|
|
|
|
} |
|
|
|
|
_dpiScaling = dpiScaling(configuration); |
|
|
|
|
const Vector2i scaledWindowSize = windowSize*_dpiScaling; |
|
|
|
|
|
|
|
|
|
Uint32 flags = SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF; |
|
|
|
|
if(configuration.windowFlags() & Configuration::WindowFlag::Resizable) { |
|
|
|
|
_flags |= Flag::Resizable; |
|
|
|
|
/* Actually not sure if this makes any difference:
|
|
|
|
|
https://github.com/kripken/emscripten/issues/1731 */
|
|
|
|
|
flags |= SDL_RESIZABLE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!(_glContext = SDL_SetVideoMode(scaledWindowSize.x(), scaledWindowSize.y(), 24, flags))) { |
|
|
|
|
Error() << "Platform::Sdl2Application::tryCreate(): cannot create context:" << SDL_GetError(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
@ -478,23 +511,23 @@ bool Sdl2Application::tryCreate(const Configuration& configuration, const GLConf
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
Vector2i Sdl2Application::windowSize() const { |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
Vector2i size; |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
SDL_GetWindowSize(_window, &size.x(), &size.y()); |
|
|
|
|
return size; |
|
|
|
|
#else |
|
|
|
|
return {_glContext->w, _glContext->h}; |
|
|
|
|
emscripten_get_canvas_element_size(nullptr, &size.x(), &size.y()); |
|
|
|
|
#endif |
|
|
|
|
return size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Vector2i Sdl2Application::framebufferSize() const { |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
Vector2i size; |
|
|
|
|
#ifndef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
SDL_GL_GetDrawableSize(_window, &size.x(), &size.y()); |
|
|
|
|
return size; |
|
|
|
|
#else |
|
|
|
|
return {_glContext->w, _glContext->h}; |
|
|
|
|
emscripten_get_canvas_element_size(nullptr, &size.x(), &size.y()); |
|
|
|
|
#endif |
|
|
|
|
return size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Sdl2Application::swapBuffers() { |
|
|
|
|
@ -567,16 +600,44 @@ void Sdl2Application::mainLoopIteration() {
|
|
|
|
|
const UnsignedInt timeBefore = _minimalLoopPeriod ? SDL_GetTicks() : 0; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
/* The resize event is not fired on window resize, so poll for the canvas
|
|
|
|
|
size here. But only if the window was requested to be resizable, to |
|
|
|
|
avoid resizing the canvas when the user doesn't want that. Related |
|
|
|
|
issue: https://github.com/kripken/emscripten/issues/1731 */
|
|
|
|
|
if(_flags & Flag::Resizable) { |
|
|
|
|
/** @todo don't hardcode "module" here, make it configurable from outside */ |
|
|
|
|
Vector2d canvasSize; |
|
|
|
|
emscripten_get_element_css_size("module", &canvasSize.x(), &canvasSize.y()); |
|
|
|
|
const Vector2i canvasSizei{canvasSize}; |
|
|
|
|
if(canvasSizei != _lastKnownCanvasSize) { |
|
|
|
|
_lastKnownCanvasSize = canvasSizei; |
|
|
|
|
const Vector2i size = _dpiScaling*canvasSizei; |
|
|
|
|
emscripten_set_canvas_element_size(nullptr, size.x(), size.y()); |
|
|
|
|
ViewportEvent e{size, size, _dpiScaling}; |
|
|
|
|
viewportEvent(e); |
|
|
|
|
_flags |= Flag::Redraw; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
SDL_Event event; |
|
|
|
|
while(SDL_PollEvent(&event)) { |
|
|
|
|
switch(event.type) { |
|
|
|
|
case SDL_WINDOWEVENT: |
|
|
|
|
switch(event.window.event) { |
|
|
|
|
case SDL_WINDOWEVENT_RESIZED: { |
|
|
|
|
#ifdef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
/* If anybody sees this assert, then emscripten finally
|
|
|
|
|
implemented resize events. Praise them for that. |
|
|
|
|
https://github.com/kripken/emscripten/issues/1731 */
|
|
|
|
|
CORRADE_ASSERT_UNREACHABLE(); |
|
|
|
|
#else |
|
|
|
|
ViewportEvent e{{event.window.data1, event.window.data2}, framebufferSize(), _dpiScaling}; |
|
|
|
|
/** @todo handle also WM_DPICHANGED events when a window is moved between displays with different DPI */ |
|
|
|
|
viewportEvent(e); |
|
|
|
|
_flags |= Flag::Redraw; |
|
|
|
|
#endif |
|
|
|
|
} break; |
|
|
|
|
case SDL_WINDOWEVENT_EXPOSED: |
|
|
|
|
_flags |= Flag::Redraw; |
|
|
|
|
@ -772,9 +833,7 @@ Sdl2Application::Configuration::Configuration():
|
|
|
|
|
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) |
|
|
|
|
_title("Magnum SDL2 Application"), |
|
|
|
|
#endif |
|
|
|
|
#ifdef CORRADE_TARGET_EMSCRIPTEN |
|
|
|
|
_size{640, 480}, |
|
|
|
|
#elif !defined(CORRADE_TARGET_IOS) |
|
|
|
|
#if !defined(CORRADE_TARGET_IOS) && !defined(CORRADE_TARGET_EMSCRIPTEN) |
|
|
|
|
_size{800, 600}, |
|
|
|
|
#else |
|
|
|
|
_size{}, /* SDL2 detects someting for us */ |
|
|
|
|
|