ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1/*
2 * Timer.h
3 * A simple, cross platform timer class
4 * based on chrono
5 * Allan Legemaate
6 * 25-03-2019
7 */
8
9#ifndef TIMER_H
10#define TIMER_H
11
12#include <chrono>
13
14// Timer class
15class Timer {
16 public:
17 // Start time
18 void start();
19
20 // Stop
21 void stop();
22
23 // Is running
24 bool isRunning() const;
25
26 // Reset timer
27 void reset();
28
29 // Get time running
30 template <typename Precision>
31 double getElapsedTime() {
32 // Get time now
33 if (running) {
34 t2 = std::chrono::high_resolution_clock::now();
35 }
36
37 // Choose precision
38 auto time_diff = std::chrono::duration_cast<Precision>(t2 - t1);
39
40 // Return time as double
41 return time_diff.count();
42 }
43
44 private:
45 // Holds time points for start and end
46 std::chrono::time_point<std::chrono::high_resolution_clock> t1{
47 std::chrono::high_resolution_clock::now()};
48 std::chrono::time_point<std::chrono::high_resolution_clock> t2{
49 std::chrono::high_resolution_clock::now()};
50 bool running{false};
51};
52
53#endif // TIMER_H
Definition Timer.h:15
void start()
Definition Timer.cpp:4
bool running
Definition Timer.h:50
std::chrono::time_point< std::chrono::high_resolution_clock > t1
Definition Timer.h:46
bool isRunning() const
Definition Timer.cpp:17
void stop()
Definition Timer.cpp:11
double getElapsedTime()
Definition Timer.h:31
void reset()
Definition Timer.cpp:22
std::chrono::time_point< std::chrono::high_resolution_clock > t2
Definition Timer.h:48