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
include
asw
util
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
15
class
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
Timer
Definition
Timer.h:15
Timer::start
void start()
Definition
Timer.cpp:4
Timer::running
bool running
Definition
Timer.h:50
Timer::t1
std::chrono::time_point< std::chrono::high_resolution_clock > t1
Definition
Timer.h:46
Timer::isRunning
bool isRunning() const
Definition
Timer.cpp:17
Timer::stop
void stop()
Definition
Timer.cpp:11
Timer::getElapsedTime
double getElapsedTime()
Definition
Timer.h:31
Timer::reset
void reset()
Definition
Timer.cpp:22
Timer::t2
std::chrono::time_point< std::chrono::high_resolution_clock > t2
Definition
Timer.h:48
Generated by
1.12.0