-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.hpp
77 lines (66 loc) · 1.86 KB
/
timer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef TIMER_H_
#define TIMER_H_
#include <chrono>
// Using a namespace to prevent potential naming conflicts and to logically group
// associated functionalities.
namespace TimerUtils {
/**
* @class Timer
* Represents a timer that allows the user to pause, resume, or return to the main menu.
*/
class Timer {
private:
std::chrono::steady_clock::time_point startTime;
std::chrono::steady_clock::duration pausedTime;
bool isPaused;
public:
/**
* Constructs a Timer object and initializes its properties.
*/
Timer() {
isPaused = false;
}
/**
* Starts the timer.
*/
void start() {
startTime = std::chrono::steady_clock::now();
pausedTime = std::chrono::steady_clock::duration::zero();
isPaused = false;
}
/**
* Pauses the timer.
*/
void pause() {
if (!isPaused) {
pausedTime += std::chrono::steady_clock::now() - startTime;
isPaused = true;
}
}
/**
* Resumes the timer.
*/
void resume() {
if (isPaused) {
startTime = std::chrono::steady_clock::now();
isPaused = false;
}
}
/**
* Returns the elapsed time in seconds.
*
* @return double The elapsed time in seconds.
*/
double getElapsedTime() {
std::chrono::steady_clock::duration elapsedTime;
if (isPaused) {
elapsedTime = pausedTime;
}
else {
elapsedTime = std::chrono::steady_clock::now() - startTime + pausedTime;
}
return std::chrono::duration<double>(elapsedTime).count();
}
};
}
#endif /* !TIMER_H_ */