Browse Source

Added Text:setFont.

Alex Szpakowski 10 years ago
parent
commit
90a3c2b8ba

+ 10 - 0
src/modules/graphics/opengl/Text.cpp

@@ -265,6 +265,16 @@ void Text::draw(float x, float y, float angle, float sx, float sy, float ox, flo
 	glDisableVertexAttribArray(ATTRIB_POS);
 	glDisableVertexAttribArray(ATTRIB_POS);
 }
 }
 
 
+void Text::setFont(Font *f)
+{
+	font.set(f);
+
+	// Invalidate the texture cache ID since the font is different. We also have
+	// to re-upload all the vertices based on the new font's textures.
+	texture_cache_id = (uint32) -1;
+	regenerateVertices();
+}
+
 Font *Text::getFont() const
 Font *Text::getFont() const
 {
 {
 	return font.get();
 	return font.get();

+ 1 - 0
src/modules/graphics/opengl/Text.h

@@ -52,6 +52,7 @@ public:
 	// Implements Drawable.
 	// Implements Drawable.
 	virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
 	virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
 
 
+	void setFont(Font *f);
 	Font *getFont() const;
 	Font *getFont() const;
 
 
 	/**
 	/**

+ 9 - 0
src/modules/graphics/opengl/wrap_Text.cpp

@@ -135,6 +135,14 @@ int w_Text_clear(lua_State *L)
 	return 0;
 	return 0;
 }
 }
 
 
+int w_Text_setFont(lua_State *L)
+{
+	Text *t = luax_checktext(L, 1);
+	Font *f = luax_checktype<Font>(L, 2, GRAPHICS_FONT_ID);
+	luax_catchexcept(L, [&](){ t->setFont(f); });
+	return 0;
+}
+
 int w_Text_getFont(lua_State *L)
 int w_Text_getFont(lua_State *L)
 {
 {
 	Text *t = luax_checktext(L, 1);
 	Text *t = luax_checktext(L, 1);
@@ -164,6 +172,7 @@ static const luaL_Reg functions[] =
 	{ "add", w_Text_add },
 	{ "add", w_Text_add },
 	{ "addf", w_Text_addf },
 	{ "addf", w_Text_addf },
 	{ "clear", w_Text_clear },
 	{ "clear", w_Text_clear },
+	{ "setFont", w_Text_setFont },
 	{ "getFont", w_Text_getFont },
 	{ "getFont", w_Text_getFont },
 	{ "getWidth", w_Text_getWidth },
 	{ "getWidth", w_Text_getWidth },
 	{ "getHeight", w_Text_getHeight },
 	{ "getHeight", w_Text_getHeight },

+ 1 - 0
src/modules/graphics/opengl/wrap_Text.h

@@ -37,6 +37,7 @@ int w_Text_setf(lua_State *L);
 int w_Text_add(lua_State *L);
 int w_Text_add(lua_State *L);
 int w_Text_addf(lua_State *L);
 int w_Text_addf(lua_State *L);
 int w_Text_clear(lua_State *L);
 int w_Text_clear(lua_State *L);
+int w_Text_setFont(lua_State *L);
 int w_Text_getFont(lua_State *L);
 int w_Text_getFont(lua_State *L);
 int w_Text_getWidth(lua_State *L);
 int w_Text_getWidth(lua_State *L);
 int w_Text_getHeight(lua_State *L);
 int w_Text_getHeight(lua_State *L);