From 8b00d67febf133e89f6a0bfabc41feed555dc4a9 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 12 Jan 2019 21:48:33 +0800 Subject: =?UTF-8?q?*=E5=8E=BB=E6=8E=89=E6=96=87=E4=BB=B6=E5=89=8D=E7=BC=80?= =?UTF-8?q?je=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/time/timer.h | 158 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/libjin/time/timer.h (limited to 'src/libjin/time/timer.h') 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 +#include + +#include "SDL2/SDL.h" + +#include "../common/object.h" + +namespace JinEngine +{ + namespace Time + { + + /// + /// + /// + class Timer : public Object + { + public: + + typedef std::function TimerCallback; + + typedef std::function 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 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 -- cgit v1.1-26-g67d0