anaglym/ag.cc
Josh Holtrop ed80f25cee added locateResource() and ag::ag_loadModel()
git-svn-id: svn://anubis/anaglym/trunk@10 99a6e188-d820-4881-8870-2d33a10e2619
2009-09-14 03:58:53 +00:00

112 lines
2.8 KiB
C++

#include "anaglym.h"
#include "ag.h"
#include "wfobj/WFObj.hh"
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
namespace ag
{
void register_functions(lua_State * L)
{
static const luaL_Reg functions[] = {
{ "print", ag_print },
{ "println", ag_println },
{ "loadModel", ag_loadModel },
{ NULL, NULL }
};
luaL_register(L, "ag", functions);
}
static int ag_print_val(lua_State * L, int index)
{
int type = lua_type(L, index);
switch (type)
{
case LUA_TNUMBER:
cout << lua_tonumber(L, index);
break;
case LUA_TSTRING:
cout << lua_tostring(L, index);
break;
case LUA_TTABLE:
cout << "{ ";
/* traverse the table and print the keys/values */
lua_checkstack(L, 3);
lua_pushnil(L);
for (bool first = true; lua_next(L, index) != 0; first = false)
{
if (!first)
cout << ", ";
cout << '[';
ag_print_val(L, lua_gettop(L) - 1);
cout << ']';
cout << " => ";
ag_print_val(L, lua_gettop(L));
lua_pop(L, 1);
}
cout << " }";
break;
case LUA_TFUNCTION:
cout << "<function>";
break;
case LUA_TUSERDATA:
cout << "<userdata>";
break;
case LUA_TTHREAD:
cout << "<thread>";
break;
case LUA_TLIGHTUSERDATA:
cout << "<lightuserdata>";
break;
}
}
int ag_print(lua_State * L)
{
int argc = lua_gettop(L);
for ( int n = 1; n <= argc; n++ )
{
ag_print_val(L, n);
}
return 0;
}
int ag_println(lua_State * L)
{
int ret = ag_print(L);
cout << endl;
return ret;
}
int ag_loadModel(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 1)
{
if (lua_type(L, -1) == LUA_TSTRING)
{
string modelname = lua_tostring(L, -1);
size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS);
if (pos != string::npos)
{
string path = locateResource(modelname);
if (path != "")
{
WFObj * obj = new WFObj();
obj->load(path);
}
}
}
}
return 0;
}
}