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 functions to draw 2d graphics (lines, rectangles)
|
||||||
- add a reload engine key that will "import" the current script
|
|
||||||
|
88
Engine.cc
88
Engine.cc
@ -137,9 +137,10 @@ Engine::~Engine()
|
|||||||
bool Engine::load(const char * program)
|
bool Engine::load(const char * program)
|
||||||
{
|
{
|
||||||
m_program_path = program;
|
m_program_path = program;
|
||||||
size_t pos = m_program_path.find_last_of("/\\");
|
m_program_directory = program;
|
||||||
m_program_path = (pos != string::npos)
|
size_t pos = m_program_directory.find_last_of("/\\");
|
||||||
? m_program_path.substr(0, pos)
|
m_program_directory = (pos != string::npos)
|
||||||
|
? m_program_directory.substr(0, pos)
|
||||||
: ".";
|
: ".";
|
||||||
|
|
||||||
string path = locateResource(FONT_NAME);
|
string path = locateResource(FONT_NAME);
|
||||||
@ -178,14 +179,7 @@ bool Engine::load(const char * program)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForFunction("init", init);
|
checkForAllHandlerFunctions();
|
||||||
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);
|
|
||||||
|
|
||||||
if (m_event_init_present)
|
if (m_event_init_present)
|
||||||
{
|
{
|
||||||
@ -199,6 +193,18 @@ bool Engine::load(const char * program)
|
|||||||
return true;
|
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)
|
void Engine::reportErrors(int status)
|
||||||
{
|
{
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
@ -230,7 +236,7 @@ string Engine::locateResource(const string & shortname)
|
|||||||
string try_path;
|
string try_path;
|
||||||
|
|
||||||
/* look for the resource relative to the loaded script's directory */
|
/* 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))
|
if (fileExists(try_path))
|
||||||
return try_path;
|
return try_path;
|
||||||
|
|
||||||
@ -453,19 +459,29 @@ bool Engine::import(const char * name)
|
|||||||
string file_path = locateResource(nm + ".lua");
|
string file_path = locateResource(nm + ".lua");
|
||||||
if (file_path != "")
|
if (file_path != "")
|
||||||
{
|
{
|
||||||
int s = luaL_loadfile(m_luaState, file_path.c_str());
|
return importFullPath(file_path.c_str());
|
||||||
if (s == 0)
|
|
||||||
{
|
|
||||||
s = lua_pcall(m_luaState, 0, 0, 0);
|
|
||||||
if (s == 0)
|
|
||||||
return true;
|
|
||||||
reportErrors(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
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)
|
GLuint Engine::loadTexture(const char * name)
|
||||||
{
|
{
|
||||||
FileLoader::Path path("", name);
|
FileLoader::Path path("", name);
|
||||||
@ -555,6 +571,9 @@ void Engine::run()
|
|||||||
? SDL_GRAB_OFF
|
? SDL_GRAB_OFF
|
||||||
: SDL_GRAB_ON);
|
: SDL_GRAB_ON);
|
||||||
break;
|
break;
|
||||||
|
case SDLK_F5:
|
||||||
|
reloadProgram();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
key_down_event(event.key.keysym.sym);
|
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,
|
void Engine::checkForFunctionFull(const std::string & lua_fn_name,
|
||||||
const std::string & event_name, bool & presentFlag)
|
const std::string & event_name, bool & presentFlag)
|
||||||
{
|
{
|
||||||
lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str());
|
if (!presentFlag) /* only look for events we do not have a handler for */
|
||||||
if (lua_isfunction(m_luaState, -1))
|
|
||||||
{
|
{
|
||||||
lua_setfield(m_luaState, LUA_GLOBALSINDEX,
|
lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str());
|
||||||
(AG_EVENT_PREFIX + event_name).c_str());
|
if (lua_isfunction(m_luaState, -1))
|
||||||
presentFlag = true;
|
{
|
||||||
}
|
lua_setfield(m_luaState, LUA_GLOBALSINDEX,
|
||||||
else
|
(AG_EVENT_PREFIX + event_name).c_str());
|
||||||
{
|
presentFlag = true;
|
||||||
lua_pop(m_luaState, 1);
|
}
|
||||||
presentFlag = false;
|
else
|
||||||
|
{
|
||||||
|
lua_pop(m_luaState, 1);
|
||||||
|
presentFlag = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -751,6 +773,12 @@ void Engine::doRegisterHandlerFull(int index,
|
|||||||
presentFlag = true;
|
presentFlag = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Engine::reloadProgram()
|
||||||
|
{
|
||||||
|
importFullPath(m_program_path.c_str());
|
||||||
|
checkForAllHandlerFunctions();
|
||||||
|
}
|
||||||
|
|
||||||
void Engine::doPhysics()
|
void Engine::doPhysics()
|
||||||
{
|
{
|
||||||
static Uint32 last_updated = 0;
|
static Uint32 last_updated = 0;
|
||||||
|
4
Engine.h
4
Engine.h
@ -133,6 +133,7 @@ class Engine
|
|||||||
bool isKeyDown(const std::string & key);
|
bool isKeyDown(const std::string & key);
|
||||||
void exit();
|
void exit();
|
||||||
bool import(const char * name);
|
bool import(const char * name);
|
||||||
|
bool importFullPath(const char * path);
|
||||||
GLuint loadTexture(const char * name);
|
GLuint loadTexture(const char * name);
|
||||||
void debug_hook(lua_Debug * debug);
|
void debug_hook(lua_Debug * debug);
|
||||||
void getScreenSize(int * width, int * height);
|
void getScreenSize(int * width, int * height);
|
||||||
@ -164,12 +165,15 @@ class Engine
|
|||||||
const std::string & event_name, bool & presentFlag);
|
const std::string & event_name, bool & presentFlag);
|
||||||
void doRegisterHandlerFull(int index,
|
void doRegisterHandlerFull(int index,
|
||||||
const std::string & event_name, bool & presentFlag);
|
const std::string & event_name, bool & presentFlag);
|
||||||
|
void reloadProgram();
|
||||||
|
void checkForAllHandlerFunctions();
|
||||||
|
|
||||||
Video & m_video;
|
Video & m_video;
|
||||||
TextureCache m_textureCache;
|
TextureCache m_textureCache;
|
||||||
EngineFileLoader * m_fileLoader;
|
EngineFileLoader * m_fileLoader;
|
||||||
lua_State * m_luaState;
|
lua_State * m_luaState;
|
||||||
std::string m_program_path;
|
std::string m_program_path;
|
||||||
|
std::string m_program_directory;
|
||||||
std::string m_engine_path;
|
std::string m_engine_path;
|
||||||
OdeWorld m_world;
|
OdeWorld m_world;
|
||||||
std::map<int, Object *> m_objects;
|
std::map<int, Object *> m_objects;
|
||||||
|
@ -34,3 +34,8 @@ function mousebutton_down_event(button)
|
|||||||
button1()
|
button1()
|
||||||
end
|
end
|
||||||
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