refactoring for object cloning

git-svn-id: svn://anubis/anaglym/trunk@62 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-10-11 14:32:34 +00:00
parent 9ca4338d32
commit f237390761
5 changed files with 60 additions and 20 deletions

View File

@ -35,7 +35,7 @@ else
GLLIBS := -lGL -lGLU
endif
export CFLAGS := $(LUAINCLUDE) $(SDLINCLUDE) $(ODEINCLUDE) -O2
export CFLAGS := $(LUAINCLUDE) $(SDLINCLUDE) $(ODEINCLUDE) -O2 -Wall
export CXXFLAGS := $(CFLAGS)
export LDFLAGS := $(LUALIBS) $(ODELIBS) $(GLLIBS) $(WINDOWSLIBS) $(SDLLIBS)

50
ag.cc
View File

@ -35,7 +35,7 @@ namespace ag
luaL_register(L, "ag", functions);
}
static int print_val(lua_State * L, int index)
static void print_val(lua_State * L, int index)
{
int type = lua_type(L, index);
switch (type)
@ -99,6 +99,21 @@ namespace ag
return ret;
}
static void createLuaObject(lua_State * L, int id)
{
lua_newtable(L);
lua_pushinteger(L, id);
lua_setfield(L, -2, "id");
lua_pushcfunction(L, object::draw);
lua_setfield(L, -2, "draw");
lua_pushcfunction(L, object::setPosition);
lua_setfield(L, -2, "setPosition");
lua_pushcfunction(L, object::getPosition);
lua_setfield(L, -2, "getPosition");
lua_pushcfunction(L, object::clone);
lua_setfield(L, -2, "clone");
}
static int loadModelSpecify(lua_State * L, bool static_data)
{
int argc = lua_gettop(L);
@ -118,15 +133,7 @@ namespace ag
if (obj->load(path))
{
int id = g_engine->addObject(obj, static_data);
lua_newtable(L);
lua_pushinteger(L, id);
lua_setfield(L, -2, "id");
lua_pushcfunction(L, object::draw);
lua_setfield(L, -2, "draw");
lua_pushcfunction(L, object::setPosition);
lua_setfield(L, -2, "setPosition");
lua_pushcfunction(L, object::getPosition);
lua_setfield(L, -2, "getPosition");
createLuaObject(L, id);
if (physpath != "")
{
Engine::Object * obj = g_engine->getObject(id);
@ -152,12 +159,12 @@ namespace ag
int loadModel(lua_State * L)
{
loadModelSpecify(L, false);
return loadModelSpecify(L, false);
}
int loadStaticModel(lua_State * L)
{
loadModelSpecify(L, true);
return loadModelSpecify(L, true);
}
int sleep(lua_State * L)
@ -279,5 +286,24 @@ namespace ag
}
return 0;
}
int clone(lua_State * L)
{
int argc = lua_gettop(L);
bool found = false;
if (argc == 1)
{
Engine::Object * obj = getObject(L, 1);
if (obj != NULL)
{
found = true;
int id = g_engine->cloneObject(obj);
createLuaObject(L, id);
}
}
if (!found)
lua_pushnil(L);
return 1;
}
}
}

1
ag.h
View File

@ -23,6 +23,7 @@ namespace ag
int draw(lua_State * L);
int setPosition(lua_State * L);
int getPosition(lua_State * L);
int clone(lua_State * L);
}
}

View File

@ -183,10 +183,17 @@ bool Engine::fileExists(const string & path)
int Engine::addObject(WFObj * obj, bool is_static)
{
int id = m_next_object_index;
int id = m_next_object_index++;
Object * o = createObject(is_static, obj->render());
m_objects[id] = o;
m_next_object_index++;
return id;
}
int Engine::cloneObject(const Engine::Object * obj)
{
int id = m_next_object_index++;
Object * o = new Object(*obj);
m_objects[id] = o;
return id;
}
@ -277,11 +284,9 @@ void Engine::run()
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
if (event.key.keysym.sym == SDLK_ESCAPE)
{
case SDLK_ESCAPE:
goto RET;
break;
goto RET;
}
break;
case SDL_QUIT:
@ -323,12 +328,18 @@ void Engine::doPhysics()
if (last_updated > 0)
{
Uint32 msec_steps = current_ticks - last_updated;
for (int i = 0; i < msec_steps; i++)
for (Uint32 i = 0; i < msec_steps; i++)
m_world.step();
}
last_updated = current_ticks;
}
Engine::Object::Object(const Engine::Object & orig)
{
m_display_list = orig.m_display_list;
m_ode_object = new OdeWorld::Object(*m_ode_object);
}
void Engine::Object::draw()
{
const dReal * pos = m_ode_object->getPosition();

View File

@ -22,6 +22,7 @@ class Engine
m_ode_object = world.createObject(is_static);
m_display_list = dl;
}
Object(const Object & orig);
~Object()
{
delete m_ode_object;
@ -57,6 +58,7 @@ class Engine
void reportErrors(int status);
OdeWorld & getWorld() { return m_world; }
int addObject(WFObj * obj, bool is_static);
int cloneObject(const Object * obj);
Object * getObject(int id);
void doPhysics();