196 lines
5.0 KiB
C++
196 lines
5.0 KiB
C++
|
|
#include "anaglym.h"
|
|
#include "ag.h"
|
|
#include "Video.h"
|
|
#include <SDL.h>
|
|
#include "wfobj/WFObj.hh"
|
|
#include <unistd.h> /* usleep() */
|
|
#include <lua.hpp>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
using namespace std;
|
|
|
|
namespace ag
|
|
{
|
|
void register_functions(lua_State * L)
|
|
{
|
|
static const luaL_Reg functions[] = {
|
|
{ "print", print },
|
|
{ "println", println },
|
|
{ "loadModel", loadModel },
|
|
{ "videoStart", videoStart },
|
|
{ "videoStop", videoStop },
|
|
{ "sleep", sleep },
|
|
{ "endFrame", endFrame },
|
|
{ NULL, NULL }
|
|
};
|
|
luaL_register(L, "ag", functions);
|
|
}
|
|
|
|
static int 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 << '[';
|
|
print_val(L, lua_gettop(L) - 1);
|
|
cout << ']';
|
|
cout << " => ";
|
|
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 print(lua_State * L)
|
|
{
|
|
int argc = lua_gettop(L);
|
|
|
|
for ( int n = 1; n <= argc; n++ )
|
|
{
|
|
print_val(L, n);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int println(lua_State * L)
|
|
{
|
|
int ret = print(L);
|
|
cout << endl;
|
|
return ret;
|
|
}
|
|
|
|
int loadModel(lua_State * L)
|
|
{
|
|
int argc = lua_gettop(L);
|
|
|
|
if (argc == 1 && 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 = g_engine->locateResource(modelname + ".obj");
|
|
string physpath = g_engine->locateResource(modelname + ".phy");
|
|
if (path != "")
|
|
{
|
|
WFObj * obj = new WFObj();
|
|
|
|
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 != "")
|
|
{
|
|
g_engine->getObject(id)->loadPhy(physpath);
|
|
}
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
delete obj;
|
|
cerr << "error loading object" << endl;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
return 1;
|
|
}
|
|
|
|
int videoStart(lua_State * L)
|
|
{
|
|
g_engine->getVideo()->start();
|
|
return 0;
|
|
}
|
|
|
|
int videoStop(lua_State * L)
|
|
{
|
|
g_engine->getVideo()->stop();
|
|
return 0;
|
|
}
|
|
|
|
int sleep(lua_State * L)
|
|
{
|
|
int argc = lua_gettop(L);
|
|
|
|
if (argc == 1)
|
|
{
|
|
if (lua_type(L, -1) == LUA_TNUMBER)
|
|
{
|
|
double seconds = lua_tonumber(L, -1);
|
|
usleep((useconds_t) (seconds * 1000000));
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|