From 321b394c397f826dbbda6e617aece10e732daadc Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 24 Feb 2023 10:54:58 +0100 Subject: fix (?) wrong global_coords unsigned offset --- editor/draw.cpp | 2 +- editor/imgui.cpp | 10 ++++------ editor/update.cpp | 4 ++-- main/draw.cpp | 15 ++++++++++----- src/global-coords.hpp | 12 ++++++------ 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/editor/draw.cpp b/editor/draw.cpp index 32319c1b..baf7f135 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -135,7 +135,7 @@ clickable_scenery* app::find_clickable_scenery(const Optional& pixel_) item = &c; } } - if (item && item->item.can_activate(item->atlas)) + if (item) return item; else return nullptr; diff --git a/editor/imgui.cpp b/editor/imgui.cpp index 6126dda0..4aca859e 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -5,6 +5,7 @@ #include "world.hpp" #include "scenery.hpp" #include "inspect.hpp" +#include "main/clickable.hpp" #include namespace floormat { @@ -155,12 +156,9 @@ void app::draw_inspector() auto b = push_id("inspector"); auto& w = M->world(); - if (cursor.tile) - { - auto [c, t] = w[*cursor.tile]; - if (auto s = t.scenery()) - tile = *cursor.tile; - } + if (cursor.pixel) + if (const auto* sc = find_clickable_scenery(cursor.pixel)) + tile = {InPlaceInit, sc->chunk, sc->pos}; if (tile) { auto [c, t] = w[*tile]; diff --git a/editor/update.cpp b/editor/update.cpp index 3c8f45fe..161f6869 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -58,7 +58,7 @@ void app::do_mouse_up_down(std::uint8_t button, bool is_down, int mods) default: case editor_mode::none: if (button == mouse_button_left) - if (auto* s = find_clickable_scenery(*cursor.pixel)) + if (auto* s = find_clickable_scenery(*cursor.pixel); s && s->item.can_activate(s->atlas)) return (void)s->item.activate(s->atlas); break; case editor_mode::floor: @@ -164,7 +164,7 @@ void app::update(float dt) do_camera(dt, keys, get_key_modifiers()); clear_non_repeated_keys(); - if ([[maybe_unused]] clickable_scenery* s = find_clickable_scenery(cursor.pixel)) + if (auto* s = find_clickable_scenery(cursor.pixel); s && s->item.can_activate(s->atlas)) M->set_cursor(std::uint32_t(Cursor::Hand)); else set_cursor_from_imgui(); diff --git a/main/draw.cpp b/main/draw.cpp index 1cb9b396..81dd7172 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -52,11 +52,16 @@ 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(); - 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(), - pixel_to_tile(Vector2d{win[0]-1, win[1]-1}).chunk()}) + const auto win = Vector2d(framebufferSize()); + + chunk_coords list[] = { + 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(), + pixel_to_tile(Vector2d{win[0]-1, win[1]-1}).chunk(), + }; + + for (auto p : list) { x0 = std::min(x0, p.x); x1 = std::max(x1, p.x); diff --git a/src/global-coords.hpp b/src/global-coords.hpp index 94b0d48e..9c2d082b 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -22,16 +22,16 @@ struct global_coords final { static constexpr std::uint32_t _0u = 1 << 15; static constexpr auto _0s = std::int32_t(_0u); - std::uint32_t x = _0u, y = _0u; + std::uint32_t x = _0u<<4, y = _0u<<4; constexpr global_coords() noexcept = default; constexpr global_coords(chunk_coords c, local_coords xy) : - x{ std::uint32_t(c.x + _0s) << 4 | (xy.x & 0x0f) }, - y{ std::uint32_t(c.y + _0s) << 4 | (xy.y & 0x0f) } + x{ std::uint32_t((c.x + _0s) << 4) | (xy.x & 0x0f) }, + y{ std::uint32_t((c.y + _0s) << 4) | (xy.y & 0x0f) } {} constexpr global_coords(std::uint32_t x, std::uint32_t y) noexcept : x{x}, y{y} {} constexpr global_coords(std::int32_t x, std::int32_t y) noexcept : - x{std::uint32_t(x + _0s)}, y{std::uint32_t(y + _0s)} + x{std::uint32_t(x + (_0s<<4))}, y{std::uint32_t(y + (_0s<<4))} {} constexpr local_coords local() const noexcept; @@ -54,12 +54,12 @@ constexpr local_coords global_coords::local() const noexcept constexpr chunk_coords global_coords::chunk() const noexcept { - return { std::int16_t((x - _0u) >> 4), std::int16_t((y - _0u) >> 4), }; + return { std::int16_t(std::int32_t((x>>4) - _0u)), std::int16_t(std::int32_t((y>>4) - _0u)), }; } constexpr Vector2i global_coords::to_signed() const noexcept { - return { std::int32_t(x - _0s), std::int32_t(y - _0s), }; + return { std::int32_t(x - (_0s<<4)), std::int32_t(y - (_0s<<4)), }; } constexpr global_coords global_coords::operator+(Vector2i vec) const noexcept -- cgit v1.2.3