diff --git a/.todo b/.todo index 8e81950..bed4164 100644 --- a/.todo +++ b/.todo @@ -1,2 +1 @@ - add functions to draw 2d graphics (lines, rectangles) -- add a reload engine key that will "import" the current script diff --git a/Engine.cc b/Engine.cc index 866ee50..ff23bf7 100644 --- a/Engine.cc +++ b/Engine.cc @@ -137,9 +137,10 @@ Engine::~Engine() bool Engine::load(const char * program) { m_program_path = program; - size_t pos = m_program_path.find_last_of("/\\"); - m_program_path = (pos != string::npos) - ? m_program_path.substr(0, pos) + m_program_directory = program; + size_t pos = m_program_directory.find_last_of("/\\"); + m_program_directory = (pos != string::npos) + ? m_program_directory.substr(0, pos) : "."; string path = locateResource(FONT_NAME); @@ -178,14 +179,7 @@ bool Engine::load(const char * program) return false; } - checkForFunction("init", init); - checkForFunction("update_event", update); - checkForFunction("update_overlay_event", update_overlay); - checkForFunction("key_down_event", key_down); - checkForFunction("key_up_event", key_up); - checkForFunction("mousebutton_down_event", mousebutton_down); - checkForFunction("mousebutton_up_event", mousebutton_up); - checkForFunction("mouse_motion_event", mouse_motion); + checkForAllHandlerFunctions(); if (m_event_init_present) { @@ -199,6 +193,18 @@ bool Engine::load(const char * program) return true; } +void Engine::checkForAllHandlerFunctions() +{ + checkForFunction("init", init); + checkForFunction("update_event", update); + checkForFunction("update_overlay_event", update_overlay); + checkForFunction("key_down_event", key_down); + checkForFunction("key_up_event", key_up); + checkForFunction("mousebutton_down_event", mousebutton_down); + checkForFunction("mousebutton_up_event", mousebutton_up); + checkForFunction("mouse_motion_event", mouse_motion); +} + void Engine::reportErrors(int status) { if (status != 0) @@ -230,7 +236,7 @@ string Engine::locateResource(const string & shortname) string try_path; /* look for the resource relative to the loaded script's directory */ - try_path = m_program_path + "/" + shortname; + try_path = m_program_directory + "/" + shortname; if (fileExists(try_path)) return try_path; @@ -453,19 +459,29 @@ bool Engine::import(const char * name) 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, 0, 0); - if (s == 0) - return true; - reportErrors(s); - } + return importFullPath(file_path.c_str()); } } return false; } +bool Engine::importFullPath(const char * path) +{ + int s = luaL_loadfile(m_luaState, path); + if (s == 0) + { + s = lua_pcall(m_luaState, 0, 0, 0); + if (s == 0) + return true; + reportErrors(s); + } + else + { + cerr << "Error " << s << " loading '" << path << "'" << endl; + } + return false; +} + GLuint Engine::loadTexture(const char * name) { FileLoader::Path path("", name); @@ -555,6 +571,9 @@ void Engine::run() ? SDL_GRAB_OFF : SDL_GRAB_ON); break; + case SDLK_F5: + reloadProgram(); + break; default: key_down_event(event.key.keysym.sym); } @@ -729,17 +748,20 @@ void Engine::mouse_motion_event(int x, int y, int xrel, int yrel) void Engine::checkForFunctionFull(const std::string & lua_fn_name, const std::string & event_name, bool & presentFlag) { - lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str()); - if (lua_isfunction(m_luaState, -1)) + if (!presentFlag) /* only look for events we do not have a handler for */ { - lua_setfield(m_luaState, LUA_GLOBALSINDEX, - (AG_EVENT_PREFIX + event_name).c_str()); - presentFlag = true; - } - else - { - lua_pop(m_luaState, 1); - presentFlag = false; + lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str()); + if (lua_isfunction(m_luaState, -1)) + { + lua_setfield(m_luaState, LUA_GLOBALSINDEX, + (AG_EVENT_PREFIX + event_name).c_str()); + presentFlag = true; + } + else + { + lua_pop(m_luaState, 1); + presentFlag = false; + } } } @@ -751,6 +773,12 @@ void Engine::doRegisterHandlerFull(int index, presentFlag = true; } +void Engine::reloadProgram() +{ + importFullPath(m_program_path.c_str()); + checkForAllHandlerFunctions(); +} + void Engine::doPhysics() { static Uint32 last_updated = 0; diff --git a/Engine.h b/Engine.h index 3ff3677..ca16705 100644 --- a/Engine.h +++ b/Engine.h @@ -133,6 +133,7 @@ class Engine bool isKeyDown(const std::string & key); void exit(); bool import(const char * name); + bool importFullPath(const char * path); GLuint loadTexture(const char * name); void debug_hook(lua_Debug * debug); void getScreenSize(int * width, int * height); @@ -164,12 +165,15 @@ class Engine const std::string & event_name, bool & presentFlag); void doRegisterHandlerFull(int index, const std::string & event_name, bool & presentFlag); + void reloadProgram(); + void checkForAllHandlerFunctions(); Video & m_video; TextureCache m_textureCache; EngineFileLoader * m_fileLoader; lua_State * m_luaState; std::string m_program_path; + std::string m_program_directory; std::string m_engine_path; OdeWorld m_world; std::map m_objects; diff --git a/tests/cannon.lua b/tests/cannon.lua index 9f8a02b..e9ca10e 100644 --- a/tests/cannon.lua +++ b/tests/cannon.lua @@ -34,3 +34,8 @@ function mousebutton_down_event(button) button1() end end + +--function update_overlay_event(width, height) +-- local tw, th = ag.getTextSize("Hi there", 18) +-- ag.drawText("Hi there", 1, 1, 1, 18, width - tw - 4, height - th - 4) +--end