restructured events a bit for efficiency
git-svn-id: svn://anubis/anaglym/trunk@110 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
3aa41bedd8
commit
978293a57e
74
Engine.cc
74
Engine.cc
@ -16,6 +16,10 @@
|
|||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
#define checkForFunction(lua_name, event) \
|
||||||
|
checkForFunctionFull(lua_name, #event, m_event_ ## event ## _present)
|
||||||
|
#define AG_EVENT_PREFIX "__ag_event_"
|
||||||
|
|
||||||
Engine * g_engine;
|
Engine * g_engine;
|
||||||
|
|
||||||
SDL_Event Engine::userEvent;
|
SDL_Event Engine::userEvent;
|
||||||
@ -45,6 +49,13 @@ Engine::Engine(const string & path)
|
|||||||
/* setup redraw SDL event structure */
|
/* setup redraw SDL event structure */
|
||||||
userEvent.type = SDL_USEREVENT;
|
userEvent.type = SDL_USEREVENT;
|
||||||
userEvent.user.code = 0;
|
userEvent.user.code = 0;
|
||||||
|
|
||||||
|
m_event_update_present = false;
|
||||||
|
m_event_key_pressed_present = false;
|
||||||
|
m_event_key_released_present = false;
|
||||||
|
m_event_mousebutton_pressed_present = false;
|
||||||
|
m_event_mousebutton_released_present = false;
|
||||||
|
m_event_mouse_motion_present = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine()
|
Engine::~Engine()
|
||||||
@ -90,6 +101,13 @@ bool Engine::load(const char * program)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkForFunction("update_event", update);
|
||||||
|
checkForFunction("key_pressed_event", key_pressed);
|
||||||
|
checkForFunction("key_released_event", key_released);
|
||||||
|
checkForFunction("mousebutton_pressed_event", mousebutton_pressed);
|
||||||
|
checkForFunction("mousebutton_released_event", mousebutton_released);
|
||||||
|
checkForFunction("mouse_motion_event", mouse_motion);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,10 +316,10 @@ void Engine::run()
|
|||||||
{
|
{
|
||||||
goto RET;
|
goto RET;
|
||||||
}
|
}
|
||||||
key_event(event.key.keysym.sym, true);
|
key_pressed_event(event.key.keysym.sym);
|
||||||
break;
|
break;
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
key_event(event.key.keysym.sym, false);
|
key_released_event(event.key.keysym.sym);
|
||||||
break;
|
break;
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
goto RET;
|
goto RET;
|
||||||
@ -325,17 +343,14 @@ void Engine::update()
|
|||||||
doPhysics();
|
doPhysics();
|
||||||
if (m_autoStartFrame)
|
if (m_autoStartFrame)
|
||||||
startFrame();
|
startFrame();
|
||||||
lua_getfield(m_luaState, LUA_GLOBALSINDEX, "update_event");
|
if (m_event_update_present)
|
||||||
if (lua_type(m_luaState, -1) == LUA_TFUNCTION)
|
|
||||||
{
|
{
|
||||||
|
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
|
||||||
|
AG_EVENT_PREFIX "update_event");
|
||||||
/* call the update function - pops the function ref from the stack */
|
/* call the update function - pops the function ref from the stack */
|
||||||
int s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
|
int s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
|
||||||
reportErrors(s);
|
reportErrors(s);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_pop(m_luaState, 1);
|
|
||||||
}
|
|
||||||
if (m_autoDrawObjects)
|
if (m_autoDrawObjects)
|
||||||
drawObjects();
|
drawObjects();
|
||||||
if (m_autoEndFrame)
|
if (m_autoEndFrame)
|
||||||
@ -343,25 +358,50 @@ void Engine::update()
|
|||||||
m_drawing = false;
|
m_drawing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::key_event(int keysym, bool pressed)
|
void Engine::key_pressed_event(int keysym)
|
||||||
{
|
{
|
||||||
if (pressed)
|
m_keysDown[sdl_keymap[keysym]] = true;
|
||||||
m_keysDown[sdl_keymap[keysym]] = true;
|
if (m_event_key_pressed_present)
|
||||||
else
|
|
||||||
m_keysDown.erase(sdl_keymap[keysym]);
|
|
||||||
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
|
|
||||||
pressed ? "key_pressed_event" : "key_released_event");
|
|
||||||
if (lua_type(m_luaState, -1) == LUA_TFUNCTION)
|
|
||||||
{
|
{
|
||||||
|
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
|
||||||
|
AG_EVENT_PREFIX "key_pressed");
|
||||||
lua_pushstring(m_luaState, sdl_keymap[keysym]);
|
lua_pushstring(m_luaState, sdl_keymap[keysym]);
|
||||||
/* call the key_event function
|
/* call the key pressed event function
|
||||||
* This pops the function ref and arguments from the stack */
|
* This pops the function ref and arguments from the stack */
|
||||||
int s = lua_pcall(m_luaState, 1, LUA_MULTRET, 0);
|
int s = lua_pcall(m_luaState, 1, LUA_MULTRET, 0);
|
||||||
reportErrors(s);
|
reportErrors(s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::key_released_event(int keysym)
|
||||||
|
{
|
||||||
|
m_keysDown.erase(sdl_keymap[keysym]);
|
||||||
|
if (m_event_key_pressed_present)
|
||||||
|
{
|
||||||
|
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
|
||||||
|
AG_EVENT_PREFIX "key_released");
|
||||||
|
lua_pushstring(m_luaState, sdl_keymap[keysym]);
|
||||||
|
/* call the key released event function
|
||||||
|
* This pops the function ref and arguments from the stack */
|
||||||
|
int s = lua_pcall(m_luaState, 1, LUA_MULTRET, 0);
|
||||||
|
reportErrors(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
lua_setfield(m_luaState, LUA_GLOBALSINDEX,
|
||||||
|
(AG_EVENT_PREFIX + event_name).c_str());
|
||||||
|
presentFlag = true;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lua_pop(m_luaState, 1);
|
lua_pop(m_luaState, 1);
|
||||||
|
presentFlag = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
Engine.h
12
Engine.h
@ -117,12 +117,15 @@ class Engine
|
|||||||
void registerLibraries();
|
void registerLibraries();
|
||||||
bool fileExists(const std::string & path);
|
bool fileExists(const std::string & path);
|
||||||
void update();
|
void update();
|
||||||
void key_event(int keysym, bool pressed);
|
void key_pressed_event(int keysym);
|
||||||
|
void key_released_event(int keysym);
|
||||||
Object * createObject(bool is_static, GLuint display_list,
|
Object * createObject(bool is_static, GLuint display_list,
|
||||||
float scale = 1.0f)
|
float scale = 1.0f)
|
||||||
{
|
{
|
||||||
return new Object(is_static, m_world, display_list, scale);
|
return new Object(is_static, m_world, display_list, scale);
|
||||||
}
|
}
|
||||||
|
void checkForFunctionFull(const std::string & lua_fn_name,
|
||||||
|
const std::string & event_name, bool & presentFlag);
|
||||||
|
|
||||||
TextureCache m_textureCache;
|
TextureCache m_textureCache;
|
||||||
EngineFileLoader * m_fileLoader;
|
EngineFileLoader * m_fileLoader;
|
||||||
@ -141,6 +144,13 @@ class Engine
|
|||||||
bool m_autoEndFrame;
|
bool m_autoEndFrame;
|
||||||
bool m_autoDrawObjects;
|
bool m_autoDrawObjects;
|
||||||
std::map<std::string, bool> m_keysDown;
|
std::map<std::string, bool> m_keysDown;
|
||||||
|
|
||||||
|
bool m_event_update_present;
|
||||||
|
bool m_event_key_pressed_present;
|
||||||
|
bool m_event_key_released_present;
|
||||||
|
bool m_event_mousebutton_pressed_present;
|
||||||
|
bool m_event_mousebutton_released_present;
|
||||||
|
bool m_event_mouse_motion_present;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Engine * g_engine;
|
extern Engine * g_engine;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user