Renamed Frame to Quad; added drawq/addq; fixed some bugs.

This commit is contained in:
rude
2009-08-17 23:35:04 +02:00
parent d4a97d49f3
commit c52c218ba1
25 changed files with 317 additions and 299 deletions
+2 -1
View File
@@ -22,8 +22,9 @@
#define LOVE_GRAPHICS_OPENGL_FONT_H
// LOVE
#include <filesystem/File.h>
#include <common/Object.h>
#include <graphics/Volatile.h>
#include <filesystem/File.h>
namespace love
{
+7 -2
View File
@@ -395,9 +395,14 @@ namespace opengl
return image;
}
Frame * Graphics::newFrame(int x, int y, int w, int h, int sw, int sh)
Quad * Graphics::newQuad(int x, int y, int w, int h, int sw, int sh)
{
return new Frame(x, y, w, h, sw, sh);
Quad::Viewport v;
v.x = x;
v.y = y;
v.w = w;
v.h = h;
return new Quad(v, sw, sh);
}
Font * Graphics::newFont(Data * data, int size)
+2 -2
View File
@@ -36,7 +36,7 @@
#include "Image.h"
#include "TrueTypeFont.h"
#include "ImageFont.h"
#include "Frame.h"
#include "Quad.h"
#include "SpriteBatch.h"
namespace love
@@ -239,7 +239,7 @@ namespace opengl
/**
* Creates a Frame
**/
Frame * newFrame(int x, int y, int w, int h, int sw, int sh);
Quad * newQuad(int x, int y, int w, int h, int sw, int sh);
/**
* Creates a Font object.
+3 -13
View File
@@ -110,21 +110,11 @@ namespace opengl
t.setTransformation(x, y, angle, sx, sy, ox, oy);
drawv(t, vertices);
}
void Image::draws(float x, float y, float angle, float sx, float sy, float ox, float oy, float rx, float ry, float rw, float rh) const
void Image::drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy) const
{
static Matrix t;
static vertex cache[4];
getRectangleVertices((int)rx, (int)ry, (int)rw, (int)rh, cache);
t.setTransformation(x, y, angle, sx, sy, ox, oy);
drawv(t, cache);
}
void Image::drawf(float x, float y, float angle, float sx, float sy, float ox, float oy, Frame * frame) const
{
static Matrix t;
const vertex * v = frame->getVertices();
const vertex * v = quad->getVertices();
t.setTransformation(x, y, angle, sx, sy, ox, oy);
drawv(t, v);
+6 -18
View File
@@ -22,13 +22,12 @@
#define LOVE_GRAPHICS_OPENGL_IMAGE_H
// LOVE
#include <common/config.h>
#include <image/ImageData.h>
#include <common/math.h>
#include <graphics/Image.h>
#include "Frame.h"
#include <common/Matrix.h>
#include <common/math.h>
#include <common/config.h>
#include <image/ImageData.h>
#include <graphics/Image.h>
#include "Quad.h"
// OpenGL
#include "GLee.h"
@@ -105,23 +104,12 @@ namespace opengl
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
/**
* This function draws a section of the image.
*
* @copydetails Drawable::draw()
* @param rx The upper-left corner of the source rectangle along the x-axis.
* @param ry The upper-left corner of the source rectangle along the y-axis.
* @param rw The width of the source rectangle.
* @param rw The height of the source rectangle.
**/
void draws(float x, float y, float angle, float sx, float sy, float ox, float oy, float rx, float ry, float rw, float rh) const;
/**
* This function draws a section of the image using a Frame object.
* This function draws a section of the image using a Quad object.
*
* @copydetails Image::draws()
* @param frame Represents the region of the Image to draw.
**/
void drawf(float x, float y, float angle, float sx, float sy, float ox, float oy, Frame * frame) const;
void drawq(Quad * quad, float x, float y, float angle, float sx, float sy, float ox, float oy) const;
/**
* Sets the filter mode.
@@ -18,7 +18,7 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Frame.h"
#include "Quad.h"
#include <common/Matrix.h>
// STD
@@ -30,26 +30,51 @@ namespace graphics
{
namespace opengl
{
Frame::Frame(int x, int y, int w, int h, int sw, int sh)
Quad::Quad(const Viewport & v, int sw, int sh)
: sw(sw), sh(sh)
{
memset(vertices, 255, sizeof(vertex)*4);
vertices[0].x = 0; vertices[0].y = 0;
vertices[1].x = 0; vertices[1].y = (float)h;
vertices[2].x = (float)w; vertices[2].y = (float)h;
vertices[3].x = (float)w; vertices[3].y = 0;
vertices[0].s = (float)x/(float)sw; vertices[0].t = (float)y/(float)sh;
vertices[1].s = (float)x/(float)sw; vertices[1].t = (float)(y+h)/(float)sh;
vertices[2].s = (float)(x+w)/(float)sw; vertices[2].t = (float)(y+h)/(float)sh;
vertices[3].s = (float)(x+w)/(float)sw; vertices[3].t = (float)y/(float)sh;
refresh(v, sw, sh);
}
Frame::~Frame()
Quad::~Quad()
{
}
void Quad::refresh(const Viewport & v, int sw, int sh)
{
viewport = v;
vertices[0].x = 0;
vertices[0].y = 0;
vertices[1].x = 0;
vertices[1].y = (float)v.h;
vertices[2].x = (float)v.w;
vertices[2].y = (float)v.h;
vertices[3].x = (float)v.w;
vertices[3].y = 0;
vertices[0].s = (float)v.x/(float)sw;
vertices[0].t = (float)v.y/(float)sh;
vertices[1].s = (float)v.x/(float)sw;
vertices[1].t = (float)(v.y+v.h)/(float)sh;
vertices[2].s = (float)(v.x+v.w)/(float)sw;
vertices[2].t = (float)(v.y+v.h)/(float)sh;
vertices[3].s = (float)(v.x+v.w)/(float)sw;
vertices[3].t = (float)v.y/(float)sh;
}
void Quad::setViewport(const Quad::Viewport & v)
{
refresh(v, sw, sh);
}
Quad::Viewport Quad::getViewport() const
{
return viewport;
}
void Frame::flip(bool x, bool y)
void Quad::flip(bool x, bool y)
{
vertex temp[4];
if (x)
@@ -70,7 +95,7 @@ namespace opengl
}
}
const vertex * Frame::getVertices() const
const vertex * Quad::getVertices() const
{
return vertices;
}
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_GRAPHICS_OPENGL_FRAME_H
#define LOVE_GRAPHICS_OPENGL_FRAME_H
#ifndef LOVE_GRAPHICS_OPENGL_QUAD_H
#define LOVE_GRAPHICS_OPENGL_QUAD_H
// LOVE
#include <common/Object.h>
@@ -32,37 +32,50 @@ namespace graphics
{
namespace opengl
{
class Frame : public Object
class Quad : public Object
{
private:
vertex vertices[4];
public:
public:
/**
* Creates a new Frame of size (w,h), using (x,y) as the top-left
* anchor point in the source image. The size of the source image is
* is specified by (sw,sh).
*
* @param x Frame source position along the x-axis.
* @param y Frame source position along the y-axis.
* @param w Frame width.
* @param h Frame width.
* @param sw Width of the source image.
* @param sh Height of the source image.
**/
Frame(int x, int y, int w, int h, int sw, int sh);
struct Viewport
{
int x, y, w, h;
};
virtual ~Frame();
private:
void flip(bool x, bool y);
vertex vertices[4];
/**
* Gets a pointer to the vertices.
**/
const vertex * getVertices() const;
Viewport viewport;
int sw, sh;
public:
/**
* Creates a new Quad of size (w,h), using (x,y) as the top-left
* anchor point in the source image. The size of the source image is
* is specified by (sw,sh).
*
* @param sw Width of the source image.
* @param sh Height of the source image.
**/
Quad(const Viewport & v, int sw, int sh);
virtual ~Quad();
void refresh(const Viewport & v, int sw, int sh);
void setViewport(const Viewport & v);
Viewport getViewport() const;
void flip(bool x, bool y);
/**
* Gets a pointer to the vertices.
**/
const vertex * getVertices() const;
};
} // opengl
} // graphics
} // love
#endif // LOVE_GRAPHICS_OPENGL_FRAME_H
#endif // LOVE_GRAPHICS_OPENGL_QUAD_H
+48 -12
View File
@@ -25,6 +25,7 @@
// LOVE
#include "Image.h"
#include "Quad.h"
namespace love
{
@@ -88,6 +89,7 @@ namespace opengl
// Get a pointer to the correct insertion position.
vertex * v = vertices + next*4;
// Needed for texture coordinates.
memcpy(v, image->getVertices(), sizeof(vertex)*4);
// Transform.
@@ -95,18 +97,30 @@ namespace opengl
t.setTransformation(x, y, a, sx, sy, ox, oy);
t.transform(v, v, 4);
if(lockp != 0)
{
// Copy into mapped memory if buffer is locked.
memcpy(lockp + (next*4), v, sizeof(vertex)*4);
}
else
{
// ... use glBufferSubData otherwise.
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferSubData(GL_ARRAY_BUFFER, (next*4)*sizeof(vertex), sizeof(vertex)*4, v);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
addv(t, v);
// Increment counter.
next++;
}
}
void SpriteBatch::addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy)
{
// Only do this if there's a free slot.
if(next < size)
{
// Get a pointer to the correct insertion position.
vertex * v = vertices + next*4;
// Needed for colors.
memcpy(v, quad->getVertices(), sizeof(vertex)*4);
// Transform.
Matrix t;
t.setTransformation(x, y, a, sx, sy, ox, oy);
t.transform(v, quad->getVertices(), 4);
addv(t, v);
// Increment counter.
next++;
@@ -169,6 +183,28 @@ namespace opengl
glPopMatrix();
}
void SpriteBatch::addv(const Matrix & t, const vertex * v)
{
// Get a pointer to the correct insertion position.
vertex * dst = vertices + next*4;
// Transform.
t.transform(dst, dst, 4);
if(lockp != 0)
{
// Copy into mapped memory if buffer is locked.
memcpy(lockp + (next*4), dst, sizeof(vertex)*4);
}
else
{
// ... use glBufferSubData otherwise.
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferSubData(GL_ARRAY_BUFFER, (next*4)*sizeof(vertex), sizeof(vertex)*4, dst);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
} // opengl
} // graphics
} // love
+6 -2
View File
@@ -43,6 +43,7 @@ namespace opengl
{
// Forward declarations.
class Image;
class Quad;
class SpriteBatch : public Drawable
{
@@ -84,8 +85,7 @@ namespace opengl
virtual ~SpriteBatch();
void add(float x, float y, float a, float sx, float sy, float ox, float oy);
//void adds(float x, float y, float a, float sx, float sy, float ox, float oy, float rx, float ry, float rw, float rh);
//void addf(float x, float y, float a, float sx, float sy, float ox, float oy, Frame * frame);
void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy);
void clear();
void * lock();
@@ -94,6 +94,10 @@ namespace opengl
// Implements Drawable.
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
private:
void addv(const Matrix & t, const vertex * v);
}; // SpriteBatch
} // opengl
@@ -1,54 +0,0 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "wrap_Frame.h"
namespace love
{
namespace graphics
{
namespace opengl
{
Frame * luax_checkframe(lua_State * L, int idx)
{
return luax_checktype<Frame>(L, idx, "Frame", GRAPHICS_FRAME_T);
}
int w_Frame_flip(lua_State *L)
{
Frame *frame = luax_checktype<Frame>(L, 1, "Frame", GRAPHICS_FRAME_T);
frame->flip(luax_toboolean(L, 2), luax_toboolean(L, 3));
return 0;
}
static const luaL_Reg w_Frame_functions[] = {
{ "flip", w_Frame_flip },
{ 0, 0 }
};
int luaopen_frame(lua_State * L)
{
return luax_register_type(L, "Frame", w_Frame_functions);
}
} // opengl
} // graphics
} // love
+17 -51
View File
@@ -168,7 +168,7 @@ namespace opengl
return 1;
}
int w_newFrame(lua_State * L)
int w_newQuad(lua_State * L)
{
int x = luaL_checkint(L, 1);
int y = luaL_checkint(L, 2);
@@ -177,12 +177,12 @@ namespace opengl
int sw = luaL_checkint(L, 5);
int sh = luaL_checkint(L, 6);
Frame * frame = instance->newFrame(x, y, w, h, sw, sh);
Quad * frame = instance->newQuad(x, y, w, h, sw, sh);
if (frame == 0)
return luaL_error(L, "Could not create frame.");
luax_newtype(L, "Frame", GRAPHICS_FRAME_T, (void*)frame);
luax_newtype(L, "Quad", GRAPHICS_QUAD_T, (void*)frame);
return 1;
}
@@ -481,40 +481,7 @@ namespace opengl
}
/**
* Draws an Image at the specified coordinates, with rotation and
* scaling along both axes.
* @param x The x-coordinate.
* @param y The y-coordinate.
* @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 offset along the x-axis.
* @param oy The offset along the y-axis.
* @param rx The upper-left corner of the source rectangle along the x-axis.
* @param ry The upper-left corner of the source rectangle along the y-axis.
* @param rw The width of the source rectangle.
* @param rw The height of the source rectangle.
**/
int w_draws(lua_State * L)
{
Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
float x = (float)luaL_optnumber(L, 2, 0.0f);
float y = (float)luaL_optnumber(L, 3, 0.0f);
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);
float oy = (float)luaL_optnumber(L, 8, 0);
float rx = (float)luaL_optnumber(L, 9, 0);
float ry = (float)luaL_optnumber(L, 10, 0);
float rw = (float)luaL_optnumber(L, 11, image->getWidth());
float rh = (float)luaL_optnumber(L, 12, image->getHeight());
image->draws(x, y, angle, sx, sy, ox, oy, rx, ry, rw, rh);
return 0;
}
/**
* Draws an Frame of an Image at the specified coordinates,
* Draws an Quad of an Image at the specified coordinates,
* with rotation and scaling along both axes.
*
* @param x The x-coordinate.
@@ -524,20 +491,20 @@ 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 f The Frame to dra.
* @param f The Quad to dra.
**/
int w_drawf(lua_State * L)
int w_drawq(lua_State * L)
{
Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
float x = (float)luaL_checknumber(L, 2);
float y = (float)luaL_checknumber(L, 3);
float angle = (float)luaL_checknumber(L, 4);
float sx = (float)luaL_checknumber(L, 5);
float sy = (float)luaL_checknumber(L, 6);
float ox = (float)luaL_checknumber(L, 7);
float oy = (float)luaL_checknumber(L, 8);
Frame * f = luax_checkframe(L, 9);
image->drawf(x, y, angle, sx, sy, ox, oy, f);
Quad * q = luax_checkframe(L, 2);
float x = (float)luaL_checknumber(L, 3);
float y = (float)luaL_checknumber(L, 4);
float angle = (float)luaL_checknumber(L, 5);
float sx = (float)luaL_checknumber(L, 6);
float sy = (float)luaL_checknumber(L, 7);
float ox = (float)luaL_checknumber(L, 8);
float oy = (float)luaL_checknumber(L, 9);
image->drawq(q, x, y, angle, sx, sy, ox, oy);
return 0;
}
@@ -715,7 +682,7 @@ namespace opengl
{ "newImage", w_newImage },
{ "newGlyph", w_newGlyph },
{ "newFrame", w_newFrame },
{ "newQuad", w_newQuad },
{ "newFont", w_newFont },
{ "newImageFont", w_newImageFont },
{ "newSpriteBatch", w_newSpriteBatch },
@@ -747,8 +714,7 @@ namespace opengl
{ "getMaxPointSize", w_getMaxPointSize },
{ "draw", w_draw },
{ "draws", w_draws },
{ "drawf", w_drawf },
{ "drawq", w_drawq },
{ "drawTest", w_drawTest },
{ "print1", w_print1 },
+2 -2
View File
@@ -25,7 +25,7 @@
#include "wrap_Font.h"
#include "wrap_Image.h"
#include "wrap_Glyph.h"
#include "wrap_Frame.h"
#include "wrap_Quad.h"
#include "wrap_SpriteBatch.h"
#include "Graphics.h"
@@ -78,7 +78,7 @@ namespace opengl
int w_getPointStyle(lua_State * L);
int w_getMaxPointSize(lua_State * L);
int w_draw(lua_State * L);
int w_draws(lua_State * L);
int w_drawq(lua_State * L);
int w_drawTest(lua_State * L);
int w_print1(lua_State * L);
int w_printf1(lua_State * L);
+79
View File
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "wrap_Quad.h"
namespace love
{
namespace graphics
{
namespace opengl
{
Quad * luax_checkframe(lua_State * L, int idx)
{
return luax_checktype<Quad>(L, idx, "Quad", GRAPHICS_QUAD_T);
}
int w_Quad_flip(lua_State *L)
{
Quad * quad = luax_checktype<Quad>(L, 1, "Quad", GRAPHICS_QUAD_T);
quad->flip(luax_toboolean(L, 2), luax_toboolean(L, 3));
return 0;
}
int w_Quad_setViewport(lua_State * L)
{
Quad * quad = luax_checktype<Quad>(L, 1, "Quad", GRAPHICS_QUAD_T);
Quad::Viewport v;
v.x = luaL_checkint(L, 2);
v.y = luaL_checkint(L, 3);
v.w = luaL_checkint(L, 4);
v.h = luaL_checkint(L, 5);
quad->setViewport(v);
return 0;
}
int w_Quad_getViewport(lua_State * L)
{
Quad * quad = luax_checktype<Quad>(L, 1, "Quad", GRAPHICS_QUAD_T);
Quad::Viewport v = quad->getViewport();
lua_pushinteger(L, v.x);
lua_pushinteger(L, v.y);
lua_pushinteger(L, v.w);
lua_pushinteger(L, v.h);
return 4;
}
static const luaL_Reg w_Quad_functions[] = {
{ "flip", w_Quad_flip },
{ "setViewport", w_Quad_setViewport },
{ "getViewport", w_Quad_getViewport },
{ 0, 0 }
};
int luaopen_frame(lua_State * L)
{
return luax_register_type(L, "Quad", w_Quad_functions);
}
} // opengl
} // graphics
} // love
@@ -18,12 +18,12 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_GRAPHICS_OPENGL_WRAP_FRAME_H
#define LOVE_GRAPHICS_OPENGL_WRAP_FRAME_H
#ifndef LOVE_GRAPHICS_OPENGL_WRAP_QUAD_H
#define LOVE_GRAPHICS_OPENGL_WRAP_QUAD_H
// LOVE
#include <common/runtime.h>
#include "Frame.h"
#include "Quad.h"
namespace love
{
@@ -31,12 +31,14 @@ namespace graphics
{
namespace opengl
{
Frame * luax_checkframe(lua_State * L, int idx);
int w_Frame_flip(lua_State *L);
Quad * luax_checkframe(lua_State * L, int idx);
int w_Quad_flip(lua_State *L);
int w_Quad_setViewport(lua_State * L);
int w_Quad_getViewport(lua_State * L);
int luaopen_frame(lua_State * L);
} // opengl
} // graphics
} // love
#endif // LOVE_GRAPHICS_OPENGL_WRAP_FRAME_H
#endif // LOVE_GRAPHICS_OPENGL_WRAP_QUAD_H
@@ -45,6 +45,21 @@ namespace opengl
return 0;
}
int w_SpriteBatch_addq(lua_State * L)
{
SpriteBatch * t = luax_checkspritebatch(L, 1);
Quad * q = luax_checktype<Quad>(L, 2, "Quad", GRAPHICS_QUAD_T);
float x = (float)luaL_optnumber(L, 3, 0.0f);
float y = (float)luaL_optnumber(L, 4, 0.0f);
float angle = (float)luaL_optnumber(L, 5, 0.0f);
float sx = (float)luaL_optnumber(L, 6, 1.0f);
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);
return 0;
}
int w_SpriteBatch_clear(lua_State * L)
{
SpriteBatch * t = luax_checkspritebatch(L, 1);
@@ -68,6 +83,7 @@ namespace opengl
static const luaL_Reg functions[] = {
{ "add", w_SpriteBatch_add },
{ "addq", w_SpriteBatch_addq },
{ "clear", w_SpriteBatch_clear },
{ "lock", w_SpriteBatch_lock },
{ "unlock", w_SpriteBatch_unlock },
@@ -32,6 +32,7 @@ namespace opengl
{
SpriteBatch * luax_checkspritebatch(lua_State * L, int idx);
int w_SpriteBatch_add(lua_State * L);
int w_SpriteBatch_addq(lua_State * L);
int w_SpriteBatch_clear(lua_State * L);
int w_SpriteBatch_lock(lua_State * L);
int w_SpriteBatch_unlock(lua_State * L);