Add Timer Support

This commit is contained in:
Kirigaya Kazuto 2017-04-04 20:33:41 +08:00
parent 499c25ddd3
commit f3b1b2208c
2 changed files with 77 additions and 1 deletions

View File

@ -803,6 +803,63 @@ namespace MiniEngine
SDL_StopTextInput(); 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() AudioPlayer::AudioPlayer()
{ {
if (!_sysAudioCounter) if (!_sysAudioCounter)

View File

@ -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 class AudioPlayer
{ {
public: public:
@ -332,7 +352,6 @@ namespace MiniEngine
Music m; Music m;
}; };
class Sound class Sound
{ {
public: public: