diff --git a/inc/timer.h b/inc/timer.h index 3e76501..c484dc8 100644 --- a/inc/timer.h +++ b/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; +}; + diff --git a/src/main.cpp b/src/main.cpp index 2af1ef8..59d8c8a 100644 --- a/src/main.cpp +++ b/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; } diff --git a/src/timer.cpp b/src/timer.cpp index 7ecd818..7d58f03 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,5 +1,6 @@ #include #include +#include #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(time).count(), + std::chrono::duration_cast(time).count() % 60, + std::chrono::duration_cast(time).count() % 60, + std::chrono::duration_cast(time).count() % 1000, + + std::chrono::duration_cast(time).count(), + std::chrono::duration_cast(time).count(), + std::chrono::duration_cast(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; +} + +