diff --git a/Engine.cc b/Engine.cc index 6181fc2..2688652 100644 --- a/Engine.cc +++ b/Engine.cc @@ -1,11 +1,13 @@ #include "ag.h" +#include "anaglym.h" #include "Engine.h" #include #include /* exit() */ #include #include #include +#include #include #include #include /* fabs() */ @@ -34,6 +36,7 @@ Engine::Engine(const string & path) m_autoStartFrame = true; m_autoEndFrame = true; m_autoDrawObjects = true; + m_fileLoader = new EngineFileLoader(this); size_t pos = path.find_last_of("\\/"); m_engine_path = (pos != string::npos) ? string(path, 0, pos) : "."; @@ -52,6 +55,7 @@ Engine::~Engine() { delete it->second; } + delete m_fileLoader; } bool Engine::load(const char * program) @@ -219,6 +223,38 @@ int Engine::setCamera(lua_State * L) return 0; } +int Engine::loadModel(const string & name, bool static_data, float scale) +{ + size_t pos = name.find_first_not_of(FILENAME_SAFE_CHARS); + if (pos == string::npos) + { + FileLoader::Path model_path("", name + ".obj"); + string phys_path = locateResource(name + ".obj"); + + WFObj * obj = new WFObj(*m_fileLoader /* TODO: texture loaders */); + + if (obj->load(model_path)) + { + int id = addObject(obj, static_data, scale); + if (phys_path != "") + { + Engine::Object * engine_obj = getObject(id); + if (engine_obj != NULL) + { + engine_obj->loadPhy(phys_path); + } + } + return id; + } + else + { + delete obj; + cerr << "error loading object" << endl; + } + } + return 0; +} + /* called by SDL when the update timer expires */ Uint32 Engine::updateCallback(Uint32 interval, void * param) { @@ -376,3 +412,50 @@ void Engine::Object::draw() glPopMatrix(); } } + + +/******** Engine::EngineFileLoader functions ********/ + +Engine::EngineFileLoader::EngineFileLoader(Engine * engine) +{ + m_engine = engine; +} + +int Engine::EngineFileLoader::getSize(const Path & path) +{ + struct stat st; + string file_path = resolvePath(path); + + if (file_path == "") + return -1; + if (stat(file_path.c_str(), &st)) + return -2; + return st.st_size; +} + +FileLoader::Buffer Engine::EngineFileLoader::load(const Path & path) +{ + string file_path = resolvePath(path); + int size = getSize(path); + if (size > 0) + { + int fd = open(file_path.c_str(), O_RDONLY); + if (fd > 0) + { + Buffer buf(size); + int num_read = read(fd, buf.data, size); + close(fd); + if (num_read > 0) + return buf; + } + } + return Buffer(0); +} + +string Engine::EngineFileLoader::resolvePath(const Path & path) +{ + string file_path = m_engine->locateResource(path.shortPath); + if (file_path == "") + file_path = path.fullPath; + return file_path; +} diff --git a/Engine.h b/Engine.h index d7e73af..99d14e5 100644 --- a/Engine.h +++ b/Engine.h @@ -7,7 +7,7 @@ #include #include #include "OdeWorld/OdeWorld.h" -#include "wfobj/WFObj.hh" +#include "wfobj/WFObj.h" class Engine { @@ -46,6 +46,17 @@ class Engine bool m_is_scaled; }; + class EngineFileLoader : public FileLoader + { + public: + EngineFileLoader(Engine * engine); + virtual int getSize(const Path & path); + virtual Buffer load(const Path & path); + protected: + std::string resolvePath(const Path & path); + Engine * m_engine; + }; + Engine(const std::string & path); ~Engine(); @@ -70,6 +81,8 @@ class Engine bool getAutoDrawObjects() { return m_autoDrawObjects; } void startFrame(); void endFrame(); + int loadModel(const std::string & name, bool static_data, + float scale = 1.0f); /* lua services */ int setCamera(lua_State * L); @@ -88,6 +101,7 @@ class Engine return new Object(is_static, m_world, display_list, scale); } + EngineFileLoader * m_fileLoader; lua_State * m_luaState; std::string m_program_path; std::string m_engine_path; diff --git a/ag.cc b/ag.cc index 94da5db..ffba979 100644 --- a/ag.cc +++ b/ag.cc @@ -4,7 +4,7 @@ #include "Engine.h" #include "Video.h" #include -#include "wfobj/WFObj.hh" +#include "wfobj/WFObj.h" #include /* usleep() */ #include #include @@ -126,49 +126,25 @@ namespace ag static int loadModelSpecify(lua_State * L, bool static_data) { + bool added = false; float scale = 1.0f; int argc = lua_gettop(L); if (argc >= 2 && lua_isnumber(L, 2)) - { scale = lua_tonumber(L, 2); - } + if (argc >= 1 && lua_isstring(L, 1)) { string modelname = lua_tostring(L, 1); - size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS); - if (pos == string::npos) + int id = g_engine->loadModel(modelname, static_data, scale); + if (id > 0) { - string path = g_engine->locateResource(modelname + ".obj"); - string physpath = g_engine->locateResource(modelname + ".phy"); - if (path != "") - { - WFObj * obj = new WFObj(); - - if (obj->load(path)) - { - int id = g_engine->addObject(obj, static_data, scale); - createLuaObject(L, id); - if (physpath != "") - { - Engine::Object * obj = g_engine->getObject(id); - if (obj != NULL) - { - obj->loadPhy(physpath); - } - } - return 1; - } - else - { - delete obj; - cerr << "error loading object" << endl; - } - } + createLuaObject(L, id); + added = true; } } - - lua_pushnil(L); + if (!added) + lua_pushnil(L); return 1; }