axmol/core/base/SimpleTimer.cpp

96 lines
3.1 KiB
C++
Raw Normal View History

2019-11-24 23:15:56 +08:00
#include "SimpleTimer.h"
2019-11-25 01:35:26 +08:00
#include "yasio/detail/object_pool.hpp"
2019-11-26 23:37:27 +08:00
#include "yasio/detail/ref_ptr.hpp"
2019-11-24 23:15:56 +08:00
#include "cocos2d.h"
2021-12-25 10:04:45 +08:00
#define STIMER_DEFINE_REFERENCE_CLASS \
private: \
unsigned int referenceCount_; \
\
public: \
void retain() { ++referenceCount_; } \
void release() \
{ \
--referenceCount_; \
if (referenceCount_ == 0) \
delete this; \
} \
\
private:
2019-11-24 23:15:56 +08:00
2019-11-25 01:35:26 +08:00
// Retrive the fake target of Simple Timer, well, any system's malloc never return a object address
// 0xffffffff, 0xfffffffe, so it's always works well.
#define STIMER_TARGET(bNative) reinterpret_cast<void*>(bNative ? STIMER_TARGET_NATIVE : STIMER_TARGET_SCRIPT)
2020-08-04 00:14:35 +08:00
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace stimer
{
static const uintptr_t STIMER_TARGET_NATIVE = ~static_cast<uintptr_t>(0);
static const uintptr_t STIMER_TARGET_SCRIPT = STIMER_TARGET_NATIVE - 1;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
struct TimerObject
{
TimerObject(vcallback_t&& callback) : callback_(std::move(callback)), referenceCount_(1) {}
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
vcallback_t callback_;
static uintptr_t s_timerId;
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
DEFINE_OBJECT_POOL_ALLOCATION(TimerObject, 128)
STIMER_DEFINE_REFERENCE_CLASS
};
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
uintptr_t TimerObject::s_timerId = 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
TIMER_ID loop(unsigned int n, float interval, vcallback_t callback, bool bNative)
{
if (n > 0 && interval >= 0)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
yasio::gc::ref_ptr<TimerObject> timerObj(new TimerObject(std::move(callback)));
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto timerId = reinterpret_cast<TIMER_ID>(++TimerObject::s_timerId);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string key = StringUtils::format("STMR#%p", timerId);
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
Director::getInstance()->getScheduler()->schedule(
[timerObj](float /*dt*/) { // lambda expression hold the reference of timerObj automatically.
2019-11-24 23:15:56 +08:00
timerObj->callback_();
2021-12-25 10:04:45 +08:00
},
STIMER_TARGET(bNative), interval, n - 1, 0, false, key);
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
return timerId;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return nullptr;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
TIMER_ID delay(float delay, vcallback_t callback, bool bNative)
{
if (delay > 0)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
yasio::gc::ref_ptr<TimerObject> timerObj(new TimerObject(std::move(callback)));
auto timerId = reinterpret_cast<TIMER_ID>(++TimerObject::s_timerId);
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
std::string key = StringUtils::format("STMR#%p", timerId);
Director::getInstance()->getScheduler()->schedule(
[timerObj](float /*dt*/) { // lambda expression hold the reference of timerObj automatically.
2019-11-24 23:15:56 +08:00
timerObj->callback_();
2021-12-25 10:04:45 +08:00
},
STIMER_TARGET(bNative), 0, 0, delay, false, key);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return timerId;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return nullptr;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void kill(TIMER_ID timerId, bool bNative)
{
std::string key = StringUtils::format("STMR#%p", timerId);
Director::getInstance()->getScheduler()->unschedule(key, STIMER_TARGET(bNative));
}
2019-11-25 01:35:26 +08:00
2021-12-25 10:04:45 +08:00
void killAll(bool bNative)
{
Director::getInstance()->getScheduler()->unscheduleAllForTarget(STIMER_TARGET(bNative));
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace stimer
2020-08-04 00:14:35 +08:00
NS_CC_END