From 988bf5d0368cb16846c02ea1482ea7b051ab97cb Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 26 Feb 2023 21:13:17 +0100 Subject: main: cache window size --- floormat/main.hpp | 3 ++- main/draw.cpp | 9 ++++----- main/events.cpp | 1 + main/main-impl.cpp | 9 +++++++-- main/main-impl.hpp | 1 - 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/floormat/main.hpp b/floormat/main.hpp index db2eb5c0..b729e9a2 100644 --- a/floormat/main.hpp +++ b/floormat/main.hpp @@ -42,7 +42,7 @@ struct floormat_main virtual int exec() = 0; virtual void quit(int status) = 0; - virtual Magnum::Math::Vector2 window_size() const noexcept = 0; + virtual Magnum::Math::Vector2 window_size() const noexcept; virtual tile_shader& shader() noexcept = 0; virtual const tile_shader& shader() const noexcept = 0; constexpr float smoothed_dt() const noexcept { return _frame_time1; } @@ -74,6 +74,7 @@ struct floormat_main protected: float _frame_time1 = 0, _frame_time2 = 0; Vector2 _dpi_scale{1, 1}, _virtual_scale{1, 1}; + Vector2i _framebuffer_size; }; } // namespace floormat diff --git a/main/draw.cpp b/main/draw.cpp index 2a3b3f7b..02f58541 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -48,7 +48,7 @@ Vector2d 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{framebufferSize()}*.5 - _shader.camera_offset(); + const Vector2d px = position - Vector2d{window_size()}*.5 - _shader.camera_offset(); return tile_shader::unproject(px*.5) / pixel_size + half; } @@ -57,7 +57,7 @@ auto main_impl::get_draw_bounds() const noexcept -> draw_bounds using limits = std::numeric_limits; auto x0 = limits::max(), x1 = limits::min(), y0 = limits::max(), y1 = limits::min(); - const auto win = Vector2d(framebufferSize()); + const auto win = Vector2d(window_size()); chunk_coords list[] = { pixel_to_tile(Vector2d{0, 0}).chunk(), @@ -79,7 +79,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 = framebufferSize(); + const auto sz = window_size(); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) @@ -96,7 +96,6 @@ void main_impl::draw_world() noexcept } _clickable_scenery.clear(); - const auto window_size = framebufferSize(); GL::Renderer::enable(GL::Renderer::Feature::DepthTest); GL::defaultFramebuffer.clearDepthStencil(0, 0); for (std::int16_t y = miny; y <= maxy; y++) @@ -127,7 +126,7 @@ void main_impl::draw_world() noexcept { const local_coords xy{i}; if (auto [atlas, s] = c[xy].scenery(); atlas) - _anim_mesh.add_clickable(_shader, window_size, pos, std::uint8_t(i), atlas, s, _clickable_scenery); + _anim_mesh.add_clickable(_shader, window_size(), pos, std::uint8_t(i), atlas, s, _clickable_scenery); } } } diff --git a/main/events.cpp b/main/events.cpp index 17db862f..6222f964 100644 --- a/main/events.cpp +++ b/main/events.cpp @@ -9,6 +9,7 @@ namespace floormat { void main_impl::viewportEvent(Platform::Sdl2Application::ViewportEvent& event) { + _framebuffer_size = event.framebufferSize(); recalc_viewport(event.framebufferSize(), event.windowSize()); app.on_viewport_event(event.framebufferSize()); } diff --git a/main/main-impl.cpp b/main/main-impl.cpp index ae3f8cfc..ba2fa3ec 100644 --- a/main/main-impl.cpp +++ b/main/main-impl.cpp @@ -15,7 +15,6 @@ 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 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(*this).isTextInputActive(); } @@ -26,7 +25,8 @@ const Platform::Sdl2Application& main_impl::application() const noexcept { retur int main_impl::exec() { - recalc_viewport(framebufferSize(), windowSize()); + _framebuffer_size = framebufferSize(); + recalc_viewport(_framebuffer_size, windowSize()); return Sdl2Application::exec(); } @@ -37,6 +37,11 @@ floormat_main* floormat_main::create(floormat_app& app, fm_settings&& options) return ret; } +Vector2i floormat_main::window_size() const noexcept +{ + return _framebuffer_size; +} + void main_impl::set_cursor(std::uint32_t cursor) noexcept { setCursor(Cursor(cursor)); diff --git a/main/main-impl.hpp b/main/main-impl.hpp index c078cb87..3ec6b8a5 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -29,7 +29,6 @@ struct main_impl final : Platform::Sdl2Application, floormat_main int exec() override; void quit(int status) override; - Magnum::Math::Vector2 window_size() const noexcept override; tile_shader& shader() noexcept override; const tile_shader& shader() const noexcept override; -- cgit v1.2.3