diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-05 21:04:20 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-05 21:56:56 +0100 |
commit | 71d8fa55ab671ae152c420738b2d9d49f8f1106d (patch) | |
tree | a4ad0eb37840245622b99ee49c1d555ccb238b02 | |
parent | a56a8cc899dc90701f1690051da4e4318e95566c (diff) |
clean up timer & nanosecond includes
-rw-r--r-- | editor/app.cpp | 1 | ||||
-rw-r--r-- | editor/app.hpp | 2 | ||||
-rw-r--r-- | editor/camera.cpp | 2 | ||||
-rw-r--r-- | editor/draw.cpp | 1 | ||||
-rw-r--r-- | editor/update.cpp | 1 | ||||
-rw-r--r-- | main/draw.cpp | 2 | ||||
-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 | ||||
-rw-r--r-- | test/critter.cpp | 3 | ||||
-rw-r--r-- | test/serializer.cpp | 4 |
19 files changed, 183 insertions, 204 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index dafea3d9..35aa1230 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -5,7 +5,6 @@ #include "editor.hpp" #include "src/anim-atlas.hpp" #include "src/critter.hpp" -#include "src/timer.hpp" #include "src/world.hpp" #include "floormat/main.hpp" #include "floormat/settings.hpp" diff --git a/editor/app.hpp b/editor/app.hpp index 4cc24259..a60d12bf 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -165,7 +165,7 @@ private: void clear_non_global_keys(); void clear_non_repeated_keys(); - void do_camera(Ns dt, const key_set& cmds, int mods); + void do_camera(const Ns& dt, const key_set& cmds, int mods); void reset_camera_offset(); [[nodiscard]] bool tests_handle_key(const key_event& e, bool is_down); diff --git a/editor/camera.cpp b/editor/camera.cpp index e71be739..ce94d5e1 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -14,7 +14,7 @@ namespace floormat { -void app::do_camera(Ns dt, const key_set& cmds, int mods) +void app::do_camera(const Ns& dt, const key_set& cmds, int mods) { if (cmds[key_camera_reset]) { diff --git a/editor/draw.cpp b/editor/draw.cpp index bd30d80b..40ba0235 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -15,7 +15,6 @@ #include "src/camera-offset.hpp" #include "src/world.hpp" #include "src/critter.hpp" -#include "src/rotation.inl" #include "src/RTree-search.hpp" #include <bit> #include <Magnum/Math/Color.h> diff --git a/editor/update.cpp b/editor/update.cpp index beb7c048..45772be2 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -12,6 +12,7 @@ #include "floormat/main.hpp" #include "src/critter.hpp" #include "src/tile-iterator.hpp" +#include "src/nanosecond.hpp" #include "src/timer.hpp" #include "keys.hpp" #include "loader/loader.hpp" diff --git a/main/draw.cpp b/main/draw.cpp index df5521b0..b8063824 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -4,7 +4,7 @@ #include "src/camera-offset.hpp" #include "src/anim-atlas.hpp" #include "main/clickable.hpp" -#include "src/timer.hpp" +#include "src/nanosecond.hpp" #include <Corrade/Containers/GrowableArray.h> #include <Corrade/Containers/ArrayView.h> #include <Magnum/GL/DefaultFramebuffer.h> 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 diff --git a/test/critter.cpp b/test/critter.cpp index 939ae811..e75be7d9 100644 --- a/test/critter.cpp +++ b/test/critter.cpp @@ -5,13 +5,12 @@ #include "src/critter.hpp" #include "src/world.hpp" #include "src/wall-atlas.hpp" -#include "src/timer.hpp" +#include "src/nanosecond.inl" #include "src/log.hpp" #include "src/point.inl" #include "loader/loader.hpp" #include <cinttypes> #include <cstdio> -// todo! find all places where singed division is used namespace floormat { diff --git a/test/serializer.cpp b/test/serializer.cpp index e85f7892..5179342c 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -7,7 +7,7 @@ #include "src/ground-atlas.hpp" #include "src/anim-atlas.hpp" #include "src/tile-iterator.hpp" -#include "src/timer.hpp" +#include "src/nanosecond.inl" #include <Corrade/Utility/Path.h> namespace floormat { @@ -53,7 +53,7 @@ chunk& test_app::make_test_chunk(world& w, chunk_coords_ ch) auto& e = *w.make_object<scenery>(w.make_id(), {ch, {K+3, K+1}}, door); const auto index = e.index(); const auto end = e.atlas->info().nframes-1; - constexpr Ns dt = Second / 60; + constexpr auto dt = Second / 60; fm_assert(e.frame == end); { auto& x = std::get<door_scenery>(e.subtype); fm_assert(!x.active); |