aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Time
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Time')
-rw-r--r--src/libjin/Time/Timer.cpp86
-rw-r--r--src/libjin/Time/Timer.h52
2 files changed, 132 insertions, 6 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;
+ }
}
}
diff --git a/src/libjin/Time/Timer.h b/src/libjin/Time/Timer.h
index 9458215..173cd95 100644
--- a/src/libjin/Time/Timer.h
+++ b/src/libjin/Time/Timer.h
@@ -4,31 +4,71 @@
#if JIN_MODULES_TIME
#include "SDL2/SDL.h"
+#include <vector>
namespace jin
{
namespace time
{
+
+ class Timers
+ {
+ public:
+ typedef void(*timer_callback)(void* prameters);
+
+ Timers();
+ ~Timers();
+
+ void update(int ms);
+
+ void every(int ms, timer_callback callback, void* paramters);
+ void after(int ms, timer_callback callback, void* paramters);
+ void repeat(int ms, int count, timer_callback callback, void* paramters);
+ private:
+ class Timer
+ {
+ public:
+ enum Type
+ {
+ EVERY,
+ AFTER,
+ REPEAT,
+ };
+ Timer(Type type, int duration, int count = 0, timer_callback callback = nullptr, void* paramters = nullptr);
+ virtual ~Timer();
+ bool process(int ms);
+ private:
+ int duration;
+ int count;
+ int tickdown;
+ int countdown;
+ Type type;
+ timer_callback callback;
+ void* paramters;
+ };
+ std::vector<Timer*> timers;
+ };
+
inline void sleep(int ms)
{
-#if JIN_TIME_SDL
+ #if JIN_TIME_SDL
SDL_Delay(ms);
-#endif
+ #endif
}
inline double getSecond()
{
-#if JIN_TIME_SDL
+ #if JIN_TIME_SDL
return SDL_GetTicks() / 1000.f;
-#endif
+ #endif
}
inline double getMilliSecond()
{
-#if JIN_TIME_SDL
+ #if JIN_TIME_SDL
return SDL_GetTicks();
-#endif
+ #endif
}
} // time