From b2c35cdf69a9084806ac7930cf4475980d596cf6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 Jan 2025 11:28:20 +0100 Subject: Separate out implementation from headers --- Timer.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'Timer.cpp') diff --git a/Timer.cpp b/Timer.cpp index afe5d24..7885e73 100644 --- a/Timer.cpp +++ b/Timer.cpp @@ -1 +1,38 @@ #include "Timer.h" + +Timer::Timer(std::chrono::milliseconds interval, bool cyclic) : + m_start_time(clock_type::now()), + m_interval(interval), + m_running(false), + m_cyclic(cyclic) +{ +} + +void Timer::start() +{ + m_running = true; + m_start_time = clock_type::now(); +} + +void Timer::stop() +{ + m_running = false; +} + +bool Timer::is_elapsed() const +{ + return m_start_time + m_interval < clock_type::now(); +} + +void Timer::update() +{ + if (m_running && is_elapsed()) { + elapsed(); + if (m_cyclic) { + start(); + } else { + stop(); + } + } +} + -- cgit v1.2.3