diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/critter.cpp | 2 | ||||
-rw-r--r-- | src/light.cpp | 2 | ||||
-rw-r--r-- | src/nanosecond.hpp | 44 | ||||
-rw-r--r-- | src/nanosecond.inl | 118 | ||||
-rw-r--r-- | src/object.cpp | 1 | ||||
-rw-r--r-- | src/point.inl | 2 | ||||
-rw-r--r-- | src/scenery.cpp | 13 | ||||
-rw-r--r-- | src/search-astar.cpp | 1 | ||||
-rw-r--r-- | src/timer-ns.cpp | 4 | ||||
-rw-r--r-- | src/timer.cpp | 1 | ||||
-rw-r--r-- | src/timer.hpp | 183 |
11 files changed, 176 insertions, 195 deletions
diff --git a/src/critter.cpp b/src/critter.cpp index d54882e6..4ec00059 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -4,7 +4,7 @@ #include "loader/loader.hpp" #include "src/world.hpp" #include "src/object.hpp" -#include "src/timer.hpp" +#include "src/nanosecond.hpp" #include "shaders/shader.hpp" #include "compat/exception.hpp" #include <cmath> diff --git a/src/light.cpp b/src/light.cpp index ead434a2..333fb327 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -1,5 +1,5 @@ #include "light.hpp" -#include "timer.hpp" +#include "nanosecond.hpp" #include "tile-constants.hpp" #include "shaders/shader.hpp" #include "loader/loader.hpp" diff --git a/src/nanosecond.hpp b/src/nanosecond.hpp new file mode 100644 index 00000000..94c30ba3 --- /dev/null +++ b/src/nanosecond.hpp @@ -0,0 +1,44 @@ +#pragma once +#include <compare> + +namespace floormat { + +struct Ns +{ + explicit constexpr Ns(): stamp{0} {} + + template<typename T> requires (std::is_integral_v<T> && std::is_unsigned_v<T>) explicit constexpr Ns(T x) : stamp{x} {} + template<typename T> requires (std::is_integral_v<T> && !std::is_unsigned_v<T>) explicit constexpr Ns(T x) : stamp{uint64_t(x)} { fm_assert(x >= T{0}); } + + explicit constexpr operator uint64_t() const { return stamp; } + explicit constexpr operator double() const = delete; + explicit constexpr operator float() const = delete; + friend Ns operator*(const Ns&, const Ns&) = delete; + + friend constexpr Ns operator+(const Ns& lhs, const Ns& rhs); + friend constexpr Ns operator-(const Ns& lhs, const Ns& rhs); + template<typename T> requires (std::is_same_v<T, double>) friend Ns operator*(const Ns& lhs, T b); + template<typename T> requires (std::is_integral_v<T> && std::is_unsigned_v<T>) friend constexpr Ns operator*(const Ns& lhs, T rhs); + template<typename T> requires (std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) < sizeof(uint64_t)) friend constexpr Ns operator*(const Ns&, T); + template<typename T> friend constexpr Ns operator*(T lhs, const Ns& rhs); + template<typename T> requires std::is_same_v<float, T> friend constexpr Ns operator*(const Ns& lhs, T rhs); + + friend constexpr uint64_t operator/(const Ns& lhs, const Ns& rhs); + friend constexpr Ns operator/(const Ns& lhs, uint64_t b); + friend constexpr uint64_t operator%(const Ns& lhs, const Ns& rhs); + + friend constexpr Ns operator%(const Ns& lhs, uint64_t b); + friend constexpr Ns& operator+=(Ns& lhs, const Ns& rhs); + + friend constexpr bool operator==(const Ns& lhs, const Ns& rhs) = default; + friend constexpr std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs); + + friend Debug& operator<<(Debug& dbg, const Ns& box); + + uint64_t stamp; +}; + +constexpr inline Ns Minute{60000000000}, Second{1000000000}, Millisecond{1000000}, Microsecond{1000}; +constexpr inline const Ns& Minutes{Minute}, Seconds{Second}, Milliseconds{Millisecond}, Microseconds{Microsecond}; + +} // namespace floormat diff --git a/src/nanosecond.inl b/src/nanosecond.inl new file mode 100644 index 00000000..6527dc02 --- /dev/null +++ b/src/nanosecond.inl @@ -0,0 +1,118 @@ +#pragma once +#include "compat/assert.hpp" +#include "nanosecond.hpp" + +namespace floormat { + +template<typename T> +requires (std::is_same_v<T, double>) +Ns operator*(const Ns& lhs, T b) +{ + constexpr double max{uint64_t{1} << 53}; + auto a = lhs.stamp; + fm_assert(b >= 0); + fm_assert(b <= max); + auto x = double(a) * b; + fm_assert(x >= 0); + fm_assert(x <= max); + return Ns{(uint64_t)x}; +} + +constexpr 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}; +} + +constexpr Ns operator-(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(a >= b); + return Ns{a - b}; +} + +template<typename T> +requires (std::is_integral_v<T> && std::is_unsigned_v<T>) +constexpr Ns operator*(const Ns& lhs, T rhs) +{ + auto a = lhs.stamp, b = uint64_t{rhs}; + auto x = a * b; + fm_assert(b == 0 || x / b == a); + return Ns{x}; +} + +template<typename T> +requires (std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) < sizeof(uint64_t)) +constexpr Ns operator*(const Ns& lhs, T rhs) +{ + fm_assert(rhs >= T{0}); + auto b = uint64_t(rhs); + auto x = lhs.stamp * b; + fm_assert(b == 0 || x / b == lhs.stamp); + return Ns{x}; +} + +template<typename T> +constexpr Ns operator*(T lhs, const Ns& rhs) +{ + return rhs * lhs; +} + +template<typename T> +requires std::is_same_v<float, T> +constexpr Ns operator*(const Ns& lhs, T rhs) +{ + constexpr float max{uint64_t{1} << 24}; + auto a = lhs.stamp; + auto x = float(a) * float{rhs}; + fm_assert(x >= 0); + fm_assert(x <= max); + return Ns{uint64_t(x)}; +} + +constexpr uint64_t operator/(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(b != 0); + return a / b; +} + +constexpr Ns operator/(const Ns& lhs, uint64_t b) +{ + auto a = lhs.stamp; + fm_assert(b != 0); + return Ns{a / b}; +} + +constexpr uint64_t operator%(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + fm_assert(b != 0); + return a % b; +} + +constexpr Ns operator%(const Ns& lhs, uint64_t b) +{ + auto a = lhs.stamp; + fm_assert(b != 0); + return Ns{a % b}; +} + +constexpr Ns& operator+=(Ns& lhs, const Ns& rhs) +{ + constexpr auto max = (uint64_t)-1; + auto b = rhs.stamp; + fm_assert(max - lhs.stamp >= b); + lhs.stamp += b; + return lhs; +} + +constexpr std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs) +{ + auto a = lhs.stamp, b = rhs.stamp; + return a <=> b; +} + +} // namespace floormat diff --git a/src/object.cpp b/src/object.cpp index 0b54a159..5fd29d29 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -8,6 +8,7 @@ #include "compat/debug.hpp" #include "compat/exception.hpp" #include "compat/limits.hpp" +#include "nanosecond.inl" #include <cmath> #include <algorithm> #include <Corrade/Containers/GrowableArray.h> diff --git a/src/point.inl b/src/point.inl index 4fd4bb07..62abbde1 100644 --- a/src/point.inl +++ b/src/point.inl @@ -1,6 +1,6 @@ #pragma once -#include "src/tile-constants.hpp" #include "point.hpp" +#include "src/tile-constants.hpp" #include <mg/Functions.h> namespace floormat { diff --git a/src/scenery.cpp b/src/scenery.cpp index aa558f52..fc018107 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -1,14 +1,13 @@ #include "scenery.hpp" +#include "compat/assert.hpp" +#include "compat/exception.hpp" #include "tile-constants.hpp" #include "anim-atlas.hpp" -#include "chunk.hpp" -#include "compat/assert.hpp" +#include "rotation.inl" +#include "nanosecond.hpp" #include "world.hpp" #include "shaders/shader.hpp" -#include "src/rotation.inl" -#include "compat/exception.hpp" -#include "src/timer.hpp" -#include <algorithm> +#include <mg/Functions.h> namespace floormat { @@ -103,7 +102,7 @@ void door_scenery::update(scenery& s, size_t, Ns dt) else p = pass_mode::see_through; s.set_bbox(s.offset, s.bbox_offset, s.bbox_size, p); - const auto new_frame = (uint16_t)std::clamp(fr, 0, nframes-1); + const auto new_frame = (uint16_t)Math::clamp(fr, 0, nframes-1); //Debug{} << "frame" << new_frame << nframes-1; s.frame = new_frame; if (!active) diff --git a/src/search-astar.cpp b/src/search-astar.cpp index 34c2aad2..b296a61e 100644 --- a/src/search-astar.cpp +++ b/src/search-astar.cpp @@ -8,7 +8,6 @@ #include "compat/heap.hpp" #include "object.hpp" #include "world.hpp" -#include "point.hpp" #include "point.inl" #include <cstdio> #include <Corrade/Containers/GrowableArray.h> diff --git a/src/timer-ns.cpp b/src/timer-ns.cpp index 1f927f14..575a0c76 100644 --- a/src/timer-ns.cpp +++ b/src/timer-ns.cpp @@ -1,4 +1,4 @@ -#include "timer.hpp" +#include "nanosecond.inl" #include "compat/assert.hpp" #include "compat/debug.hpp" #include <cr/Debug.h> @@ -6,8 +6,6 @@ namespace floormat { - - Debug& operator<<(Debug& dbg, const Ns& val) { const char* unit; diff --git a/src/timer.cpp b/src/timer.cpp index b068c55d..57442018 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,5 +1,6 @@ #include "timer.hpp" #include "compat/assert.hpp" +#include "nanosecond.hpp" #include <ctime> #include <cstdio> #include <chrono> diff --git a/src/timer.hpp b/src/timer.hpp index 36874e9e..67556c86 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,188 +1,9 @@ #pragma once -#include "compat/assert.hpp" #include <compare> namespace floormat { -// todo! move to .inl - -struct Ns -{ - explicit constexpr Ns(): stamp{0} {} - - - template<typename T> - requires (std::is_integral_v<T> && std::is_unsigned_v<T>) - explicit constexpr Ns(T x) : stamp{x} {} - - template<typename T> - requires (std::is_integral_v<T> && !std::is_unsigned_v<T>) - explicit constexpr Ns(T x) : stamp{uint64_t(x)} { fm_assert(x >= T{0}); } - -#if 0 - template<typename T> - requires std::is_same_v<T, float> - explicit constexpr Ns(T x) : stamp{} - { - constexpr float max{uint64_t{1} << 24}; - fm_assert(x >= 0); - fm_assert(x <= max); - stamp = uint64_t(x); - } - - template<typename T> - requires std::is_same_v<T, double> - explicit constexpr Ns(T x) : stamp{} - { - constexpr double max{uint64_t{1} << 53}; - fm_assert(x >= 0); - fm_assert(x <= max); - stamp = uint64_t(x); - } -#endif - - explicit constexpr operator uint64_t() const { return stamp; } - - explicit constexpr operator double() const = delete; - explicit constexpr operator float() const = delete; - - template<typename T> - requires (std::is_same_v<T, double>) - friend Ns operator*(const Ns& lhs, T b) - { - constexpr double max{uint64_t{1} << 53}; - auto a = lhs.stamp; - fm_assert(b >= 0); - fm_assert(b <= max); - auto x = double(a) * b; - fm_assert(x <= max); - fm_assert(x >= 0); - return Ns{(uint64_t)x}; - } - - friend constexpr 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}; - } - - friend constexpr Ns operator-(const Ns& lhs, const Ns& rhs) - { - auto a = lhs.stamp, b = rhs.stamp; - fm_assert(a >= b); - return Ns{a - b}; - } - - friend Ns operator*(const Ns&, const Ns&) = delete; - - template<typename T> - requires (std::is_integral_v<T> && std::is_unsigned_v<T>) - friend constexpr Ns operator*(const Ns& lhs, T rhs) - { - auto a = lhs.stamp, b = uint64_t{rhs}; - auto x = a * b; - fm_assert(b == 0 || x / b == a); - return Ns{x}; - } - - template<typename T> - requires (std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) < sizeof(uint64_t)) - friend constexpr Ns operator*(const Ns& lhs, T rhs) - { - fm_assert(rhs >= T{0}); - auto b = uint64_t(rhs); - auto x = lhs.stamp * b; - fm_assert(b == 0 || x / b == lhs.stamp); - return Ns{x}; - } - - template<typename T> - friend constexpr Ns operator*(T lhs, const Ns& rhs) { return rhs * lhs; } - - template<typename T> - requires std::is_same_v<float, T> - friend constexpr Ns operator*(const Ns& lhs, T rhs) - { - constexpr float max{uint64_t{1} << 24}; - auto a = lhs.stamp; - auto x = float(a) * float{rhs}; - fm_assert(x >= 0); - fm_assert(x <= max); - return Ns{uint64_t(x)}; - } - -#if 0 - template<typename T> requires (!std::is_same_v<Ns, T>) - friend constexpr Ns operator*(const T& lhs, const Ns& rhs) { return rhs * lhs; } -#endif - - friend constexpr uint64_t operator/(const Ns& lhs, const Ns& rhs) - { - auto a = lhs.stamp, b = rhs.stamp; - fm_assert(b != 0); - return a / b; - } - - friend constexpr Ns operator/(const Ns& lhs, uint64_t b) - { - auto a = lhs.stamp; - fm_assert(b != 0); - return Ns{a / b}; - } - -#if 0 - template<typename T> - requires std::is_floating_point_v<T> - friend constexpr double operator/(const Ns& lhs, double b) - { - fm_assert(b != 0.); - auto x = double{lhs.stamp / b}; - constexpr auto max = double{uint64_t{1} << 53}; - fm_assert(x <= max); - return x; - } -#endif - - friend constexpr uint64_t operator%(const Ns& lhs, const Ns& rhs) - { - auto a = lhs.stamp, b = rhs.stamp; - fm_assert(b != 0); - return a % b; - } - - friend constexpr Ns operator%(const Ns& lhs, uint64_t b) - { - auto a = lhs.stamp; - fm_assert(b != 0); - return Ns{a % b}; - } - - friend constexpr Ns& operator+=(Ns& lhs, const Ns& rhs) - { - constexpr auto max = (uint64_t)-1; - auto b = rhs.stamp; - fm_assert(max - lhs.stamp >= b); - lhs.stamp += b; - return lhs; - } - - friend constexpr bool operator==(const Ns& lhs, const Ns& rhs) = default; - - friend constexpr std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs) - { - auto a = lhs.stamp, b = rhs.stamp; - return a <=> b; - } - - friend Debug& operator<<(Debug& dbg, const Ns& box); - - uint64_t stamp; -}; - -constexpr inline Ns Minute{60000000000}, Second{1000000000}, Millisecond{1000000}, Microsecond{1000}; -constexpr inline const Ns& Minutes{Minute}, Seconds{Second}, Milliseconds{Millisecond}, Microseconds{Microsecond}; +struct Ns; struct Time final { @@ -201,7 +22,7 @@ private: static uint64_t init() noexcept; }; -constexpr inline size_t fm_DATETIME_BUF_SIZE = 32; +#define fm_DATETIME_BUF_SIZE 32 const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]); } // namespace floormat |