Skip to content

Commit

Permalink
Resizable and draw event, copy more dpi stuff from Sdl2Application
Browse files Browse the repository at this point in the history
For some reason there is no clearing happening, but drawEvent() is being
called.

Signed-off-by: Squareys <squareys@googlemail.com>
  • Loading branch information
Squareys committed Apr 19, 2019
1 parent 8da4c79 commit 19d9c9f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 28 deletions.
34 changes: 20 additions & 14 deletions src/Magnum/Platform/EmscriptenApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ void EmscriptenApplication::create(const Configuration& configuration, const GLC

bool EmscriptenApplication::tryCreate(const Configuration& configuration) {
#ifdef MAGNUM_TARGET_GL
if(!(configuration.windowFlags() & Configuration::WindowFlag::Contextless))
{
if(!(configuration.windowFlags() & Configuration::WindowFlag::Contextless)) {
return tryCreate(configuration, GLConfiguration{});
}
#endif
if(configuration.windowFlags() & Configuration::WindowFlag::Resizable) {
_flags |= Flag::Resizable;
}

/* Resize window and match it to the selected format */
double w, h;
Expand All @@ -98,6 +100,9 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration) {
#ifdef MAGNUM_TARGET_GL
bool EmscriptenApplication::tryCreate(const Configuration& configuration, const GLConfiguration& glConfiguration) {
CORRADE_ASSERT(_context->version() == GL::Version::None, "Platform::EmscriptenApplication::tryCreate(): window with OpenGL context already created", false);
if(configuration.windowFlags() & Configuration::WindowFlag::Resizable) {
_flags |= Flag::Resizable;
}
/* Create emscripten WebGL context */
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
Expand All @@ -118,8 +123,10 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration, const
(glConfiguration.flags() & GLConfiguration::Flag::EnableExtensionsByDefault) != GLConfiguration::Flags{};

#ifdef MAGNUM_TARGET_GLES3
/* WebGL 2 */
attrs.majorVersion = 2;
#elif defined(MAGNUM_TARGET_GLES2)
/* WebGL 1 */
attrs.minorVersion = 1;
#else
#error unsupported OpenGL ES version
Expand All @@ -134,8 +141,8 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration, const
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context =
emscripten_webgl_create_context("#canvas", &attrs);
if(!context) {
Error() << "Platform::EmscriptenApplication::tryCreate(): cannot create WebGL context:";
/*<< Implementation::glErrorString(glGetError()); */
Error{} << "Platform::EmscriptenApplication::tryCreate(): cannot create WebGL context:";
/*<< Implementation::glErrorString(glGetError{}); */
return false;
}

Expand All @@ -161,7 +168,6 @@ void EmscriptenApplication::swapBuffers() {
}

void EmscriptenApplication::setupCallbacks() {
Debug() << "EmscriptenApplication::setupCallbacks()";
auto mousePressedCallback =
[](int, const EmscriptenMouseEvent* event, void* userData) -> Int {
MouseEvent e{event};
Expand Down Expand Up @@ -324,7 +330,9 @@ EmscriptenApplication::KeyEvent::Key EmscriptenApplication::KeyEvent::toKey(cons
return Key::Unknown;
}

/* Uh... I don't know these... map? */
// Uh... I don't know these... map?

// TODO: else if is a thing also
if(strcmp(keyCode, "Backspace") == 0) {
return Key::Backspace;
}
Expand Down Expand Up @@ -480,16 +488,13 @@ void EmscriptenApplication::mainLoopIteration() {
/* 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(true) {// _flags & Flag::Resizable) {
Vector2d canvasSize;
issue: https://github.com/kripken/emscripten/issues/1731 */
if(_flags & Flag::Resizable) {
/* Emscripten 1.38.27 changed to generic CSS selectors from element
IDs depending on -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1
being set (which we can't detect at compile time). See above for the
reason why we hardcode #canvas here.
emscripten_get_element_css_size("#canvas", &canvasSize.x(), &canvasSize.y());
const Vector2i canvasSizei{canvasSize};
reason why we hardcode #canvas here. */
const Vector2i canvasSizei{windowSize()};
if(canvasSizei != _lastKnownCanvasSize) {
_lastKnownCanvasSize = canvasSizei;
const Vector2i size = _dpiScaling*canvasSizei;
Expand All @@ -499,7 +504,8 @@ void EmscriptenApplication::mainLoopIteration() {
_flags |= Flag::Redraw;
}
}
*/

drawEvent();
}

int EmscriptenApplication::exec() {
Expand Down
95 changes: 86 additions & 9 deletions src/Magnum/Platform/EmscriptenApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,28 @@ class EmscriptenApplication {
*/
Vector2i windowSize() const;

/**
* @brief DPI scaling
*
* How the content should be scaled relative to system defaults for
* given @ref windowSize(). If a window is not created yet, returns
* zero vector, use @ref dpiScaling(const Configuration&) const for
* calculating a value independently. See @ref Platform-Sdl2Application-dpi
* for more information.
* @see @ref Sdl2Application::dpiScaling(), @ref framebufferSize()
*/
Vector2 dpiScaling() const { return _dpiScaling; }

/**
* @brief DPI scaling for given configuration
*
* Calculates DPI scaling that would be used when creating a window
* with given @p configuration. Takes into account DPI scaling policy
* and custom scaling specified on the command-line. See
* @ref Platform-Sdl2Application-dpi for more information.
*/
Vector2 dpiScaling(const Configuration& configuration) const;

protected:
/**
* @brief Swap buffers
Expand Down Expand Up @@ -354,10 +376,14 @@ class EmscriptenApplication {

private:
enum class Flag: UnsignedByte {
Redraw = 1 << 0
Redraw = 1 << 0,
Resizable = 1 << 1,
};
typedef Containers::EnumSet<Flag> Flags;

Vector2 _dpiScaling;
Vector2i _lastKnownCanvasSize;

Flags _flags;

#ifdef MAGNUM_TARGET_GL
Expand Down Expand Up @@ -578,7 +604,9 @@ class EmscriptenApplication::Configuration {
* @ref tryCreate(const Configuration&) to prevent implicit
* creation of an OpenGL context.
*/
Contextless = 1 << 8
Contextless = 1 << 0,

Resizable = 1 << 1
};


Expand Down Expand Up @@ -647,27 +675,76 @@ class EmscriptenApplication::Configuration {
*/
class EmscriptenApplication::ViewportEvent {
public:
/** @brief Copying is not allowed */
ViewportEvent(const ViewportEvent&) = delete;

/** @brief Moving is not allowed */
ViewportEvent(ViewportEvent&&) = delete;

/** @brief Copying is not allowed */
ViewportEvent& operator=(const ViewportEvent&) = delete;

/** @brief Moving is not allowed */
ViewportEvent& operator=(ViewportEvent&&) = delete;

/**
* @brief Window size
*
* @see @ref EmscriptenApplication::windowSize()
* On some platforms with HiDPI displays, window size can be different
* from @ref framebufferSize(). See @ref Platform-Sdl2Application-dpi
* for more information.
* @see @ref Sdl2Application::windowSize()
*/
Vector2i windowSize() const { return _windowSize; }

#if defined(MAGNUM_TARGET_GL) || defined(DOXYGEN_GENERATING_OUTPUT)
/**
* @brief Framebuffer size
*
* The same as @ref windowSize().
* @todo this might not be true, implement properly!
* On some platforms with HiDPI displays, framebuffer size can be
* different from @ref windowSize(). See
* @ref Platform-Sdl2Application-dpi for more information.
*
* @note This function is available only if Magnum is compiled with
* @ref MAGNUM_TARGET_GL enabled (done by default). See
* @ref building-features for more information.
*
* @see @ref Sdl2Application::framebufferSize()
*/
Vector2i framebufferSize() const { return _windowSize; }
Vector2i framebufferSize() const { return _framebufferSize; }
#endif

/**
* @brief DPI scaling
*
* On some platforms moving an app between displays can result in DPI
* scaling value being changed in tandem with a window/framebuffer
* size. Simply resizing a window doesn't change the DPI scaling value.
* See @ref Platform-Sdl2Application-dpi for more information.
* @see @ref EmscriptenApplication::dpiScaling()
*/
Vector2 dpiScaling() const { return _dpiScaling; }

private:
friend EmscriptenApplication;

explicit ViewportEvent(const Vector2i& windowSize): _windowSize{windowSize} {}

Vector2i _windowSize;
explicit ViewportEvent(
const Vector2i& windowSize,
#ifdef MAGNUM_TARGET_GL
const Vector2i& framebufferSize,
#endif
const Vector2& dpiScaling):
_windowSize{windowSize},
#ifdef MAGNUM_TARGET_GL
_framebufferSize{framebufferSize},
#endif
_dpiScaling{dpiScaling} {}

const Vector2i _windowSize;
#ifdef MAGNUM_TARGET_GL
const Vector2i _framebufferSize;
#endif
const Vector2 _dpiScaling;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Magnum/Platform/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ endif()

if(WITH_EMSCRIPTENAPPLICATION)
add_executable(PlatformEmscriptenApplicationTest EmscriptenApplicationTest.cpp)
target_link_libraries(PlatformEmscriptenApplicationTest PRIVATE MagnumEmscriptenApplication)
target_link_libraries(PlatformEmscriptenApplicationTest PRIVATE MagnumEmscriptenApplication MagnumGL)
set_target_properties(PlatformEmscriptenApplicationTest PROPERTIES FOLDER "Magnum/Platform/Test")
# So we can spin up a webserver in the build dir for easy testing
file(COPY
Expand Down
19 changes: 15 additions & 4 deletions src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,30 @@
*/

#include "Magnum/Platform/EmscriptenApplication.h"
#include "Magnum/GL/Renderer.h"
#include "Magnum/GL/DefaultFramebuffer.h"
#include "Magnum/Math/Color.h"

namespace Magnum { namespace Platform { namespace Test {

struct EmscriptenApplicationTest: Platform::Application {
/* For testing resize events */
explicit EmscriptenApplicationTest(const Arguments& arguments): Platform::Application{arguments, Configuration{}/*.setWindowFlags(Configuration::WindowFlag::Resizable)*/} {}
explicit EmscriptenApplicationTest(const Arguments& arguments):
Platform::Application{arguments, Configuration{}
.setWindowFlags(Configuration::WindowFlag::Resizable)} {
}

virtual void drawEvent() override {
glViewport(0, 0, 640, 480);
GL::Renderer::setClearColor(Color4{1.0, 1.0, 1.0, 1.0});
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);

swapBuffers();
}

/* For testing HiDPI resize events */
void viewportEvent(ViewportEvent&) override {
//Debug{} << "viewport event" << event.windowSize() << event.framebufferSize() << event.dpiScaling();
void viewportEvent(ViewportEvent& event) override {
Debug{} << "viewport event" << event.windowSize() << event.framebufferSize() << event.dpiScaling();
}

/* For testing event coordinates */
Expand All @@ -47,7 +58,7 @@ struct EmscriptenApplicationTest: Platform::Application {

/* For testing keyboard capture */
void keyPressEvent(KeyEvent& event) override {
Debug{} << int(event.key());//event.keyName();
Debug{} << int(event.key());
}
};

Expand Down

0 comments on commit 19d9c9f

Please sign in to comment.