diff options
author | chai <chaifix@163.com> | 2018-08-06 16:22:00 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-06 16:22:00 +0800 |
commit | 89e7a9ecfad9a54633eb3ebbab1d1f13ad78826f (patch) | |
tree | 1ba8a806af1b8e7750d6b637c296f08e13cf268e /src/libjin/Time/Timer.cpp | |
parent | ea9769944a2db3abe15f537dab67e16fdfc20bef (diff) |
*update
Diffstat (limited to 'src/libjin/Time/Timer.cpp')
-rw-r--r-- | src/libjin/Time/Timer.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/libjin/Time/Timer.cpp b/src/libjin/Time/Timer.cpp index fe72a90..cfdb4bd 100644 --- a/src/libjin/Time/Timer.cpp +++ b/src/libjin/Time/Timer.cpp @@ -1,12 +1,98 @@ #include "../modules.h" #if JIN_MODULES_TIME +#include "Timer.h" + namespace jin { namespace time { + Timers::Timers() + : timers() + { + } + + Timers::~Timers() + { + for (int i = 0; i < timers.size(); ++i) + delete timers[i]; + } + + void Timers::update(int ms) + { + std::vector<Timer*>::iterator it = timers.begin(); + for (; it != timers.end(); ) + { + if (!(*it)->process(ms)) + { + Timer* t = *it; + timers.erase(it); + delete t; + return; + } + ++it; + } + } + + void Timers::every(int ms, timer_callback callback, void* p) + { + Timer* t = new Timer(Timer::EVERY, ms, 0, callback, p); + timers.push_back(t); + } + + void Timers::after(int ms, timer_callback callback, void* p) + { + Timer* t = new Timer(Timer::AFTER, ms, 0, callback, p); + timers.push_back(t); + } + + void Timers::repeat(int ms, int count, timer_callback callback, void* p) + { + Timer* t = new Timer(Timer::REPEAT, ms, count, callback, p); + timers.push_back(t); + } + + Timers::Timer::Timer(Type t, int d, int c, timer_callback f, void* p) + : type(t) + , duration(d) + , count(c) + , tickdown(d) + , countdown(c) + , callback(f) + , paramters(p) + { + } + + Timers::Timer::~Timer() + { + } + + bool Timers::Timer::process(int ms) + { + tickdown -= ms; + if (tickdown <= 0) + { + tickdown = duration; + if (callback != nullptr) + callback(paramters); + if (type == EVERY) + { + } + else if (type == AFTER) + { + return false; + } + else if (type == REPEAT) + { + --countdown; + if (countdown <= 0) + return false; + } + } + return true; + } } } |