aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/time/timer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/time/timer.h')
-rw-r--r--src/libjin/time/timer.h158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/libjin/time/timer.h b/src/libjin/time/timer.h
new file mode 100644
index 0000000..48e51ba
--- /dev/null
+++ b/src/libjin/time/timer.h
@@ -0,0 +1,158 @@
+#ifndef __JE_TIMER_H__
+#define __JE_TIMER_H__
+#include "../core/configuration.h"
+#if defined(jin_time)
+
+#include <vector>
+#include <functional>
+
+#include "SDL2/SDL.h"
+
+#include "../common/object.h"
+
+namespace JinEngine
+{
+ namespace Time
+ {
+
+ ///
+ ///
+ ///
+ class Timer : public Object
+ {
+ public:
+
+ typedef std::function<void(void*)> TimerCallback;
+
+ typedef std::function<void(void*)> FinishCallback;
+
+ ///
+ ///
+ ///
+ class Handler : public Object
+ {
+ public:
+ friend class Timer;
+ enum Type
+ {
+ EVERY,
+ AFTER,
+ REPEAT,
+ };
+ Handler(Type type, float duration, int count = 0, TimerCallback callback = nullptr, void* paramters = nullptr, FinishCallback finishcallback = nullptr);
+ virtual ~Handler();
+ void process(float dt);
+
+ protected:
+ int count;
+ int countdown;
+ float duration;
+ float tickdown;
+ Type type;
+ TimerCallback callback;
+ FinishCallback finishCallback;
+ void* paramters;
+ bool canceled;
+ };
+
+ ///
+ ///
+ ///
+ Timer();
+
+ ///
+ ///
+ ///
+ ~Timer();
+
+ ///
+ ///
+ ///
+ void update(float dt);
+
+ ///
+ ///
+ ///
+ Handler* every(float dt, TimerCallback callback, void* paramters = nullptr, FinishCallback finish = nullptr);
+
+ ///
+ ///
+ ///
+ Handler* after(float dt, TimerCallback callback, void* paramters, FinishCallback finish);
+
+ ///
+ ///
+ ///
+ Handler* repeat(float dt, int count, TimerCallback callback, void* paramters, FinishCallback finish);
+
+ ///
+ ///
+ ///
+ void cancel(Handler* handler);
+
+ ///
+ ///
+ ///
+ void cancelAll();
+
+ private:
+
+ std::vector<Handler*> mHandlers;
+
+ };
+
+ ///
+ ///
+ ///
+ 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
+ }
+
+ ///
+ /// Delta time between frames.
+ ///
+ extern float deltaTime;
+
+ inline void step()
+ {
+ static float previous = 0;
+ static float current = getSecond();
+ previous = current;
+ current = getSecond();
+ deltaTime = current - previous;
+ }
+
+ inline float getDeltaTime()
+ {
+ return deltaTime;
+ }
+
+ } // namespace Time
+} // namespace JinEngine
+
+#endif // defined(jin_time)
+
+#endif // __JE_TIMER_H__ \ No newline at end of file