From 2081cf7277b6e8a177877839b3d3a2d5ce9513f6 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 1 Nov 2009 21:27:20 +0000 Subject: [PATCH] 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 --- .todo | 2 -- Engine.cc | 47 +++++++++++++++++++++++++++++++++------ Engine.h | 5 ++++- ag.cc | 18 +++++++++++++++ ag.h | 2 ++ tests/managed_objects.lua | 11 +++++++++ tests/rot_camera.lua | 5 +++++ 7 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 tests/rot_camera.lua diff --git a/.todo b/.todo index 684643e..66fc68c 100644 --- a/.todo +++ b/.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 diff --git a/Engine.cc b/Engine.cc index 4fa726a..02c35b7 100644 --- a/Engine.cc +++ b/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; } diff --git a/Engine.h b/Engine.h index 930aaa6..0e2dd9c 100644 --- a/Engine.h +++ b/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 m_keysDown; + SDL_Event m_updateEvent; + SDL_Event m_exitEvent; bool m_event_update_present; bool m_event_key_down_present; diff --git a/ag.cc b/ag.cc index 60ed2ee..11ed01b 100644 --- a/ag.cc +++ b/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 > args) { diff --git a/ag.h b/ag.h index 0c735cb..595f91e 100644 --- a/ag.h +++ b/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); diff --git a/tests/managed_objects.lua b/tests/managed_objects.lua index 0524a0b..74b6d1d 100644 --- a/tests/managed_objects.lua +++ b/tests/managed_objects.lua @@ -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 diff --git a/tests/rot_camera.lua b/tests/rot_camera.lua new file mode 100644 index 0000000..f05a580 --- /dev/null +++ b/tests/rot_camera.lua @@ -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