From f0f340dec7821cee103ab9267ef941a917ef4dc4 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 18 Nov 2018 23:31:17 +0800 Subject: =?UTF-8?q?*=E7=9B=AE=E5=BD=95=E6=94=B9=E4=B8=BA=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/time/je_timer.h | 137 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/libjin/time/je_timer.h (limited to 'src/libjin/time/je_timer.h') diff --git a/src/libjin/time/je_timer.h b/src/libjin/time/je_timer.h new file mode 100644 index 0000000..31cd322 --- /dev/null +++ b/src/libjin/time/je_timer.h @@ -0,0 +1,137 @@ +#ifndef __JE_TIMER_H__ +#define __JE_TIMER_H__ +#include "../core/je_configuration.h" +#if defined(jin_time) + +#include +#include + +#include "SDL2/SDL.h" + +namespace JinEngine +{ + namespace Time + { + + /// + /// + /// + class Timer + { + public: + + typedef std::function TimerCallback; + + typedef std::function FinishCallback; + + /// + /// + /// + class Handler + { + 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, FinishCallback finish); + + /// + /// + /// + 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 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 + } + + } // namespace Time +} // namespace JinEngine + +#endif // defined(jin_time) + +#endif // __JE_TIMER_H__ \ No newline at end of file -- cgit v1.1-26-g67d0