added Engine::createObject() factory method, using new OdeWorld::Object interface

git-svn-id: svn://anubis/anaglym/trunk@55 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-10-08 04:12:09 +00:00
parent d595278039
commit f260d83d47
3 changed files with 45 additions and 45 deletions

4
ag.cc
View File

@ -117,7 +117,7 @@ namespace ag
if (obj->load(path)) if (obj->load(path))
{ {
int id = g_engine->addObject(obj); int id = g_engine->addObject(obj, static_data);
lua_newtable(L); lua_newtable(L);
lua_pushinteger(L, id); lua_pushinteger(L, id);
lua_setfield(L, -2, "id"); lua_setfield(L, -2, "id");
@ -132,7 +132,7 @@ namespace ag
Engine::Object * obj = g_engine->getObject(id); Engine::Object * obj = g_engine->getObject(id);
if (obj != NULL) if (obj != NULL)
{ {
obj->loadPhy(physpath, static_data); obj->loadPhy(physpath);
} }
} }
return 1; return 1;

View File

@ -176,13 +176,10 @@ bool Engine::fileExists(const string & path)
return false; return false;
} }
int Engine::addObject(WFObj * obj) int Engine::addObject(WFObj * obj, bool is_static)
{ {
int id = m_next_object_index; int id = m_next_object_index;
Object * o = new Object(); Object * o = createObject(is_static, obj->render());
o->wfobj = obj;
o->display_list = obj->render();
/* TODO: stl map insertion valid like this? */
m_objects[id] = o; m_objects[id] = o;
m_next_object_index++; m_next_object_index++;
return id; return id;
@ -327,39 +324,17 @@ void Engine::doPhysics()
last_updated = current_ticks; last_updated = current_ticks;
} }
void Engine::Object::loadPhy(const std::string & path, bool static_data)
{
is_static = static_data;
geoms = g_engine->m_world.loadPhy(path, &body, static_data);
}
void Engine::Object::setPosition(double x, double y, double z)
{
if (body != 0)
{
dBodySetPosition(body, x, y, z);
}
}
void Engine::Object::getPosition(double * x, double * y, double * z)
{
if (body != 0)
{
const dReal * pos = dBodyGetPosition(body);
*x = pos[0];
*y = pos[1];
*z = pos[2];
}
}
void Engine::Object::draw() void Engine::Object::draw()
{ {
if (body != 0) const dReal * pos = m_ode_object->getPosition();
OdeWorld::pushTransform(dBodyGetPosition(body), const dReal * rot = m_ode_object->getRotation();
dBodyGetRotation(body)); bool transform = pos != NULL && rot != NULL;
glCallList(display_list);
if (body != 0) if (transform)
OdeWorld::pushTransform(pos, rot);
glCallList(m_display_list);
if (transform)
glPopMatrix(); glPopMatrix();
} }

View File

@ -17,13 +17,34 @@ class Engine
class Object class Object
{ {
public: public:
Object() { body = 0; is_static = false; } Object(bool is_static, OdeWorld & world, GLuint dl)
WFObj * wfobj; {
GLuint display_list; m_ode_object = world.createObject(is_static);
std::vector<dGeomID> geoms; m_display_list = dl;
dBodyID body; }
~Object()
{
delete m_ode_object;
}
void setPosition(double x, double y, double z)
{
m_ode_object->setPosition(x, y, z);
}
void getPosition(double * x, double * y, double * z)
{
m_ode_object->getPosition(x, y, z);
}
void loadPhy(const std::string & path)
{
m_ode_object->loadPhy(path);
}
void draw(); void draw();
protected:
OdeWorld::Object * m_ode_object;
GLuint m_display_list;
}; };
Engine(); Engine();
@ -35,7 +56,7 @@ class Engine
void run(); void run();
void reportErrors(int status); void reportErrors(int status);
OdeWorld & getWorld() { return m_world; } OdeWorld & getWorld() { return m_world; }
int addObject(WFObj * obj); int addObject(WFObj * obj, bool is_static);
Object * getObject(int id); Object * getObject(int id);
void doPhysics(); void doPhysics();
@ -52,6 +73,10 @@ class Engine
void registerLibraries(); void registerLibraries();
bool fileExists(const std::string & path); bool fileExists(const std::string & path);
void update(); void update();
Object * createObject(bool is_static, GLuint display_list)
{
return new Object(is_static, m_world, display_list);
}
lua_State * m_luaState; lua_State * m_luaState;
Video * m_video; Video * m_video;