157 lines
3.5 KiB
C
157 lines
3.5 KiB
C
|
|
// 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/Timer.h>
|
|||
|
|
|
|||
|
|
namespace kiwano
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
KGE_DECLARE_SMART_PTR(Ticker);
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ʱ<EFBFBD><CAB1>
|
|||
|
|
class KGE_API Ticker
|
|||
|
|
: public ObjectBase
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
/// @param interval <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
/// @param times <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1 Ϊ<><CEAA><EFBFBD>ã<EFBFBD>
|
|||
|
|
static TickerPtr Create(Duration interval, int times = -1);
|
|||
|
|
|
|||
|
|
Ticker();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ʱ
|
|||
|
|
/// @return <20>Ƿ<EFBFBD><C7B7>ﵽ<EFBFBD><EFB5BD>ʱʱ<CAB1><CAB1>
|
|||
|
|
virtual bool Tick();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ʱ
|
|||
|
|
/// @param dt ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// @return <20>Ƿ<EFBFBD><C7B7>ﵽ<EFBFBD><EFB5BD>ʱʱ<CAB1><CAB1>
|
|||
|
|
virtual bool Tick(Duration dt);
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡʱ<C8A1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Duration GetDeltaTime();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ͣ״̬
|
|||
|
|
bool IsPausing() const;
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ͣ<EFBFBD><CDA3>ʱ<EFBFBD><CAB1>
|
|||
|
|
void Pause();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
void Resume();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
int GetTickedTimes() const;
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD>ܱ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
int GetTotalTickTimes() const;
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD>ñ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ܱ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
void SetTotalTickTimes(int times);
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
Duration GetInterval() const;
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD>ñ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
void SetInterval(Duration interval);
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>
|
|||
|
|
TimerPtr GetTimer();
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD>ü<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
void SetTimer(TimerPtr timer);
|
|||
|
|
|
|||
|
|
/// \~chinese
|
|||
|
|
/// @brief <20><><EFBFBD>ñ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
void Reset();
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
int ticked_times_;
|
|||
|
|
int total_times_;
|
|||
|
|
Duration interval_;
|
|||
|
|
Duration elapsed_time_;
|
|||
|
|
Duration delta_time_;
|
|||
|
|
Duration error_time_;
|
|||
|
|
TimerPtr timer_;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
inline void Ticker::Pause()
|
|||
|
|
{
|
|||
|
|
KGE_ASSERT(timer_);
|
|||
|
|
timer_->Pause();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline void Ticker::Resume()
|
|||
|
|
{
|
|||
|
|
KGE_ASSERT(timer_);
|
|||
|
|
timer_->Resume();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline bool Ticker::IsPausing() const
|
|||
|
|
{
|
|||
|
|
if (timer_)
|
|||
|
|
return timer_->IsPausing();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline int Ticker::GetTickedTimes() const
|
|||
|
|
{
|
|||
|
|
return ticked_times_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline int Ticker::GetTotalTickTimes() const
|
|||
|
|
{
|
|||
|
|
return total_times_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline void Ticker::SetTotalTickTimes(int times)
|
|||
|
|
{
|
|||
|
|
total_times_ = times;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline Duration Ticker::GetInterval() const
|
|||
|
|
{
|
|||
|
|
return interval_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
inline void Ticker::SetInterval(Duration interval)
|
|||
|
|
{
|
|||
|
|
interval_ = interval;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} // namespace kiwano
|