moved video start and stop functionality into Engine, calling update() lua function on redraw events

git-svn-id: svn://anubis/anaglym/trunk@47 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-10-05 16:52:12 +00:00
parent 5eac13339f
commit fccfb572e2
3 changed files with 91 additions and 6 deletions

4
ag.cc
View File

@ -24,8 +24,10 @@ namespace ag
{ "println", println }, { "println", println },
{ "loadModel", loadModel }, { "loadModel", loadModel },
{ "loadStaticModel", loadStaticModel }, { "loadStaticModel", loadStaticModel },
#if 0
{ "videoStart", videoStart }, { "videoStart", videoStart },
{ "videoStop", videoStop }, { "videoStop", videoStop },
#endif
{ "sleep", sleep }, { "sleep", sleep },
{ "startFrame", startFrame }, { "startFrame", startFrame },
{ "endFrame", endFrame }, { "endFrame", endFrame },
@ -161,6 +163,7 @@ namespace ag
loadModelSpecify(L, true); loadModelSpecify(L, true);
} }
#if 0
int videoStart(lua_State * L) int videoStart(lua_State * L)
{ {
g_engine->getVideo()->start(); g_engine->getVideo()->start();
@ -172,6 +175,7 @@ namespace ag
g_engine->getVideo()->stop(); g_engine->getVideo()->stop();
return 0; return 0;
} }
#endif
int sleep(lua_State * L) int sleep(lua_State * L)
{ {

View File

@ -17,6 +17,8 @@ static void usage();
Engine * g_engine; Engine * g_engine;
SDL_Event Engine::userEvent;
static void usage() static void usage()
{ {
cerr << "Usage: anaglym [options] program.lua[c]" << endl; cerr << "Usage: anaglym [options] program.lua[c]" << endl;
@ -52,7 +54,7 @@ int main(int argc, char * argv[])
} }
g_engine = new Engine(); g_engine = new Engine();
g_engine->load(program); if (g_engine->load(program))
g_engine->run(); g_engine->run();
delete g_engine; delete g_engine;
@ -72,10 +74,18 @@ Engine::Engine()
m_up[0] = 0; m_up[0] = 0;
m_up[1] = 0; m_up[1] = 0;
m_up[2] = 1; m_up[2] = 1;
m_drawing = false;
/* setup redraw SDL event structure */
userEvent.type = SDL_USEREVENT;
userEvent.user.code = 0;
m_video->start();
} }
Engine::~Engine() Engine::~Engine()
{ {
m_video->stop();
lua_close(m_luaState);
for (std::map<int, Object *>::iterator it = m_objects.begin(); for (std::map<int, Object *>::iterator it = m_objects.begin();
it != m_objects.end(); it != m_objects.end();
it++) it++)
@ -99,7 +109,7 @@ bool Engine::load(const char * program)
int s = luaL_loadfile(m_luaState, program); int s = luaL_loadfile(m_luaState, program);
if ( s == 0 ) if (s == 0)
{ {
// execute Lua program // execute Lua program
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0); s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
@ -107,11 +117,16 @@ bool Engine::load(const char * program)
else else
{ {
cerr << "Warning: problem loading '" << program << "'" << endl; cerr << "Warning: problem loading '" << program << "'" << endl;
return 1; return false;
} }
if (s != 0)
{
reportErrors(s); reportErrors(s);
lua_close(m_luaState); return false;
}
return true;
} }
void Engine::reportErrors(int status) void Engine::reportErrors(int status)
@ -231,8 +246,68 @@ int Engine::setCamera(lua_State * L)
return 0; return 0;
} }
/* called by SDL when the update timer expires */
Uint32 Engine::updateCallback(Uint32 interval, void * param)
{
Engine * engine = (Engine *) param;
return engine->updateCallback(interval);
}
/* member update function to be called by our registered
* SDL callback non-member function */
Uint32 Engine::updateCallback(Uint32 interval)
{
if (!m_drawing)
{
SDL_PushEvent(&userEvent);
}
return interval;
}
void Engine::run() void Engine::run()
{ {
/* register a screen redrawing SDL event */
SDL_AddTimer(20, &updateCallback, this);
SDL_Event event;
while (SDL_WaitEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
goto RET;
break;
}
break;
case SDL_QUIT:
goto RET;
break;
case SDL_USEREVENT:
if (event.user.code == 0)
{
update();
}
break;
}
}
RET:
;
}
void Engine::update()
{
m_drawing = true;
lua_getfield(m_luaState, LUA_GLOBALSINDEX, "update");
if (lua_type(m_luaState, -1) == LUA_TFUNCTION)
{
int s = lua_pcall(m_luaState, 0, 0, 0);
reportErrors(s);
}
lua_pop(m_luaState, 1);
m_drawing = false;
} }
void Engine::Object::loadPhy(const std::string & path, bool static_data) void Engine::Object::loadPhy(const std::string & path, bool static_data)

View File

@ -48,8 +48,13 @@ class Engine
int setCamera(lua_State * L); int setCamera(lua_State * L);
protected: protected:
static Uint32 updateCallback(Uint32 interval, void * param);
static SDL_Event userEvent;
Uint32 updateCallback(Uint32 interval);
void registerLibraries(); void registerLibraries();
bool fileExists(const std::string & path); bool fileExists(const std::string & path);
void update();
lua_State * m_luaState; lua_State * m_luaState;
Video * m_video; Video * m_video;
@ -60,6 +65,7 @@ class Engine
GLdouble m_eye[3]; GLdouble m_eye[3];
GLdouble m_center[3]; GLdouble m_center[3];
GLdouble m_up[3]; GLdouble m_up[3];
bool m_drawing;
}; };
extern Engine * g_engine; extern Engine * g_engine;