From d4df124dcca5f46a087b56e0a25a27347ec176ef Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 13 Nov 2009 05:16:16 +0000 Subject: [PATCH] added ag::getTextSize(), varying text size demo in tests/cratestack.lua git-svn-id: svn://anubis/anaglym/trunk@165 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 4 +++- Engine.h | 3 ++- ag.cc | 16 ++++++++++++++++ ag.h | 1 + tests/cratestack.lua | 10 +++++++++- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Engine.cc b/Engine.cc index 87dd32b..8c62c75 100644 --- a/Engine.cc +++ b/Engine.cc @@ -488,8 +488,10 @@ void Engine::drawText(const char * text, GLfloat r, GLfloat g, GLfloat b, glPopMatrix(); } -void Engine::getTextSize(const char * text, float * width, float * height) +void Engine::getTextSize(const char * text, int ptsize, + float * width, float * height) { + m_font->FaceSize(ptsize); FTBBox box = m_font->BBox(text); *width = box.Upper().Xf() - box.Lower().Xf(); *height = box.Upper().Yf() - box.Lower().Yf(); diff --git a/Engine.h b/Engine.h index d509f83..42512af 100644 --- a/Engine.h +++ b/Engine.h @@ -138,7 +138,8 @@ class Engine void getScreenSize(int * width, int * height); void drawText(const char * text, GLfloat r, GLfloat g, GLfloat b, int ptsize, float x, float y); - void getTextSize(const char * text, float * width, float * height); + void getTextSize(const char * text, int ptsize, + float * width, float * height); /* lua services */ int setCamera(lua_State * L); diff --git a/ag.cc b/ag.cc index ba8656b..2ff6dc3 100644 --- a/ag.cc +++ b/ag.cc @@ -45,6 +45,7 @@ namespace ag { "loadTexture", loadTexture }, { "getScreenSize", getScreenSize }, { "drawText", drawText}, + { "getTextSize", getTextSize}, { "createBox", createBox}, { "createStaticBox", createStaticBox}, @@ -371,6 +372,21 @@ namespace ag return 0; } + int getTextSize(lua_State * L) + { + float width = 0.0f; + float height = 0.0f; + int argc = lua_gettop(L); + if (argc == 2 && lua_isstring(L, 1) && lua_isnumber(L, 2)) + { + g_engine->getTextSize(lua_tostring(L, 1), lua_tonumber(L, 2), + &width, &height); + } + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; + } + static void addManagedObject(lua_State * L, bool is_static, OdeWorld::GeomType geom_type, refptr< vector > args) { diff --git a/ag.h b/ag.h index bb27093..ffb1207 100644 --- a/ag.h +++ b/ag.h @@ -31,6 +31,7 @@ namespace ag int loadTexture(lua_State * L); int getScreenSize(lua_State * L); int drawText(lua_State * L); + int getTextSize(lua_State * L); int createBox(lua_State * L); int createStaticBox(lua_State * L); diff --git a/tests/cratestack.lua b/tests/cratestack.lua index 3054fc8..0aa7ea3 100644 --- a/tests/cratestack.lua +++ b/tests/cratestack.lua @@ -44,5 +44,13 @@ function mouse_moves(x, y, xrel, yrel) end function update_overlay_event() - ag.drawText("Hi there everyone!", 1, 0.7, 0, 16, 0, 0) + y = 4 + size = 16 + for i = 0, 17 do + local message = "Hello there" + local width, height = ag.getTextSize(message, size) + ag.drawText(message, 1, 0.7, 0, size, 2, y) + y = y + height + 4 + size = size + 4 + end end