aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Time
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Time')
-rw-r--r--src/libjin/Time/Timer.cpp100
-rw-r--r--src/libjin/Time/Timer.h78
-rw-r--r--src/libjin/Time/je_timer.cpp100
-rw-r--r--src/libjin/Time/je_timer.h117
4 files changed, 217 insertions, 178 deletions
diff --git a/src/libjin/Time/Timer.cpp b/src/libjin/Time/Timer.cpp
deleted file mode 100644
index cfdb4bd..0000000
--- a/src/libjin/Time/Timer.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-#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;
- }
-
-}
-}
-
-#endif // JIN_MODULES_TIME \ No newline at end of file
diff --git a/src/libjin/Time/Timer.h b/src/libjin/Time/Timer.h
deleted file mode 100644
index 173cd95..0000000
--- a/src/libjin/Time/Timer.h
+++ /dev/null
@@ -1,78 +0,0 @@
-#ifndef __JIN_TIMER_H
-#define __JIN_TIMER_H
-#include "../modules.h"
-#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
- SDL_Delay(ms);
- #endif
- }
-
- inline double getSecond()
- {
- #if JIN_TIME_SDL
- return SDL_GetTicks() / 1000.f;
- #endif
- }
-
- inline double getMilliSecond()
- {
- #if JIN_TIME_SDL
- return SDL_GetTicks();
- #endif
- }
-
-} // time
-} // jin
-
-#endif // JIN_MODULES_TIME
-#endif // __JIN_TIMER_H \ No newline at end of file
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)
diff --git a/src/libjin/Time/je_timer.h b/src/libjin/Time/je_timer.h
new file mode 100644
index 0000000..b6b4b9e
--- /dev/null
+++ b/src/libjin/Time/je_timer.h
@@ -0,0 +1,117 @@
+#ifndef __JE_TIMER_H
+#define __JE_TIMER_H
+#include "../core/je_configuration.h"
+#if defined(jin_time)
+
+#include <vector>
+#include "SDL2/SDL.h"
+
+namespace JinEngine
+{
+ 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 == jin_time_sdl
+ SDL_Delay(ms);
+ #endif
+ }
+
+ ///
+ ///
+ ///
+ inline double getSecond()
+ {
+ #if jin_time == jin_time_sdl
+ return SDL_GetTicks() / 1000.f;
+ #endif
+ }
+
+ ///
+ ///
+ ///
+ inline double getMilliSecond()
+ {
+ #if jin_time == jin_time_sdl
+ return SDL_GetTicks();
+ #endif
+ }
+
+ } // namespace Time
+} // namespace JinEngine
+
+#endif // defined(jin_time)
+
+#endif // __JE_TIMER_H \ No newline at end of file