Added Transform objects to love.math (resolves issue #1228).

- Added love.math.newTransform.
- Added love.graphics.applyTransform and love.graphics.replaceTransform.
- love.graphics.draw/print/printf, Text:add/addf, and SpriteBatch:add can accept a Transform argument.
- love.graphics.push can accept an optional Transform as a second argument, which will apply the Transform after pushing the matrix stack,
- Shader:send can accept Transform objects when sending to a mat4 uniform variable.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-11-24 21:30:45 -04:00
parent 9d2ee6d909
commit 14aa6965b3
19 changed files with 820 additions and 94 deletions
+10
View File
@@ -2092,6 +2092,16 @@ void Graphics::origin()
pixelSizeStack.back() = 1;
}
void Graphics::applyTransform(love::math::Transform *transform)
{
gl.getTransform() *= transform->getMatrix();
}
void Graphics::replaceTransform(love::math::Transform *transform)
{
gl.getTransform() = transform->getMatrix();
}
Vector Graphics::transformPoint(Vector point)
{
Vector p;
+6
View File
@@ -38,6 +38,8 @@
#include "video/VideoStream.h"
#include "math/Transform.h"
#include "Font.h"
#include "Image.h"
#include "graphics/Quad.h"
@@ -488,6 +490,10 @@ public:
void translate(float x, float y);
void shear(float kx, float ky);
void origin();
void applyTransform(love::math::Transform *transform);
void replaceTransform(love::math::Transform *transform);
Vector transformPoint(Vector point);
Vector inverseTransformPoint(Vector point);
+81 -51
View File
@@ -1667,24 +1667,37 @@ int w_draw(lua_State *L)
startidx = 2;
}
float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
float sy = (float) luaL_optnumber(L, startidx + 4, sx);
float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
if (luax_istype(L, startidx, MATH_TRANSFORM_ID))
{
math::Transform *tf = luax_totype<math::Transform>(L, startidx, MATH_TRANSFORM_ID);
luax_catchexcept(L, [&]() {
if (texture && quad)
instance()->drawq(texture, quad, tf->getMatrix());
else
instance()->draw(drawable, tf->getMatrix());
});
}
else
{
float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
float sy = (float) luaL_optnumber(L, startidx + 4, sx);
float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
luax_catchexcept(L, [&]() {
if (texture && quad)
instance()->drawq(texture, quad, m);
else if (drawable)
instance()->draw(drawable, m);
});
luax_catchexcept(L, [&]() {
if (texture && quad)
instance()->drawq(texture, quad, m);
else if (drawable)
instance()->draw(drawable, m);
});
}
return 0;
}
@@ -1694,19 +1707,27 @@ int w_print(lua_State *L)
std::vector<Font::ColoredString> str;
luax_checkcoloredstring(L, 1, str);
float x = (float)luaL_optnumber(L, 2, 0.0);
float y = (float)luaL_optnumber(L, 3, 0.0);
float angle = (float)luaL_optnumber(L, 4, 0.0f);
float sx = (float)luaL_optnumber(L, 5, 1.0f);
float sy = (float)luaL_optnumber(L, 6, sx);
float ox = (float)luaL_optnumber(L, 7, 0.0f);
float oy = (float)luaL_optnumber(L, 8, 0.0f);
float kx = (float)luaL_optnumber(L, 9, 0.0f);
float ky = (float)luaL_optnumber(L, 10, 0.0f);
if (luax_istype(L, 2, MATH_TRANSFORM_ID))
{
math::Transform *tf = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
luax_catchexcept(L, [&](){ instance()->print(str, tf->getMatrix()); });
}
else
{
float x = (float)luaL_optnumber(L, 2, 0.0);
float y = (float)luaL_optnumber(L, 3, 0.0);
float angle = (float)luaL_optnumber(L, 4, 0.0f);
float sx = (float)luaL_optnumber(L, 5, 1.0f);
float sy = (float)luaL_optnumber(L, 6, sx);
float ox = (float)luaL_optnumber(L, 7, 0.0f);
float oy = (float)luaL_optnumber(L, 8, 0.0f);
float kx = (float)luaL_optnumber(L, 9, 0.0f);
float ky = (float)luaL_optnumber(L, 10, 0.0f);
Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
luax_catchexcept(L, [&](){ instance()->print(str, m); });
luax_catchexcept(L, [&](){ instance()->print(str, m); });
}
return 0;
}
@@ -1715,36 +1736,38 @@ int w_printf(lua_State *L)
std::vector<Font::ColoredString> str;
luax_checkcoloredstring(L, 1, str);
float x = (float)luaL_checknumber(L, 2);
float y = (float)luaL_checknumber(L, 3);
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;
Font::AlignMode align = Font::ALIGN_LEFT;
Matrix4 m;
if (lua_gettop(L) >= 5)
int formatidx = 4;
if (luax_istype(L, 2, MATH_TRANSFORM_ID))
{
if (!lua_isnil(L, 5))
{
const char *str = luaL_checkstring(L, 5);
if (!Font::getConstant(str, align))
return luaL_error(L, "Incorrect alignment: %s", str);
}
math::Transform *tf = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
m = tf->getMatrix();
formatidx = 3;
}
else
{
float x = (float)luaL_checknumber(L, 2);
float y = (float)luaL_checknumber(L, 3);
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);
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);
m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
}
Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
float wrap = (float)luaL_checknumber(L, formatidx);
const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
if (astr != nullptr && !Font::getConstant(astr, align))
return luaL_error(L, "Incorrect alignment: %s", astr);
luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
return 0;
@@ -2048,6 +2071,13 @@ int w_push(lua_State *L)
return luaL_error(L, "Invalid graphics stack type: %s", sname);
luax_catchexcept(L, [&](){ instance()->push(stype); });
if (luax_istype(L, 2, MATH_TRANSFORM_ID))
{
math::Transform *t = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
instance()->applyTransform(t);
}
return 0;
}
@@ -21,6 +21,7 @@
#include "wrap_Shader.h"
#include "graphics/wrap_Texture.h"
#include "math/MathModule.h"
#include "math/Transform.h"
#include <string>
#include <algorithm>
@@ -164,6 +165,13 @@ int w_Shader_sendMatrices(lua_State *L, int startidx, Shader *shader, const Shad
for (int i = 0; i < count; i++)
{
if (columns == 4 && rows == 4 && luax_istype(L, startidx + i, MATH_TRANSFORM_ID))
{
math::Transform *t = luax_totype<math::Transform>(L, startidx + i, MATH_TRANSFORM_ID);
memcpy(&values[i * 16], t->getMatrix().getElements(), sizeof(float) * 16);
continue;
}
luaL_checktype(L, startidx + i, LUA_TTABLE);
lua_rawgeti(L, startidx + i, 1);
@@ -23,6 +23,7 @@
#include "Image.h"
#include "Canvas.h"
#include "graphics/wrap_Texture.h"
#include "math/wrap_Transform.h"
// C++
#include <typeinfo>
@@ -51,24 +52,37 @@ static inline int w_SpriteBatch_add_or_set(lua_State *L, SpriteBatch *t, int sta
else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
return luax_typerror(L, startidx, "Quad");
float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
float sy = (float) luaL_optnumber(L, startidx + 4, sx);
float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
if (luax_istype(L, startidx, MATH_TRANSFORM_ID))
{
math::Transform *tf = luax_totype<math::Transform>(L, startidx, MATH_TRANSFORM_ID);
luax_catchexcept(L, [&]() {
if (quad)
index = t->addq(quad, tf->getMatrix(), index);
else
index = t->add(tf->getMatrix(), index);
});
}
else
{
float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
float sy = (float) luaL_optnumber(L, startidx + 4, sx);
float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
luax_catchexcept(L, [&]() {
if (quad)
index = t->addq(quad, m, index);
else
index = t->add(m, index);
});
luax_catchexcept(L, [&]() {
if (quad)
index = t->addq(quad, m, index);
else
index = t->add(m, index);
});
}
return index;
}
+45 -26
View File
@@ -19,6 +19,7 @@
**/
#include "wrap_Text.h"
#include "math/wrap_Transform.h"
namespace love
{
@@ -126,24 +127,33 @@ int w_Text_add(lua_State *L)
{
Text *t = luax_checktext(L, 1);
int index = 0;
std::vector<Font::ColoredString> text;
luax_checkcoloredstring(L, 2, text);
float x = (float) luaL_optnumber(L, 3, 0.0);
float y = (float) luaL_optnumber(L, 4, 0.0);
float a = (float) luaL_optnumber(L, 5, 0.0);
float sx = (float) luaL_optnumber(L, 6, 1.0);
float sy = (float) luaL_optnumber(L, 7, sx);
float ox = (float) luaL_optnumber(L, 8, 0.0);
float oy = (float) luaL_optnumber(L, 9, 0.0);
float kx = (float) luaL_optnumber(L, 10, 0.0);
float ky = (float) luaL_optnumber(L, 11, 0.0);
if (luax_istype(L, 3, MATH_TRANSFORM_ID))
{
math::Transform *tf = luax_totype<math::Transform>(L, 3, MATH_TRANSFORM_ID);
luax_catchexcept(L, [&](){ index = t->add(text, tf->getMatrix()); });
}
else
{
float x = (float) luaL_optnumber(L, 3, 0.0);
float y = (float) luaL_optnumber(L, 4, 0.0);
float a = (float) luaL_optnumber(L, 5, 0.0);
float sx = (float) luaL_optnumber(L, 6, 1.0);
float sy = (float) luaL_optnumber(L, 7, sx);
float ox = (float) luaL_optnumber(L, 8, 0.0);
float oy = (float) luaL_optnumber(L, 9, 0.0);
float kx = (float) luaL_optnumber(L, 10, 0.0);
float ky = (float) luaL_optnumber(L, 11, 0.0);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
luax_catchexcept(L, [&](){ index = t->add(text, m); });
}
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
int index = 0;
luax_catchexcept(L, [&](){ index = t->add(text, m); });
lua_pushnumber(L, index + 1);
return 1;
}
@@ -151,6 +161,8 @@ int w_Text_addf(lua_State *L)
{
Text *t = luax_checktext(L, 1);
int index = 0;
std::vector<Font::ColoredString> text;
luax_checkcoloredstring(L, 2, text);
@@ -162,21 +174,28 @@ int w_Text_addf(lua_State *L)
if (!Font::getConstant(alignstr, align))
return luaL_error(L, "Invalid align mode: %s", alignstr);
float x = (float) luaL_optnumber(L, 5, 0.0);
float y = (float) luaL_optnumber(L, 6, 0.0);
float a = (float) luaL_optnumber(L, 7, 0.0);
float sx = (float) luaL_optnumber(L, 8, 1.0);
float sy = (float) luaL_optnumber(L, 9, sx);
float ox = (float) luaL_optnumber(L, 10, 0.0);
float oy = (float) luaL_optnumber(L, 11, 0.0);
float kx = (float) luaL_optnumber(L, 12, 0.0);
float ky = (float) luaL_optnumber(L, 13, 0.0);
if (luax_istype(L, 5, MATH_TRANSFORM_ID))
{
math::Transform *tf = luax_totype<math::Transform>(L, 5, MATH_TRANSFORM_ID);
luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, tf->getMatrix()); });
}
else
{
float x = (float) luaL_optnumber(L, 5, 0.0);
float y = (float) luaL_optnumber(L, 6, 0.0);
float a = (float) luaL_optnumber(L, 7, 0.0);
float sx = (float) luaL_optnumber(L, 8, 1.0);
float sy = (float) luaL_optnumber(L, 9, sx);
float ox = (float) luaL_optnumber(L, 10, 0.0);
float oy = (float) luaL_optnumber(L, 11, 0.0);
float kx = (float) luaL_optnumber(L, 12, 0.0);
float ky = (float) luaL_optnumber(L, 13, 0.0);
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, m); });
}
Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
int index = 0;
luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, m); });
lua_pushnumber(L, index + 1);
return 1;
}