added Engine::Object and ag::object::load; filled in ag::loadModel a bit more

git-svn-id: svn://anubis/anaglym/trunk@33 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-09-27 21:01:40 +00:00
parent 8a7e077604
commit 639af8d59f
4 changed files with 119 additions and 23 deletions

62
ag.cc
View File

@ -2,6 +2,7 @@
#include "anaglym.h" #include "anaglym.h"
#include "ag.h" #include "ag.h"
#include "Video.h" #include "Video.h"
#include <SDL.h>
#include "wfobj/WFObj.hh" #include "wfobj/WFObj.hh"
#include <unistd.h> /* usleep() */ #include <unistd.h> /* usleep() */
#include <lua.hpp> #include <lua.hpp>
@ -22,6 +23,7 @@ namespace ag
{ "videoStart", videoStart }, { "videoStart", videoStart },
{ "videoStop", videoStop }, { "videoStop", videoStop },
{ "sleep", sleep }, { "sleep", sleep },
{ "endFrame", endFrame },
{ NULL, NULL } { NULL, NULL }
}; };
luaL_register(L, "ag", functions); luaL_register(L, "ag", functions);
@ -95,28 +97,47 @@ namespace ag
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 1) if (argc == 1 && lua_type(L, -1) == LUA_TSTRING)
{
if (lua_type(L, -1) == LUA_TSTRING)
{ {
string modelname = lua_tostring(L, -1); string modelname = lua_tostring(L, -1);
size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS); size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS);
if (pos == string::npos) if (pos == string::npos)
{ {
string path = g_engine->locateResource(modelname); string path = g_engine->locateResource(modelname + ".obj");
string physpath = g_engine->locateResource(modelname + ".phy");
if (path != "") if (path != "")
{ {
WFObj * obj = new WFObj(); WFObj * obj = new WFObj();
if (!obj->load(path)) if (obj->load(path))
{
int id = g_engine->addObject(obj);
lua_newtable(L);
lua_pushinteger(L, id);
lua_setfield(L, -2, "id");
lua_pushcfunction(L, object::draw);
lua_setfield(L, -2, "draw");
if (physpath != "")
{
/* TODO: ODE info */
#if 0
dGeomID gid =
g_engine->getWorld().loadPhy(physpath);
#endif
}
return 1;
}
else
{
delete obj;
cerr << "error loading object" << endl; cerr << "error loading object" << endl;
GLuint dl = obj->render();
} }
} }
} }
} }
return 0; lua_pushnil(L);
return 1;
} }
int videoStart(lua_State * L) int videoStart(lua_State * L)
@ -146,4 +167,31 @@ namespace ag
return 0; return 0;
} }
int endFrame(lua_State * L)
{
SDL_GL_SwapBuffers();
return 0;
}
namespace object
{
int draw(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 1 && lua_type(L, -1) == LUA_TTABLE)
{
lua_getfield(L, -1, "id");
if (lua_type(L, -1) == LUA_TNUMBER)
{
int id = lua_tointeger(L, -1);
Engine::Object * obj = g_engine->getObject(id);
if (obj != NULL)
obj->draw();
}
lua_pop(L, 1);
}
return 0;
}
}
} }

6
ag.h
View File

@ -13,6 +13,12 @@ namespace ag
int videoStart(lua_State * L); int videoStart(lua_State * L);
int videoStop(lua_State * L); int videoStop(lua_State * L);
int sleep(lua_State * L); int sleep(lua_State * L);
int endFrame(lua_State * L);
namespace object
{
int draw(lua_State * L);
}
} }
#endif #endif

View File

@ -57,22 +57,20 @@ int main(int argc, char * argv[])
return 0; return 0;
} }
void Engine::reportErrors(int status)
{
if (status != 0)
{
cerr << "Engine: Error: " << lua_tostring(m_luaState, -1) << endl;
lua_pop(m_luaState, 1); // remove error message
}
}
Engine::Engine() Engine::Engine()
{ {
m_video = new Video(); m_video = new Video();
m_next_object_index = 1;
} }
Engine::~Engine() Engine::~Engine()
{ {
for (std::map<int, Object *>::iterator it = m_objects.begin();
it != m_objects.end();
it++)
{
delete it->second;
}
delete m_video; delete m_video;
} }
@ -105,6 +103,15 @@ bool Engine::load(const char * program)
lua_close(m_luaState); lua_close(m_luaState);
} }
void Engine::reportErrors(int status)
{
if (status != 0)
{
cerr << "Engine: Error: " << lua_tostring(m_luaState, -1) << endl;
lua_pop(m_luaState, 1); // remove error message
}
}
void Engine::registerLibraries() void Engine::registerLibraries()
{ {
/* Load the Lua string library */ /* Load the Lua string library */
@ -143,6 +150,23 @@ bool Engine::fileExists(const string & path)
return false; return false;
} }
int Engine::addObject(WFObj * obj)
{
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? */
m_objects[id] = o;
m_next_object_index++;
return id;
}
Engine::Object * Engine::getObject(int id)
{
return m_objects.find(id) != m_objects.end() ? m_objects[id] : NULL;
}
void Engine::run() void Engine::run()
{ {
} }

View File

@ -4,13 +4,25 @@
#include <string> #include <string>
#include <lua.hpp> #include <lua.hpp>
#include <map>
#include "Video.h" #include "Video.h"
#include "OdeWorld/OdeWorld.h"
#include "wfobj/WFObj.hh"
#define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" #define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
class Engine class Engine
{ {
public: public:
class Object
{
public:
WFObj * wfobj;
GLuint display_list;
void draw() { glCallList(display_list); }
};
Engine(); Engine();
~Engine(); ~Engine();
@ -19,6 +31,9 @@ class Engine
bool load(const char * program); bool load(const char * program);
void run(); void run();
void reportErrors(int status); void reportErrors(int status);
OdeWorld & getWorld() { return m_world; }
int addObject(WFObj * obj);
Object * getObject(int id);
protected: protected:
void registerLibraries(); void registerLibraries();
@ -27,6 +42,9 @@ class Engine
lua_State * m_luaState; lua_State * m_luaState;
Video * m_video; Video * m_video;
std::string m_program_path; std::string m_program_path;
OdeWorld m_world;
std::map<int, Object *> m_objects;
int m_next_object_index;
}; };
extern Engine * g_engine; extern Engine * g_engine;