From f237390761a0b01a53042735d29aa77d9d23a472 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Oct 2009 14:32:34 +0000 Subject: [PATCH] refactoring for object cloning git-svn-id: svn://anubis/anaglym/trunk@62 99a6e188-d820-4881-8870-2d33a10e2619 --- Makefile | 2 +- ag.cc | 50 ++++++++++++++++++++++++++++++++++++++------------ ag.h | 1 + anaglym.cc | 25 ++++++++++++++++++------- anaglym.h | 2 ++ 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 635ff84..60c2806 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/ag.cc b/ag.cc index 3f845d8..8215b3c 100644 --- a/ag.cc +++ b/ag.cc @@ -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; + } } } diff --git a/ag.h b/ag.h index a779a2a..5979475 100644 --- a/ag.h +++ b/ag.h @@ -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); } } diff --git a/anaglym.cc b/anaglym.cc index 7adad70..ef47efc 100644 --- a/anaglym.cc +++ b/anaglym.cc @@ -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(); diff --git a/anaglym.h b/anaglym.h index 8f10705..fba18a6 100644 --- a/anaglym.h +++ b/anaglym.h @@ -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();