added an update_overlay event and drawing text with Engine::drawText()

git-svn-id: svn://anubis/anaglym/trunk@163 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-11-13 04:24:13 +00:00
parent f9c3bcfcc7
commit c478b44d28
5 changed files with 68 additions and 149 deletions

View File

@ -107,6 +107,7 @@ Engine::Engine(const string & path, Video & video)
m_exitEvent.user.code = 1;
m_event_update_present = false;
m_event_update_overlay_present = false;
m_event_key_down_present = false;
m_event_key_up_present = false;
m_event_mousebutton_down_present = false;
@ -127,12 +128,6 @@ Engine::~Engine()
{
delete it->second;
}
for (std::map<int, FTFont *>::iterator it = m_textboxes.begin();
it != m_textboxes.end();
it++)
{
delete it->second;
}
delete m_fileLoader;
if (m_font != NULL)
delete m_font;
@ -158,10 +153,6 @@ bool Engine::load(const char * program)
{
cerr << "Error loading font '" << path << "'" << endl;
}
else
{
m_font->FaceSize(20);
}
}
int s = luaL_loadfile(m_luaState, program);
@ -170,7 +161,7 @@ bool Engine::load(const char * program)
{
// execute Lua program
lua_sethook(m_luaState, ::debug_hook, LUA_MASKCOUNT, HOOK_STEPS);
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
s = lua_pcall(m_luaState, 0, 0, 0);
}
else
{
@ -187,6 +178,7 @@ bool Engine::load(const char * program)
}
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);
@ -362,6 +354,8 @@ int Engine::registerEventHandler(lua_State * L)
string event = lua_tostring(L, 2);
if (event == "update")
doRegisterHandler(1, update);
else if (event == "update_overlay")
doRegisterHandler(1, update_overlay);
else if (event == "key_down")
doRegisterHandler(1, key_down);
else if (event == "key_up")
@ -384,6 +378,8 @@ int Engine::clearEventHandler(lua_State * L)
string event = lua_tostring(L, 1);
if (event == "update")
doClearHandler(update);
else if (event == "update_overlay")
doClearHandler(update_overlay);
else if (event == "key_down")
doClearHandler(key_down);
else if (event == "key_up")
@ -449,7 +445,7 @@ bool Engine::import(const char * name)
int s = luaL_loadfile(m_luaState, file_path.c_str());
if (s == 0)
{
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
s = lua_pcall(m_luaState, 0, 0, 0);
if (s == 0)
return true;
reportErrors(s);
@ -474,35 +470,22 @@ void Engine::debug_hook(lua_Debug * debug)
}
}
int Engine::createTextBox(const char * text, int mode,
Uint8 r, Uint8 g, Uint8 b,
Uint8 br, Uint8 bg, Uint8 bb)
{
GLfloat color[] = {1.0, 0.7, 0.1, 0.5};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
glPushMatrix();
glTranslatef(-10, 0, 6);
glRotatef(90, 1, 0, 0);
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_NORMALIZE);
glScalef(0.1, 0.1, 0.1);
m_font->Render(text);
glPopAttrib();
glPopMatrix();
return 1;
}
void Engine::getScreenSize(int * width, int * height)
{
*width = m_video.getWidth();
*height = m_video.getHeight();
}
void Engine::drawText(int id, int x, int y)
void Engine::drawText(const char * text, GLfloat r, GLfloat g, GLfloat b,
int ptsize, int x, int y)
{
if (m_textboxes.find(id) != m_textboxes.end())
{
}
m_font->FaceSize(ptsize);
glPushMatrix();
glTranslatef(x, y, 0);
GLfloat color[] = {r, g, b, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
m_font->Render(text);
glPopMatrix();
}
/* called by SDL when the update timer expires */
@ -603,16 +586,47 @@ void Engine::update_event()
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
EVENT_HANDLER_AG_NAME(update));
/* 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, 0, 0);
reportErrors(s);
}
if (m_autoDrawObjects)
drawObjects();
update_overlay_event();
if (m_autoEndFrame)
endFrame();
m_drawing = false;
}
void Engine::update_overlay_event()
{
if (m_event_update_overlay_present)
{
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
int width = m_video.getWidth();
int height = m_video.getHeight();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, 0, height, -1.01, 1.01);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
EVENT_HANDLER_AG_NAME(update_overlay));
lua_pushnumber(m_luaState, width);
lua_pushnumber(m_luaState, height);
/* call the update_overlay function
* - pops the function ref from the stack, then the arguments */
int s = lua_pcall(m_luaState, 2, 0, 0);
reportErrors(s);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
}
}
void Engine::key_down_event(int keysym)
{
m_keysDown[sdl_keymap[keysym]] = true;
@ -623,7 +637,7 @@ void Engine::key_down_event(int keysym)
lua_pushstring(m_luaState, sdl_keymap[keysym]);
/* call the key down event function
* 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, 0, 0);
reportErrors(s);
}
}
@ -638,7 +652,7 @@ void Engine::key_up_event(int keysym)
lua_pushstring(m_luaState, sdl_keymap[keysym]);
/* call the key up event function
* 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, 0, 0);
reportErrors(s);
}
}
@ -654,7 +668,7 @@ void Engine::mousebutton_down_event(int button, int x, int y)
lua_pushnumber(m_luaState, y);
/* call the mouse button down event function
* This pops the function ref and arguments from the stack */
int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0);
int s = lua_pcall(m_luaState, 3, 0, 0);
reportErrors(s);
}
}
@ -670,7 +684,7 @@ void Engine::mousebutton_up_event(int button, int x, int y)
lua_pushnumber(m_luaState, y);
/* call the mouse button up event function
* This pops the function ref and arguments from the stack */
int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0);
int s = lua_pcall(m_luaState, 3, 0, 0);
reportErrors(s);
}
}
@ -687,7 +701,7 @@ void Engine::mouse_motion_event(int x, int y, int xrel, int yrel)
lua_pushnumber(m_luaState, yrel);
/* call the mouse motion event function
* This pops the function ref and arguments from the stack */
int s = lua_pcall(m_luaState, 4, LUA_MULTRET, 0);
int s = lua_pcall(m_luaState, 4, 0, 0);
reportErrors(s);
}
}

View File

@ -135,11 +135,9 @@ class Engine
bool import(const char * name);
GLuint loadTexture(const char * name);
void debug_hook(lua_Debug * debug);
int createTextBox(const char * text, int mode,
Uint8 r, Uint8 g, Uint8 b,
Uint8 br = 0, Uint8 bg = 0, Uint8 bb = 0);
void getScreenSize(int * width, int * height);
void drawText(int id, int x, int y);
void drawText(const char * text, GLfloat r, GLfloat g, GLfloat b,
int ptsize, int x, int y);
/* lua services */
int setCamera(lua_State * L);
@ -154,6 +152,7 @@ class Engine
void registerLibraries();
bool fileExists(const std::string & path);
void update_event();
void update_overlay_event();
void key_down_event(int keysym);
void key_up_event(int keysym);
void mousebutton_down_event(int button, int x, int y);
@ -173,7 +172,6 @@ class Engine
OdeWorld m_world;
std::map<int, Object *> m_objects;
int m_next_object_index;
std::map<int, FTFont *> m_textboxes;
int m_next_text_index;
GLdouble m_eye[3];
GLdouble m_center[3];
@ -190,6 +188,7 @@ class Engine
FTFont * m_font;
bool m_event_update_present;
bool m_event_update_overlay_present;
bool m_event_key_down_present;
bool m_event_key_up_present;
bool m_event_mousebutton_down_present;

101
ag.cc
View File

@ -44,9 +44,7 @@ namespace ag
{ "import", import },
{ "loadTexture", loadTexture },
{ "getScreenSize", getScreenSize },
{ "createSolidTextBox", createSolidTextBox},
{ "createShadedTextBox", createShadedTextBox},
{ "createBlendedTextBox", createBlendedTextBox},
{ "drawText", drawText},
{ "createBox", createBox},
{ "createStaticBox", createStaticBox},
@ -350,46 +348,7 @@ namespace ag
return 2;
}
static int createTextBoxCommon(lua_State * L, int type, const char * text,
Uint8 r, Uint8 g, Uint8 b, Uint8 br, Uint8 bg, Uint8 bb)
{
int id = g_engine->createTextBox(text, type, r, g, b, br, bg, bb);
if (id != 0)
{
lua_newtable(L);
lua_pushinteger(L, id);
lua_setfield(L, -2, "id");
lua_pushcfunction(L, textbox::draw);
lua_setfield(L, -2, "draw");
}
else
{
lua_pushnil(L);
}
return 1;
}
int createSolidTextBox(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 4
&& lua_isstring(L, 1)
&& lua_isnumber(L, 2)
&& lua_isnumber(L, 3)
&& lua_isnumber(L, 4))
{
return createTextBoxCommon(L, 0,
lua_tostring(L, 1),
lua_tonumber(L, 2),
lua_tonumber(L, 3),
lua_tonumber(L, 4),
0, 0, 0);
}
lua_pushnil(L);
return 1;
}
int createShadedTextBox(lua_State * L)
int drawText(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 7
@ -401,37 +360,15 @@ namespace ag
&& lua_isnumber(L, 6)
&& lua_isnumber(L, 7))
{
return createTextBoxCommon(L, 1,
lua_tostring(L, 1),
g_engine->drawText(lua_tostring(L, 1),
lua_tonumber(L, 2),
lua_tonumber(L, 3),
lua_tonumber(L, 4),
lua_tonumber(L, 5),
lua_tonumber(L, 6),
lua_tonumber(L, 7));
lua_tointeger(L, 5),
lua_tointeger(L, 6),
lua_tointeger(L, 7));
}
lua_pushnil(L);
return 1;
}
int createBlendedTextBox(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 4
&& lua_isstring(L, 1)
&& lua_isnumber(L, 2)
&& lua_isnumber(L, 3)
&& lua_isnumber(L, 4))
{
return createTextBoxCommon(L, 2,
lua_tostring(L, 1),
lua_tonumber(L, 2),
lua_tonumber(L, 3),
lua_tonumber(L, 4),
0, 0, 0);
}
lua_pushnil(L);
return 1;
return 0;
}
static void addManagedObject(lua_State * L, bool is_static,
@ -855,28 +792,4 @@ namespace ag
return 0;
}
}
namespace textbox
{
int draw(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 3
&& lua_istable(L, 1)
&& lua_isnumber(L, 2)
&& lua_isnumber(L, 3))
{
lua_getfield(L, 1, "id");
if (lua_isnumber(L, -1))
{
int id = lua_tonumber(L, -1);
g_engine->drawText(id,
lua_tonumber(L, 2),
lua_tonumber(L, 3));
}
lua_pop(L, 1);
}
return 0;
}
}
}

9
ag.h
View File

@ -30,9 +30,7 @@ namespace ag
int import(lua_State * L);
int loadTexture(lua_State * L);
int getScreenSize(lua_State * L);
int createSolidTextBox(lua_State * L);
int createShadedTextBox(lua_State * L);
int createBlendedTextBox(lua_State * L);
int drawText(lua_State * L);
int createBox(lua_State * L);
int createStaticBox(lua_State * L);
@ -61,11 +59,6 @@ namespace ag
int setColor(lua_State * L);
int setTexture(lua_State * L);
}
namespace textbox
{
int draw(lua_State * L);
}
}
#endif

View File

@ -43,6 +43,6 @@ function mouse_moves(x, y, xrel, yrel)
ag.println("mouse_moves ", x, ", ", y)
end
function update_event()
ag.createSolidTextBox("Hi there everyone!", 0, 0, 0)
function update_overlay_event()
ag.drawText("Hi there everyone!", 1, 0.7, 0, 16, 40, 40)
end