summaryrefslogtreecommitdiffhomepage
path: root/src
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 /src
parent9a0399ae67ad378d2f746114dd297230104cf018 (diff)
w
Diffstat (limited to 'src')
-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
5 files changed, 41 insertions, 11 deletions
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]);