maintaining an event queue for passing events to the engine thread from the main SDL thread

git-svn-id: svn://anubis/anaglym/trunk@79 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-10-14 01:20:58 +00:00
parent a630609767
commit 917a226e0f
3 changed files with 57 additions and 11 deletions

View File

@ -28,7 +28,6 @@ Engine::Engine(const string & path)
m_up[0] = 0;
m_up[1] = 0;
m_up[2] = 1;
m_drawing = false;
m_autoPhysics = true;
size_t pos = path.find_last_of("\\/");
@ -215,7 +214,6 @@ int Engine::setCamera(lua_State * L)
void Engine::update()
{
m_drawing = true;
if (m_autoPhysics)
doPhysics();
lua_getfield(m_luaState, LUA_GLOBALSINDEX, "update");
@ -229,7 +227,6 @@ void Engine::update()
{
lua_pop(m_luaState, 1);
}
m_drawing = false;
}
void Engine::doPhysics()

View File

@ -60,6 +60,7 @@ class Engine
void drawObjects();
void setAutoPhysics(bool autoPhysics) { m_autoPhysics = autoPhysics; }
bool getAutoPhysics() { return m_autoPhysics; }
void update();
/* lua services */
int startFrame(lua_State * L);
@ -69,7 +70,6 @@ class Engine
protected:
void registerLibraries();
bool fileExists(const std::string & path);
void update();
Object * createObject(bool is_static, GLuint display_list,
float scale = 1.0f)
{
@ -85,7 +85,6 @@ class Engine
GLdouble m_eye[3];
GLdouble m_center[3];
GLdouble m_up[3];
bool m_drawing;
bool m_autoPhysics;
};

View File

@ -6,14 +6,29 @@
#include <stdlib.h> /* exit() */
#include <iostream>
#include <string>
#include <queue>
using namespace std;
enum EventType { EVENT_UPDATE };
typedef struct
{
EventType type;
} Event;
static void usage();
static void mainloop();
static void update();
static void addEvent(const Event & event);
static Uint32 updateCallback(Uint32 interval, void * param);
static bool lastDrawCompleted;
static bool engine_running;
static SDL_Event userEvent;
static SDL_cond * event_condition;
static SDL_mutex * event_mutex;
static SDL_mutex * event_queue_mutex;
static queue<Event> event_queue;
Uint32 g_ticks;
static void usage()
@ -22,6 +37,21 @@ static void usage()
exit(42);
}
static int engine_thread(void * param)
{
const char * program = (const char *) param;
if (g_engine->load(program))
{
for (;;)
{
SDL_mutexP(event_mutex);
SDL_CondWait(event_condition, event_mutex);
}
}
engine_running = false;
return 0;
}
int main(int argc, char * argv[])
{
const char * program = NULL;
@ -53,9 +83,13 @@ int main(int argc, char * argv[])
}
lastDrawCompleted = true;
engine_running = true;
/* setup SDL update event */
userEvent.type = SDL_USEREVENT;
userEvent.user.code = 0;
event_condition = SDL_CreateCond();
event_mutex = SDL_CreateMutex();
event_queue_mutex = SDL_CreateMutex();
Video video;
#if 0
@ -66,12 +100,12 @@ int main(int argc, char * argv[])
#endif
g_engine = new Engine(argv[0]);
if (g_engine->load(program))
{
mainloop();
}
delete g_engine;
SDL_CreateThread(engine_thread, (void *) program);
mainloop();
SDL_DestroyCond(event_condition);
delete g_engine;
video.stop();
return 0;
@ -94,6 +128,8 @@ static void mainloop()
while (SDL_WaitEvent(&event))
{
if (!engine_running)
goto RET;
switch (event.type)
{
case SDL_KEYDOWN:
@ -108,7 +144,7 @@ static void mainloop()
case SDL_USEREVENT:
if (event.user.code == 0)
{
/* TODO: update */
update();
}
break;
}
@ -117,3 +153,17 @@ RET:
;
}
static void update()
{
Event update_event;
update_event.type = EVENT_UPDATE;
addEvent(update_event);
}
static void addEvent(const Event & event)
{
SDL_mutexP(event_queue_mutex);
event_queue.push(event);
SDL_mutexV(event_queue_mutex);
SDL_CondSignal(event_condition);
}