Browse Source

Add new variants of love.graphics.print/printf which accept a Font argument, rather than relying on global love.graphics state to determine the Font to use.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
0fde3fde35

+ 12 - 6
src/modules/graphics/Graphics.cpp

@@ -717,20 +717,26 @@ void Graphics::print(const std::vector<Font::ColoredString> &str, const Matrix4
 {
 {
 	checkSetDefaultFont();
 	checkSetDefaultFont();
 
 
-	DisplayState &state = states.back();
+	if (states.back().font.get() != nullptr)
+		print(str, states.back().font.get(), m);
+}
 
 
-	if (state.font.get() != nullptr)
-		state.font->print(this, str, m, state.color);
+void Graphics::print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m)
+{
+	font->print(this, str, m, states.back().color);
 }
 }
 
 
 void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
 void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
 {
 {
 	checkSetDefaultFont();
 	checkSetDefaultFont();
 
 
-	DisplayState &state = states.back();
+	if (states.back().font.get() != nullptr)
+		printf(str, states.back().font.get(), wrap, align, m);
+}
 
 
-	if (state.font.get() != nullptr)
-		state.font->printf(this, str, wrap, align, m, state.color);
+void Graphics::printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m)
+{
+	font->printf(this, str, wrap, align, m, states.back().color);
 }
 }
 
 
 /**
 /**

+ 2 - 0
src/modules/graphics/Graphics.h

@@ -593,11 +593,13 @@ public:
 	 * Draws text at the specified coordinates
 	 * Draws text at the specified coordinates
 	 **/
 	 **/
 	void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
 	void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
+	void print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m);
 
 
 	/**
 	/**
 	 * Draws formatted text on screen at the specified coordinates.
 	 * Draws formatted text on screen at the specified coordinates.
 	 **/
 	 **/
 	void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
 	void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
+	void printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m);
 
 
 	/**
 	/**
 	 * Draws a point at (x,y).
 	 * Draws a point at (x,y).

+ 42 - 17
src/modules/graphics/wrap_Graphics.cpp

@@ -2028,10 +2028,22 @@ int w_print(lua_State *L)
 	std::vector<Font::ColoredString> str;
 	std::vector<Font::ColoredString> str;
 	luax_checkcoloredstring(L, 1, str);
 	luax_checkcoloredstring(L, 1, str);
 
 
-	luax_checkstandardtransform(L, 2, [&](const Matrix4 &m)
+	if (luax_istype(L, 2, Font::type))
 	{
 	{
-		luax_catchexcept(L, [&](){ instance()->print(str, m); });
-	});
+		Font *font = luax_checkfont(L, 2);
+
+		luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
+		{
+			luax_catchexcept(L, [&](){ instance()->print(str, font, m); });
+		});
+	}
+	else
+	{
+		luax_checkstandardtransform(L, 2, [&](const Matrix4 &m)
+		{
+			luax_catchexcept(L, [&](){ instance()->print(str, m); });
+		});
+	}
 
 
 	return 0;
 	return 0;
 }
 }
@@ -2041,29 +2053,38 @@ int w_printf(lua_State *L)
 	std::vector<Font::ColoredString> str;
 	std::vector<Font::ColoredString> str;
 	luax_checkcoloredstring(L, 1, str);
 	luax_checkcoloredstring(L, 1, str);
 
 
+	Font *font = nullptr;
+	int startidx = 2;
+
+	if (luax_istype(L, startidx, Font::type))
+	{
+		font = luax_checkfont(L, startidx);
+		startidx++;
+	}
+
 	Font::AlignMode align = Font::ALIGN_LEFT;
 	Font::AlignMode align = Font::ALIGN_LEFT;
 	Matrix4 m;
 	Matrix4 m;
 
 
-	int formatidx = 4;
+	int formatidx = startidx + 2;
 
 
-	if (luax_istype(L, 2, math::Transform::type))
+	if (luax_istype(L, startidx, math::Transform::type))
 	{
 	{
-		math::Transform *tf = luax_totype<math::Transform>(L, 2);
+		math::Transform *tf = luax_totype<math::Transform>(L, startidx);
 		m = tf->getMatrix();
 		m = tf->getMatrix();
-		formatidx = 3;
+		formatidx = startidx + 1;
 	}
 	}
 	else
 	else
 	{
 	{
-		float x = (float)luaL_checknumber(L, 2);
-		float y = (float)luaL_checknumber(L, 3);
+		float x = (float)luaL_checknumber(L, startidx + 0);
+		float y = (float)luaL_checknumber(L, startidx + 1);
 
 
-		float angle = (float) luaL_optnumber(L, 6, 0.0f);
-		float sx = (float) luaL_optnumber(L, 7, 1.0f);
-		float sy = (float) luaL_optnumber(L, 8, sx);
-		float ox = (float) luaL_optnumber(L, 9, 0.0f);
-		float oy = (float) luaL_optnumber(L, 10, 0.0f);
-		float kx = (float) luaL_optnumber(L, 11, 0.0f);
-		float ky = (float) luaL_optnumber(L, 12, 0.0f);
+		float angle = (float) luaL_optnumber(L, startidx + 4, 0.0f);
+		float sx = (float) luaL_optnumber(L, startidx + 5, 1.0f);
+		float sy = (float) luaL_optnumber(L, startidx + 6, sx);
+		float ox = (float) luaL_optnumber(L, startidx + 7, 0.0f);
+		float oy = (float) luaL_optnumber(L, startidx + 8, 0.0f);
+		float kx = (float) luaL_optnumber(L, startidx + 9, 0.0f);
+		float ky = (float) luaL_optnumber(L, startidx + 10, 0.0f);
 
 
 		m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
 		m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
 	}
 	}
@@ -2074,7 +2095,11 @@ int w_printf(lua_State *L)
 	if (astr != nullptr && !Font::getConstant(astr, align))
 	if (astr != nullptr && !Font::getConstant(astr, align))
 		return luaL_error(L, "Incorrect alignment: %s", astr);
 		return luaL_error(L, "Incorrect alignment: %s", astr);
 
 
-	luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
+	if (font != nullptr)
+		luax_catchexcept(L, [&](){ instance()->printf(str, font, wrap, align, m); });
+	else
+		luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
+
 	return 0;
 	return 0;
 }
 }