moved loadPhy() functionality from OdeWorld to Engine::Object
git-svn-id: svn://anubis/anaglym/trunk@121 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
8549511901
commit
1ade25beaa
94
Engine.cc
94
Engine.cc
@ -11,6 +11,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <math.h> /* fabs() */
|
#include <math.h> /* fabs() */
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
@ -28,10 +29,34 @@ using namespace std;
|
|||||||
#define doClearHandler(event) \
|
#define doClearHandler(event) \
|
||||||
do { EVENT_PRESENT_FLAG(event) = false; } while(0)
|
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;
|
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)
|
Engine::Engine(const string & path, Video & video)
|
||||||
: m_video(video)
|
: m_video(video)
|
||||||
{
|
{
|
||||||
@ -321,21 +346,18 @@ int Engine::loadModel(const string & name, bool static_data, float scale)
|
|||||||
if (pos == string::npos)
|
if (pos == string::npos)
|
||||||
{
|
{
|
||||||
FileLoader::Path model_path("", name + ".obj");
|
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);
|
WFObj * obj = new WFObj(*m_fileLoader, m_textureCache);
|
||||||
|
|
||||||
if (obj->load(model_path))
|
if (obj->load(model_path))
|
||||||
{
|
{
|
||||||
int id = addObject(obj, static_data, scale);
|
int id = addObject(obj, static_data, scale);
|
||||||
if (phys_path != "")
|
|
||||||
{
|
|
||||||
Engine::Object * engine_obj = getObject(id);
|
Engine::Object * engine_obj = getObject(id);
|
||||||
if (engine_obj != NULL)
|
if (engine_obj != NULL)
|
||||||
{
|
{
|
||||||
engine_obj->loadPhy(phys_path);
|
engine_obj->loadPhy(phys_path);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -580,6 +602,8 @@ void Engine::drawObjects()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/******** Engine::Object functions ********/
|
||||||
|
|
||||||
Engine::Object::Object(bool is_static, OdeWorld & world, GLuint dl, float scale)
|
Engine::Object::Object(bool is_static, OdeWorld & world, GLuint dl, float scale)
|
||||||
{
|
{
|
||||||
m_ode_object = world.createObject(is_static, 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<float> 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()
|
void Engine::Object::draw()
|
||||||
{
|
{
|
||||||
if (m_is_visible)
|
if (m_is_visible)
|
||||||
|
5
Engine.h
5
Engine.h
@ -30,10 +30,7 @@ class Engine
|
|||||||
{
|
{
|
||||||
m_ode_object->getPosition(x, y, z);
|
m_ode_object->getPosition(x, y, z);
|
||||||
}
|
}
|
||||||
void loadPhy(const std::string & path)
|
void loadPhy(const FileLoader::Path & path);
|
||||||
{
|
|
||||||
m_ode_object->loadPhy(path);
|
|
||||||
}
|
|
||||||
void setVisible(bool visible) { m_is_visible = visible; }
|
void setVisible(bool visible) { m_is_visible = visible; }
|
||||||
bool getVisible() { return m_is_visible; }
|
bool getVisible() { return m_is_visible; }
|
||||||
void addForce(dReal fx, dReal fy, dReal fz)
|
void addForce(dReal fx, dReal fy, dReal fz)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user