载入中...
搜索中...
未找到
Timer.cpp
浏览该文件的文档.
1#include "timer/Timer.h"
2
3#include <simplesquirrel/simplesquirrel.hpp>
4
5#include <SDL2/SDL.h>
6
7namespace eve::timer {
8
10
12 SDL_InitSubSystem(SDL_INIT_TIMER);
13 freq_ = SDL_GetPerformanceFrequency();
14 start_ = SDL_GetPerformanceCounter();
15 prev_ = start_;
16 delta_ = 0.f;
17}
18
19float Timer::getTime() const {
20 if (freq_ == 0) return 0.f;
21 return float(double(SDL_GetPerformanceCounter() - start_) / double(freq_));
22}
23
24float Timer::getDelta() const { return delta_; }
25
26float Timer::step() {
27 if (freq_ == 0) {
28 delta_ = 0.f;
29 return delta_;
30 }
31 uint64_t now = SDL_GetPerformanceCounter();
32 delta_ = float(double(now - prev_) / double(freq_));
33 prev_ = now;
34 return delta_;
35}
36
37void Timer::expose(ssq::Table& table) {
38 auto cls = table.addClass(name, Timer::create, false);
39 expose(cls);
40}
41
42void Timer::expose(ssq::Class& cls) {
43 cls.addFunc("getName", &Timer::getName);
44 cls.addFunc("getTime", &Timer::getTime);
45 cls.addFunc("getDelta", &Timer::getDelta);
46 cls.addFunc("step", &Timer::step);
47}
48
49} // namespace eve::timer
HSQOBJECT cls
Definition ECS.cpp:21
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
High-resolution frame/elapsed timer backed by SDL_GetPerformanceCounter(). Script: timer <- eve....
Definition Timer.h:13
float step()
Advances the frame clock and returns the new delta in seconds.
Definition Timer.cpp:26
float getDelta() const
Seconds between the last two step() calls (0 until the first step).
Definition Timer.cpp:24
float getTime() const
Seconds since the Timer was created (0 if the timer is unavailable). Returns float (not double): Simp...
Definition Timer.cpp:19