add Timer
This commit is contained in:
parent
430e46a5a3
commit
fed74ddb0a
|
|
@ -32,6 +32,7 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\core\Singleton.h" />
|
<ClInclude Include="..\..\src\kiwano\core\Singleton.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\String.h" />
|
<ClInclude Include="..\..\src\kiwano\core\String.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\Time.h" />
|
<ClInclude Include="..\..\src\kiwano\core\Time.h" />
|
||||||
|
<ClInclude Include="..\..\src\kiwano\core\Timer.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\core\Xml.h" />
|
<ClInclude Include="..\..\src\kiwano\core\Xml.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\kiwano.h" />
|
<ClInclude Include="..\..\src\kiwano\kiwano.h" />
|
||||||
<ClInclude Include="..\..\src\kiwano\config.h" />
|
<ClInclude Include="..\..\src\kiwano\config.h" />
|
||||||
|
|
@ -141,6 +142,7 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Time.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Task.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\Task.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\core\TaskManager.cpp" />
|
<ClCompile Include="..\..\src\kiwano\core\TaskManager.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\kiwano\core\Timer.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\Application.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\FileSystem.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\FileSystem.cpp" />
|
||||||
<ClCompile Include="..\..\src\kiwano\platform\Input.cpp" />
|
<ClCompile Include="..\..\src\kiwano\platform\Input.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -327,6 +327,9 @@
|
||||||
<ClInclude Include="..\..\src\kiwano\core\TaskManager.h">
|
<ClInclude Include="..\..\src\kiwano\core\TaskManager.h">
|
||||||
<Filter>core</Filter>
|
<Filter>core</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\kiwano\core\Timer.h">
|
||||||
|
<Filter>core</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
|
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
|
||||||
|
|
@ -539,6 +542,9 @@
|
||||||
<ClCompile Include="..\..\src\kiwano\core\Task.cpp">
|
<ClCompile Include="..\..\src\kiwano\core\Task.cpp">
|
||||||
<Filter>core</Filter>
|
<Filter>core</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\kiwano\core\Timer.cpp">
|
||||||
|
<Filter>core</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="suppress_warning.ruleset" />
|
<None Include="suppress_warning.ruleset" />
|
||||||
|
|
|
||||||
|
|
@ -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 <kiwano/core/Timer.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -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 <kiwano/core/Time.h>
|
||||||
|
#include <kiwano/core/ObjectBase.h>
|
||||||
|
|
||||||
|
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
|
||||||
Loading…
Reference in New Issue