From 310d7d2639ebc6ecba0e3367a401595e1e8f974b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 1 Mar 2023 18:08:50 +0100 Subject: src: optimize bbox replacement in update loop --- src/chunk.hpp | 33 +-------------------------------- src/chunk.inl | 29 +++++++++++++++++++++++++++++ src/scenery.cpp | 11 ++++++----- src/scenery.hpp | 2 +- 4 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 src/chunk.inl (limited to 'src') diff --git a/src/chunk.hpp b/src/chunk.hpp index 54f59e2d..3ec02130 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -2,7 +2,6 @@ #include "tile.hpp" #include "tile-iterator.hpp" #include "scenery.hpp" -#include #include #include #include @@ -95,8 +94,7 @@ struct chunk final const RTree* rtree() const noexcept; RTree* rtree() noexcept; - template F> void with_scenery_bbox_update(tile_ref t, F&& fun); - template F> void with_scenery_bbox_update(std::size_t i, F&& fun); + template void with_scenery_bbox_update(std::size_t idx, F&& fun); private: std::array, TILE_COUNT> _ground_atlases; @@ -132,33 +130,4 @@ private: void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b); }; -template F> -void chunk::with_scenery_bbox_update(tile_ref t, F&& fun) -{ - if (is_passability_modified()) - return fun(t); - else - { - bbox x0, x; - std::size_t i = t.index(); - bool b0 = _bbox_for_scenery(i, x0); - fun(t); - _replace_bbox(x0, x, b0, _bbox_for_scenery(i, x)); - } -} - -template F> -void chunk::with_scenery_bbox_update(std::size_t i, F&& fun) -{ - if (is_passability_modified()) - return fun(); - else - { - bbox x0, x; - bool b0 = _bbox_for_scenery(i, x0); - fun(); - _replace_bbox(x0, x, b0, _bbox_for_scenery(i, x)); - } -} - } // namespace floormat diff --git a/src/chunk.inl b/src/chunk.inl new file mode 100644 index 00000000..5abc1e12 --- /dev/null +++ b/src/chunk.inl @@ -0,0 +1,29 @@ +#pragma once +#include "chunk.hpp" + +namespace floormat { + +template void chunk::with_scenery_bbox_update(std::size_t i, F&& fun) +{ + static_assert(std::is_invocable_v); + static_assert(std::is_convertible_v || std::is_same_v); + if (is_passability_modified()) + fun(); + else + { + bbox x0, x; + bool b0 = _bbox_for_scenery(i, x0); + if constexpr(std::is_same_v>) + { + fun(); + _replace_bbox(x0, x, b0, _bbox_for_scenery(i, x)); + } + else + { + if (fun()) + _replace_bbox(x0, x, b0, _bbox_for_scenery(i, x)); + } + } +} + +} // namespace floormat diff --git a/src/scenery.cpp b/src/scenery.cpp index f21f95c2..17f2066c 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -3,6 +3,7 @@ #include "chunk.hpp" #include "compat/assert.hpp" #include "rotation.inl" +#include "chunk.inl" #include namespace floormat { @@ -66,7 +67,7 @@ scenery::scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open, void scenery_ref::rotate(rotation new_r) { - c->with_scenery_bbox_update(idx, [&] { + c->with_scenery_bbox_update(idx, [&]() { auto& s = frame; s.bbox_offset = rotate_point(s.bbox_offset, s.r, new_r); s.bbox_size = rotate_size(s.bbox_size, s.r, new_r); @@ -79,18 +80,18 @@ bool scenery_ref::can_activate() const noexcept return frame.interactive; } -void scenery_ref::update(float dt) +bool scenery_ref::update(float dt) { auto& s = frame; if (!s.active) - return; + return false; switch (s.type) { default: case scenery_type::none: case scenery_type::generic: - break; + return false; case scenery_type::door: fm_assert(atlas); auto& anim = *atlas; @@ -116,7 +117,7 @@ void scenery_ref::update(float dt) s.frame = (scenery::frame_t)std::clamp(fr, 0, nframes-1); if (!s.active) s.delta = s.closing = 0; - break; + return true; } } diff --git a/src/scenery.hpp b/src/scenery.hpp index 3a8fb241..1db631fb 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -98,7 +98,7 @@ struct scenery_ref final { bool can_activate() const noexcept; bool activate(); - void update(float dt); + bool update(float dt); void rotate(rotation r); private: -- cgit v1.2.3