Cogs.Core
Timer.h
1#pragma once
2
3#include "../FoundationBase.h"
4
5#include <cstdint>
6
7namespace Cogs
8{
9#if defined(__EMSCRIPTEN__)
10 typedef double TimePerf;
11 typedef int64_t Time;
12#else
13 typedef int64_t TimePerf;
14 typedef int64_t Time;
15#endif
16
18 COGSFOUNDATION_API TimePerf perfTime();
20 COGSFOUNDATION_API Time currentTime();
21
23 COGSFOUNDATION_API TimePerf perfTimeReference();
24 COGSFOUNDATION_API Time currentTimeReference();
25
27 COGSFOUNDATION_API int64_t perfTimeToMicroseconds(TimePerf time);
28 COGSFOUNDATION_API int64_t perfTimeToMilliseconds(TimePerf time);
29 COGSFOUNDATION_API double perfTimeToSeconds(TimePerf time);
30
31 COGSFOUNDATION_API int64_t currentTimeToMicroseconds(Time time);
32 COGSFOUNDATION_API int64_t currentTimeToMilliseconds(Time time);
33 COGSFOUNDATION_API double currentTimeToSeconds(Time time);
34
45 class COGSFOUNDATION_API Timer
46 {
47 public:
48 static inline Timer startNew()
49 {
50 Timer timer;
51 timer.start();
52 return timer;
53 }
54
55 void start()
56 {
57 running = true;
58 startTime = Cogs::perfTime();
59 }
60 void stop()
61 {
62 running = false;
63 stopTime = Cogs::perfTime();
64 }
65
66 int64_t elapsedMicroseconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToMicroseconds(scalePerfTime(stopTime - startTime)); }
67 int64_t elapsedMilliseconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToMilliseconds(scalePerfTime(stopTime - startTime)); }
68 double elapsedSeconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToSeconds(scalePerfTime(stopTime - startTime)); }
69
70 static int64_t currentTimeMicroseconds() { return currentTimeToMicroseconds(currentTime()); }
71 static int64_t currentTimeMilliseconds() { return currentTimeToMilliseconds(currentTime()); }
72
73#if defined(__EMSCRIPTEN__)
74 void setScale(int64_t s) { scale = static_cast<double>(s) / 1000.0; }
75 void resetScale() { scale = 1.0; }
76 int64_t getScale() const { return static_cast<int64_t>(1000.0 * scale); }
77 TimePerf scalePerfTime(TimePerf time) { return time*scale; }
78#else
79 void setScale(int64_t s) { scale = s; }
80 void resetScale() { scale = 1000; }
81 int64_t getScale() const { return scale; }
82 TimePerf scalePerfTime(TimePerf time) { return time*scale/1000; }
83#endif
84
85 private:
86 TimePerf startTime = {};
87 TimePerf stopTime = {};
88#if defined(__EMSCRIPTEN__)
89 double scale = 1.0f;
90#else
91 int64_t scale = 1000;
92#endif
93 bool running = false;
94 };
95}
High-resolution performance timer.
Definition: Timer.h:46
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
COGSFOUNDATION_API int64_t perfTimeToMicroseconds(TimePerf time)
Convertion functions for timestamps.
COGSFOUNDATION_API TimePerf perfTime()
High resolution performance timer. Returns an implementation defined absolute timestamp,...
COGSFOUNDATION_API TimePerf perfTimeReference()
Functions that provide a reference time point at the start of the application startup....
Definition: Timer.cpp:16
COGSFOUNDATION_API Time currentTime()
High resolution clock time (NTP / UTC time). Returns an implementation defined absolute timestamp,...