From f3b1b2208c9d6f1fa110d6852ec1408f2a68fb14 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 4 Apr 2017 20:33:41 +0800 Subject: [PATCH] Add Timer Support --- MiniEngine.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ MiniEngine.h | 21 ++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index ffc7594..0dc49e1 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -803,6 +803,63 @@ namespace MiniEngine SDL_StopTextInput(); } + Timer::Timer() + { + _enabled=false; + _detached=false; + id=-1; + } + + Timer::Timer(SDL_TimerCallback callback,Uint32 interval,void* param) : Timer() + { + _callback=callback; + _interval=interval; + _param=param; + } + + int Timer::enable() + { + if(_enabled) + { + return -1; + } + else + { + id=SDL_AddTimer(_interval,_callback,_param); + if(id<0) return -2; + _enabled=true; + return 0; + } + } + + int Timer::disable() + { + if(_enabled) + { + SDL_RemoveTimer(id); + _enabled=false; + id=-1; + return 0; + } + else + { + return -1; + } + } + + void Timer::detach() + { + _detached=true; + } + + Timer::~Timer() + { + if(!_detached) + { + disable(); + } + } + AudioPlayer::AudioPlayer() { if (!_sysAudioCounter) diff --git a/MiniEngine.h b/MiniEngine.h index b87c2ca..c1864f7 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -282,6 +282,26 @@ namespace MiniEngine }; }; + class Timer + { + public: + Timer(); + /// Uint32 func(Uint32,void*) ... + Timer(SDL_TimerCallback callback,Uint32 interval,void* param); + int enable(); + int disable(); + bool isenable(); + void detach(); + ~Timer(); + private: + SDL_TimerCallback _callback; + Uint32 _interval; + void* _param; + SDL_TimerID id; + bool _enabled; + bool _detached; + }; + class AudioPlayer { public: @@ -332,7 +352,6 @@ namespace MiniEngine Music m; }; - class Sound { public: