Update Event Loop to do-while style

This commit is contained in:
Kirigaya Kazuto 2017-07-01 20:34:34 +08:00
parent ff862f1dea
commit 1ca4e2bc3f
2 changed files with 56 additions and 52 deletions

View File

@ -99,7 +99,7 @@ LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&)
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void()>& event_callback)
{
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback();return 0;}));
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(); return 0;}));
}
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback)
@ -169,17 +169,20 @@ void Looper::dispatch()
}
void Looper::run()
{
while(_running)
do
{
if(_update)
{
updater();
_update=false;
}
while(!_update&&WaitEvent(_e))
{
dispatch();
}
updater();
_update=false;
}
while(_running);
}
Event Looper::GetLastEvent()
{
@ -189,14 +192,10 @@ void Looper::needupdate()
{
_update=true;
}
void Looper::needstop()
{
_running=false;
}
void Looper::stop()
{
needstop();
needupdate();
_running=false;
_update=true;
}
void Looper::reset()
{
@ -219,20 +218,27 @@ LooperID Looper::_getNextID(const _SDLEventType_& event_type)
Poller::Poller()
{
idler=[](){};
idler=[]() {};
}
void Poller::reset()
{
Looper::reset();
idler=[](){};
idler=[]() {};
}
void Poller::run()
{
int pollret=1;
while(_running)
do
{
if(_update)
{
updater();
_update=false;
}
while(!_update&&(pollret=PollEvent(_e)))
{
dispatch();
@ -240,12 +246,9 @@ void Poller::run()
/// 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)
{
updater();
_update=false;
}
}
while(_running);
}
LooperWithTime::LooperWithTime(int Timeout_ms)
@ -266,8 +269,14 @@ int LooperWithTime::getTimeout() const
void LooperWithTime::run()
{
int timeret = 1;
while (_running)
do
{
if (_update)
{
updater();
_update = false;
}
while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms)))
{
dispatch();
@ -275,12 +284,8 @@ void LooperWithTime::run()
/// 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;
}
}
while (_running);
}
}/// End of namespace MiniEngine

View File

@ -66,7 +66,6 @@ public:
void run();
Event GetLastEvent();
void needupdate();
void needstop();
void stop();
void reset();