Add stopwatch
This commit is contained in:
parent
9e1089819d
commit
0e7776e2ca
19
inc/timer.h
19
inc/timer.h
@ -29,3 +29,22 @@ private:
|
||||
std::chrono::system_clock::time_point end_time;
|
||||
};
|
||||
|
||||
class StopWatch
|
||||
{
|
||||
public:
|
||||
StopWatch();
|
||||
TimeDuration GetTime();
|
||||
void Start();
|
||||
void Pause();
|
||||
void Resume();
|
||||
void Clear();
|
||||
|
||||
public:
|
||||
bool active = false;
|
||||
|
||||
private:
|
||||
std::chrono::system_clock::time_point start_time;
|
||||
std::chrono::system_clock::duration paused_duration;
|
||||
bool paused = false;
|
||||
};
|
||||
|
||||
|
31
src/main.cpp
31
src/main.cpp
@ -107,6 +107,7 @@ int main()
|
||||
glBindVertexArray(0);
|
||||
|
||||
Timer timer;
|
||||
StopWatch stopwatch;
|
||||
GlobalShortcuts shortcuts("timer_overlay");
|
||||
|
||||
if (shortcuts.createSession() != 0) {
|
||||
@ -114,27 +115,35 @@ int main()
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
shortcuts.addShortcut("time1", "Adds 1 minute to the timer", "ALT+F5", [](void* timer_ptr)
|
||||
{
|
||||
shortcuts.addShortcut("time1", "Adds 1 minute to the timer", "ALT+F5", [](void* timer_ptr) {
|
||||
((Timer*)timer_ptr)->AddMinutes(1);
|
||||
}, &timer);
|
||||
shortcuts.addShortcut("time5", "Adds 5 minutes to the timer", "ALT+F6", [](void* timer_ptr)
|
||||
{
|
||||
shortcuts.addShortcut("time5", "Adds 5 minutes to the timer", "ALT+F6", [](void* timer_ptr) {
|
||||
((Timer*)timer_ptr)->AddMinutes(5);
|
||||
}, &timer);
|
||||
shortcuts.addShortcut("time15", "Adds 15 minutes to the timer", "ALT+F7", [](void* timer_ptr)
|
||||
{
|
||||
shortcuts.addShortcut("time15", "Adds 15 minutes to the timer", "ALT+F7", [](void* timer_ptr) {
|
||||
((Timer*)timer_ptr)->AddMinutes(15);
|
||||
}, &timer);
|
||||
shortcuts.addShortcut("time60", "Adds 1 hour to the timer", "ALT+F8", [](void* timer_ptr)
|
||||
{
|
||||
shortcuts.addShortcut("time60", "Adds 1 hour to the timer", "ALT+F8", [](void* timer_ptr) {
|
||||
((Timer*)timer_ptr)->AddMinutes(60);
|
||||
}, &timer);
|
||||
shortcuts.addShortcut("timeclear", "Clears the timer", "ALT+F9", [](void* timer_ptr)
|
||||
{
|
||||
shortcuts.addShortcut("timeclear", "Clears the timer", "ALT+F9", [](void* timer_ptr) {
|
||||
((Timer*)timer_ptr)->Clear();
|
||||
}, &timer);
|
||||
|
||||
shortcuts.addShortcut("swstart", "Starts the stopwatch", "CTRL+F5", [](void* stopwatch_ptr) {
|
||||
((StopWatch*)stopwatch_ptr)->Start();
|
||||
}, &stopwatch);
|
||||
shortcuts.addShortcut("swclear", "Clears the stopwatch", "CTRL+F6", [](void* stopwatch_ptr) {
|
||||
((StopWatch*)stopwatch_ptr)->Clear();
|
||||
}, &stopwatch);
|
||||
shortcuts.addShortcut("swpause", "Pauses the stopwatch", "CTRL+F7", [](void* stopwatch_ptr) {
|
||||
((StopWatch*)stopwatch_ptr)->Pause();
|
||||
}, &stopwatch);
|
||||
shortcuts.addShortcut("swresume", "Resumes the stopwatch", "CTRL+F8", [](void* stopwatch_ptr) {
|
||||
((StopWatch*)stopwatch_ptr)->Resume();
|
||||
}, &stopwatch);
|
||||
|
||||
if (!shortcuts.alreadyBound()) {
|
||||
std::cout << "Requsting to bind keys" << std::endl;
|
||||
if (shortcuts.bindKeys() != 0) {
|
||||
@ -154,7 +163,7 @@ int main()
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
TimeDuration time_diff = timer.GetTimeLeft();
|
||||
TimeDuration time_diff = stopwatch.active ? stopwatch.GetTime() : timer.GetTimeLeft();
|
||||
if (time_diff.seconds == last_frame_seconds && !time_diff.negative) {
|
||||
continue;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
@ -49,3 +50,66 @@ void Timer::Clear()
|
||||
end_time = std::chrono::system_clock::now() - std::chrono::seconds(60);
|
||||
}
|
||||
|
||||
|
||||
|
||||
StopWatch::StopWatch()
|
||||
: active(false), paused(false) { }
|
||||
|
||||
TimeDuration StopWatch::GetTime()
|
||||
{
|
||||
if (!active) {
|
||||
return TimeDuration{
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0,
|
||||
false,
|
||||
};
|
||||
}
|
||||
|
||||
auto time = paused ? paused_duration : std::chrono::system_clock::now() - start_time;
|
||||
|
||||
return TimeDuration{
|
||||
std::chrono::duration_cast<std::chrono::hours>(time).count(),
|
||||
std::chrono::duration_cast<std::chrono::minutes>(time).count() % 60,
|
||||
std::chrono::duration_cast<std::chrono::seconds>(time).count() % 60,
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(time).count() % 1000,
|
||||
|
||||
std::chrono::duration_cast<std::chrono::minutes>(time).count(),
|
||||
std::chrono::duration_cast<std::chrono::seconds>(time).count(),
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(time).count(),
|
||||
|
||||
false,
|
||||
};
|
||||
}
|
||||
|
||||
void StopWatch::Start()
|
||||
{
|
||||
if (!active) {
|
||||
start_time = std::chrono::system_clock::now();
|
||||
active = true;
|
||||
paused = false;
|
||||
}
|
||||
}
|
||||
|
||||
void StopWatch::Pause()
|
||||
{
|
||||
if (!paused) {
|
||||
paused_duration = std::chrono::system_clock::now() - start_time;
|
||||
paused = true;
|
||||
}
|
||||
}
|
||||
|
||||
void StopWatch::Resume()
|
||||
{
|
||||
if (paused) {
|
||||
start_time = std::chrono::system_clock::now() - paused_duration;
|
||||
paused = false;
|
||||
}
|
||||
}
|
||||
|
||||
void StopWatch::Clear()
|
||||
{
|
||||
active = false;
|
||||
paused = false;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user