diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2023-02-11 17:53:05 +0100 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-02-11 17:59:32 +0100 |
| commit | fbb2582a66be0b98166e1f6ac21eb218aefd82e1 (patch) | |
| tree | e87a7be1b82d544e9692c0fc86dc4eebc8979dc3 | |
| parent | ada0f5e07d1a67d5067d44e48806a25db30c6135 (diff) | |
src/scenery, save: store dt as 16-bit fixed point
Bump save proto version.
| -rw-r--r-- | serialize/world-impl.hpp | 2 | ||||
| -rw-r--r-- | serialize/world-reader.cpp | 7 | ||||
| -rw-r--r-- | src/scenery.cpp | 9 | ||||
| -rw-r--r-- | src/scenery.hpp | 2 |
4 files changed, 13 insertions, 7 deletions
diff --git a/serialize/world-impl.hpp b/serialize/world-impl.hpp index e74ebda2..59c57e78 100644 --- a/serialize/world-impl.hpp +++ b/serialize/world-impl.hpp @@ -33,7 +33,7 @@ template<typename T> constexpr inline T int_max = std::numeric_limits<T>::max(); constexpr inline std::size_t atlas_name_max = 128; constexpr inline auto null_atlas = (atlasid)-1LL; -constexpr inline proto_t proto_version = 3; +constexpr inline proto_t proto_version = 4; constexpr inline proto_t min_proto_version = 1; constexpr inline auto chunk_magic = (std::uint16_t)~0xc0d3; constexpr inline auto scenery_magic = (std::uint16_t)~0xb00b; diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index f5459203..4d27c27f 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -173,7 +173,12 @@ void reader_state::read_chunks(reader_t& s) else sc.frame.frame << s; if (sc.frame.active) - sc.frame.delta << s; + { + if (PROTO >= 4) [[likely]] + sc.frame.delta << s; + else + sc.frame.delta = (std::uint16_t)Math::clamp(int(s.read<float>() * 65535), 0, 65535); + } } t.scenery() = sc; } diff --git a/src/scenery.cpp b/src/scenery.cpp index 89c04eec..848029da 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -71,10 +71,11 @@ void scenery::update(float dt, const anim_atlas& anim) const auto nframes = (int)anim.info().nframes; fm_debug_assert(anim.info().fps > 0 && anim.info().fps <= 0xff); - delta += dt; - const float frame_time = 1.f/hz; - const auto n = int(delta / frame_time); - delta -= frame_time * n; + auto delta_ = int(delta) + int(65535u * dt); + delta_ = std::min(65535, delta_); + const auto frame_time = int(1.f/hz * 65535); + const auto n = (std::uint8_t)std::clamp(delta_ / frame_time, 0, 255); + delta = (std::uint16_t)std::clamp(delta_ - frame_time*n, 0, 65535); fm_debug_assert(delta >= 0); const std::int8_t dir = closing ? 1 : -1; const int fr = frame + dir*n; diff --git a/src/scenery.hpp b/src/scenery.hpp index 44d2c6b5..7cdc1f4c 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -32,7 +32,7 @@ struct scenery final using frame_t = std::uint16_t; - float delta = 0; + std::uint16_t delta = 0; frame_t frame = 0; rotation r : 3 = rotation::N; scenery_type type : 3 = scenery_type::none; |
