summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-03-01 18:08:50 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-03-01 18:08:50 +0100
commit310d7d2639ebc6ecba0e3367a401595e1e8f974b (patch)
tree68edc60fbc97957fe79da4ba9bb2f86d26a10184
parent257c2420545eb632a53c0fe3cd317be613dcb272 (diff)
src: optimize bbox replacement in update loop
-rw-r--r--editor/imgui-inspect.cpp1
-rw-r--r--editor/inspect-types.cpp1
-rw-r--r--editor/update.cpp5
-rw-r--r--src/chunk.hpp33
-rw-r--r--src/chunk.inl29
-rw-r--r--src/scenery.cpp11
-rw-r--r--src/scenery.hpp2
7 files changed, 42 insertions, 40 deletions
diff --git a/editor/imgui-inspect.cpp b/editor/imgui-inspect.cpp
index 529ef1a9..d4bf9da2 100644
--- a/editor/imgui-inspect.cpp
+++ b/editor/imgui-inspect.cpp
@@ -4,6 +4,7 @@
#include "floormat/main.hpp"
#include "src/world.hpp"
#include "imgui-raii.hpp"
+#include "chunk.inl"
namespace floormat {
diff --git a/editor/inspect-types.cpp b/editor/inspect-types.cpp
index 1168a88a..185cfad7 100644
--- a/editor/inspect-types.cpp
+++ b/editor/inspect-types.cpp
@@ -7,6 +7,7 @@
#include "inspect.hpp"
#include "loader/loader.hpp"
#include "chunk.hpp"
+#include "chunk.inl"
#include <Corrade/Containers/ArrayViewStl.h>
//#define TEST_STR
diff --git a/editor/update.cpp b/editor/update.cpp
index a907117a..62dcc977 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -5,6 +5,7 @@
#include "main/clickable.hpp"
#include "floormat/events.hpp"
#include "floormat/main.hpp"
+#include "chunk.inl"
namespace floormat {
@@ -164,8 +165,8 @@ void app::update_world(float dt)
for (std::int16_t y = miny; y <= maxy; y++)
for (std::int16_t x = minx; x <= maxx; x++)
for (auto& c = world[chunk_coords{x, y}]; auto [x, k, pt] : c)
- if (auto sc = x.scenery())
- c.with_scenery_bbox_update(sc.index(), [&] { sc.update(dt); });
+ if (auto sc = x.scenery(); sc && sc.can_activate())
+ c.with_scenery_bbox_update(sc.index(), [&] { return sc.update(dt); });
}
void app::set_cursor()
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 <concepts>
#include <type_traits>
#include <array>
#include <memory>
@@ -95,8 +94,7 @@ struct chunk final
const RTree* rtree() const noexcept;
RTree* rtree() noexcept;
- template<std::invocable<tile_ref&> F> void with_scenery_bbox_update(tile_ref t, F&& fun);
- template<std::invocable<> F> void with_scenery_bbox_update(std::size_t i, F&& fun);
+ template<typename F> void with_scenery_bbox_update(std::size_t idx, F&& fun);
private:
std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases;
@@ -132,33 +130,4 @@ private:
void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b);
};
-template<std::invocable<tile_ref&> 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<std::invocable<> 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<typename F> void chunk::with_scenery_bbox_update(std::size_t i, F&& fun)
+{
+ static_assert(std::is_invocable_v<F>);
+ static_assert(std::is_convertible_v<decltype(fun()), bool> || std::is_same_v<void, decltype(fun())>);
+ if (is_passability_modified())
+ fun();
+ else
+ {
+ bbox x0, x;
+ bool b0 = _bbox_for_scenery(i, x0);
+ if constexpr(std::is_same_v<void, std::decay_t<decltype(fun())>>)
+ {
+ 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 <algorithm>
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: