diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-03 21:41:44 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-04 00:16:57 +0200 |
commit | 9bb683fe25e6c7cdaa50276433628dc0e1073f9f (patch) | |
tree | 639125819afef89f3c4d72bd3bb5f174d544828a /main | |
parent | de99a85a63aa5cb85a4f5e67f8d5b1e7f3671c47 (diff) |
editor: move duplicated code to pixel_to_point()
Diffstat (limited to 'main')
-rw-r--r-- | main/main-impl.hpp | 3 | ||||
-rw-r--r-- | main/projection.cpp | 19 |
2 files changed, 19 insertions, 3 deletions
diff --git a/main/main-impl.hpp b/main/main-impl.hpp index 39e07d4c..66f143d1 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -65,8 +65,9 @@ struct main_impl final : Platform::Sdl2Application, floormat_main fm_settings& settings() noexcept override; const fm_settings& settings() const noexcept override; - global_coords pixel_to_tile(Vector2d position) const noexcept override; + global_coords pixel_to_tile(Vector2d position, int8_t z_level = 0) const noexcept override; Vector2d pixel_to_tile_(Vector2d position) const noexcept override; + point pixel_to_point(Vector2d position, int8_t z_level = 0) const noexcept override; ArrayView<const clickable> clickable_scenery() const noexcept override; ArrayView<clickable> clickable_scenery() noexcept override; diff --git a/main/projection.cpp b/main/projection.cpp index 8b32777e..b7ee01da 100644 --- a/main/projection.cpp +++ b/main/projection.cpp @@ -1,15 +1,16 @@ #include "main-impl.hpp" #include "src/tile-constants.hpp" +#include "src/point.hpp" #include <algorithm> #include <limits> namespace floormat { -global_coords main_impl::pixel_to_tile(Vector2d position) const noexcept +global_coords main_impl::pixel_to_tile(Vector2d position, int8_t z_level) const noexcept { auto vec = pixel_to_tile_(position); auto vec_ = Math::floor(vec); - return { (int32_t)vec_.x(), (int32_t)vec_.y(), 0 }; + return { (int32_t)vec_.x(), (int32_t)vec_.y(), z_level }; } Vector2d main_impl::pixel_to_tile_(Vector2d position) const noexcept @@ -20,6 +21,20 @@ Vector2d main_impl::pixel_to_tile_(Vector2d position) const noexcept return tile_shader::unproject(px*.5) / pixel_size + half; } +point main_impl::pixel_to_point(Vector2d pixel, int8_t z_level) const noexcept +{ + const auto tileʹ = pixel_to_tile_(pixel); + const auto tileʹʹ = Math::floor(tileʹ); + const auto tile = global_coords{(int)tileʹʹ.x(), (int)tileʹʹ.y(), z_level}; + const auto subpixelʹ = Math::fmod(Vector2(tileʹ), 1.f); + const auto subpixelʹ_neg = Vector2{Vector2i(tile.chunk()) < Vector2i{}}; + auto subpixel = TILE_SIZE2 * (subpixelʹ + subpixelʹ_neg); + constexpr auto half_tile = Vector2(iTILE_SIZE2/2); + subpixel -= half_tile; + subpixel = Math::clamp(Math::round(subpixel), -half_tile, half_tile-Vector2{1.f}); + return point{ tile, Vector2b{subpixel} }; +} + auto main_impl::get_draw_bounds() const noexcept -> draw_bounds { using limits = std::numeric_limits<int16_t>; |