added F5 reload script key which will re-load the current script and discover any newly added event handler functions
git-svn-id: svn://anubis/anaglym/trunk@168 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
6de2b93e3d
commit
5bb04ca7f2
1
.todo
1
.todo
@ -1,2 +1 @@
|
||||
- add functions to draw 2d graphics (lines, rectangles)
|
||||
- add a reload engine key that will "import" the current script
|
||||
|
56
Engine.cc
56
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,7 +459,15 @@ 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());
|
||||
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);
|
||||
@ -461,7 +475,9 @@ bool Engine::import(const char * name)
|
||||
return true;
|
||||
reportErrors(s);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "Error " << s << " loading '" << path << "'" << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -728,6 +747,8 @@ 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)
|
||||
{
|
||||
if (!presentFlag) /* only look for events we do not have a handler for */
|
||||
{
|
||||
lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str());
|
||||
if (lua_isfunction(m_luaState, -1))
|
||||
@ -742,6 +763,7 @@ void Engine::checkForFunctionFull(const std::string & lua_fn_name,
|
||||
presentFlag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::doRegisterHandlerFull(int index,
|
||||
const std::string & event_name, bool & presentFlag)
|
||||
@ -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;
|
||||
|
4
Engine.h
4
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<int, Object *> m_objects;
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user