From 1ade25beaa564e132c845326da73019f9bd9540d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 24 Oct 2009 01:13:43 +0000 Subject: [PATCH] moved loadPhy() functionality from OdeWorld to Engine::Object git-svn-id: svn://anubis/anaglym/trunk@121 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 100 +++++++++++++++++++++++++++++++++++++++++++++++++----- Engine.h | 5 +-- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/Engine.cc b/Engine.cc index 3c65773..519e900 100644 --- a/Engine.cc +++ b/Engine.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include /* fabs() */ #include @@ -28,10 +29,34 @@ using namespace std; #define doClearHandler(event) \ do { EVENT_PRESENT_FLAG(event) = false; } while(0) -Engine * g_engine; +#define WHITESPACE " \t\r\n\f" +/* Global data */ +Engine * g_engine; SDL_Event Engine::userEvent; +/* static helper functions */ +static string trim(const string & orig) +{ + string result = orig; + size_t pos = result.find_first_not_of(WHITESPACE); + if (pos == string::npos) + { + result = ""; + } + else + { + if (pos > 0) + result = result.substr(pos, result.length() - pos); + pos = result.find_last_not_of(WHITESPACE); + if (pos < result.length() - 1) + result = result.substr(0, pos + 1); + } + return result; +} + +/******** Engine functions ********/ + Engine::Engine(const string & path, Video & video) : m_video(video) { @@ -321,20 +346,17 @@ int Engine::loadModel(const string & name, bool static_data, float scale) if (pos == string::npos) { FileLoader::Path model_path("", name + ".obj"); - string phys_path = locateResource(name + ".phy"); + FileLoader::Path phys_path("", name + ".phy"); WFObj * obj = new WFObj(*m_fileLoader, m_textureCache); 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::Object * engine_obj = getObject(id); - if (engine_obj != NULL) - { - engine_obj->loadPhy(phys_path); - } + engine_obj->loadPhy(phys_path); } return id; } @@ -580,6 +602,8 @@ void Engine::drawObjects() } } +/******** Engine::Object functions ********/ + Engine::Object::Object(bool is_static, OdeWorld & world, GLuint dl, float scale) { m_ode_object = world.createObject(is_static, scale); @@ -614,6 +638,66 @@ Engine::Object::~Object() } } +void Engine::Object::loadPhy(const FileLoader::Path & path) +{ + FileLoader::Buffer buff = g_engine->m_fileLoader->load(path); + if (buff.size <= 0) + return; + string str(buff.data, buff.size); + stringstream istr(str, ios_base::in); + while (!istr.eof()) + { + string line; + getline(istr, line); + line = trim(line); + if (line == "" || line[0] == '#') + continue; + size_t pos = line.find_first_of(WHITESPACE); + if (pos == string::npos) + continue; + string type = line.substr(0, pos); + pos = line.find("\"", pos); + if (pos == string::npos) + continue; + size_t pos2 = line.find("\"", pos + 1); + if (pos2 == string::npos) + continue; + string name = line.substr(pos + 1, pos2 - pos - 1); + pos = pos2 + 1; + vector args; + for (;;) + { + pos = line.find_first_not_of(WHITESPACE, pos); + if (pos == string::npos) + break; + pos2 = line.find_first_of(WHITESPACE, pos); + string n = line.substr(pos, pos2 - pos); + float f = atof(n.c_str()); + args.push_back(f); + if (pos2 == string::npos) + break; + pos = pos2 + 1; + } + if (type == "cube") + { + m_ode_object->addCube(args); + } + else if (type == "sphere") + { + m_ode_object->addSphere(args); + } + else if (type == "cylinder") + { + m_ode_object->addCylinder(args); + } + else if (type == "plane") + { + m_ode_object->addPlane(args); + } + } + m_ode_object->finalize(); +} + void Engine::Object::draw() { if (m_is_visible) diff --git a/Engine.h b/Engine.h index 638c0c2..29d6b65 100644 --- a/Engine.h +++ b/Engine.h @@ -30,10 +30,7 @@ class Engine { m_ode_object->getPosition(x, y, z); } - void loadPhy(const std::string & path) - { - m_ode_object->loadPhy(path); - } + void loadPhy(const FileLoader::Path & path); void setVisible(bool visible) { m_is_visible = visible; } bool getVisible() { return m_is_visible; } void addForce(dReal fx, dReal fy, dReal fz)