added autoStartFrame and autoEndFrame

git-svn-id: svn://anubis/anaglym/trunk@87 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-10-14 04:03:04 +00:00
parent 3d5a5b1ecb
commit 53e98eb57a
5 changed files with 40 additions and 17 deletions

View File

@ -31,6 +31,8 @@ Engine::Engine(const string & path)
m_up[2] = 1; m_up[2] = 1;
m_drawing = false; m_drawing = false;
m_autoPhysics = true; m_autoPhysics = true;
m_autoStartFrame = true;
m_autoEndFrame = true;
size_t pos = path.find_last_of("\\/"); size_t pos = path.find_last_of("\\/");
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : "."; m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
@ -165,7 +167,7 @@ Engine::Object * Engine::getObject(int id)
return m_objects.find(id) != m_objects.end() ? m_objects[id] : NULL; return m_objects.find(id) != m_objects.end() ? m_objects[id] : NULL;
} }
int Engine::startFrame(lua_State * L) void Engine::startFrame()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -173,13 +175,11 @@ int Engine::startFrame(lua_State * L)
gluLookAt(m_eye[0], m_eye[1], m_eye[2], gluLookAt(m_eye[0], m_eye[1], m_eye[2],
m_center[0], m_center[1], m_center[2], m_center[0], m_center[1], m_center[2],
m_up[0], m_up[1], m_up[2]); m_up[0], m_up[1], m_up[2]);
return 0;
} }
int Engine::endFrame(lua_State * L) void Engine::endFrame()
{ {
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
return 0;
} }
int Engine::setCamera(lua_State * L) int Engine::setCamera(lua_State * L)
@ -272,6 +272,8 @@ void Engine::update()
m_drawing = true; m_drawing = true;
if (m_autoPhysics) if (m_autoPhysics)
doPhysics(); doPhysics();
if (m_autoStartFrame)
startFrame();
lua_getfield(m_luaState, LUA_GLOBALSINDEX, "update"); lua_getfield(m_luaState, LUA_GLOBALSINDEX, "update");
if (lua_type(m_luaState, -1) == LUA_TFUNCTION) if (lua_type(m_luaState, -1) == LUA_TFUNCTION)
{ {
@ -283,6 +285,8 @@ void Engine::update()
{ {
lua_pop(m_luaState, 1); lua_pop(m_luaState, 1);
} }
if (m_autoEndFrame)
endFrame();
m_drawing = false; m_drawing = false;
} }

View File

@ -62,10 +62,14 @@ class Engine
void drawObjects(); void drawObjects();
void setAutoPhysics(bool autoPhysics) { m_autoPhysics = autoPhysics; } void setAutoPhysics(bool autoPhysics) { m_autoPhysics = autoPhysics; }
bool getAutoPhysics() { return m_autoPhysics; } bool getAutoPhysics() { return m_autoPhysics; }
void setAutoStartFrame(bool asf) { m_autoStartFrame = asf; }
bool getAutoStartFrame() { return m_autoStartFrame; }
void setAutoEndFrame(bool asf) { m_autoEndFrame = asf; }
bool getAutoEndFrame() { return m_autoEndFrame; }
void startFrame();
void endFrame();
/* lua services */ /* lua services */
int startFrame(lua_State * L);
int endFrame(lua_State * L);
int setCamera(lua_State * L); int setCamera(lua_State * L);
protected: protected:
@ -93,6 +97,8 @@ class Engine
GLdouble m_up[3]; GLdouble m_up[3];
bool m_drawing; bool m_drawing;
bool m_autoPhysics; bool m_autoPhysics;
bool m_autoStartFrame;
bool m_autoEndFrame;
}; };
extern Engine * g_engine; extern Engine * g_engine;

29
ag.cc
View File

@ -33,6 +33,8 @@ namespace ag
{ "doPhysics", doPhysics }, { "doPhysics", doPhysics },
{ "drawObjects", drawObjects }, { "drawObjects", drawObjects },
{ "setAutoPhysics", setAutoPhysics }, { "setAutoPhysics", setAutoPhysics },
{ "setAutoStartFrame", setAutoStartFrame },
{ "setAutoEndFrame", setAutoEndFrame },
{ NULL, NULL } { NULL, NULL }
}; };
luaL_register(L, "ag", functions); luaL_register(L, "ag", functions);
@ -201,12 +203,14 @@ namespace ag
int startFrame(lua_State * L) int startFrame(lua_State * L)
{ {
return g_engine->startFrame(L); g_engine->startFrame();
return 0;
} }
int endFrame(lua_State * L) int endFrame(lua_State * L)
{ {
return g_engine->endFrame(L); g_engine->endFrame();
return 0;
} }
int setCamera(lua_State * L) int setCamera(lua_State * L)
@ -235,15 +239,24 @@ namespace ag
int setAutoPhysics(lua_State * L) int setAutoPhysics(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 1 && lua_isboolean(L, 1))
if (argc == 1)
{
if (lua_isboolean(L, 1))
{
g_engine->setAutoPhysics(lua_toboolean(L, 1)); g_engine->setAutoPhysics(lua_toboolean(L, 1));
} return 0;
} }
int setAutoStartFrame(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 1 && lua_isboolean(L, 1))
g_engine->setAutoStartFrame(lua_toboolean(L, 1));
return 0;
}
int setAutoEndFrame(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 1 && lua_isboolean(L, 1))
g_engine->setAutoEndFrame(lua_toboolean(L, 1));
return 0; return 0;
} }

2
ag.h
View File

@ -19,6 +19,8 @@ namespace ag
int doPhysics(lua_State * L); int doPhysics(lua_State * L);
int drawObjects(lua_State * L); int drawObjects(lua_State * L);
int setAutoPhysics(lua_State * L); int setAutoPhysics(lua_State * L);
int setAutoStartFrame(lua_State * L);
int setAutoEndFrame(lua_State * L);
namespace object namespace object
{ {

View File

@ -2,9 +2,7 @@
function update() function update()
ballx, bally, ballz = ball:getPosition() ballx, bally, ballz = ball:getPosition()
ag.setCamera(7, -6, 15, ballx, bally, ballz, 0, 0, 1) ag.setCamera(7, -6, 15, ballx, bally, ballz, 0, 0, 1)
ag.startFrame()
ag.drawObjects() ag.drawObjects()
ag.endFrame()
end end
--ag.setCamera(8, -8, 15, -8, 8, 4, 0, 0, 1) --ag.setCamera(8, -8, 15, -8, 8, 4, 0, 0, 1)