From 2ecddc44e79682f84e864a22018603d0dd0312b4 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 20:51:56 +0800 Subject: [PATCH] Add Poller Add implement of Looper::operator - --- MiniEngine_Event.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ MiniEngine_Event.h | 15 ++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) 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; +};