diff --git a/doc/changelog.dox b/doc/changelog.dox index 299a2e88e..7a5b9be6f 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -72,6 +72,8 @@ See also: @ref Platform::GlfwApplication::setCursor() and @ref Platform::EmscriptenApplication::setCursor() (see [mosra/magnum#383](https://github.com/mosra/magnum/pull/383)) +- Added @ref Platform::GlfwApplication::setWindowSize() and + @ref Platform::Sdl2Application::setWindowSize() - Window icon management using @ref Platform::Sdl2Application::setWindowIcon() and @ref Platform::GlfwApplication::setWindowIcon() (see [mosra/magnum#393](https://github.com/mosra/magnum/issues/393)) diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index 9d7df72f0..cc9f53e45 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -647,6 +647,13 @@ Vector2i GlfwApplication::windowSize() const { return size; } +void GlfwApplication::setWindowSize(const Vector2i& size) { + CORRADE_ASSERT(_window, "Platform::GlfwApplication::setWindowSize(): no window opened", ); + + const Vector2i newSize = _dpiScaling*size; + glfwSetWindowSize(_window, newSize.x(), newSize.y()); +} + #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 void GlfwApplication::setMinWindowSize(const Vector2i& size) { CORRADE_ASSERT(_window, "Platform::GlfwApplication::setMinWindowSize(): no window opened", ); diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index b8bada13b..39d5cfab0 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -355,6 +355,18 @@ class GlfwApplication { */ Vector2i windowSize() const; + /** + * @brief Set window size + * @param size The size, in screen coordinates + * @m_since_latest + * + * To make the sizing work independently of the display DPI, @p size is + * internally multiplied with @ref dpiScaling() before getting applied. + * Expects that a window is already created. + * @see @ref setMinWindowSize(), @ref setMaxWindowSize() + */ + void setWindowSize(const Vector2i& size); + #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 || defined(DOXYGEN_GENERATING_OUTPUT) /** * @brief Set window minimum size @@ -364,8 +376,9 @@ class GlfwApplication { * If a value is set to @cpp -1 @ce, it will disable/remove the * corresponding limit. To make the sizing work independently of the * display DPI, @p size is internally multiplied with @ref dpiScaling() - * before getting applied. + * before getting applied. Expects that a window is already created. * @note Supported since GLFW 3.2. + * @see @ref setMaxWindowSize(), @ref setWindowSize() */ void setMinWindowSize(const Vector2i& size = {-1, -1}); @@ -377,8 +390,9 @@ class GlfwApplication { * If a value is set to @cpp -1 @ce, it will disable/remove the * corresponding limit. To make the sizing work independently of the * display DPI, @p size is internally multiplied with @ref dpiScaling() - * before getting applied. + * before getting applied. Expects that a window is already created. * @note Supported since GLFW 3.2. + * @see @ref setMinWindowSize(), @ref setMaxWindowSize() */ void setMaxWindowSize(const Vector2i& size = {-1, -1}); #endif diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 00afd9e47..1f5ae7e24 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -639,6 +639,13 @@ Vector2i Sdl2Application::windowSize() const { } #ifndef CORRADE_TARGET_EMSCRIPTEN +void Sdl2Application::setWindowSize(const Vector2i& size) { + CORRADE_ASSERT(_window, "Platform::Sdl2Application::setWindowSize(): no window opened", ); + + const Vector2i newSize = _dpiScaling*size; + SDL_SetWindowSize(_window, newSize.x(), newSize.y()); +} + void Sdl2Application::setMinWindowSize(const Vector2i& size) { CORRADE_ASSERT(_window, "Platform::Sdl2Application::setMinWindowSize(): no window opened", ); diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 9bc616870..2dae1f491 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -637,6 +637,19 @@ class Sdl2Application { Vector2i windowSize() const; #if !defined(CORRADE_TARGET_EMSCRIPTEN) || defined(DOXYGEN_GENERATING_OUTPUT) + /** + * @brief Set window size + * @param size The size, in screen coordinates + * @m_since_latest + * + * To make the sizing work independently of the display DPI, @p size is + * internally multiplied with @ref dpiScaling() before getting applied. + * Expects that a window is already created. + * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + * @see @ref setMinWindowSize(), @ref setMaxWindowSize() + */ + void setWindowSize(const Vector2i& size); + /** * @brief Set minimum window size * @param size The minimum size, in screen coordinates @@ -645,8 +658,10 @@ class Sdl2Application { * Note that, unlike in @ref GlfwApplication, SDL2 doesn't have a way * to disable/remove a size limit. To make the sizing work * independently of the display DPI, @p size is internally multiplied - * with @ref dpiScaling() before getting applied. + * with @ref dpiScaling() before getting applied. Expects that a window + * is already created. * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + * @see @ref setMaxWindowSize(), @ref setWindowSize() */ void setMinWindowSize(const Vector2i& size); @@ -658,8 +673,10 @@ class Sdl2Application { * Note that, unlike in @ref GlfwApplication, SDL2 doesn't have a way * to disable/remove a size limit. To make the sizing work * independently of the display DPI, @p size is internally multiplied - * with @ref dpiScaling() before getting applied. + * with @ref dpiScaling() before getting applied. Expects that a window + * is already created. * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + * @see @ref setMinWindowSize(), @ref setMaxWindowSize() */ void setMaxWindowSize(const Vector2i& size); #endif diff --git a/src/Magnum/Platform/Test/GlfwApplicationTest.cpp b/src/Magnum/Platform/Test/GlfwApplicationTest.cpp index 9ea1ef775..4b56a1bf6 100644 --- a/src/Magnum/Platform/Test/GlfwApplicationTest.cpp +++ b/src/Magnum/Platform/Test/GlfwApplicationTest.cpp @@ -61,6 +61,9 @@ struct GlfwApplicationTest: Platform::Application { } else if(event.key() == KeyEvent::Key::T) { Debug{} << "setting window title"; setWindowTitle("This is a UTF-8 Window Title™!"); + } else if(event.key() == KeyEvent::Key::S) { + Debug{} << "setting window size, which should trigger a viewport event"; + setWindowSize(Vector2i{300, 200}); } #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 else if(event.key() == KeyEvent::Key::W) { diff --git a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp index 9e4cbd2c6..accd868c8 100644 --- a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp +++ b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp @@ -87,7 +87,10 @@ struct Sdl2ApplicationTest: Platform::Application { setWindowTitle("This is a UTF-8 Window Title™!"); } #ifndef CORRADE_TARGET_EMSCRIPTEN - else if(event.key() == KeyEvent::Key::W) { + else if(event.key() == KeyEvent::Key::S) { + Debug{} << "setting window size, which should trigger a viewport event"; + setWindowSize(Vector2i{300, 200}); + } else if(event.key() == KeyEvent::Key::W) { Debug{} << "setting max window size, which should trigger a viewport event"; setMaxWindowSize(Vector2i{700, 500}); }