From fed74ddb0afe6dba935a16028eae7691073346bc Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 19 May 2020 21:42:06 +0800 Subject: [PATCH] add Timer --- projects/kiwano/kiwano.vcxproj | 2 + projects/kiwano/kiwano.vcxproj.filters | 6 ++ src/kiwano/core/Timer.cpp | 104 +++++++++++++++++++++++++ src/kiwano/core/Timer.h | 60 ++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 src/kiwano/core/Timer.cpp create mode 100644 src/kiwano/core/Timer.h diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index c6948709..b356af76 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -32,6 +32,7 @@ + @@ -141,6 +142,7 @@ + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index 75592486..7cb988ec 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -327,6 +327,9 @@ core + + core + @@ -539,6 +542,9 @@ core + + core + diff --git a/src/kiwano/core/Timer.cpp b/src/kiwano/core/Timer.cpp new file mode 100644 index 00000000..e86894a7 --- /dev/null +++ b/src/kiwano/core/Timer.cpp @@ -0,0 +1,104 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include + +namespace kiwano +{ + +Timer::Timer() + : is_stopped_(false) +{ +} + +Timer::~Timer() {} + +Duration Timer::GetDeltaTime() const +{ + return delta_time_; +} + +Duration Timer::GetTotalTime() const +{ + if (is_stopped_) + return paused_time_ - start_time_ - total_idle_time_; + return current_time_ - start_time_ - total_idle_time_; +} + +void Timer::Start() +{ + if (is_stopped_) + { + const auto now = Time::Now(); + + // add the duration of the pause to the total idle time + total_idle_time_ += (now - paused_time_); + + // set the previous time to the current time + previous_time_ = now; + + paused_time_ = Time(); + is_stopped_ = false; + } +} + +void Timer::Stop() +{ + if (!is_stopped_) + { + const auto now = Time::Now(); + + paused_time_ = now; + is_stopped_ = true; + } +} + +void Timer::Tick() +{ + if (is_stopped_) + { + delta_time_ = 0; + return; + } + + current_time_ = Time::Now(); + + // compute the time elapsed since the previous frame + delta_time_ = (current_time_ - previous_time_); + + // set previous time to current time, as in the next tick, this frame will be the previous frame + previous_time_ = current_time_; + + // delta time can be negative if the processor goes idle for example + if (delta_time_ < 0) + delta_time_ = 0; +} + +void Timer::Reset() +{ + const auto now = Time::Now(); + + start_time_ = now; + previous_time_ = now; + paused_time_ = Time(); + is_stopped_ = false; +} + +} // namespace kiwano diff --git a/src/kiwano/core/Timer.h b/src/kiwano/core/Timer.h new file mode 100644 index 00000000..e3b3e2a0 --- /dev/null +++ b/src/kiwano/core/Timer.h @@ -0,0 +1,60 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include +#include + +namespace kiwano +{ + +KGE_DECLARE_SMART_PTR(Timer); + +class Timer + : public ObjectBase +{ +public: + Timer(); + + virtual ~Timer(); + + Duration GetDeltaTime() const; + + Duration GetTotalTime() const; + + void Start(); + + void Stop(); + + void Tick(); + + void Reset(); + +private: + bool is_stopped_; + Time start_time_; + Time paused_time_; + Time current_time_; + Time previous_time_; + Duration delta_time_; + Duration total_idle_time_; +}; + +} // namespace kiwano