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

View File

@ -176,13 +176,10 @@ bool Engine::fileExists(const string & path)
return false;
}
int Engine::addObject(WFObj * obj)
int Engine::addObject(WFObj * obj, bool is_static)
{
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? */
Object * o = createObject(is_static, obj->render());
m_objects[id] = o;
m_next_object_index++;
return id;
@ -327,39 +324,17 @@ void Engine::doPhysics()
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()
{
if (body != 0)
OdeWorld::pushTransform(dBodyGetPosition(body),
dBodyGetRotation(body));
glCallList(display_list);
if (body != 0)
const dReal * pos = m_ode_object->getPosition();
const dReal * rot = m_ode_object->getRotation();
bool transform = pos != NULL && rot != NULL;
if (transform)
OdeWorld::pushTransform(pos, rot);
glCallList(m_display_list);
if (transform)
glPopMatrix();
}

View File

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