From 558a7fa472a18cb48c5c898931c7b2c6fb988376 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 1 Mar 2024 11:35:35 +0100 Subject: src: add scaling factor to allocate_frame_time() --- src/critter.cpp | 3 ++- src/object.cpp | 12 ++++++------ src/object.hpp | 4 ++-- src/scenery.cpp | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/critter.cpp b/src/critter.cpp index fb3d7e52..491d8566 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -176,7 +176,7 @@ void critter::update_movement(size_t i, Ns dt, rotation new_r) fm_assert(new_r < rotation_COUNT); fm_assert(is_dynamic()); - auto nframes = allocate_frame_time(dt * speed); + auto nframes = allocate_frame_time(dt, speed); if (nframes == 0) { static unsigned foo; @@ -242,6 +242,7 @@ critter::critter(object_id id, class chunk& c, const critter_proto& proto) : if (!name) name = "(Unnamed)"_s; fm_soft_assert(atlas->check_rotation(r)); + fm_soft_assert(speed >= 0); object::set_bbox_(offset, bbox_offset, Vector2ub(iTILE_SIZE2/2), pass); } diff --git a/src/object.cpp b/src/object.cpp index 9aca4705..1b0aad58 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -269,16 +269,16 @@ void object::move_to(Magnum::Vector2i delta) move_to(i, delta, r); } -uint32_t object::allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz) +uint32_t object::allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz, float speed) { fm_assert(hz > 0); fm_assert(dt >= Ns{0}); constexpr auto ns_in_sec = Ns::Type{Ns(1e9)}; //const auto count = Ns::Type{ns_in_sec / hz} + accum}; - const auto nsecs = Ns::Type{dt} + accum * ns_in_sec / Ns::Type{65535}; + const auto ticks = Ns::Type(float(Ns::Type(dt)) * speed) + accum * ns_in_sec / Ns::Type{65535}; const auto frame_duration = ns_in_sec / hz; - const auto frames = (uint32_t)(nsecs / frame_duration); - const auto rem = nsecs % frame_duration; + 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}); [[maybe_unused]] const auto old_accum = accum; @@ -294,12 +294,12 @@ uint32_t object::allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz) return frames; } -uint32_t object::allocate_frame_time(Ns dt) +uint32_t object::allocate_frame_time(Ns dt, float speed) { fm_assert(atlas); auto hz = atlas->info().fps; fm_assert(hz > 0); - return allocate_frame_time(dt, delta, hz); + return allocate_frame_time(dt, delta, hz, speed); } void object::set_bbox_(Vector2b offset_, Vector2b bb_offset_, Vector2ub bb_size_, pass_mode pass_) diff --git a/src/object.hpp b/src/object.hpp index e462c8db..92ddb07f 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -85,8 +85,8 @@ struct object void move_to(size_t& i, Vector2i delta, rotation new_r); void move_to(Vector2i delta); - static uint32_t allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz); - uint32_t allocate_frame_time(Ns dt); + static uint32_t allocate_frame_time(Ns dt, uint16_t& accum, uint32_t hz, float speed); + uint32_t allocate_frame_time(Ns dt, float speed); protected: object(object_id id, class chunk& c, const object_proto& proto); diff --git a/src/scenery.cpp b/src/scenery.cpp index b5fd3b59..937fc922 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -90,7 +90,7 @@ void door_scenery::update(scenery& s, size_t, Ns dt) auto& anim = *s.atlas; const auto nframes = (int)anim.info().nframes; fm_debug_assert(anim.info().fps > 0 && anim.info().fps <= 0xff); - const auto n = (int)s.allocate_frame_time(dt); + const auto n = (int)s.allocate_frame_time(dt, 1); if (n == 0) return; const int8_t dir = closing ? 1 : -1; -- cgit v1.2.3