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

@ -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()
{
@ -231,8 +230,15 @@ void Poller::reset()
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();