diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-01 23:58:00 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-01 23:58:00 +0100 |
commit | dee84335974f43f4d3661c5c7df481310357c24c (patch) | |
tree | 0ec8900c6d4768a7ba4af851e010c495c0b74585 /main | |
parent | c9ed7f5a14037a87f74650e4426056cbdec59116 (diff) |
main, imgui: use magnum's virtual dpi scale
Diffstat (limited to 'main')
-rw-r--r-- | main/draw.cpp | 27 | ||||
-rw-r--r-- | main/events.cpp | 15 | ||||
-rw-r--r-- | main/main-impl.cpp | 4 | ||||
-rw-r--r-- | main/main-impl.hpp | 2 | ||||
-rw-r--r-- | main/setup.cpp | 3 |
5 files changed, 26 insertions, 25 deletions
diff --git a/main/draw.cpp b/main/draw.cpp index 2f9bc737..9da9fbd7 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -12,11 +12,13 @@ namespace floormat { -void main_impl::recalc_viewport(Vector2i size) noexcept +void main_impl::recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept { + _dpi_scale = dpiScaling(); + _virtual_scale = Vector2(fb_size) / Vector2(win_size); update_window_state(); - _shader.set_scale(Vector2{size}); - GL::defaultFramebuffer.setViewport({{}, size }); + _shader.set_scale(Vector2{fb_size}); + GL::defaultFramebuffer.setViewport({{}, fb_size }); // -- state --- using R = GL::Renderer; @@ -28,17 +30,17 @@ void main_impl::recalc_viewport(Vector2i size) noexcept GL::Renderer::enable(R::Feature::ScissorTest); GL::Renderer::enable(R::Feature::DepthClamp); GL::Renderer::setDepthFunction(R::DepthFunction::Greater); - GL::Renderer::setScissor({{}, size}); + GL::Renderer::setScissor({{}, fb_size}); // -- user-- - app.on_viewport_event(size); + app.on_viewport_event(fb_size); } global_coords main_impl::pixel_to_tile(Vector2d position) const noexcept { constexpr Vector2d pixel_size(TILE_SIZE2); constexpr Vector2d half{.5, .5}; - const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset(); + const Vector2d px = position - Vector2d{framebufferSize()}*.5 - _shader.camera_offset(); const Vector2d vec = tile_shader::unproject(px*.5) / pixel_size + half; const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); return { x, y }; @@ -49,7 +51,7 @@ auto main_impl::get_draw_bounds() const noexcept -> draw_bounds using limits = std::numeric_limits<std::int16_t>; auto x0 = limits::max(), x1 = limits::min(), y0 = limits::max(), y1 = limits::min(); - for (const auto win = Vector2d(windowSize()); + for (const auto win = Vector2d(framebufferSize()); auto p : {pixel_to_tile(Vector2d{0, 0}).chunk(), pixel_to_tile(Vector2d{win[0]-1, 0}).chunk(), pixel_to_tile(Vector2d{0, win[1]-1}).chunk(), @@ -66,7 +68,7 @@ auto main_impl::get_draw_bounds() const noexcept -> draw_bounds void main_impl::draw_world() noexcept { const auto [minx, maxx, miny, maxy] = get_draw_bounds(); - const auto sz = windowSize(); + const auto sz = framebufferSize(); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) @@ -99,7 +101,7 @@ void main_impl::draw_world() noexcept void main_impl::draw_anim() noexcept { - const auto sz = windowSize(); + const auto sz = framebufferSize(); const auto [minx, maxx, miny, maxy] = get_draw_bounds(); _clickable_scenery.clear(); @@ -178,10 +180,11 @@ void main_impl::do_update() void main_impl::drawEvent() { - _dpi_scale = 1; +#ifndef __linux__ if (int index = SDL_GetWindowDisplayIndex(window()); index >= 0) - if (float dpi = 96; !SDL_GetDisplayDPI(index, &dpi, nullptr, nullptr)) - _dpi_scale = dpi / 96; + if (float hdpi = 96, vdpi = 96; !SDL_GetDisplayDPI(index, nullptr, &hdpi, &vdpi)) + _dpi_scale = { hdpi / 96, vdpi / 96 }; +#endif _shader.set_tint({1, 1, 1, 1}); diff --git a/main/events.cpp b/main/events.cpp index 922b2d23..11076b54 100644 --- a/main/events.cpp +++ b/main/events.cpp @@ -9,14 +9,13 @@ namespace floormat { void main_impl::viewportEvent(Platform::Sdl2Application::ViewportEvent& event) { - fm_assert(event.framebufferSize() == event.windowSize()); - recalc_viewport(event.windowSize()); - app.on_viewport_event(event.windowSize()); + recalc_viewport(event.framebufferSize(), event.windowSize()); + app.on_viewport_event(event.framebufferSize()); } void main_impl::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) { - app.on_mouse_up_down({event.position(), + app.on_mouse_up_down({event.position() * _virtual_scale, (SDL_Keymod)(std::uint16_t)event.modifiers(), mouse_button(SDL_BUTTON((std::uint8_t)event.button())), std::uint8_t(std::min(255, event.clickCount()))}, @@ -25,7 +24,7 @@ void main_impl::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) void main_impl::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) { - app.on_mouse_up_down({event.position(), + app.on_mouse_up_down({event.position() * _virtual_scale, (SDL_Keymod)(std::uint16_t)event.modifiers(), mouse_button(SDL_BUTTON((std::uint8_t)event.button())), std::uint8_t(std::min(255, event.clickCount()))}, @@ -34,14 +33,14 @@ void main_impl::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) void main_impl::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) { - app.on_mouse_move({event.position(), event.relativePosition(), - (mouse_button)(std::uint8_t)(std::uint32_t)event.buttons(), + app.on_mouse_move({event.position() * _virtual_scale, + (mouse_button)(std::uint8_t)std::uint32_t{event.buttons()}, (SDL_Keymod)(std::uint16_t)event.modifiers()}); } void main_impl::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event) { - app.on_mouse_scroll({event.offset(), event.position(), + app.on_mouse_scroll({event.offset(), event.position() * _virtual_scale, (SDL_Keymod)(std::uint16_t)event.modifiers()}); } diff --git a/main/main-impl.cpp b/main/main-impl.cpp index 0790a0d2..1da2d4ee 100644 --- a/main/main-impl.cpp +++ b/main/main-impl.cpp @@ -13,7 +13,7 @@ struct world& main_impl::world() noexcept { return _world; } SDL_Window* main_impl::window() noexcept { return Sdl2Application::window(); } fm_settings& main_impl::settings() noexcept { return s; } const fm_settings& main_impl::settings() const noexcept { return s; } -Vector2i main_impl::window_size() const noexcept { return windowSize(); } +Vector2i main_impl::window_size() const noexcept { return framebufferSize(); } tile_shader& main_impl::shader() noexcept { return _shader; } const tile_shader& main_impl::shader() const noexcept { return _shader; } bool main_impl::is_text_input_active() const noexcept { return const_cast<main_impl&>(*this).isTextInputActive(); } @@ -22,7 +22,7 @@ void main_impl::stop_text_input() noexcept { stopTextInput(); } int main_impl::exec() { - recalc_viewport(windowSize()); + recalc_viewport(framebufferSize(), windowSize()); return Sdl2Application::exec(); } diff --git a/main/main-impl.hpp b/main/main-impl.hpp index cc17f9bc..74ed8222 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -86,7 +86,7 @@ private: bool do_sleep = false; } dt_expected; - void recalc_viewport(Vector2i size) noexcept; + void recalc_viewport(Vector2i fb_size, Vector2i win_size) noexcept; void draw_world() noexcept; void draw_anim() noexcept; diff --git a/main/setup.cpp b/main/setup.cpp index 72bf3550..38df0ccf 100644 --- a/main/setup.cpp +++ b/main/setup.cpp @@ -20,7 +20,6 @@ main_impl::main_impl(floormat_app& app, fm_settings&& se, int& fake_argc) noexce else (void)setSwapInterval(0); set_fp_mask(); - fm_assert(framebufferSize() == windowSize()); _clickable_scenery.reserve(128); timeline.start(); } @@ -58,7 +57,7 @@ auto main_impl::make_conf(const fm_settings& s) -> Configuration } return Configuration{} .setTitle(s.title) - .setSize(s.resolution, Vector2(1, 1)) + .setSize(s.resolution, Configuration::DpiScalingPolicy::Virtual) .setWindowFlags(make_window_flags(s)); } |