载入中...
搜索中...
未找到
Timer.cpp
浏览该文件的文档.
1#include "timer/Timer.h"
2
3#include <simplesquirrel/simplesquirrel.hpp>
4
5#if __has_include(<SDL2/SDL.h>)
6#include <SDL2/SDL.h>
7#else
8#include <SDL.h>
9#endif
10
11#include <chrono>
12#include <limits>
13
14namespace eve::timer {
15
17
19 SDL_InitSubSystem(SDL_INIT_TIMER);
20 eve::cap::provide<eve::service::ITimer>(this);
21 eve::cap::provide<eve::ITimeSource>(this);
22 freq_ = SDL_GetPerformanceFrequency();
23 start_ = monotonicNow();
24 simulationClock_ = std::make_unique<eve::SimulationClock>(*this, eve::Duration::fromNanoseconds(16666667));
25}
26
28 eve::cap::revoke<eve::ITimeSource>(this);
29 eve::cap::revoke<eve::service::ITimer>(this);
30}
31
32float Timer::getTime() const {
33 auto elapsed = monotonicNow().since(start_);
34 if (!elapsed) return 0.f;
35 return static_cast<float>(elapsed.value().seconds());
36}
37
38float Timer::getDelta() const { return static_cast<float>(getDeltaDuration().seconds()); }
39
40float Timer::step() {
41 auto result = stepSimulation();
42 if (!result) {
43 result.ignore("legacy Timer::step facade");
44 return 0.f;
45 }
46 result.ignore("legacy Timer::step facade");
47 return getDelta();
48}
49
51 if (freq_ == 0) return eve::MonotonicTimestamp::zero();
52 const long double counter = static_cast<long double>(SDL_GetPerformanceCounter());
53 const long double nanos = counter * 1000000000.0L / static_cast<long double>(freq_);
54 if (nanos >= static_cast<long double>(std::numeric_limits<std::int64_t>::max()))
55 return eve::MonotonicTimestamp(std::numeric_limits<std::int64_t>::max());
56 return eve::MonotonicTimestamp(static_cast<std::int64_t>(nanos));
57}
58
60 const auto now = std::chrono::system_clock::now().time_since_epoch();
61 const auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
62 return eve::WallClockTimestamp(nanos);
63}
64
66
67eve::SimulationClock& Timer::simulationClock() { return *simulationClock_; }
68
70 return simulationClock_ ? simulationClock_->lastFrameDelta() : eve::Duration::zero();
71}
72
74 return simulationClock_ ? simulationClock_->frameIndex() : eve::FrameIndex::zero();
75}
76
77void Timer::expose(ssq::Table& table) {
78 auto cls = table.addClass(name, Timer::create, false);
79 expose(cls);
80}
81
82void Timer::expose(ssq::Class& cls) {
83 cls.addFunc("getName", &Timer::getName);
84 cls.addFunc("getTime", &Timer::getTime);
85 cls.addFunc("getDelta", &Timer::getDelta);
86 cls.addFunc("step", &Timer::step);
87}
88
89} // namespace eve::timer
HSQOBJECT cls
Definition ECS.cpp:21
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
Signed, fixed-resolution simulation duration in nanoseconds.
Definition Time.h:43
static constexpr Duration fromNanoseconds(std::int64_t nanoseconds) noexcept
Construct an exact duration from nanoseconds.
Definition Time.h:55
static constexpr Duration zero() noexcept
Return zero duration.
Definition Time.h:60
double seconds() const noexcept
Return this duration as seconds for legacy/presentation APIs.
Definition Time.cpp:28
virtual std::string getName() const =0
Returns the name.
Timestamp from a monotonic source.
Definition Time.h:104
Result< Duration > since(MonotonicTimestamp earlier) const
Compute elapsed duration from an earlier timestamp.
Definition Time.cpp:48
static constexpr MonotonicTimestamp zero() noexcept
Return the zero timestamp.
Definition Time.h:113
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Owner-thread fixed-step scheduler driven by an injected time source.
Definition Time.h:201
Wall-clock timestamp used only for metadata and diagnostics.
Definition Time.h:138
static constexpr StrongUint64 zero() noexcept
Returns the zero value for this strong type.
High-resolution frame/elapsed timer backed by SDL_GetPerformanceCounter(). Script: timer <- eve....
Definition Timer.h:20
~Timer() override
Timer.
Definition Timer.cpp:27
eve::Duration getDeltaDuration() const
Return the last source duration observed by step().
Definition Timer.cpp:69
float step()
Advances the frame clock and returns the new delta in seconds.
Definition Timer.cpp:40
eve::Result< std::vector< eve::SimulationStep > > stepSimulation()
Advance the injected fixed-step clock and return emitted simulation steps.
Definition Timer.cpp:65
float getDelta() const
Seconds between the last two step() calls (0 until the first step).
Definition Timer.cpp:38
eve::WallClockTimestamp wallClockNow() const override
Read wall-clock metadata from the system clock.
Definition Timer.cpp:59
float getTime() const
Seconds since the Timer was created (0 if the timer is unavailable). Returns float (not double): Simp...
Definition Timer.cpp:32
eve::FrameIndex getFrameIndex() const
Return the presentation frame ordinal maintained by the simulation clock.
Definition Timer.cpp:73
eve::SimulationClock & simulationClock()
Return the current deterministic simulation clock. Borrowed for Timer lifetime.
Definition Timer.cpp:67
Timer()
Timer.
Definition Timer.cpp:18
eve::MonotonicTimestamp monotonicNow() const override
Read the monotonic timestamp used by this timer.
Definition Timer.cpp:50