diff --git a/MiniEngine.cpp b/MiniEngine.cpp index b9f8df1..8303ab8 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1257,14 +1257,28 @@ namespace MiniEngine SDL_StopTextInput(); } + /// Global Executor For class Timer + Uint32 _global_timer_executor(Uint32 interval,void* param) + { + printf("DEBUG: Global Timer Executor.\n"); + auto p=reinterpret_cast*>(param); + return (*p)(interval); + } + Timer::Timer() { _enabled=false; _detached=false; + _delete_on_disable=false; id=-1; } Timer::Timer(SDL_TimerCallback callback,Uint32 interval,void* param) : Timer() + { + _real_timer_call(callback,interval,param); + } + + void Timer::_real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param) { _callback=callback; _interval=interval; @@ -1293,6 +1307,15 @@ namespace MiniEngine SDL_RemoveTimer(id); _enabled=false; id=-1; + _callback=nullptr; + + if(_delete_on_disable) + { + _delete_delegator(reinterpret_cast*>(_param)); + _delete_on_disable=false; + } + + _param=nullptr; return 0; } else @@ -1314,6 +1337,12 @@ namespace MiniEngine } } + //static + void Timer::_delete_delegator(std::function* param) + { + delete param; + } + AudioPlayer::AudioPlayer() { if (!_sysAudioCounter) diff --git a/MiniEngine.h b/MiniEngine.h index 837a6a7..da5154a 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -426,24 +426,43 @@ namespace MiniEngine }; }; + Uint32 _global_timer_executor(Uint32 interval,void* param); + class Timer { public: Timer(); /// Uint32 func(Uint32,void*) ... + template + Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer() + { + auto realCall=[&](Uint32 interval)->Uint32{return callable(interval,args...);}; + auto pfunc=new std::function(realCall); + _real_timer_call(_global_timer_executor,interval,pfunc); + } + + /// Restore For Capability Timer(SDL_TimerCallback callback,Uint32 interval,void* param); + int enable(); int disable(); bool isenable(); void detach(); ~Timer(); + + static void _delete_delegator(std::function* Delegator); private: + + void _real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param); + SDL_TimerCallback _callback; Uint32 _interval; void* _param; SDL_TimerID id; bool _enabled; bool _detached; + /// Reserved Variable For Template variable Parameter + bool _delete_on_disable; }; class AudioPlayer