aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Time/je_timer.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-23 12:23:58 +0800
committerchai <chaifix@163.com>2018-10-23 12:23:58 +0800
commit40fc27154fe754181934dc7ee31375e6bdfb33fc (patch)
tree897ad98d759bc308ef66561181ba88b85f2ccd47 /src/libjin/Time/je_timer.cpp
parent1480c9445100075c9e1a894eb07c0ef727b509a1 (diff)
*merge from minimal
Diffstat (limited to 'src/libjin/Time/je_timer.cpp')
-rw-r--r--src/libjin/Time/je_timer.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/libjin/Time/je_timer.cpp b/src/libjin/Time/je_timer.cpp
new file mode 100644
index 0000000..a2f2486
--- /dev/null
+++ b/src/libjin/Time/je_timer.cpp
@@ -0,0 +1,100 @@
+#include "../core/je_configuration.h"
+#if defined(jin_time)
+
+#include "je_timer.h"
+
+namespace JinEngine
+{
+ 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;
+ }
+
+ } // namespace Time
+} // namespace JinEngine
+
+#endif // defined(jin_time)