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
36 class COGSFOUNDATION_API Timer
37 {
38 public:
39 static inline Timer startNew()
40 {
41 Timer timer;
42 timer.start();
43 return timer;
44 }
45
46 void start()
47 {
48 running = true;
49 startTime = Cogs::perfTime();
50 }
51 void stop()
52 {
53 running = false;
54 stopTime = Cogs::perfTime();
55 }
56
57 int64_t elapsedMicroseconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToMicroseconds(scalePerfTime(stopTime - startTime)); }
58 int64_t elapsedMilliseconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToMilliseconds(scalePerfTime(stopTime - startTime)); }
59 double elapsedSeconds() { if(running) stopTime = Cogs::perfTime(); return perfTimeToSeconds(scalePerfTime(stopTime - startTime)); }
60
61 static int64_t currentTimeMicroseconds() { return currentTimeToMicroseconds(currentTime()); }
62 static int64_t currentTimeMilliseconds() { return currentTimeToMilliseconds(currentTime()); }
63
64#if defined(EMSCRIPTEN)
65 void setScale(int64_t s) { scale = static_cast<double>(s) / 1000.0; }
66 void resetScale() { scale = 1.0; }
67 int64_t getScale() const { return static_cast<int64_t>(1000.0 * scale); }
68 TimePerf scalePerfTime(TimePerf time) { return time*scale; }
69#else
70 void setScale(int64_t s) { scale = s; }
71 void resetScale() { scale = 1000; }
72 int64_t getScale() const { return scale; }
73 TimePerf scalePerfTime(TimePerf time) { return time*scale/1000; }
74#endif
75
76 private:
77 TimePerf startTime = {};
78 TimePerf stopTime = {};
79#if defined(EMSCRIPTEN)
80 double scale = 1.0f;
81#else
82 int64_t scale = 1000;
83#endif
84 bool running = false;
85 };
86}
Old timer class.
Definition: Timer.h:37
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,...