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() void Looper::run()
{ {
while(_running) do
{ {
if(_update)
{
updater();
_update=false;
}
while(!_update&&WaitEvent(_e)) while(!_update&&WaitEvent(_e))
{ {
dispatch(); dispatch();
} }
updater();
_update=false;
} }
while(_running);
} }
Event Looper::GetLastEvent() Event Looper::GetLastEvent()
{ {
@ -189,14 +192,10 @@ void Looper::needupdate()
{ {
_update=true; _update=true;
} }
void Looper::needstop()
{
_running=false;
}
void Looper::stop() void Looper::stop()
{ {
needstop(); _running=false;
needupdate(); _update=true;
} }
void Looper::reset() void Looper::reset()
{ {
@ -231,8 +230,15 @@ void Poller::reset()
void Poller::run() void Poller::run()
{ {
int pollret=1; int pollret=1;
while(_running)
do
{ {
if(_update)
{
updater();
_update=false;
}
while(!_update&&(pollret=PollEvent(_e))) while(!_update&&(pollret=PollEvent(_e)))
{ {
dispatch(); 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 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(!pollret) idler();
if(_update)
{
updater();
_update=false;
}
} }
while(_running);
} }
LooperWithTime::LooperWithTime(int Timeout_ms) LooperWithTime::LooperWithTime(int Timeout_ms)
@ -266,8 +269,14 @@ int LooperWithTime::getTimeout() const
void LooperWithTime::run() void LooperWithTime::run()
{ {
int timeret = 1; int timeret = 1;
while (_running) do
{ {
if (_update)
{
updater();
_update = false;
}
while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms)))
{ {
dispatch(); 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 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 (!timeret) idler();
if (_update)
{
updater();
_update = false;
}
} }
while (_running);
} }
}/// End of namespace MiniEngine }/// End of namespace MiniEngine

View File

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