Add shear transformation and transform object (issue #152).

Add origin parameters to love.graphics.print.

New shear parameters kx,ky for:
- love.graphics.draw,
- love.graphics.drawq,
- love.graphics.print,
- SpriteBatch:add,
- SpriteBatch:addq.

Transform objects apply to:
- love.graphics.draw,
- love.graphics.drawq,
- love.graphics.print.
This commit is contained in:
vrld
2011-07-05 14:33:34 +02:00
parent ce5927379d
commit d420d68048
20 changed files with 214 additions and 55 deletions
+3 -1
View File
@@ -51,8 +51,10 @@ namespace graphics
* @param sy The scale factor along the y-axis.
* @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.
**/
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const = 0;
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const = 0;
};
} // graphics
+5 -4
View File
@@ -25,6 +25,7 @@
#include <libraries/utf8/utf8.h>
#include <common/math.h>
#include <common/Matrix.h>
#include <math.h>
#include <algorithm> // for max
@@ -141,14 +142,14 @@ namespace opengl
return static_cast<float>(height);
}
void Font::print(std::string text, float x, float y, float angle, float sx, float sy)
void Font::print(std::string text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
float dx = 0.0f; // spacing counter for newline handling
glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f);
glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
Matrix t;
t.setTransformation(ceil(x), ceil(y), angle, sx, sy, ox, oy, kx, ky);
glMultMatrixf((const GLfloat*)t.getElements());
try
{
utf8::iterator<std::string::iterator> i (text.begin(), text.begin(), text.end());
+7 -1
View File
@@ -93,8 +93,14 @@ namespace opengl
* @param x The x-coordinate.
* @param y The y-coordinate.
* @param angle The amount of rotation.
* @param sx Scale along the x axis.
* @param sy Scale along the y axis.
* @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 print(std::string text, float x, float y, float angle = 0.0f, float sx = 1.0f, float sy = 1.0f);
void print(std::string text, float x, float y, float angle = 0.0f, float sx = 1.0f, float sy = 1.0f, float ox = 0.0f, float oy = 0.0f, float kx = 0.0f, float ky = 0.0f);
/**
* Prints the character at the designated position.
+2 -2
View File
@@ -250,10 +250,10 @@ namespace opengl
strategy->bindFBO(previous);
}
void Framebuffer::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
void Framebuffer::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
static Matrix t;
t.setTransformation(x, y, angle, sx, sy, ox, oy);
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
glPushMatrix();
glMultMatrixf((const GLfloat*)t.getElements());
+1 -1
View File
@@ -32,7 +32,7 @@ namespace opengl
void clear(const Color& c);
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
love::image::ImageData * getImageData(love::image::Image * image);
void setFilter(const Image::Filter &f);
+9 -2
View File
@@ -715,12 +715,12 @@ namespace opengl
return (int)max;
}
void Graphics::print( const char * str, float x, float y , float angle, float sx, float sy)
void Graphics::print( const char * str, float x, float y , float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
if(currentFont != 0)
{
std::string text(str);
currentFont->print(text, x, y, angle, sx, sy);
currentFont->print(text, x, y, angle, sx, sy, ox, oy, kx, ky);
}
}
@@ -1025,6 +1025,13 @@ namespace opengl
glTranslatef(x, y, 0);
}
void Graphics::shear(float kx, float ky)
{
Matrix t;
t.setShear(kx, ky);
glMultMatrixf((const GLfloat*)t.getElements());
}
void Graphics::drawTest(Image * image, float x, float y, float a, float sx, float sy, float ox, float oy)
{
image->bind();
+6 -1
View File
@@ -397,8 +397,12 @@ namespace opengl
* @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 print(const char * str, float x, float y , float angle = 0.0f, float sx = 1.0f, float sy = 1.0f);
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.
@@ -502,6 +506,7 @@ namespace opengl
void rotate(float r);
void scale(float x, float y = 1.0f);
void translate(float x, float y);
void shear(float kx, float ky);
void drawTest(Image * image, float x, float y, float a, float sx, float sy, float ox, float oy);
+4 -4
View File
@@ -102,20 +102,20 @@ namespace opengl
vertices[3].s = tx+tw; vertices[3].t = ty;
}
void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
static Matrix t;
t.setTransformation(x, y, angle, sx, sy, ox, oy);
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
drawv(t, vertices);
}
void Image::drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy) const
void Image::drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
static Matrix t;
const vertex * v = quad->getVertices();
t.setTransformation(x, y, angle, sx, sy, ox, oy);
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
drawv(t, v);
}
+2 -2
View File
@@ -111,7 +111,7 @@ namespace opengl
/**
* @copydoc Drawable::draw()
**/
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
/**
* This function draws a section of the image using a Quad object.
@@ -119,7 +119,7 @@ namespace opengl
* @copydetails Image::draws()
* @param frame Represents the region of the Image to draw.
**/
void drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy) const;
void drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
/**
* Sets the filter mode.
@@ -383,17 +383,16 @@ namespace opengl
return pLast == pEnd;
}
void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
if(sprite == 0) return; // just in case of failure
glPushMatrix();
glPushAttrib(GL_CURRENT_BIT);
glTranslatef(x, y, 0);
glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
glTranslatef( ox, oy, 0);
Matrix t;
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
glMultMatrixf((const GLfloat*)t.getElements());
particle * p = pStart;
while(p != pLast)
@@ -401,7 +400,7 @@ namespace opengl
glPushMatrix();
glColor4f(p->color.r, p->color.g, p->color.b, p->color.a);
sprite->draw(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY);
sprite->draw(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY, 0.0f, 0.0f);
glPopMatrix();
p++;
+1 -1
View File
@@ -425,7 +425,7 @@ namespace opengl
* @param x The x-coordinate.
* @param y The y-coordinate.
**/
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
/**
* Updates the particle system.
+6 -6
View File
@@ -101,7 +101,7 @@ namespace opengl
glDeleteBuffers(2, vbo);
}
void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy)
void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
{
// Only do this if there's a free slot.
if(next < size)
@@ -114,7 +114,7 @@ namespace opengl
// Transform.
Matrix t;
t.setTransformation(x, y, a, sx, sy, ox, oy);
t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
t.transform(v, v, 4);
addv(v);
@@ -124,7 +124,7 @@ namespace opengl
}
}
void SpriteBatch::addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy)
void SpriteBatch::addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
{
// Only do this if there's a free slot.
if(next < size)
@@ -137,7 +137,7 @@ namespace opengl
// Transform.
Matrix t;
t.setTransformation(x, y, a, sx, sy, ox, oy);
t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
t.transform(v, v, 4);
addv(v);
@@ -180,13 +180,13 @@ namespace opengl
image->retain();
}
void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
static Matrix t;
glPushMatrix();
t.setTransformation(x, y, angle, sx, sy, ox, oy);
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
glMultMatrixf((const GLfloat*)t.getElements());
image->bind();
+3 -3
View File
@@ -89,8 +89,8 @@ namespace opengl
bool loadVolatile();
void unloadVolatile();
void add(float x, float y, float a, float sx, float sy, float ox, float oy);
void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy);
void add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky);
void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky);
void clear();
void * lock();
@@ -99,7 +99,7 @@ namespace opengl
void setImage(Image * newimage);
// Implements Drawable.
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
private:
+24 -4
View File
@@ -694,6 +694,8 @@ namespace opengl
* @param sy The scale factor along the y-axis. (1 = normal).
* @param ox The offset along the x-axis.
* @param oy The offset along the y-axis.
* @param kx Shear along the x-axis.
* @param ky Shear along the y-axis.
**/
int w_draw(lua_State * L)
{
@@ -705,7 +707,9 @@ namespace opengl
float sy = (float)luaL_optnumber(L, 6, sx);
float ox = (float)luaL_optnumber(L, 7, 0);
float oy = (float)luaL_optnumber(L, 8, 0);
drawable->draw(x, y, angle, sx, sy, ox, oy);
float kx = (float)luaL_optnumber(L, 9, 0);
float ky = (float)luaL_optnumber(L, 10, 0);
drawable->draw(x, y, angle, sx, sy, ox, oy, kx, ky);
return 0;
}
@@ -721,6 +725,8 @@ namespace opengl
* @param sy The scale factor along the y-axis. (1 = normal).
* @param ox The offset along the x-axis.
* @param oy The offset along the y-axis.
* @param kx Shear along the x-axis.
* @param ky Shear along the y-axis.
**/
int w_drawq(lua_State * L)
{
@@ -733,7 +739,9 @@ namespace opengl
float sy = (float)luaL_optnumber(L, 7, sx);
float ox = (float)luaL_optnumber(L, 8, 0);
float oy = (float)luaL_optnumber(L, 9, 0);
image->drawq(q, x, y, angle, sx, sy, ox, oy);
float kx = (float)luaL_optnumber(L, 9, 0);
float ky = (float)luaL_optnumber(L, 10, 0);
image->drawq(q, x, y, angle, sx, sy, ox, oy, kx, ky);
return 0;
}
@@ -759,9 +767,13 @@ namespace opengl
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);
try
{
instance->print(str, x, y, angle, sx, sy);
instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);
}
catch (love::Exception e)
{
@@ -1013,6 +1025,14 @@ namespace opengl
return 0;
}
int w_shear(lua_State * L)
{
float kx = (float)luaL_checknumber(L, 1);
float ky = (float)luaL_checknumber(L, 2);
instance->shear(kx, ky);
return 0;
}
int w_hasFocus(lua_State * L)
{
luax_pushboolean(L, instance->hasFocus());
@@ -1103,8 +1123,8 @@ namespace opengl
{ "pop", w_pop },
{ "rotate", w_rotate },
{ "scale", w_scale },
{ "translate", w_translate },
{ "shear", w_shear },
{ "hasFocus", w_hasFocus },
@@ -102,6 +102,7 @@ namespace opengl
int w_rotate(lua_State * L);
int w_scale(lua_State * L);
int w_translate(lua_State * L);
int w_shear(lua_State * L);
int w_hasFocus(lua_State * L);
extern "C" LOVE_EXPORT int luaopen_love_graphics(lua_State * L);
@@ -41,7 +41,9 @@ namespace opengl
float sy = (float)luaL_optnumber(L, 6, sx);
float ox = (float)luaL_optnumber(L, 7, 0);
float oy = (float)luaL_optnumber(L, 8, 0);
t->add(x, y, angle, sx, sy, ox, oy);
float kx = (float)luaL_optnumber(L, 10, 0);
float ky = (float)luaL_optnumber(L, 11, 0);
t->add(x, y, angle, sx, sy, ox, oy, kx, ky);
return 0;
}
@@ -56,7 +58,9 @@ namespace opengl
float sy = (float)luaL_optnumber(L, 7, sx);
float ox = (float)luaL_optnumber(L, 8, 0);
float oy = (float)luaL_optnumber(L, 9, 0);
t->addq(q, x, y, angle, sx, sy, ox, oy);
float kx = (float)luaL_optnumber(L, 10, 0);
float ky = (float)luaL_optnumber(L, 11, 0);
t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky);
return 0;
}