-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
48 lines (48 loc) · 1012 Bytes
/
Timer.h
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
#pragma once
#include <chrono>
#include <thread>
class Timer {
public:
void ticker() {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (!paused)
ms++;
if (!running)
break;
}
return;
}
void stop() {
tickerthread = nullptr;
running = false;
ms = 0;
}
void reset() {
tickerthread = nullptr;
running = false;
ms = 0;
running = true;
tickerthread = new std::thread([this] { ticker(); });
}
void start() {
running = true;
tickerthread = new std::thread([this] { ticker(); });
tickerthread->detach();
}
void pause() {
tickerthread = nullptr;
running = false;
}
void resume() {
start();
}
int elapsedMilliseconds() {
return ms;
}
private:
bool running;
bool paused;
int ms = 0;
std::thread* tickerthread;
};