Magic_Game/src/kiwano/utils/Ticker.h

153 lines
3.4 KiB
C
Raw Normal View History

2020-05-20 00:47:46 +08:00
// 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
2020-05-20 23:48:56 +08:00
#include <kiwano/utils/Timer.h>
2020-05-20 00:47:46 +08:00
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>
2020-05-26 15:20:02 +08:00
/// @param tick_count <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1 Ϊ<><CEAA><EFBFBD>ã<EFBFBD>
2020-07-22 21:08:48 +08:00
Ticker(Duration interval, int tick_count = -1);
2020-05-20 00:47:46 +08:00
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>
2020-05-26 15:20:02 +08:00
int GetTickedCount() const;
2020-05-20 00:47:46 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD>ܱ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
2020-05-26 15:20:02 +08:00
int GetTotalTickCount() const;
2020-05-20 00:47:46 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ñ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ܱ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
2020-05-26 15:20:02 +08:00
void SetTotalTickCount(int count);
2020-05-20 00:47:46 +08:00
/// \~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);
2020-05-27 16:38:58 +08:00
/// \~chinese
/// @brief <20><>ȡʱ<C8A1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Duration GetErrorTime() const;
2020-05-20 00:47:46 +08:00
/// \~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:
2020-05-20 10:16:54 +08:00
bool is_paused_;
2020-05-26 15:20:02 +08:00
int ticked_count_;
int total_tick_count_;
2020-05-20 00:47:46 +08:00
Duration interval_;
Duration elapsed_time_;
Duration delta_time_;
Duration error_time_;
TimerPtr timer_;
};
inline bool Ticker::IsPausing() const
{
2020-05-20 10:16:54 +08:00
return is_paused_;
2020-05-20 00:47:46 +08:00
}
2020-05-26 15:20:02 +08:00
inline int Ticker::GetTickedCount() const
2020-05-20 00:47:46 +08:00
{
2020-05-26 15:20:02 +08:00
return ticked_count_;
2020-05-20 00:47:46 +08:00
}
2020-05-26 15:20:02 +08:00
inline int Ticker::GetTotalTickCount() const
2020-05-20 00:47:46 +08:00
{
2020-05-26 15:20:02 +08:00
return total_tick_count_;
2020-05-20 00:47:46 +08:00
}
2020-05-26 15:20:02 +08:00
inline void Ticker::SetTotalTickCount(int count)
2020-05-20 00:47:46 +08:00
{
2020-05-26 15:20:02 +08:00
total_tick_count_ = count;
2020-05-20 00:47:46 +08:00
}
inline Duration Ticker::GetInterval() const
{
return interval_;
}
inline void Ticker::SetInterval(Duration interval)
{
interval_ = interval;
}
2020-05-27 16:38:58 +08:00
inline Duration Ticker::GetErrorTime() const
{
return error_time_;
}
2020-05-20 00:47:46 +08:00
} // namespace kiwano