processing events in the engine thread
git-svn-id: svn://anubis/anaglym/trunk@80 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
917a226e0f
commit
59818a253d
32
anaglym.cc
32
anaglym.cc
@ -22,7 +22,7 @@ static void update();
|
|||||||
static void addEvent(const Event & event);
|
static void addEvent(const Event & event);
|
||||||
static Uint32 updateCallback(Uint32 interval, void * param);
|
static Uint32 updateCallback(Uint32 interval, void * param);
|
||||||
|
|
||||||
static bool lastDrawCompleted;
|
static bool lastUpdateCompleted;
|
||||||
static bool engine_running;
|
static bool engine_running;
|
||||||
static SDL_Event userEvent;
|
static SDL_Event userEvent;
|
||||||
static SDL_cond * event_condition;
|
static SDL_cond * event_condition;
|
||||||
@ -46,6 +46,31 @@ static int engine_thread(void * param)
|
|||||||
{
|
{
|
||||||
SDL_mutexP(event_mutex);
|
SDL_mutexP(event_mutex);
|
||||||
SDL_CondWait(event_condition, event_mutex);
|
SDL_CondWait(event_condition, event_mutex);
|
||||||
|
bool moreEvents = true;
|
||||||
|
while (moreEvents)
|
||||||
|
{
|
||||||
|
Event event;
|
||||||
|
SDL_mutexP(event_queue_mutex);
|
||||||
|
if (event_queue.size() < 1)
|
||||||
|
moreEvents = false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
event = event_queue.front();
|
||||||
|
event_queue.pop();
|
||||||
|
}
|
||||||
|
SDL_mutexV(event_queue_mutex);
|
||||||
|
if (moreEvents)
|
||||||
|
{
|
||||||
|
/* process the event */
|
||||||
|
switch (event.type)
|
||||||
|
{
|
||||||
|
case EVENT_UPDATE:
|
||||||
|
g_engine->update();
|
||||||
|
lastUpdateCompleted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
engine_running = false;
|
engine_running = false;
|
||||||
@ -82,7 +107,7 @@ int main(int argc, char * argv[])
|
|||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
lastDrawCompleted = true;
|
lastUpdateCompleted = true;
|
||||||
engine_running = true;
|
engine_running = true;
|
||||||
/* setup SDL update event */
|
/* setup SDL update event */
|
||||||
userEvent.type = SDL_USEREVENT;
|
userEvent.type = SDL_USEREVENT;
|
||||||
@ -114,7 +139,7 @@ int main(int argc, char * argv[])
|
|||||||
/* called by SDL when the update timer expires */
|
/* called by SDL when the update timer expires */
|
||||||
static Uint32 updateCallback(Uint32 interval, void * param)
|
static Uint32 updateCallback(Uint32 interval, void * param)
|
||||||
{
|
{
|
||||||
if (lastDrawCompleted)
|
if (lastUpdateCompleted)
|
||||||
SDL_PushEvent(&userEvent);
|
SDL_PushEvent(&userEvent);
|
||||||
return interval;
|
return interval;
|
||||||
}
|
}
|
||||||
@ -157,6 +182,7 @@ static void update()
|
|||||||
{
|
{
|
||||||
Event update_event;
|
Event update_event;
|
||||||
update_event.type = EVENT_UPDATE;
|
update_event.type = EVENT_UPDATE;
|
||||||
|
lastUpdateCompleted = false;
|
||||||
addEvent(update_event);
|
addEvent(update_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user