diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 7f2e4fc..63c4618 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -164,7 +164,7 @@ namespace MiniEngine { auto p = toSDLPoint(); auto r = rect.toSDLRect(); - return SDL_PointInRect(&p, &r); + return ( SDL_PointInRect(&p, &r) == SDL_TRUE ); } ColorMode::ColorMode(int R, int G, int B) @@ -1582,12 +1582,12 @@ namespace MiniEngine bool MusicPlayer::isPlaying() { - return Mix_PlayingMusic(); + return (Mix_PlayingMusic() == 1); } bool MusicPlayer::isPaused() { - return Mix_PausedMusic(); + return (Mix_PausedMusic() == 1); } int MusicPlayer::isFading() diff --git a/MiniEngine.h b/MiniEngine.h index 7898a89..6412c3b 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -7,6 +7,9 @@ #include #include #include + +/// VC++ does not implied C++ exception. Use this to ignore compile warning on this. +#pragma warning (disable:4290) #else /// CodeBlocks (MinGW Compiler) #include diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 3f42628..6f39595 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -34,67 +34,39 @@ Looper::Looper() LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(ev);})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);})); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(lp);})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);})); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback();})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();})); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;})); + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;})); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;})); + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(ev); return 0;})); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(lp); return 0;})); + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(lp); return 0;})); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback();return 0;})); + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback();return 0;})); } LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) @@ -202,6 +174,16 @@ void Looper::reset() updater=[](){}; } +LooperID Looper::_getNextID(const _SDLEventType_& event_type) +{ + LooperID id; + id._looper_cnt = _loop_cnt; + id._type_id = event_type; + + ++_loop_cnt; + return id; +} + Poller::Poller() { idler=[](){}; @@ -223,7 +205,7 @@ void Poller::run() dispatch(); } - /// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call update. + /// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call updater. if(!pollret) idler(); if(_update) { @@ -232,3 +214,38 @@ void Poller::run() } } } + +LooperWithTime::LooperWithTime(int Timeout_ms) +{ + _timeout_ms = Timeout_ms; +} + +void LooperWithTime::setTimeout(int ms) +{ + _timeout_ms = ms; +} + +int LooperWithTime::getTimeout() const +{ + return _timeout_ms; +} + +void LooperWithTime::run() +{ + int timeret = 1; + while (_running) + { + while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) + { + dispatch(); + } + + /// If timeret is not 0 (new event request update), or timeret is 0 (Time out) but Idle function requests update, then call updater. + if (!timeret) idler(); + if (_update) + { + updater(); + _update = false; + } + } +} diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 91217b2..4884bd5 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -60,6 +60,7 @@ public: std::function updater; protected: + LooperID _getNextID(const _SDLEventType_ & event_type); Event _e; bool _running,_update; std::map<_SDLEventType_,std::list>>> _evmap; @@ -75,3 +76,14 @@ public: std::function idler; }; + +class LooperWithTime : public Poller +{ +public: + LooperWithTime(int Timeout_ms=10); + void setTimeout(int ms); + int getTimeout() const; + void run(); +protected: + int _timeout_ms; +}; \ No newline at end of file