Added all standard optional draw parameters to love.graphics.printf: angle, sx, sy, ox, oy, kx, and ky

This commit is contained in:
Alex Szpakowski
2013-03-18 16:15:21 -03:00
parent 991b15d670
commit 288971a3ad
3 changed files with 72 additions and 31 deletions
+43 -25
View File
@@ -722,7 +722,7 @@ void Graphics::print(const char *str, float x, float y , float angle, float sx,
} }
} }
void Graphics::printf(const char *str, float x, float y, float wrap, AlignMode align) void Graphics::printf(const char *str, float x, float y, float wrap, AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{ {
if (currentFont == 0) if (currentFont == 0)
return; return;
@@ -731,34 +731,52 @@ void Graphics::printf(const char *str, float x, float y, float wrap, AlignMode a
string text(str); string text(str);
vector<string> lines_to_draw = currentFont->getWrap(text, wrap); vector<string> lines_to_draw = currentFont->getWrap(text, wrap);
// now for the actual printing glPushMatrix();
vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
float letter_spacing = 0.0f; static Matrix t;
for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter) t.setTransformation(ceil(x), ceil(y), angle, sx, sy, ox, oy, kx, ky);
glMultMatrixf((const GLfloat *)t.getElements());
x = y = 0.0f;
try
{ {
float width = static_cast<float>(currentFont->getWidth(*line_iter)); // now for the actual printing
switch (align) vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
float letter_spacing = 0.0f;
for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
{ {
case ALIGN_RIGHT: float width = static_cast<float>(currentFont->getWidth(*line_iter));
currentFont->print(*line_iter, ceil(x + wrap - width), ceil(y)); switch (align)
break; {
case ALIGN_CENTER: case ALIGN_RIGHT:
currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y)); currentFont->print(*line_iter, ceil(x + (wrap - width)), ceil(y), 0.0f);
break; break;
case ALIGN_JUSTIFY: case ALIGN_CENTER:
if (line_iter->length() > 1 && (line_iter+1) != line_end) currentFont->print(*line_iter, ceil(x + (wrap - width) / 2), ceil(y), 0.0f);
letter_spacing = (wrap - width) / float(line_iter->length() - 1); break;
else case ALIGN_JUSTIFY:
letter_spacing = 0.0f; if (line_iter->length() > 1 && (line_iter+1) != line_end)
currentFont->print(*line_iter, ceil(x), ceil(y), letter_spacing); letter_spacing = (wrap - width) / float(line_iter->length() - 1);
break; else
case ALIGN_LEFT: letter_spacing = 0.0f;
default: currentFont->print(*line_iter, ceil(x), ceil(y), letter_spacing);
currentFont->print(*line_iter, ceil(x), ceil(y)); break;
break; case ALIGN_LEFT:
default:
currentFont->print(*line_iter, ceil(x), ceil(y), 0.0f);
break;
}
y += currentFont->getHeight() * currentFont->getLineHeight();
} }
y += currentFont->getHeight() * currentFont->getLineHeight();
} }
catch (love::Exception &)
{
glPopMatrix();
throw;
}
glPopMatrix();
} }
/** /**
+9 -2
View File
@@ -392,7 +392,7 @@ public:
* @param kx Shear along the x-axis. * @param kx Shear along the x-axis.
* @param ky Shear along the y-axis. * @param ky Shear along the y-axis.
**/ **/
void print(const char *str, float x, float y , float angle, float sx, float sy, float ox, float oy, float kx, float ky); void print(const char *str, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
/** /**
* Draw formatted text on screen at the specified coordinates. * Draw formatted text on screen at the specified coordinates.
@@ -402,8 +402,15 @@ public:
* @param y The y-coordinate. * @param y The y-coordinate.
* @param wrap The maximum width of the text area. * @param wrap The maximum width of the text area.
* @param align Where to align the text. * @param align Where to align the text.
* @param angle The amount of rotation.
* @param sx The scale factor along the x-axis. (1 = normal).
* @param sy The scale factor along the y-axis. (1 = normal).
* @param ox The origin offset along the x-axis.
* @param oy The origin offset along the y-axis.
* @param kx Shear along the x-axis.
* @param ky Shear along the y-axis.
**/ **/
void printf(const char *str, float x, float y, float wrap, AlignMode align); void printf(const char *str, float x, float y, float wrap, AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
/** /**
* Draws a point at (x,y). * Draws a point at (x,y).
+20 -4
View File
@@ -1055,18 +1055,34 @@ int w_printf(lua_State *L)
float y = (float)luaL_checknumber(L, 3); float y = (float)luaL_checknumber(L, 3);
float wrap = (float)luaL_checknumber(L, 4); float wrap = (float)luaL_checknumber(L, 4);
float angle = 0.0f;
float sx = 1.0f, sy = 1.0f;
float ox = 0.0f, oy = 0.0f;
float kx = 0.0f, ky = 0.0f;
Graphics::AlignMode align = Graphics::ALIGN_LEFT; Graphics::AlignMode align = Graphics::ALIGN_LEFT;
if (lua_gettop(L) >= 5) if (lua_gettop(L) >= 5)
{ {
const char *str = luaL_checkstring(L, 5); if (!lua_isnil(L, 5))
if (!Graphics::getConstant(str, align)) {
return luaL_error(L, "Incorrect alignment: %s", str); const char *str = luaL_checkstring(L, 5);
if (!Graphics::getConstant(str, align))
return luaL_error(L, "Incorrect alignment: %s", str);
}
angle = (float) luaL_optnumber(L, 6, 0.0f);
sx = (float) luaL_optnumber(L, 7, 1.0f);
sy = (float) luaL_optnumber(L, 8, sx);
ox = (float) luaL_optnumber(L, 9, 0.0f);
oy = (float) luaL_optnumber(L, 10, 0.0f);
kx = (float) luaL_optnumber(L, 11, 0.0f);
ky = (float) luaL_optnumber(L, 12, 0.0f);
} }
try try
{ {
instance->printf(str, x, y, wrap, align); instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);
} }
catch(love::Exception e) catch(love::Exception e)
{ {