Skip to content

Commit

Permalink
🧼 [TimeSpeed] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Mar 12, 2024
1 parent 15052f5 commit 8a8f517
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Cool/Exporter/VideoExportProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ VideoExportProcess::VideoExportProcess(VideoExportParams const& params, TimeSpee
, _total_nb_of_frames_in_sequence{static_cast<int>(std::ceil((params.end - params.beginning) * params.fps))}
, _frame_numbering_offset{static_cast<int>(std::ceil(params.beginning * params.fps))} // Makes sure than if we export frames from 0 to 10 seconds, and then decide to extend that video and export frames from 10 to 20 seconds, that second batch of frames will have numbers that follow the ones of the first batch, allowing us to create a unified image sequence with numbers that match up.
{
_clock.set_time(params.beginning);
_clock.set_time(params.beginning, true /* force_delta_time_to_ignore_the_change */);
_clock.time_speed().value() = time_speed;
_thread_pool.start();
}
Expand Down
34 changes: 13 additions & 21 deletions src/Cool/Time/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ void Clock::toggle_play_pause()

static auto as_float_in_seconds_from_nanoseconds(int64_t time_in_nanoseconds) -> float
{
int64_t const bob = 1'000'000'000; // 10^9
return static_cast<float>(time_in_nanoseconds / bob)
int64_t const bob = 1'000'000'000;
return static_cast<float>(time_in_nanoseconds / bob) // NOLINT(bugprone-integer-division)
+ static_cast<float>(time_in_nanoseconds % bob) / static_cast<float>(bob);
}

auto Clock::delta_time_in_seconds() const -> float
{
return as_float_in_seconds_from_nanoseconds(_delta_time_in_nanoseconds + _extra_delta_time_in_nanoseconds);
return as_float_in_seconds_from_nanoseconds(_delta_time_in_nanoseconds);
}

auto Clock::time_in_seconds() const -> float
Expand All @@ -52,30 +52,22 @@ void Clock::set_time(float new_time_in_seconds, bool force_delta_time_to_ignore_
{
auto const floor = std::floor(new_time_in_seconds);
auto const fract = new_time_in_seconds - floor;
auto const bob = static_cast<int64_t>(floor) * 1'000'000'000 // TODO(TimeSpeed) Don't hardcode the 10^9
+ static_cast<int64_t>(fract * 1'000'000'000);
auto const bob = static_cast<int64_t>(floor) * 1'000'000'000
+ static_cast<int64_t>(fract * 1'000'000'000.f);
if (!force_delta_time_to_ignore_the_change)
{
_extra_delta_time_in_nanoseconds = bob - _time_in_nanoseconds; // TODO(TimeSpeed) store a vector of these, in case we call multiple set time between frames. OR just store time during each update, and compute delta time from that
_used_extra_delta_time_in_nanoseconds = false;
}
_extra_delta_time_in_nanoseconds += bob - _time_in_nanoseconds;
_time_in_nanoseconds = bob;
}

void Clock::update()
{
if (!_is_playing)
{
_delta_time_in_nanoseconds = 0;
return;
}
_delta_time_in_nanoseconds = static_cast<int64_t>(
static_cast<float>(update_and_get_delta_time_in_nanoseconds()) * _time_speed.value().value
);
if (_used_extra_delta_time_in_nanoseconds)
_extra_delta_time_in_nanoseconds = 0;
_used_extra_delta_time_in_nanoseconds = true;
_time_in_nanoseconds += _delta_time_in_nanoseconds;
int64_t const real_time_elapsed = _is_playing
? static_cast<int64_t>(static_cast<float>(update_and_get_delta_time_in_nanoseconds()) * _time_speed.value().value)
: 0;
_time_in_nanoseconds += real_time_elapsed;
_delta_time_in_nanoseconds = real_time_elapsed
+ _extra_delta_time_in_nanoseconds;
_extra_delta_time_in_nanoseconds = 0;
}

} // namespace Cool
4 changes: 3 additions & 1 deletion src/Cool/Time/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class Clock {
Clock() = default;
virtual ~Clock() = default;

/// Current time
[[nodiscard]] auto time_in_seconds() const -> float;
/// Time elapsed since the last call to update() (or 0 if update() has not been called yet).
/// Guaranteed to be constant in-between two update() calls (only changes when update is called, so that you can properly integrate with your delta time and not miss any change).
[[nodiscard]] auto delta_time_in_seconds() const -> float;

/// Must be called once per frame
Expand Down Expand Up @@ -38,7 +41,6 @@ class Clock {
int64_t _time_in_nanoseconds{0};
int64_t _delta_time_in_nanoseconds{0};
int64_t _extra_delta_time_in_nanoseconds{0};
bool _used_extra_delta_time_in_nanoseconds{true};
bool _is_playing{true};
bool _is_being_changed_in_GUI{false};
SharedVariable<TimeSpeed> _time_speed{{VariableData<TimeSpeed>{
Expand Down
12 changes: 12 additions & 0 deletions src/Cool/Utils/sum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <numeric>

namespace Cool {

template<typename T>
auto sum(std::vector<T> const& v) -> T
{
return std::accumulate(v.begin(), v.end(), T{0});
}

} // namespace Cool

0 comments on commit 8a8f517

Please sign in to comment.