summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-03-01 17:34:04 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-03-01 17:34:04 +0100
commit10e22ed4fb0bd21d9d268c5c6f1ac1c7d9e4b621 (patch)
tree56e4c085cb9d29f4d2db19e208d57e983a1aa318
parent9a0399ae67ad378d2f746114dd297230104cf018 (diff)
w
-rw-r--r--editor/app.cpp2
-rw-r--r--floormat/app.hpp3
-rw-r--r--main/main-impl.hpp1
-rw-r--r--src/critter.cpp3
-rw-r--r--src/object.cpp10
-rw-r--r--src/timer-ns.cpp25
-rw-r--r--src/timer.cpp7
-rw-r--r--src/timer.hpp7
-rw-r--r--test/serializer.cpp2
9 files changed, 44 insertions, 16 deletions
diff --git a/editor/app.cpp b/editor/app.cpp
index bda4dc6c..ca4dd853 100644
--- a/editor/app.cpp
+++ b/editor/app.cpp
@@ -5,7 +5,7 @@
#include "editor.hpp"
#include "src/anim-atlas.hpp"
#include "src/critter.hpp"
-#include "src/timer-fwd.hpp"
+#include "src/timer.hpp"
#include "src/world.hpp"
#include "floormat/main.hpp"
#include "floormat/settings.hpp"
diff --git a/floormat/app.hpp b/floormat/app.hpp
index 01f94b7f..e387c0c4 100644
--- a/floormat/app.hpp
+++ b/floormat/app.hpp
@@ -4,8 +4,6 @@ namespace Magnum::Math { template<typename T> class Vector2; template<class T> c
namespace floormat {
-using Ns = Math::Nanoseconds<int64_t>;
-
struct mouse_move_event;
struct mouse_button_event;
struct mouse_scroll_event;
@@ -17,6 +15,7 @@ union any_event;
struct chunk_coords;
struct chunk_coords_;
class chunk;
+struct Ns;
struct floormat_app
{
diff --git a/main/main-impl.hpp b/main/main-impl.hpp
index c056dfa9..e9d996bf 100644
--- a/main/main-impl.hpp
+++ b/main/main-impl.hpp
@@ -2,7 +2,6 @@
#include "floormat/main.hpp"
#include "floormat/settings.hpp"
#include "src/world.hpp"
-#include "src/timer-fwd.hpp"
#include "src/timer.hpp"
#include "draw/ground.hpp"
#include "draw/wall.hpp"
diff --git a/src/critter.cpp b/src/critter.cpp
index 491d8566..03281ef6 100644
--- a/src/critter.cpp
+++ b/src/critter.cpp
@@ -179,14 +179,13 @@ void critter::update_movement(size_t i, Ns dt, rotation new_r)
auto nframes = allocate_frame_time(dt, speed);
if (nframes == 0)
{
- static unsigned foo;
+ //static unsigned foo;
//Debug{} << ++foo << "stopped";
return;
}
const auto rotations = rotation_to_similar(new_r);
const unsigned nvecs = (int)new_r & 1 ? 3 : 1;
-
if (r != new_r)
//if (is_dynamic())
rotate(i, new_r);
diff --git a/src/object.cpp b/src/object.cpp
index e834525f..e440ab7a 100644
--- a/src/object.cpp
+++ b/src/object.cpp
@@ -274,17 +274,17 @@ uint32_t object::allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz, float
fm_assert(hz > 0);
fm_assert(dt >= Ns{0});
constexpr auto ns_in_sec = Ns(1e9);
- constexpr auto u16_max = uint16_t{65535};
+ constexpr auto u16_max = uint64_t{65535};
//const auto count = Ns::Type{ns_in_sec / hz} + accum};
const auto from_accum = uint64_t{accum} * ns_in_sec / u16_max;
- const auto from_dt = float{dt} * speed;
- fm_assert(from_dt <= float{1 << 24});
+ const auto from_dt = Ns(uint64_t(float{dt} * speed));
+ fm_assert(from_dt <= Ns{1 << 24});
const auto ticks = from_dt + from_accum;
const auto frame_duration = ns_in_sec / hz;
const auto frames = (uint32_t)(ticks / frame_duration);
const auto rem = (uint32_t)(ticks % frame_duration);
- const auto new_accum_ = rem * u16_max / ns_in_sec;
- const auto new_accum = (uint16_t)Math::clamp(new_accum_, Ns{0}, u16_max);
+ const auto new_accum_ = rem * u16_max / uint64_t{ns_in_sec};
+ const auto new_accum = (uint16_t)Math::clamp(new_accum_, uint64_t{0}, u16_max);
[[maybe_unused]] const auto old_accum = accum;
accum = new_accum;
#if 0
diff --git a/src/timer-ns.cpp b/src/timer-ns.cpp
index a940e1f5..7a2fe087 100644
--- a/src/timer-ns.cpp
+++ b/src/timer-ns.cpp
@@ -1,11 +1,12 @@
#include "timer.hpp"
#include "compat/assert.hpp"
+#include <cr/Debug.h>
namespace floormat {
namespace {
-#if 1
+#if 0
constexpr auto MAX = (uint64_t)-1, HALF = MAX/2;
static_assert(MAX - (MAX-0) <= 0);
@@ -44,7 +45,13 @@ Ns operator*(const Ns& lhs, uint64_t b)
auto x = a * b;
//fm_assert(!(a != 0 && x / a != b));
fm_assert(a == 0 || x / a == b);
- return Ns{a * b};
+ return Ns{x};
+}
+
+Ns operator*(uint64_t a, const Ns& rhs)
+{
+ auto b = rhs.stamp;
+ return Ns{a} * b;
}
uint64_t operator/(const Ns& lhs, const Ns& rhs)
@@ -54,6 +61,20 @@ uint64_t operator/(const Ns& lhs, const Ns& rhs)
return a / b;
}
+Ns operator/(const Ns& lhs, uint64_t b)
+{
+ auto a = lhs.stamp;
+ fm_assert(b != 0);
+ return Ns{a / b};
+}
+
+uint64_t operator%(const Ns& lhs, const Ns& rhs)
+{
+ auto a = lhs.stamp, b = rhs.stamp;
+ fm_assert(b != 0);
+ return a % b;
+}
+
Ns operator%(const Ns& lhs, uint64_t b)
{
auto a = lhs.stamp;
diff --git a/src/timer.cpp b/src/timer.cpp
index f663aaf0..8e9ed917 100644
--- a/src/timer.cpp
+++ b/src/timer.cpp
@@ -65,6 +65,11 @@ const char* format_datetime_to_string(char (&buf)[fm_DATETIME_BUF_SIZE])
return buf;
}
-
+Ns operator-(const Time& lhs, const Time& rhs) noexcept
+{
+ auto a = lhs.stamp, b = rhs.stamp;
+ fm_assert(a >= b);
+ return Ns{a - b};
+}
} // namespace floormat
diff --git a/src/timer.hpp b/src/timer.hpp
index 6972e372..43637b36 100644
--- a/src/timer.hpp
+++ b/src/timer.hpp
@@ -20,10 +20,13 @@ struct Ns
friend Ns operator+(const Ns& lhs, const Ns& rhs);
friend Ns operator-(const Ns& lhs, const Ns& rhs);
friend Ns operator*(const Ns& lhs, uint64_t rhs);
+ friend Ns operator*(uint64_t lhs, const Ns& rhs);
+
friend uint64_t operator/(const Ns& lhs, const Ns& rhs);
friend Ns operator/(const Ns& lhs, uint64_t rhs);
friend uint64_t operator%(const Ns& lhs, const Ns& rhs);
friend Ns operator%(const Ns& lhs, uint64_t rhs);
+
friend bool operator==(const Ns& lhs, const Ns& rhs);
friend std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs);
};
@@ -33,7 +36,7 @@ struct Time final
static Time now() noexcept;
bool operator==(const Time&) const noexcept;
std::strong_ordering operator<=>(const Time&) const noexcept;
- friend Ns operator-(const Time& a, const Time& b) noexcept;
+ friend Ns operator-(const Time& lhs, const Time& rhs) noexcept;
[[nodiscard]] Ns update(const Time& ts = now()) & noexcept;
static float to_seconds(const Ns& ts) noexcept;
@@ -45,6 +48,8 @@ private:
static uint64_t init() noexcept;
};
+Debug& operator<<(Debug& dbg, Ns box);
+
constexpr inline size_t fm_DATETIME_BUF_SIZE = 32;
const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]);
diff --git a/test/serializer.cpp b/test/serializer.cpp
index cc9405ef..61a5d509 100644
--- a/test/serializer.cpp
+++ b/test/serializer.cpp
@@ -54,7 +54,7 @@ chunk& test_app::make_test_chunk(world& w, chunk_coords_ ch)
const auto index = e.index();
const auto end = e.atlas->info().nframes-1;
constexpr auto second = Ns(1e9);
- constexpr auto dt = second / 60;
+ constexpr auto dt = Ns{second.stamp / 60};
fm_assert(e.frame == end);
{ auto& x = std::get<door_scenery>(e.subtype);
fm_assert(!x.active);