diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-27 13:27:34 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-27 13:27:34 +0100 |
commit | 07d7c30d33a3adfc24031d63582ac0b7b71bd96b (patch) | |
tree | 8c958a810cd19b0de857276a542dfd1d43215fe6 /editor | |
parent | 2828671f29154e244fd826b71e0458b2f7dfcba9 (diff) |
wip
Diffstat (limited to 'editor')
-rw-r--r-- | editor/tests-private.hpp | 15 | ||||
-rw-r--r-- | editor/tests.cpp | 18 | ||||
-rw-r--r-- | editor/tests/path-test.cpp | 2 | ||||
-rw-r--r-- | editor/tests/raycast-test.cpp | 98 |
4 files changed, 117 insertions, 16 deletions
diff --git a/editor/tests-private.hpp b/editor/tests-private.hpp index 7967256e..15c6abaf 100644 --- a/editor/tests-private.hpp +++ b/editor/tests-private.hpp @@ -5,8 +5,7 @@ #include "src/object-id.hpp" #include "floormat/events.hpp" #include <Corrade/Containers/StringView.h> -#include <memory> -#include <vector> +#include <Corrade/Containers/Pointer.h> namespace floormat { struct app; } @@ -34,7 +33,7 @@ protected: void label_left(StringView label, float width); enum class Test : uint32_t { - none, path, COUNT, + none, path, raycast, COUNT, }; struct tests_data final : tests_data_ @@ -43,22 +42,24 @@ struct tests_data final : tests_data_ void switch_to(Test i); - static std::unique_ptr<base_test> make_test_none(); - static std::unique_ptr<base_test> make_test_path(); + static Pointer<base_test> make_test_none(); + static Pointer<base_test> make_test_path(); + static Pointer<base_test> make_test_raycast(); - std::unique_ptr<base_test> current_test; + Pointer<base_test> current_test; Test current_index = Test::none; struct test_tuple { StringView name; Test t; - std::unique_ptr<base_test>(*ctor)(); + Pointer<base_test>(*ctor)(); }; static constexpr test_tuple fields[] = { { "None"_s, Test::none, &tests_data::make_test_none, }, { "Path"_s, Test::path, &tests_data::make_test_path, }, + { "Raycasting"_s, Test::raycast, &tests_data::make_test_raycast }, }; }; diff --git a/editor/tests.cpp b/editor/tests.cpp index 1521fff7..f7d7565e 100644 --- a/editor/tests.cpp +++ b/editor/tests.cpp @@ -9,6 +9,8 @@ namespace floormat::tests { +static_assert(arraySize(tests_data::fields) == (size_t)Test::COUNT); + void label_left(StringView label, float width) { float x = ImGui::GetCursorPosX(); @@ -18,7 +20,7 @@ void label_left(StringView label, float width) ImGui::SetNextItemWidth(-1); } -std::unique_ptr<base_test> tests_data::make_test_none() { return {}; } +Pointer<base_test> tests_data::make_test_none() { return {}; } } // namespace floormat::tests @@ -38,7 +40,7 @@ using namespace floormat::imgui; void tests_data::switch_to(Test i) { - fm_assert((size_t)i < std::size(fields)); + fm_assert((size_t)i < arraySize(fields)); current_index = Test::none; current_test = make_test_none(); switch (i) @@ -58,13 +60,13 @@ safe_ptr<tests_data_> tests_data_::make() void app::tests_pre_update() { - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) x->update_pre(*this); } void app::tests_post_update() { - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) x->update_post(*this); } @@ -72,7 +74,7 @@ bool app::tests_handle_mouse_click(const mouse_button_event& e, bool is_down) { update_cursor_tile(cursor.pixel); - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) return x->handle_mouse_click(*this, e, is_down); else return false; @@ -80,7 +82,7 @@ bool app::tests_handle_mouse_click(const mouse_button_event& e, bool is_down) bool app::tests_handle_key(const key_event& e, bool is_down) { - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) return x->handle_key(*this, e, is_down); else return false; @@ -88,7 +90,7 @@ bool app::tests_handle_key(const key_event& e, bool is_down) bool app::tests_handle_mouse_move(const mouse_move_event& e) { - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) return x->handle_mouse_move(*this, e); else return false; @@ -128,7 +130,7 @@ void app::draw_tests_pane(float width) void app::draw_tests_overlay() { - if (const auto& x = tests().current_test) + if (auto& x = tests().current_test) x->draw_overlay(*this); } diff --git a/editor/tests/path-test.cpp b/editor/tests/path-test.cpp index 4ed750ec..c03ef990 100644 --- a/editor/tests/path-test.cpp +++ b/editor/tests/path-test.cpp @@ -243,6 +243,6 @@ void path_test::draw_ui(app& a, float width) } } -std::unique_ptr<base_test> tests_data::make_test_path() { return std::make_unique<path_test>(); } +Pointer<base_test> tests_data::make_test_path() { return Pointer<path_test>{InPlaceInit}; } } // namespace floormat::tests diff --git a/editor/tests/raycast-test.cpp b/editor/tests/raycast-test.cpp new file mode 100644 index 00000000..549760c3 --- /dev/null +++ b/editor/tests/raycast-test.cpp @@ -0,0 +1,98 @@ +#include "../tests-private.hpp" +#include <array> +#include <Magnum/Math/Functions.h> +#include <Magnum/Math/Vector2.h> + +namespace floormat::tests { + +namespace { + +struct aabb_result +{ + Vector2 ts; + float tmin, tmax; + bool result; +}; + +std::array<uint8_t, 2> ray_aabb_signs(Vector2 ray_dir_inv_norm) +{ + bool signs[2]; + for (unsigned d = 0; d < 2; ++d) + signs[d] = std::signbit(ray_dir_inv_norm[d]); + return { signs[0], signs[1] }; +} + +// https://tavianator.com/2022/ray_box_boundary.html +// +aabb_result ray_aabb_intersection(Vector2 ray_origin, Vector2 ray_dir_inv_norm, + std::array<Vector2, 2> box_minmax, std::array<uint8_t, 2> signs) +{ + using Math::min; + using Math::max; + + float ts[2]; + float tmin = 0, tmax = 16777216; + + for (unsigned d = 0; d < 2; ++d) + { + float bmin = box_minmax[signs[d]][d]; + float bmax = box_minmax[!signs[d]][d]; + + float dmin = (bmin - ray_origin[d]) * ray_dir_inv_norm[d]; + float dmax = (bmax - ray_origin[d]) * ray_dir_inv_norm[d]; + + ts[d] = dmin; + tmin = max(dmin, tmin); + tmax = min(dmax, tmax); + } + + return { {ts[0], ts[1] }, tmin, tmax, tmin < tmax }; +} + +} // namespace + +struct raycast_test : base_test +{ + ~raycast_test() noexcept override; + + bool handle_key(app& a, const key_event& e, bool is_down) override + { + return false; + } + + bool handle_mouse_click(app& a, const mouse_button_event& e, bool is_down) override + { + return false; + } + + bool handle_mouse_move(app& a, const mouse_move_event& e) override + { + return false; + } + + void draw_overlay(app& a) override + { + + } + + void draw_ui(app& a, float width) override + { + + } + + void update_pre(app& a) override + { + + } + + void update_post(app& a) override + { + + } +}; + +raycast_test::~raycast_test() noexcept = default; + +Pointer<base_test> tests_data::make_test_raycast() { return Pointer<raycast_test>{InPlaceInit}; } + +} // namespace floormat::tests |