diff --git a/ag.cc b/ag.cc index 49e3f4c..9a15fc9 100644 --- a/ag.cc +++ b/ag.cc @@ -2,6 +2,7 @@ #include "anaglym.h" #include "ag.h" #include "Video.h" +#include #include "wfobj/WFObj.hh" #include /* usleep() */ #include @@ -22,6 +23,7 @@ namespace ag { "videoStart", videoStart }, { "videoStop", videoStop }, { "sleep", sleep }, + { "endFrame", endFrame }, { NULL, NULL } }; luaL_register(L, "ag", functions); @@ -95,28 +97,47 @@ namespace ag { int argc = lua_gettop(L); - if (argc == 1) + if (argc == 1 && lua_type(L, -1) == LUA_TSTRING) { - if (lua_type(L, -1) == LUA_TSTRING) + string modelname = lua_tostring(L, -1); + size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS); + if (pos == string::npos) { - string modelname = lua_tostring(L, -1); - size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS); - if (pos == string::npos) + string path = g_engine->locateResource(modelname + ".obj"); + string physpath = g_engine->locateResource(modelname + ".phy"); + if (path != "") { - string path = g_engine->locateResource(modelname); - if (path != "") - { - WFObj * obj = new WFObj(); + WFObj * obj = new WFObj(); - if (!obj->load(path)) - cerr << "error loading object" << endl; - GLuint dl = obj->render(); + if (obj->load(path)) + { + int id = g_engine->addObject(obj); + lua_newtable(L); + lua_pushinteger(L, id); + lua_setfield(L, -2, "id"); + lua_pushcfunction(L, object::draw); + lua_setfield(L, -2, "draw"); + if (physpath != "") + { + /* TODO: ODE info */ +#if 0 + dGeomID gid = + g_engine->getWorld().loadPhy(physpath); +#endif + } + return 1; + } + else + { + delete obj; + cerr << "error loading object" << endl; } } } } - return 0; + lua_pushnil(L); + return 1; } int videoStart(lua_State * L) @@ -146,4 +167,31 @@ namespace ag return 0; } + + int endFrame(lua_State * L) + { + SDL_GL_SwapBuffers(); + return 0; + } + + namespace object + { + int draw(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_type(L, -1) == LUA_TTABLE) + { + lua_getfield(L, -1, "id"); + if (lua_type(L, -1) == LUA_TNUMBER) + { + int id = lua_tointeger(L, -1); + Engine::Object * obj = g_engine->getObject(id); + if (obj != NULL) + obj->draw(); + } + lua_pop(L, 1); + } + return 0; + } + } } diff --git a/ag.h b/ag.h index 5d94c7f..b1af69c 100644 --- a/ag.h +++ b/ag.h @@ -13,6 +13,12 @@ namespace ag int videoStart(lua_State * L); int videoStop(lua_State * L); int sleep(lua_State * L); + int endFrame(lua_State * L); + + namespace object + { + int draw(lua_State * L); + } } #endif diff --git a/anaglym.cc b/anaglym.cc index 80f9a01..614f1a2 100644 --- a/anaglym.cc +++ b/anaglym.cc @@ -57,22 +57,20 @@ int main(int argc, char * argv[]) return 0; } -void Engine::reportErrors(int status) -{ - if (status != 0) - { - cerr << "Engine: Error: " << lua_tostring(m_luaState, -1) << endl; - lua_pop(m_luaState, 1); // remove error message - } -} - Engine::Engine() { m_video = new Video(); + m_next_object_index = 1; } Engine::~Engine() { + for (std::map::iterator it = m_objects.begin(); + it != m_objects.end(); + it++) + { + delete it->second; + } delete m_video; } @@ -105,6 +103,15 @@ bool Engine::load(const char * program) lua_close(m_luaState); } +void Engine::reportErrors(int status) +{ + if (status != 0) + { + cerr << "Engine: Error: " << lua_tostring(m_luaState, -1) << endl; + lua_pop(m_luaState, 1); // remove error message + } +} + void Engine::registerLibraries() { /* Load the Lua string library */ @@ -143,6 +150,23 @@ bool Engine::fileExists(const string & path) return false; } +int Engine::addObject(WFObj * obj) +{ + int id = m_next_object_index; + Object * o = new Object(); + o->wfobj = obj; + o->display_list = obj->render(); + /* TODO: stl map insertion valid like this? */ + m_objects[id] = o; + m_next_object_index++; + return id; +} + +Engine::Object * Engine::getObject(int id) +{ + return m_objects.find(id) != m_objects.end() ? m_objects[id] : NULL; +} + void Engine::run() { } diff --git a/anaglym.h b/anaglym.h index d711aea..829af9e 100644 --- a/anaglym.h +++ b/anaglym.h @@ -4,13 +4,25 @@ #include #include +#include #include "Video.h" +#include "OdeWorld/OdeWorld.h" +#include "wfobj/WFObj.hh" #define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" class Engine { public: + class Object + { + public: + WFObj * wfobj; + GLuint display_list; + + void draw() { glCallList(display_list); } + }; + Engine(); ~Engine(); @@ -19,14 +31,20 @@ class Engine bool load(const char * program); void run(); void reportErrors(int status); + OdeWorld & getWorld() { return m_world; } + int addObject(WFObj * obj); + Object * getObject(int id); protected: void registerLibraries(); bool fileExists(const std::string & path); lua_State * m_luaState; - Video * m_video; + Video * m_video; std::string m_program_path; + OdeWorld m_world; + std::map m_objects; + int m_next_object_index; }; extern Engine * g_engine;