diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 92469ca..3f42628 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -29,6 +29,7 @@ Looper::Looper() { _update=_running=true; _loop_cnt=0; + updater=[](){}; } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { @@ -149,6 +150,11 @@ bool Looper::remove(const LooperID& looperid) return false; } +bool Looper::operator -(const LooperID& looperid) +{ + return remove(looperid); +} + void Looper::dispatch() { for(auto callbackPack:_evmap[_e.type]) @@ -165,6 +171,8 @@ void Looper::run() dispatch(); } + updater(); + _update=false; } } @@ -191,4 +199,36 @@ void Looper::reset() _update=true; _evmap.clear(); _loop_cnt=0; + updater=[](){}; +} + +Poller::Poller() +{ + idler=[](){}; +} + +void Poller::reset() +{ + Looper::reset(); + idler=[](){}; +} + +void Poller::run() +{ + int pollret=1; + while(_running) + { + while(!_update&&(pollret=PollEvent(_e))) + { + 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) idler(); + if(_update) + { + updater(); + _update=false; + } + } } diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 85ac06f..91217b2 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -56,9 +56,22 @@ public: void needstop(); void stop(); void reset(); -private: + + std::function updater; + +protected: Event _e; bool _running,_update; std::map<_SDLEventType_,std::list>>> _evmap; int _loop_cnt; }; + +class Poller : public Looper +{ +public: + Poller(); + void run(); + void reset(); + + std::function idler; +};