added ag::exit() and ag::import() to exit the lua script and import other lua modules
git-svn-id: svn://anubis/anaglym/trunk@146 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
f8b15d60ff
commit
2081cf7277
2
.todo
2
.todo
@ -1,5 +1,3 @@
|
||||
- add lua interface to import modules
|
||||
- add lua interface to exit program
|
||||
- add plane constructor taking 4 parameters for (a,b,c,d)
|
||||
- support loading and applying textures for managed objects
|
||||
- debug hooks for loading and execution to halt infinite lua loops
|
||||
|
47
Engine.cc
47
Engine.cc
@ -33,7 +33,6 @@ using namespace std;
|
||||
|
||||
/* Global data */
|
||||
Engine * g_engine;
|
||||
SDL_Event Engine::userEvent;
|
||||
|
||||
/* static helper functions */
|
||||
static string trim(const string & orig)
|
||||
@ -80,9 +79,11 @@ Engine::Engine(const string & path, Video & video)
|
||||
size_t pos = path.find_last_of("\\/");
|
||||
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
|
||||
|
||||
/* setup redraw SDL event structure */
|
||||
userEvent.type = SDL_USEREVENT;
|
||||
userEvent.user.code = 0;
|
||||
/* setup SDL user event structures */
|
||||
m_updateEvent.type = SDL_USEREVENT;
|
||||
m_updateEvent.user.code = 0;
|
||||
m_exitEvent.type = SDL_USEREVENT;
|
||||
m_exitEvent.user.code = 1;
|
||||
|
||||
m_event_update_present = false;
|
||||
m_event_key_down_present = false;
|
||||
@ -383,6 +384,33 @@ bool Engine::isKeyDown(const std::string & key)
|
||||
return m_keysDown.find(key) != m_keysDown.end();
|
||||
}
|
||||
|
||||
void Engine::exit()
|
||||
{
|
||||
SDL_PushEvent(&m_exitEvent);
|
||||
}
|
||||
|
||||
bool Engine::import(const char * name)
|
||||
{
|
||||
string nm(name);
|
||||
size_t pos = nm.find_first_not_of(FILENAME_SAFE_CHARS);
|
||||
if (pos == string::npos)
|
||||
{
|
||||
string file_path = locateResource(nm + ".lua");
|
||||
if (file_path != "")
|
||||
{
|
||||
int s = luaL_loadfile(m_luaState, file_path.c_str());
|
||||
if (s == 0)
|
||||
{
|
||||
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
|
||||
if (s == 0)
|
||||
return true;
|
||||
reportErrors(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* called by SDL when the update timer expires */
|
||||
Uint32 Engine::updateCallback(Uint32 interval, void * param)
|
||||
{
|
||||
@ -396,7 +424,7 @@ Uint32 Engine::updateCallback(Uint32 interval)
|
||||
{
|
||||
if (!m_drawing)
|
||||
{
|
||||
SDL_PushEvent(&userEvent);
|
||||
SDL_PushEvent(&m_updateEvent);
|
||||
}
|
||||
return interval;
|
||||
}
|
||||
@ -452,9 +480,14 @@ void Engine::run()
|
||||
goto RET;
|
||||
break;
|
||||
case SDL_USEREVENT:
|
||||
if (event.user.code == 0)
|
||||
switch (event.user.code)
|
||||
{
|
||||
update_event();
|
||||
case 0:
|
||||
update_event();
|
||||
break;
|
||||
case 1:
|
||||
goto RET;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
5
Engine.h
5
Engine.h
@ -128,6 +128,8 @@ class Engine
|
||||
int loadModel(const std::string & name, bool static_data,
|
||||
float scale = 1.0f);
|
||||
bool isKeyDown(const std::string & key);
|
||||
void exit();
|
||||
bool import(const char * name);
|
||||
|
||||
/* lua services */
|
||||
int setCamera(lua_State * L);
|
||||
@ -137,7 +139,6 @@ class Engine
|
||||
|
||||
protected:
|
||||
static Uint32 updateCallback(Uint32 interval, void * param);
|
||||
static SDL_Event userEvent;
|
||||
|
||||
Uint32 updateCallback(Uint32 interval);
|
||||
void registerLibraries();
|
||||
@ -171,6 +172,8 @@ class Engine
|
||||
bool m_autoEndFrame;
|
||||
bool m_autoDrawObjects;
|
||||
std::map<std::string, bool> m_keysDown;
|
||||
SDL_Event m_updateEvent;
|
||||
SDL_Event m_exitEvent;
|
||||
|
||||
bool m_event_update_present;
|
||||
bool m_event_key_down_present;
|
||||
|
18
ag.cc
18
ag.cc
@ -40,6 +40,8 @@ namespace ag
|
||||
{ "isKeyDown", isKeyDown },
|
||||
{ "registerEventHandler", registerEventHandler },
|
||||
{ "clearEventHandler", clearEventHandler },
|
||||
{ "exit", exit },
|
||||
{ "import", import },
|
||||
|
||||
{ "createBox", createBox},
|
||||
{ "createStaticBox", createStaticBox},
|
||||
@ -298,6 +300,22 @@ namespace ag
|
||||
return g_engine->clearEventHandler(L);
|
||||
}
|
||||
|
||||
int exit(lua_State * L)
|
||||
{
|
||||
g_engine->exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int import(lua_State * L)
|
||||
{
|
||||
int argc = lua_gettop(L);
|
||||
if (argc == 1 && lua_isstring(L, 1))
|
||||
lua_pushboolean(L, g_engine->import(lua_tostring(L, 1)));
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void addManagedObject(lua_State * L, bool is_static,
|
||||
OdeWorld::GeomType geom_type, refptr< vector<float> > args)
|
||||
{
|
||||
|
2
ag.h
2
ag.h
@ -26,6 +26,8 @@ namespace ag
|
||||
int isKeyDown(lua_State * L);
|
||||
int registerEventHandler(lua_State * L);
|
||||
int clearEventHandler(lua_State * L);
|
||||
int exit(lua_State * L);
|
||||
int import(lua_State * L);
|
||||
|
||||
int createBox(lua_State * L);
|
||||
int createStaticBox(lua_State * L);
|
||||
|
@ -3,11 +3,20 @@ ground = ag.createStaticPlane(0, 0, 0, 0, 0, 0)
|
||||
ground:setColor(0.2, 1.0, 0.2)
|
||||
ag.setCamera(10, -10, 10, 0, 0, 0)
|
||||
|
||||
if (ag.import("rot_camera") ~= true) then
|
||||
ag.println("error importing rot_camera")
|
||||
ag.exit()
|
||||
end
|
||||
|
||||
function init(obj)
|
||||
obj:setColor(math.random(), math.random(), math.random())
|
||||
obj:setPosition((math.random() - 0.5) * 10, (math.random() - 0.5) * 10, 10)
|
||||
end
|
||||
|
||||
function update_event()
|
||||
rot_camera()
|
||||
end
|
||||
|
||||
function key_down_event(key)
|
||||
if (key == "b") then
|
||||
box = ag.createBox(1, 1, 1)
|
||||
@ -21,5 +30,7 @@ function key_down_event(key)
|
||||
elseif (key == "s") then
|
||||
sphere = ag.createSphere(0.8)
|
||||
init(sphere)
|
||||
elseif (key == "q") then
|
||||
ag.exit()
|
||||
end
|
||||
end
|
||||
|
5
tests/rot_camera.lua
Normal file
5
tests/rot_camera.lua
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
function rot_camera()
|
||||
local angle = ag.elapsedTime() / 5000 * math.pi
|
||||
ag.setCamera(15 * math.cos(angle), 15 * math.sin(angle), 12, 0, 0, 2)
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user