Merged default into minor

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2014-09-20 00:34:34 -03:00
20 changed files with 311 additions and 119 deletions
+44 -35
View File
@@ -86,6 +86,8 @@ ParticleSystem::ParticleSystem(Texture *texture, uint32 size)
, radialAccelerationMax(0)
, tangentialAccelerationMin(0)
, tangentialAccelerationMax(0)
, linearDampingMin(0.0f)
, linearDampingMax(0.0f)
, sizeVariation(0)
, rotationMin(0)
, rotationMax(0)
@@ -135,6 +137,8 @@ ParticleSystem::ParticleSystem(const ParticleSystem &p)
, radialAccelerationMax(p.radialAccelerationMax)
, tangentialAccelerationMin(p.tangentialAccelerationMin)
, tangentialAccelerationMax(p.tangentialAccelerationMax)
, linearDampingMin(p.linearDampingMin)
, linearDampingMax(p.linearDampingMax)
, sizes(p.sizes)
, sizeVariation(p.sizeVariation)
, rotationMin(p.rotationMin)
@@ -284,6 +288,10 @@ void ParticleSystem::initParticle(Particle *p, float t)
max = tangentialAccelerationMax;
p->tangentialAcceleration = (float) rng.random(min, max);
min = linearDampingMin;
max = linearDampingMax;
p->linearDamping = (float) rng.random(min, max);
p->sizeOffset = (float) rng.random(sizeVariation); // time offset for size change
p->sizeIntervalSize = (1.0f - (float) rng.random(sizeVariation)) - p->sizeOffset;
p->size = sizes[(size_t)(p->sizeOffset - .5f) * (sizes.size() - 1)];
@@ -460,12 +468,10 @@ void ParticleSystem::setParticleLifetime(float min, float max)
particleLifeMax = max;
}
void ParticleSystem::getParticleLifetime(float *min, float *max) const
void ParticleSystem::getParticleLifetime(float &min, float &max) const
{
if (min)
*min = particleLifeMin;
if (max)
*max = particleLifeMax;
min = particleLifeMin;
max = particleLifeMax;
}
void ParticleSystem::setPosition(float x, float y)
@@ -531,12 +537,10 @@ void ParticleSystem::setSpeed(float min, float max)
speedMax = max;
}
void ParticleSystem::getSpeed(float *min, float *max) const
void ParticleSystem::getSpeed(float &min, float &max) const
{
if (min)
*min = speedMin;
if (max)
*max = speedMax;
min = speedMin;
max = speedMax;
}
void ParticleSystem::setLinearAcceleration(float x, float y)
@@ -551,12 +555,10 @@ void ParticleSystem::setLinearAcceleration(float xmin, float ymin, float xmax, f
linearAccelerationMax = love::Vector(xmax, ymax);
}
void ParticleSystem::getLinearAcceleration(love::Vector *min, love::Vector *max) const
void ParticleSystem::getLinearAcceleration(love::Vector &min, love::Vector &max) const
{
if (min)
*min = linearAccelerationMin;
if (max)
*max = linearAccelerationMax;
min = linearAccelerationMin;
max = linearAccelerationMax;
}
void ParticleSystem::setRadialAcceleration(float acceleration)
@@ -570,12 +572,10 @@ void ParticleSystem::setRadialAcceleration(float min, float max)
radialAccelerationMax = max;
}
void ParticleSystem::getRadialAcceleration(float *min, float *max) const
void ParticleSystem::getRadialAcceleration(float &min, float &max) const
{
if (min)
*min = radialAccelerationMin;
if (max)
*max = radialAccelerationMax;
min = radialAccelerationMin;
max = radialAccelerationMax;
}
void ParticleSystem::setTangentialAcceleration(float acceleration)
@@ -589,12 +589,22 @@ void ParticleSystem::setTangentialAcceleration(float min, float max)
tangentialAccelerationMax = max;
}
void ParticleSystem::getTangentialAcceleration(float *min, float *max) const
void ParticleSystem::getTangentialAcceleration(float &min, float &max) const
{
if (min)
*min = tangentialAccelerationMin;
if (max)
*max = tangentialAccelerationMax;
min = tangentialAccelerationMin;
max = tangentialAccelerationMax;
}
void ParticleSystem::setLinearDamping(float min, float max)
{
linearDampingMin = min;
linearDampingMax = max;
}
void ParticleSystem::getLinearDamping(float &min, float &max) const
{
min = linearDampingMin;
max = linearDampingMax;
}
void ParticleSystem::setSize(float size)
@@ -634,12 +644,10 @@ void ParticleSystem::setRotation(float min, float max)
rotationMax = max;
}
void ParticleSystem::getRotation(float *min, float *max) const
void ParticleSystem::getRotation(float &min, float &max) const
{
if (min)
*min = rotationMin;
if (max)
*max = rotationMax;
min = rotationMin;
max = rotationMax;
}
void ParticleSystem::setSpin(float spin)
@@ -654,12 +662,10 @@ void ParticleSystem::setSpin(float start, float end)
spinEnd = end;
}
void ParticleSystem::getSpin(float *start, float *end) const
void ParticleSystem::getSpin(float &start, float &end) const
{
if (start)
*start = spinStart;
if (end)
*end = spinEnd;
start = spinStart;
end = spinEnd;
}
void ParticleSystem::setSpinVariation(float variation)
@@ -931,6 +937,9 @@ void ParticleSystem::update(float dt)
// Update velocity.
p->velocity += (radial + tangential + p->linearAcceleration) * dt;
// Apply damping.
p->velocity *= 1.0f / (1.0f + p->linearDamping * dt);
// Modify position.
ppos += p->velocity * dt;
+23 -7
View File
@@ -162,7 +162,7 @@ public:
* @param[out] min The minimum life.
* @param[out] max The maximum life.
**/
void getParticleLifetime(float *min, float *max) const;
void getParticleLifetime(float &min, float &max) const;
/**
* Sets the position of the center of the emitter.
@@ -249,7 +249,7 @@ public:
* @param[out] min The minimum speed.
* @param[out] max The maximum speed.
**/
void getSpeed(float *min, float *max) const;
void getSpeed(float &min, float &max) const;
/**
* Sets the linear acceleration (the acceleration along the x and y axes).
@@ -272,7 +272,7 @@ public:
* @param[out] min The minimum acceleration.
* @param[out] max The maximum acceleration.
**/
void getLinearAcceleration(love::Vector *min, love::Vector *max) const;
void getLinearAcceleration(love::Vector &min, love::Vector &max) const;
/**
* Sets the radial acceleration (the acceleration towards the particle emitter).
@@ -292,7 +292,7 @@ public:
* @param[out] min The minimum amount of radial acceleration.
* @param[out] max The maximum amount of radial acceleration.
**/
void getRadialAcceleration(float *min, float *max) const;
void getRadialAcceleration(float &min, float &max) const;
/**
* Sets the tangential acceleration (the acceleration perpendicular to the particle's direction).
@@ -312,7 +312,18 @@ public:
* @param[out] min The minimum tangential acceleration.
* @param[out] max The maximum tangential acceleration.
**/
void getTangentialAcceleration(float *min, float *max) const;
void getTangentialAcceleration(float &min, float &max) const;
/**
* Sets the amount of linear damping. Damping reduces the velocity of
* particles over time. A value of 0 corresponds to no damping.
**/
void setLinearDamping(float min, float max);
/**
* Gets the current amount of linear damping.
**/
void getLinearDamping(float &min, float &max) const;
/**
* Sets the size of the sprite (1.0 being the default size).
@@ -360,7 +371,7 @@ public:
* @param[out] min The minimum initial rotation.
* @param[out] max The maximum initial rotation.
**/
void getRotation(float *min, float *max) const;
void getRotation(float &min, float &max) const;
/**
* Sets the spin of the sprite.
@@ -380,7 +391,7 @@ public:
* @param[out] start The initial spin, in radians / s.
* @param[out] end The final spin, in radians / s.
**/
void getSpin(float *start, float *end) const;
void getSpin(float &start, float &end) const;
/**
* Sets the variation of the start spin (0 being no variation and 1 being a random spin between start and end).
@@ -533,6 +544,8 @@ protected:
float radialAcceleration;
float tangentialAcceleration;
float linearDamping;
float size;
float sizeOffset;
float sizeIntervalSize;
@@ -619,6 +632,9 @@ protected:
float tangentialAccelerationMin;
float tangentialAccelerationMax;
float linearDampingMin;
float linearDampingMax;
// Size.
std::vector<float> sizes;
float sizeVariation;
+26 -53
View File
@@ -47,7 +47,7 @@ SpriteBatch::SpriteBatch(Texture *texture, int size, int usage)
, next(0)
, color(0)
, array_buf(nullptr)
, element_buf(nullptr)
, element_buf(size)
, buffer_used_offset(0)
, buffer_used_size(0)
{
@@ -74,18 +74,15 @@ SpriteBatch::SpriteBatch(Texture *texture, int size, int usage)
try
{
array_buf = VertexBuffer::Create(vertex_size, GL_ARRAY_BUFFER, gl_usage);
element_buf = new VertexIndex(size);
}
catch (love::Exception &)
{
delete array_buf;
delete element_buf;
throw;
}
catch (std::bad_alloc &)
{
delete array_buf;
delete element_buf;
throw love::Exception("Out of memory.");
}
}
@@ -94,7 +91,6 @@ SpriteBatch::~SpriteBatch()
{
delete color;
delete array_buf;
delete element_buf;
}
int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
@@ -103,20 +99,9 @@ int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, fl
if ((index == -1 && next >= size) || index < -1 || index >= size)
return -1;
Vertex sprite[4];
Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
// Needed for colors.
memcpy(sprite, texture->getVertices(), sizeof(Vertex) * 4);
// Transform.
Matrix t;
t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
t.transform(sprite, sprite, 4);
if (color)
setColorv(sprite, *color);
addv(sprite, (index == -1) ? next : index);
addv(texture->getVertices(), t, (index == -1) ? next : index);
// Increment counter.
if (index == -1)
@@ -131,19 +116,9 @@ int SpriteBatch::addq(Quad *quad, float x, float y, float a, float sx, float sy,
if ((index == -1 && next >= size) || index < -1 || index >= next)
return -1;
Vertex sprite[4];
Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
// Needed for colors.
memcpy(sprite, quad->getVertices(), sizeof(Vertex) * 4);
Matrix t;
t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
t.transform(sprite, sprite, 4);
if (color)
setColorv(sprite, *color);
addv(sprite, (index == -1) ? next : index);
addv(quad->getVertices(), t, (index == -1) ? next : index);
// Increment counter.
if (index == -1)
@@ -171,7 +146,7 @@ void SpriteBatch::setTexture(Texture *newtexture)
texture.set(newtexture);
}
Texture *SpriteBatch::getTexture()
Texture *SpriteBatch::getTexture() const
{
return texture.get();
}
@@ -216,40 +191,32 @@ void SpriteBatch::setBufferSize(int newsize)
}
size_t vertex_size = sizeof(Vertex) * 4 * newsize;
VertexBuffer *new_array_buf = nullptr;
VertexIndex *new_element_buf = nullptr;
try
{
new_array_buf = VertexBuffer::Create(vertex_size, array_buf->getTarget(), array_buf->getUsage());
new_element_buf = new VertexIndex(newsize);
// Copy as much of the old data into the new VertexBuffer as can fit.
VertexBuffer::Bind bind(*new_array_buf);
void *new_data = new_array_buf->map();
memcpy(new_data, old_data, sizeof(Vertex) * 4 * std::min(newsize, size));
element_buf = VertexIndex(newsize);
}
catch (love::Exception &)
{
delete new_array_buf;
delete new_element_buf;
throw;
}
// Copy as much of the old data into the new VertexBuffer as can fit.
{
VertexBuffer::Bind bind(*new_array_buf);
new_array_buf->fill(0, sizeof(Vertex) * 4 * std::min(newsize, size), old_data);
}
// We don't need to unmap the old VertexBuffer since we're deleting it.
delete array_buf;
delete element_buf;
array_buf = new_array_buf;
element_buf = new_element_buf;
size = newsize;
next = std::min(next, newsize);
// The new VertexBuffer isn't mapped, so we should reset these variables.
buffer_used_offset = buffer_used_size = 0;
}
int SpriteBatch::getBufferSize() const
@@ -266,8 +233,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
if (next == 0)
return;
static Matrix t;
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
Matrix t(x, y, angle, sx, sy, ox, oy, kx, ky);
OpenGL::TempTransform transform(gl);
transform.get() *= t;
@@ -275,7 +241,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
texture->predraw();
VertexBuffer::Bind array_bind(*array_buf);
VertexBuffer::Bind element_bind(*element_buf->getVertexBuffer());
VertexBuffer::Bind element_bind(*element_buf.getVertexBuffer());
// Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
array_buf->unmap(buffer_used_offset, buffer_used_size);
@@ -297,7 +263,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
gl.prepareDraw();
gl.drawElements(GL_TRIANGLES, element_buf->getIndexCount(next), element_buf->getType(), element_buf->getPointer(0));
gl.drawElements(GL_TRIANGLES, element_buf.getIndexCount(next), element_buf.getType(), element_buf.getPointer(0));
glDisableVertexAttribArray(ATTRIB_TEXCOORD);
glDisableVertexAttribArray(ATTRIB_POS);
@@ -311,9 +277,16 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
texture->postdraw();
}
void SpriteBatch::addv(const Vertex *v, int index)
void SpriteBatch::addv(const Vertex *v, const Matrix &m, int index)
{
static const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
// Needed for colors.
Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
m.transform(sprite, sprite, 4);
if (color)
setColorv(sprite, *color);
VertexBuffer::Bind bind(*array_buf);
@@ -321,7 +294,7 @@ void SpriteBatch::addv(const Vertex *v, int index)
// on draw.)
array_buf->map();
array_buf->fill(index * sprite_size, sprite_size, v);
array_buf->fill(index * sprite_size, sprite_size, sprite);
buffer_used_offset = std::min(buffer_used_offset, index * sprite_size);
buffer_used_size = std::max(buffer_used_size, (index + 1) * sprite_size - buffer_used_offset);
+4 -6
View File
@@ -26,13 +26,13 @@
// LOVE
#include "common/math.h"
#include "common/Object.h"
#include "common/Matrix.h"
#include "common/StringMap.h"
#include "graphics/Drawable.h"
#include "graphics/Volatile.h"
#include "graphics/Color.h"
#include "graphics/Quad.h"
#include "VertexBuffer.h"
namespace love
{
@@ -43,8 +43,6 @@ namespace opengl
// Forward declarations.
class Texture;
class VertexBuffer;
class VertexIndex;
class SpriteBatch : public Drawable
{
@@ -68,7 +66,7 @@ public:
void flush();
void setTexture(Texture *newtexture);
Texture *getTexture();
Texture *getTexture() const;
/**
* Set the current color for this SpriteBatch. The sprites added
@@ -115,7 +113,7 @@ public:
private:
void addv(const Vertex *v, int index);
void addv(const Vertex *v, const Matrix &m, int index);
/**
* Set the color for vertices.
@@ -139,7 +137,7 @@ private:
Color *color;
VertexBuffer *array_buf;
VertexIndex *element_buf;
VertexIndex element_buf;
// The portion of the vertex buffer that's been modified while mapped.
size_t buffer_used_offset;
+15 -1
View File
@@ -50,7 +50,7 @@ VertexBuffer::VertexBuffer(size_t size, GLenum target, GLenum usage, MemoryBacki
, backing(backing)
, vbo(0)
, memory_map(nullptr)
, is_dirty(true)
, is_dirty(false)
{
if (getMemoryBacking() == BACKING_FULL)
memory_map = (char *) malloc(getSize());
@@ -249,6 +249,20 @@ VertexIndex::VertexIndex(size_t size)
addSize(size);
}
VertexIndex::VertexIndex(const VertexIndex &other)
: size(other.size)
{
addSize(size);
}
VertexIndex &VertexIndex::operator = (const VertexIndex &other)
{
addSize(other.size);
removeSize(size);
size = other.size;
return *this;
}
VertexIndex::~VertexIndex()
{
removeSize(size);
@@ -344,6 +344,9 @@ public:
*/
VertexIndex(size_t size);
VertexIndex(const VertexIndex &other);
VertexIndex &operator = (const VertexIndex &other);
/**
* Removes an entry from the list of sizes and resizes the VertexBuffer
* if needed.
@@ -28,6 +28,7 @@
#include "scripts/graphics.lua.h"
#include <cassert>
#include <cstring>
namespace love
{
@@ -412,6 +413,19 @@ int w_newShader(lua_State *L)
lua_call(L, 1, 1);
lua_replace(L, i);
}
else
{
// Check if the argument looks like a filepath - we want a nicer
// error for misspelled filepath arguments.
size_t slen = 0;
const char *str = lua_tolstring(L, i, &slen);
if (slen > 0 && slen < 256 && !strchr(str, '\n'))
{
const char *ext = strchr(str, '.');
if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
return luaL_error(L, "Could not open file %s. Does not exist.", str);
}
}
}
bool has_arg1 = lua_isstring(L, 1);
@@ -164,7 +164,7 @@ int w_ParticleSystem_getParticleLifetime(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getParticleLifetime(&min, &max);
t->getParticleLifetime(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
@@ -278,7 +278,7 @@ int w_ParticleSystem_getSpeed(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getSpeed(&min, &max);
t->getSpeed(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
@@ -299,7 +299,7 @@ int w_ParticleSystem_getLinearAcceleration(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
love::Vector min, max;
t->getLinearAcceleration(&min, &max);
t->getLinearAcceleration(min, max);
lua_pushnumber(L, min.x);
lua_pushnumber(L, min.y);
lua_pushnumber(L, max.x);
@@ -320,7 +320,7 @@ int w_ParticleSystem_getRadialAcceleration(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getRadialAcceleration(&min, &max);
t->getRadialAcceleration(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
@@ -339,7 +339,26 @@ int w_ParticleSystem_getTangentialAcceleration(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getTangentialAcceleration(&min, &max);
t->getTangentialAcceleration(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
}
int w_ParticleSystem_setLinearDamping(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
float arg2 = (float)luaL_optnumber(L, 3, arg1);
t->setLinearDamping(arg1, arg2);
return 0;
}
int w_ParticleSystem_getLinearDamping(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getLinearDamping(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
@@ -411,7 +430,7 @@ int w_ParticleSystem_getRotation(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float min, max;
t->getRotation(&min, &max);
t->getRotation(min, max);
lua_pushnumber(L, min);
lua_pushnumber(L, max);
return 2;
@@ -430,7 +449,7 @@ int w_ParticleSystem_getSpin(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
float start, end;
t->getSpin(&start, &end);
t->getSpin(start, end);
lua_pushnumber(L, start);
lua_pushnumber(L, end);
return 2;
@@ -730,6 +749,8 @@ static const luaL_Reg functions[] =
{ "getRadialAcceleration", w_ParticleSystem_getRadialAcceleration },
{ "setTangentialAcceleration", w_ParticleSystem_setTangentialAcceleration },
{ "getTangentialAcceleration", w_ParticleSystem_getTangentialAcceleration },
{ "setLinearDamping", w_ParticleSystem_setLinearDamping },
{ "getLinearDamping", w_ParticleSystem_getLinearDamping },
{ "setSizes", w_ParticleSystem_setSizes },
{ "getSizes", w_ParticleSystem_getSizes },
{ "setSizeVariation", w_ParticleSystem_setSizeVariation },
@@ -63,6 +63,8 @@ int w_ParticleSystem_setRadialAcceleration(lua_State *L);
int w_ParticleSystem_getRadialAcceleration(lua_State *L);
int w_ParticleSystem_setTangentialAcceleration(lua_State *L);
int w_ParticleSystem_getTangentialAcceleration(lua_State *L);
int w_ParticleSystem_setLinearDamping(lua_State *L);
int w_ParticleSystem_getLinearDamping(lua_State *L);
int w_ParticleSystem_setSizes(lua_State *L);
int w_ParticleSystem_getSizes(lua_State *L);
int w_ParticleSystem_setSizeVariation(lua_State *L);