diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-01 12:56:43 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-01 12:56:43 +0100 |
commit | 03f6789a7f5360290a11899b4961bc083d04092a (patch) | |
tree | 9a62f8031025c6458b9aca4aabb90d65d50effe3 /src | |
parent | b204d90b324fa00a1ed1c6cf2a0ba05c25e73407 (diff) |
w
Diffstat (limited to 'src')
-rw-r--r-- | src/object.cpp | 12 | ||||
-rw-r--r-- | src/object.hpp | 2 | ||||
-rw-r--r-- | src/timer-fwd.hpp | 28 | ||||
-rw-r--r-- | src/timer-ns.cpp | 53 | ||||
-rw-r--r-- | src/timer.cpp | 14 | ||||
-rw-r--r-- | src/timer.hpp | 44 |
6 files changed, 108 insertions, 45 deletions
diff --git a/src/object.cpp b/src/object.cpp index 6bab7186..0cadcada 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -273,14 +273,18 @@ 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::Type{Ns(1e9)}; + constexpr auto ns_in_sec = Ns(1e9); + constexpr auto u16_max = uint16_t{65535}; //const auto count = Ns::Type{ns_in_sec / hz} + accum}; - const auto ticks = Ns::Type(float(Ns::Type(dt)) * speed) + accum * ns_in_sec / Ns::Type{65535}; + const auto from_accum = accum * ns_in_sec / u16_max; + const auto from_dt = float(from_dt) * speed; + fm_assert(from_dt <= float{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 * Ns::Type{65535} / ns_in_sec; - const auto new_accum = (uint16_t)Math::clamp(new_accum_, Ns::Type{0}, Ns::Type{65535}); + const auto new_accum_ = rem * u16_max / ns_in_sec; + const auto new_accum = (uint16_t)Math::clamp(new_accum_, Ns{0}, u16_max); [[maybe_unused]] const auto old_accum = accum; accum = new_accum; #if 0 diff --git a/src/object.hpp b/src/object.hpp index 92ddb07f..f15699bb 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -6,7 +6,6 @@ #include "src/object-type.hpp" #include "src/object-id.hpp" #include "src/point.hpp" -#include "src/timer-fwd.hpp" #include <memory> namespace floormat { @@ -15,6 +14,7 @@ template<typename T> struct object_type_; class anim_atlas; class world; class chunk; +struct Ns; struct object_proto { diff --git a/src/timer-fwd.hpp b/src/timer-fwd.hpp deleted file mode 100644 index 9831319a..00000000 --- a/src/timer-fwd.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include <compare> - -namespace Magnum::Math { template<class T> class Nanoseconds; } - -namespace floormat { - -using Ns = Math::Nanoseconds<int64_t>; -//long double operator/(Ns a, Ns b) noexcept; - -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; - [[nodiscard]] Ns update(const Time& ts = now()) & noexcept; - - static float to_seconds(const Ns& ts) noexcept; - static float to_milliseconds(const Ns& ts) noexcept; - - uint64_t stamp = init(); - -private: - static uint64_t init() noexcept; -}; - -} // namespace floormat diff --git a/src/timer-ns.cpp b/src/timer-ns.cpp new file mode 100644 index 00000000..de60e230 --- /dev/null +++ b/src/timer-ns.cpp @@ -0,0 +1,53 @@ +#include "timer.hpp" +#include "compat/assert.hpp" + +namespace floormat { + +Ns operator+(const Ns& lhs, const Ns& rhs) +{ + constexpr auto max = (uint64_t)-1; + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(max - a < b); + return Ns{a + b}; +} + +Ns operator-(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(a >= b); + 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, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(b != 0); + return Ns{a % b}; +} + +bool operator==(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + return a == b; +} + +std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + return a <=> b; +} + +Ns::operator uint64_t() const { return stamp; } +Ns::operator float() const { return float(stamp); } +uint64_t Ns::operator*() const { return stamp; } +Ns::Ns() : stamp{0} {} +Ns::Ns(uint64_t x) : stamp{x} {} + +} // namespace floormat diff --git a/src/timer.cpp b/src/timer.cpp index 0b87ac06..f663aaf0 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -35,14 +35,6 @@ Time Time::now() noexcept return {ret}; } -Ns operator-(const Time& a, const Time& b) noexcept -{ - fm_assert(a.stamp >= b.stamp); - auto ret = a.stamp - b.stamp; - fm_assert(ret < uint64_t{1} << 63); - return Ns{ int64_t(ret) }; -} - Ns Time::update(const Time& ts) & noexcept { auto ret = ts - *this; @@ -54,8 +46,8 @@ uint64_t Time::init() noexcept { return get_time(); } bool Time::operator==(const Time&) const noexcept = default; std::strong_ordering Time::operator<=>(const Time&) const noexcept = default; -float Time::to_seconds(const Ns& ts) noexcept { return float(Ns::Type{ts} * 1e-9L); } -float Time::to_milliseconds(const Ns& ts) noexcept { return float(Ns::Type{ts} * 1e-6L); } +float Time::to_seconds(const Ns& ts) noexcept { return float{ts} * 1e-9f; } +float Time::to_milliseconds(const Ns& ts) noexcept { return float{ts} * 1e-6f; } const char* format_datetime_to_string(char (&buf)[fm_DATETIME_BUF_SIZE]) { @@ -73,4 +65,6 @@ const char* format_datetime_to_string(char (&buf)[fm_DATETIME_BUF_SIZE]) return buf; } + + } // namespace floormat diff --git a/src/timer.hpp b/src/timer.hpp index d9b0f602..24818722 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,9 +1,49 @@ #pragma once -#include "timer-fwd.hpp" -#include <mg/Time.h> // todo! replace with my own +#include <compare> namespace floormat { +struct Ns; + +struct Ns +{ + friend Ns operator+(const Ns& lhs, const Ns& rhs); + friend Ns operator-(const Ns& lhs, const Ns& rhs); + friend uint64_t operator/(const Ns& lhs, const Ns& rhs); + friend Ns operator%(const Ns& lhs, const Ns& rhs); + + friend bool operator==(const Ns& lhs, const Ns& rhs); + friend std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs); + + explicit operator uint64_t() const; + explicit operator float() const; + uint64_t operator*() const; + + uint64_t stamp; + static constexpr uint64_t Min = 0, Max = (uint64_t)-1; + static constexpr uint64_t Second = 1000000000, Millisecond = 1000000; + + explicit constexpr Ns(): stamp{0} {} + explicit constexpr Ns(uint64_t x) : stamp{x} {} +}; + +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; + [[nodiscard]] Ns update(const Time& ts = now()) & noexcept; + + static float to_seconds(const Ns& ts) noexcept; + static float to_milliseconds(const Ns& ts) noexcept; + + uint64_t stamp = init(); + +private: + static uint64_t init() noexcept; +}; + constexpr inline size_t fm_DATETIME_BUF_SIZE = 32; const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]); |