imported Löve GLES branch (changeset 1ba9037e558b)

This commit is contained in:
Martin Felis
2013-12-05 18:05:13 +01:00
parent 41b2db04a7
commit 2644d1ee18
615 changed files with 150300 additions and 0 deletions
+188
View File
@@ -0,0 +1,188 @@
/**
* Copyright (c) 2006-2013 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 "BezierCurve.h"
#include "common/Exception.h"
#include <cmath>
using namespace std;
namespace
{
/**
* Subdivide Bezier polygon.
**/
void subdivide(vector<love::Vector> &points, int k)
{
if (k <= 0)
return;
// subdivision using de casteljau - subdivided control polygons are
// on the 'edges' of the computation scheme, e.g:
//
// ------LEFT------->
// b00 b10 b20 b30
// b01 b11 b21 .---
// b02 b12 .---'
// b03 .---'RIGHT
// <--'
//
// the subdivided control polygon is:
// b00, b10, b20, b30, b21, b12, b03
vector<love::Vector> left, right;
left.reserve(points.size());
right.reserve(points.size());
for (size_t step = 1; step < points.size(); ++step)
{
left.push_back(points[0]);
right.push_back(points[points.size() - step]);
for (size_t i = 0; i < points.size() - step; ++i)
points[i] = (points[i] + points[i+1]) * .5;
}
left.push_back(points[0]);
right.push_back(points[0]);
// recurse
subdivide(left, k-1);
subdivide(right, k-1);
// merge (right is in reversed order)
points.resize(left.size() + right.size() - 1);
for (size_t i = 0; i < left.size(); ++i)
points[i] = left[i];
for (size_t i = 1; i < right.size(); ++i)
points[i-1 + left.size()] = right[right.size() - i - 1];
}
}
namespace love
{
namespace math
{
BezierCurve::BezierCurve(const vector<Vector> &pts)
: controlPoints(pts)
{
}
BezierCurve BezierCurve::getDerivative() const
{
if (getDegree() < 1)
throw Exception("Cannot derive a curve of degree < 1.");
// actually we can, it just doesn't make any sense.
vector<Vector> forward_differences(controlPoints.size()-1);
float degree = float(getDegree());
for (size_t i = 0; i < forward_differences.size(); ++i)
forward_differences[i] = (controlPoints[i+1] - controlPoints[i]) * degree;
return BezierCurve(forward_differences);
}
const Vector &BezierCurve::getControlPoint(int i) const
{
if (i < 0)
i += controlPoints.size();
if (i < 0 || (size_t) i >= controlPoints.size())
throw Exception("Invalid control point index");
return controlPoints[i];
}
void BezierCurve::setControlPoint(int i, const Vector &point)
{
if (i < 0)
i += controlPoints.size();
if (i < 0 || (size_t) i >= controlPoints.size())
throw Exception("Invalid control point index");
controlPoints[i] = point;
}
void BezierCurve::insertControlPoint(const Vector &point, int pos)
{
if (pos < 0)
pos += controlPoints.size() + 1;
if (pos < 0 ||(size_t) pos > controlPoints.size())
throw Exception("Invalid control point index");
controlPoints.insert(controlPoints.begin() + pos, point);
}
void BezierCurve::translate(const Vector &t)
{
for (size_t i = 0; i < controlPoints.size(); ++i)
controlPoints[i] += t;
}
void BezierCurve::rotate(double phi, const Vector &center)
{
float c = cos(phi), s = sin(phi);
for (size_t i = 0; i < controlPoints.size(); ++i)
{
Vector v = controlPoints[i] - center;
controlPoints[i].x = c * v.x - s * v.y + center.x;
controlPoints[i].y = s * v.x + c * v.y + center.y;
}
}
void BezierCurve::scale(double s, const Vector &center)
{
for (size_t i = 0; i < controlPoints.size(); ++i)
controlPoints[i] = (controlPoints[i] - center) * s + center;
}
Vector BezierCurve::evaluate(double t) const
{
if (t < 0 || t > 1)
throw Exception("Invalid evaluation parameter: must be between 0 and 1");
if (controlPoints.size() < 2)
throw Exception("Invalid Bezier curve: Not enough control points.");
// de casteljau
vector<Vector> points(controlPoints);
for (size_t step = 1; step < controlPoints.size(); ++step)
for (size_t i = 0; i < controlPoints.size() - step; ++i)
points[i] = points[i] * (1-t) + points[i+1] * t;
return points[0];
}
vector<Vector> BezierCurve::render(size_t accuracy) const
{
if (controlPoints.size() < 2)
throw Exception("Invalid Bezier curve: Not enough control points.");
vector<Vector> vertices(controlPoints);
subdivide(vertices, accuracy);
return vertices;
}
} // namespace math
} // namespace love
+124
View File
@@ -0,0 +1,124 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_BEZIER_CURVE_H
#define LOVE_MATH_BEZIER_CURVE_H
// LOVE
#include "common/Object.h"
#include "common/Vector.h"
#include <vector>
namespace love
{
namespace math
{
class BezierCurve : public Object
{
public:
/**
* @param controlPoints Control polygon of the curve.
**/
BezierCurve(const std::vector<Vector> &controlPoints);
/**
* @returns Degree of the curve
**/
size_t getDegree() const
{
return controlPoints.size() - 1;
}
/**
* @returns First derivative of the curve.
*/
BezierCurve getDerivative() const;
/**
* @returns i'th control point.
**/
const Vector &getControlPoint(int i) const;
/**
* Sets the i'th control point.
* @param i Control point to change.
* @param point New control point.
**/
void setControlPoint(int i, const Vector &point);
/**
* Insert a new control point before the i'th control point.
* If i < 0, Lua string indexing rules apply.
* @param point Control point to insert.
* @param pos Position to insert.
**/
void insertControlPoint(const Vector &point, int pos = -1);
/**
* @returns Number of control points.
**/
size_t getControlPointCount() const
{
return controlPoints.size();
}
/**
* Move the curve.
* @param t Translation vector.
*/
void translate(const Vector &t);
/**
* Rotate the curve.
* @param phi Rotation angle (radians).
* @param center Rotation center.
*/
void rotate(double phi, const Vector &center);
/**
* Scale the curve.
* @param phi Scale factor.
* @param center Scale center.
*/
void scale(double phi, const Vector &center);
/**
* Evaluates the curve at time t.
* @param t Curve parameter, must satisfy 0 <= t <= 1.
**/
Vector evaluate(double t) const;
/**
* Renders the curve by subdivision.
* @param accuracy The 'fineness' of the curve.
* @returns A polygon chain that approximates the bezier curve.
**/
std::vector<Vector> render(size_t accuracy = 4) const;
private:
std::vector<Vector> controlPoints;
};
}
}
#endif
+197
View File
@@ -0,0 +1,197 @@
/**
* Copyright (c) 2006-2013 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 "MathModule.h"
#include "common/Vector.h"
#include "BezierCurve.h"
// STL
#include <cmath>
#include <list>
#include <iostream>
using std::list;
using std::vector;
using love::Vertex;
namespace
{
// check if an angle is oriented counter clockwise
inline bool is_oriented_ccw(const Vertex &a, const Vertex &b, const Vertex &c)
{
// return det(b-a, c-a) >= 0
return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) >= 0;
}
// check if a and b are on the same side of the line c->d
bool on_same_side(const Vertex &a, const Vertex &b, const Vertex &c, const Vertex &d)
{
float px = d.x - c.x, py = d.y - c.y;
// return det(p, a-c) * det(p, b-c) >= 0
float l = px * (a.y - c.y) - py * (a.x - c.x);
float m = px * (b.y - c.y) - py * (b.x - c.x);
return l * m >= 0;
}
// checks is p is contained in the triangle abc
inline bool point_in_triangle(const Vertex &p, const Vertex &a, const Vertex &b, const Vertex &c)
{
return on_same_side(p,a, b,c) && on_same_side(p,b, a,c) && on_same_side(p,c, a,b);
}
// checks if any vertex in `vertices' is in the triangle abc.
bool any_point_in_triangle(const list<const Vertex *> &vertices, const Vertex &a, const Vertex &b, const Vertex &c)
{
list<const Vertex *>::const_iterator it, end = vertices.end();
for (it = vertices.begin(); it != end; ++it)
{
const Vertex *p = *it;
if ((p != &a) && (p != &b) && (p != &c) && point_in_triangle(*p, a,b,c)) // oh god...
return true;
}
return false;
}
inline bool is_ear(const Vertex &a, const Vertex &b, const Vertex &c, const list<const Vertex *> &vertices)
{
return is_oriented_ccw(a,b,c) && !any_point_in_triangle(vertices, a,b,c);
}
}
namespace love
{
namespace math
{
Math Math::instance;
Math::Math()
: rng()
{
// prevent the runtime from free()-ing this
retain();
}
RandomGenerator *Math::newRandomGenerator()
{
return new RandomGenerator();
}
BezierCurve *Math::newBezierCurve(const vector<Vector> &points)
{
return new BezierCurve(points);
}
vector<Triangle> Math::triangulate(const vector<Vertex> &polygon)
{
if (polygon.size() < 3)
throw love::Exception("Not a polygon");
else if (polygon.size() == 3)
return vector<Triangle>(1, Triangle(polygon[0], polygon[1], polygon[2]));
// collect list of connections and record leftmost item to check if the polygon
// has the expected winding
vector<size_t> next_idx(polygon.size()), prev_idx(polygon.size());
size_t idx_lm = 0;
for (size_t i = 0; i < polygon.size(); ++i)
{
const Vertex &lm = polygon[idx_lm], &p = polygon[i];
if (p.x < lm.x || (p.x == lm.x && p.y < lm.y))
idx_lm = i;
next_idx[i] = i+1;
prev_idx[i] = i-1;
}
next_idx[next_idx.size()-1] = 0;
prev_idx[0] = prev_idx.size()-1;
// check if the polygon has the expected winding and reverse polygon if needed
if (!is_oriented_ccw(polygon[prev_idx[idx_lm]], polygon[idx_lm], polygon[next_idx[idx_lm]]))
next_idx.swap(prev_idx);
// collect list of concave polygons
list<const Vertex *> concave_vertices;
for (size_t i = 0; i < polygon.size(); ++i)
{
if (!is_oriented_ccw(polygon[prev_idx[i]], polygon[i], polygon[next_idx[i]]))
concave_vertices.push_back(&polygon[i]);
}
// triangulation according to kong
vector<Triangle> triangles;
size_t n_vertices = polygon.size();
size_t current = 1, skipped = 0, next, prev;
while (n_vertices > 3)
{
next = next_idx[current];
prev = prev_idx[current];
const Vertex &a = polygon[prev], &b = polygon[current], &c = polygon[next];
if (is_ear(a,b,c, concave_vertices))
{
triangles.push_back(Triangle(a,b,c));
next_idx[prev] = next;
prev_idx[next] = prev;
concave_vertices.remove(&b);
--n_vertices;
skipped = 0;
}
else if (++skipped > n_vertices)
{
throw love::Exception("Cannot triangulate polygon.");
}
current = next;
}
next = next_idx[current];
prev = prev_idx[current];
triangles.push_back(Triangle(polygon[prev], polygon[current], polygon[next]));
return triangles;
}
bool Math::isConvex(const std::vector<Vertex> &polygon)
{
if (polygon.size() < 3)
return false;
// a polygon is convex if all corners turn in the same direction
// turning direction can be determined using the cross-product of
// the forward difference vectors
size_t i = polygon.size() - 2, j = polygon.size() - 1, k = 0;
Vector p(polygon[j].x - polygon[i].x, polygon[j].y - polygon[i].y);
Vector q(polygon[k].x - polygon[j].x, polygon[k].y - polygon[j].y);
float winding = p ^ q;
while (k+1 < polygon.size())
{
i = j; j = k; k++;
p.x = polygon[j].x - polygon[i].x;
p.y = polygon[j].y - polygon[i].y;
q.x = polygon[k].x - polygon[j].x;
q.y = polygon[k].y - polygon[j].y;
if ((p^q) * winding < 0)
return false;
}
return true;
}
} // math
} // love
+180
View File
@@ -0,0 +1,180 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_MODMATH_H
#define LOVE_MATH_MODMATH_H
#include "RandomGenerator.h"
// LOVE
#include "common/Module.h"
#include "common/math.h"
#include "common/Vector.h"
#include "common/int.h"
// Noise
#include "libraries/noise1234/simplexnoise1234.h"
// STL
#include <vector>
namespace love
{
namespace math
{
class BezierCurve;
class Math : public Module
{
private:
RandomGenerator rng;
public:
virtual ~Math()
{}
inline void setRandomSeed(RandomGenerator::Seed seed)
{
rng.setSeed(seed);
}
inline void setRandomSeed(uint32 low, uint32 high)
{
rng.setSeed(low, high);
}
inline RandomGenerator::Seed getRandomSeed() const
{
return rng.getSeed();
}
inline void getRandomSeed(uint32 &low, uint32 &high) const
{
rng.getSeed(low, high);
}
/**
* @copydoc RandomGenerator::random()
**/
inline double random()
{
return rng.random();
}
/**
* @copydoc RandomGenerator::random(double)
**/
inline double random(double max)
{
return rng.random(max);
}
/**
* @copydoc RandomGenerator::random(double,double)
**/
inline double random(double min, double max)
{
return rng.random(min, max);
}
/**
* @copydoc RandomGenerator::randomNormal()
**/
inline double randomNormal(double stddev)
{
return rng.randomNormal(stddev);
}
/**
* Create a new random number generator.
**/
RandomGenerator *newRandomGenerator();
/**
* Creates a new bezier curve.
**/
BezierCurve *newBezierCurve(const std::vector<Vector> &points);
virtual const char *getName() const
{
return "love.math";
}
/**
* Triangulate a simple polygon.
*
* @param polygon Polygon to triangulate. Must not intersect itself.
* @return List of triangles the polygon is composed of.
**/
std::vector<Triangle> triangulate(const std::vector<Vertex> &polygon);
/**
* Checks whether a polygon is convex.
*
* @param polygon Polygon to test.
* @return True if the polygon is convex, false otherwise.
**/
bool isConvex(const std::vector<Vertex> &polygon);
/**
* Calculate Simplex noise for the specified coordinate(s).
*
* @return Noise value in the range of [0, 1].
**/
float noise(float x) const;
float noise(float x, float y) const;
float noise(float x, float y, float z) const;
float noise(float x, float y, float z, float w) const;
static Math instance;
private:
Math();
}; // Math
inline float Math::noise(float x) const
{
return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
}
inline float Math::noise(float x, float y) const
{
return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
}
inline float Math::noise(float x, float y, float z) const
{
return SimplexNoise1234::noise(x, y, z) * 0.5f + 0.5f;
}
inline float Math::noise(float x, float y, float z, float w) const
{
return SimplexNoise1234::noise(x, y, z, w) * 0.5f + 0.5f;
}
} // math
} // love
#endif
@@ -0,0 +1,87 @@
/**
* Copyright (c) 2006-2013 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.
**/
#include "RandomGenerator.h"
// STL
#include <cmath>
namespace love
{
namespace math
{
// 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in
// George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003
RandomGenerator::RandomGenerator()
: last_randomnormal(std::numeric_limits<double>::infinity())
{
// because it is too big for some compilers to handle ... if you know what
// i mean
#ifdef LOVE_BIG_ENDIAN
seed.b32.a = 0x0139408D;
seed.b32.b = 0xCBBF7A44;
#else
seed.b32.b = 0x0139408D;
seed.b32.a = 0xCBBF7A44;
#endif
rng_state = seed;
}
void RandomGenerator::setSeed(RandomGenerator::Seed newseed)
{
// 0 xor 0 is still 0, so Xorshift can't generate new numbers.
if (newseed.b64 == 0)
throw love::Exception("Invalid random seed.");
seed = newseed;
rng_state = seed;
}
uint64 RandomGenerator::rand()
{
rng_state.b64 ^= (rng_state.b64 << 13);
rng_state.b64 ^= (rng_state.b64 >> 7);
rng_state.b64 ^= (rng_state.b64 << 17);
return rng_state.b64;
}
// BoxMuller transform
double RandomGenerator::randomNormal(double stddev)
{
// use cached number if possible
if (last_randomnormal != std::numeric_limits<double>::infinity())
{
double r = last_randomnormal;
last_randomnormal = std::numeric_limits<double>::infinity();
return r * stddev;
}
double r = sqrt(-2.0 * log(1. - random()));
double phi = 2.0 * LOVE_M_PI * (1. - random());
last_randomnormal = r * cos(phi);
return r * sin(phi) * stddev;
}
} // math
} // love
+152
View File
@@ -0,0 +1,152 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_RANDOM_GENERATOR_H
#define LOVE_MATH_RANDOM_GENERATOR_H
// LOVE
#include "common/config.h"
#include "common/Exception.h"
#include "common/math.h"
#include "common/int.h"
#include "common/Object.h"
// STL
#include <limits>
namespace love
{
namespace math
{
class RandomGenerator : public Object
{
public:
union Seed
{
uint64 b64;
struct
{
uint32 a;
uint32 b;
} b32;
};
RandomGenerator();
virtual ~RandomGenerator() {}
/**
* Set pseudo-random seed.
* It's up to the implementation how to use this.
**/
void setSeed(Seed seed);
/**
* Separately set the low and high bits of the pseudo-random seed.
**/
inline void setSeed(uint32 low, uint32 high)
{
Seed newseed;
#ifdef LOVE_BIG_ENDIAN
newseed.b32.a = high;
newseed.b32.b = low;
#else
newseed.b32.b = high;
newseed.b32.a = low;
#endif
setSeed(newseed);
}
inline Seed getSeed() const
{
return seed;
}
inline void getSeed(uint32 &low, uint32 &high) const
{
#ifdef LOVE_BIG_ENDIAN
high = seed.b32.a;
low = seed.b32.b;
#else
high = seed.b32.b;
low = seed.b32.a;
#endif
}
/**
* Return uniformly distributed pseudo random integer.
*
* @return Pseudo random integer in [0,2^64).
**/
uint64 rand();
/**
* Get uniformly distributed pseudo random number in [0,1).
*
* @return Pseudo random number in [0,1).
**/
inline double random()
{
return double(rand()) / (double(std::numeric_limits<uint64>::max()) + 1.0);
}
/**
* Get uniformly distributed pseudo random number in [0,max).
*
* @return Pseudo random number in [0,max).
**/
inline double random(double max)
{
return random() * max;
}
/**
* Get uniformly distributed pseudo random number in [min, max).
*
* @return Pseudo random number in [min, max).
**/
inline double random(double min, double max)
{
return random() * (max - min) + min;
}
/**
* Get normally distributed pseudo random number.
*
* @param stddev Standard deviation of the distribution.
* @return Normally distributed random number with mean 0 and variance (stddev)².
**/
double randomNormal(double stddev);
private:
Seed seed;
Seed rng_state;
double last_randomnormal;
}; // RandomGenerator
} // math
} // love
#endif // LOVE_MATH_RANDOM_GENERATOR_H
@@ -0,0 +1,189 @@
/**
* Copyright (c) 2006-2013 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.
**/
#include "common/Exception.h"
#include "wrap_BezierCurve.h"
#include <cmath>
namespace love
{
namespace math
{
BezierCurve *luax_checkbeziercurve(lua_State *L, int idx)
{
return luax_checktype<BezierCurve>(L, idx, "BezierCurve", MATH_BEZIER_CURVE_T);
}
int w_BezierCurve_getDegree(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
lua_pushnumber(L, curve->getDegree());
return 1;
}
int w_BezierCurve_getDerivative(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
BezierCurve *deriv = new BezierCurve(curve->getDerivative());
luax_pushtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, deriv);
return 1;
}
int w_BezierCurve_getControlPoint(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
int idx = luaL_checkinteger(L, 2);
if (idx > 0) // 1-indexing
idx--;
EXCEPT_GUARD(
Vector v = curve->getControlPoint(idx);
lua_pushnumber(L, v.x);
lua_pushnumber(L, v.y);
)
return 2;
}
int w_BezierCurve_setControlPoint(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
int idx = luaL_checkinteger(L, 2);
float vx = (float) luaL_checknumber(L, 3);
float vy = (float) luaL_checknumber(L, 4);
if (idx > 0) // 1-indexing
idx--;
EXCEPT_GUARD(curve->setControlPoint(idx, Vector(vx,vy));)
return 0;
}
int w_BezierCurve_insertControlPoint(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
float vx = (float) luaL_checknumber(L, 2);
float vy = (float) luaL_checknumber(L, 3);
int idx = luaL_optinteger(L, 4, -1);
if (idx > 0) // 1-indexing
idx--;
EXCEPT_GUARD(curve->insertControlPoint(Vector(vx,vy), idx);)
return 0;
}
int w_BezierCurve_getControlPointCount(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
lua_pushinteger(L, curve->getControlPointCount());
return 1;
}
int w_BezierCurve_translate(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
float dx = (float) luaL_checknumber(L, 2);
float dy = (float) luaL_checknumber(L, 3);
curve->translate(Vector(dx,dy));
return 0;
}
int w_BezierCurve_rotate(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
double phi = luaL_checknumber(L, 2);
float ox = (float) luaL_optnumber(L, 3, 0);
float oy = (float) luaL_optnumber(L, 4, 0);
curve->rotate(phi, Vector(ox,oy));
return 0;
}
int w_BezierCurve_scale(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
double s = luaL_checknumber(L, 2);
float ox = (float) luaL_optnumber(L, 3, 0);
float oy = (float) luaL_optnumber(L, 4, 0);
curve->scale(s, Vector(ox,oy));
return 0;
}
int w_BezierCurve_evaluate(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
double t = luaL_checknumber(L, 2);
EXCEPT_GUARD(
Vector v = curve->evaluate(t);
lua_pushnumber(L, v.x);
lua_pushnumber(L, v.y);
)
return 2;
}
int w_BezierCurve_render(lua_State *L)
{
BezierCurve *curve = luax_checkbeziercurve(L, 1);
int accuracy = luaL_optinteger(L, 2, 5);
std::vector<Vector> points;
EXCEPT_GUARD(points = curve->render(accuracy);)
lua_createtable(L, points.size()*2, 0);
for (size_t i = 0; i < points.size(); ++i)
{
lua_pushnumber(L, points[i].x);
lua_rawseti(L, -2, 2*i+1);
lua_pushnumber(L, points[i].y);
lua_rawseti(L, -2, 2*i+2);
}
return 1;
}
static const luaL_Reg functions[] =
{
{"getDegree", w_BezierCurve_getDegree},
{"getDerivative", w_BezierCurve_getDerivative},
{"getControlPoint", w_BezierCurve_getControlPoint},
{"setControlPoint", w_BezierCurve_setControlPoint},
{"insertControlPoint", w_BezierCurve_insertControlPoint},
{"getControlPointCount", w_BezierCurve_getControlPointCount},
{"translate", w_BezierCurve_translate},
{"rotate", w_BezierCurve_rotate},
{"scale", w_BezierCurve_scale},
{"evaluate", w_BezierCurve_evaluate},
{"render", w_BezierCurve_render},
{ 0, 0 }
};
extern "C" int luaopen_beziercurve(lua_State *L)
{
return luax_register_type(L, "BezierCurve", functions);
}
} // math
} // love
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_WRAP_BEZIER_CURVE_H
#define LOVE_MATH_WRAP_BEZIER_CURVE_H
// LOVE
#include "BezierCurve.h"
#include "common/runtime.h"
namespace love
{
namespace math
{
BezierCurve *luax_checkbeziercurve(lua_State *L, int idx);
int w_BezierCurve_getDegree(lua_State *L);
int w_BezierCurve_getDerivative(lua_State *L);
int w_BezierCurve_getControlPoint(lua_State *L);
int w_BezierCurve_setControlPoint(lua_State *L);
int w_BezierCurve_insertControlPoint(lua_State *L);
int w_BezierCurve_getControlPointCount(lua_State *L);
int w_BezierCurve_translate(lua_State *L);
int w_BezierCurve_rotate(lua_State *L);
int w_BezierCurve_scale(lua_State *L);
int w_BezierCurve_evaluate(lua_State *L);
int w_BezierCurve_render(lua_State *L);
extern "C" int luaopen_beziercurve(lua_State *L);
} // math
} // love
#endif // LOVE_MATH_WRAP_RANDOM_GENERATOR_H
+316
View File
@@ -0,0 +1,316 @@
/**
* Copyright (c) 2006-2013 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.
**/
#include "wrap_Math.h"
#include "wrap_RandomGenerator.h"
#include "wrap_BezierCurve.h"
#include "MathModule.h"
#include "BezierCurve.h"
#include <cmath>
#include <iostream>
namespace love
{
namespace math
{
int w_setRandomSeed(lua_State *L)
{
EXCEPT_GUARD(Math::instance.setRandomSeed(luax_checkrandomseed(L, 1));)
return 0;
}
int w_getRandomSeed(lua_State *L)
{
uint32 low = 0, high = 0;
Math::instance.getRandomSeed(low, high);
lua_pushnumber(L, (lua_Number) low);
lua_pushnumber(L, (lua_Number) high);
return 2;
}
int w_random(lua_State *L)
{
return luax_getrandom(L, 1, Math::instance.random());
}
int w_randomNormal(lua_State *L)
{
double stddev = luaL_optnumber(L, 1, 1.0);
double mean = luaL_optnumber(L, 2, 0.0);
double r = Math::instance.randomNormal(stddev);
lua_pushnumber(L, r + mean);
return 1;
}
int w_newRandomGenerator(lua_State *L)
{
RandomGenerator::Seed s;
if (lua_gettop(L) > 0)
s = luax_checkrandomseed(L, 1);
RandomGenerator *t = Math::instance.newRandomGenerator();
if (lua_gettop(L) > 0)
{
bool should_error = false;
try
{
t->setSeed(s);
}
catch (love::Exception &e)
{
t->release();
should_error = true;
lua_pushstring(L, e.what());
}
if (should_error)
return luaL_error(L, "%s", lua_tostring(L, -1));
}
luax_pushtype(L, "RandomGenerator", MATH_RANDOM_GENERATOR_T, t);
return 1;
}
int w_newBezierCurve(lua_State *L)
{
std::vector<Vector> points;
if (lua_istable(L, 1))
{
size_t top = lua_objlen(L, 1);
points.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, i+1);
Vector v;
v.x = (float) luaL_checknumber(L, -2);
v.y = (float) luaL_checknumber(L, -1);
points.push_back(v);
lua_pop(L, 2);
}
}
else
{
size_t top = lua_gettop(L);
points.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
Vector v;
v.x = (float) luaL_checknumber(L, i);
v.y = (float) luaL_checknumber(L, i+1);
points.push_back(v);
}
}
BezierCurve *curve = Math::instance.newBezierCurve(points);
luax_pushtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, curve);
return 1;
}
int w_triangulate(lua_State *L)
{
std::vector<Vertex> vertices;
if (lua_istable(L, 1))
{
size_t top = lua_objlen(L, 1);
vertices.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, i+1);
Vertex v;
v.x = (float) luaL_checknumber(L, -2);
v.y = (float) luaL_checknumber(L, -1);
vertices.push_back(v);
lua_pop(L, 2);
}
}
else
{
size_t top = lua_gettop(L);
vertices.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
Vertex v;
v.x = (float) luaL_checknumber(L, i);
v.y = (float) luaL_checknumber(L, i+1);
vertices.push_back(v);
}
}
if (vertices.size() < 3)
return luaL_error(L, "Need at least 3 vertices to triangulate");
std::vector<Triangle> triangles;
EXCEPT_GUARD(
if (vertices.size() == 3)
triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2]));
else
triangles = Math::instance.triangulate(vertices);
)
lua_createtable(L, triangles.size(), 0);
for (size_t i = 0; i < triangles.size(); ++i)
{
const Triangle &tri = triangles[i];
lua_createtable(L, 6, 0);
lua_pushnumber(L, tri.a.x);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, tri.a.y);
lua_rawseti(L, -2, 2);
lua_pushnumber(L, tri.b.x);
lua_rawseti(L, -2, 3);
lua_pushnumber(L, tri.b.y);
lua_rawseti(L, -2, 4);
lua_pushnumber(L, tri.c.x);
lua_rawseti(L, -2, 5);
lua_pushnumber(L, tri.c.y);
lua_rawseti(L, -2, 6);
lua_rawseti(L, -2, i+1);
}
return 1;
}
int w_isConvex(lua_State *L)
{
std::vector<Vertex> vertices;
if (lua_istable(L, 1))
{
size_t top = lua_objlen(L, 1);
vertices.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, i+1);
Vertex v;
v.x = (float) luaL_checknumber(L, -2);
v.y = (float) luaL_checknumber(L, -1);
vertices.push_back(v);
lua_pop(L, 2);
}
}
else
{
size_t top = lua_gettop(L);
vertices.reserve(top / 2);
for (size_t i = 1; i <= top; i += 2)
{
Vertex v;
v.x = (float) luaL_checknumber(L, i);
v.y = (float) luaL_checknumber(L, i+1);
vertices.push_back(v);
}
}
lua_pushboolean(L, Math::instance.isConvex(vertices));
return 1;
}
int w_noise(lua_State *L)
{
float w, x, y, z;
float val;
switch (lua_gettop(L))
{
case 1:
x = (float) luaL_checknumber(L, 1);
val = Math::instance.noise(x);
break;
case 2:
x = (float) luaL_checknumber(L, 1);
y = (float) luaL_checknumber(L, 2);
val = Math::instance.noise(x, y);
break;
case 3:
x = (float) luaL_checknumber(L, 1);
y = (float) luaL_checknumber(L, 2);
z = (float) luaL_checknumber(L, 3);
val = Math::instance.noise(x, y, z);
break;
case 4:
default:
x = (float) luaL_checknumber(L, 1);
y = (float) luaL_checknumber(L, 2);
z = (float) luaL_checknumber(L, 3);
w = (float) luaL_checknumber(L, 4);
val = Math::instance.noise(x, y, z, w);
break;
}
lua_pushnumber(L, (lua_Number) val);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "setRandomSeed", w_setRandomSeed },
{ "getRandomSeed", w_getRandomSeed },
{ "random", w_random },
{ "randomNormal", w_randomNormal },
{ "newRandomGenerator", w_newRandomGenerator },
{ "newBezierCurve", w_newBezierCurve },
{ "triangulate", w_triangulate },
{ "isConvex", w_isConvex },
{ "noise", w_noise },
{ 0, 0 }
};
static const lua_CFunction types[] =
{
luaopen_randomgenerator,
luaopen_beziercurve,
0
};
extern "C" int luaopen_love_math(lua_State *L)
{
Math::instance.retain();
WrappedModule w;
w.module = &Math::instance;
w.name = "math";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
int n = luax_register_module(L, w);
return n;
}
} // math
} // love
+48
View File
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_WRAP_MATH_H
#define LOVE_MATH_WRAP_MATH_H
// LOVE
#include "common/config.h"
#include "common/runtime.h"
namespace love
{
namespace math
{
int w_setRandomSeed(lua_State *L);
int w_getRandomSeed(lua_State *L);
int w_random(lua_State *L);
int w_randomNormal(lua_State *L);
int w_newRandomGenerator(lua_State *L);
int w_newBezierCurve(lua_State *L);
int w_triangulate(lua_State *L);
int w_isConvex(lua_State *L);
int w_noise(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_math(lua_State *L);
} // random
} // love
#endif // LOVE_MATH_WRAP_MATH_H
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2006-2013 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.
**/
#include "wrap_RandomGenerator.h"
#include <cmath>
#include <algorithm>
namespace love
{
namespace math
{
template <typename T>
static T checkrandomseed_part(lua_State *L, int idx)
{
double num = luaL_checknumber(L, idx);
double inf = std::numeric_limits<double>::infinity();
// Disallow conversions from infinity and NaN.
if (num == inf || num == -inf || num != num)
luaL_argerror(L, idx, "invalid random seed");
return (T) num;
}
RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx)
{
RandomGenerator::Seed s;
if (!lua_isnoneornil(L, idx + 1))
{
uint32 low = checkrandomseed_part<uint32>(L, idx);
uint32 high = checkrandomseed_part<uint32>(L, idx + 1);
#ifdef LOVE_BIG_ENDIAN
s.b32.a = high;
s.b32.b = low;
#else
s.b32.b = high;
s.b32.a = low;
#endif
}
else
s.b64 = checkrandomseed_part<uint64>(L, idx);
return s;
}
int luax_getrandom(lua_State *L, int startidx, double r)
{
int l, u;
// from lua 5.1.4 source code: lmathlib.c:185 ff.
switch (lua_gettop(L) - (startidx - 1))
{
case 0:
lua_pushnumber(L, r);
break;
case 1:
u = luaL_checkint(L, startidx);
luaL_argcheck(L, 1 <= u, startidx, "interval is empty");
lua_pushnumber(L, floor(r * u) + 1);
break;
case 2:
l = luaL_checkint(L, startidx);
u = luaL_checkint(L, startidx + 1);
luaL_argcheck(L, l <= u, startidx + 1, "interval is empty");
lua_pushnumber(L, floor(r * (u - l + 1)) + l);
break;
default:
return luaL_error(L, "wrong number of arguments");
}
return 1;
}
RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
{
return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
}
int w_RandomGenerator_setSeed(lua_State *L)
{
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
EXCEPT_GUARD(rng->setSeed(luax_checkrandomseed(L, 2));)
return 0;
}
int w_RandomGenerator_getSeed(lua_State *L)
{
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
uint32 low = 0, high = 0;
rng->getSeed(low, high);
lua_pushnumber(L, (lua_Number) low);
lua_pushnumber(L, (lua_Number) high);
return 2;
}
int w_RandomGenerator_random(lua_State *L)
{
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
return luax_getrandom(L, 2, rng->random());
}
int w_RandomGenerator_randomNormal(lua_State *L)
{
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
double stddev = luaL_optnumber(L, 2, 1.0);
double mean = luaL_optnumber(L, 3, 0.0);
double r = rng->randomNormal(stddev);
lua_pushnumber(L, r + mean);
return 1;
}
static const luaL_Reg functions[] =
{
{ "setSeed", w_RandomGenerator_setSeed },
{ "getSeed", w_RandomGenerator_getSeed },
{ "random", w_RandomGenerator_random },
{ "randomNormal", w_RandomGenerator_randomNormal },
{ 0, 0 }
};
extern "C" int luaopen_randomgenerator(lua_State *L)
{
return luax_register_type(L, "RandomGenerator", functions);
}
} // math
} // love
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2006-2013 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.
**/
#ifndef LOVE_MATH_WRAP_RANDOM_GENERATOR_H
#define LOVE_MATH_WRAP_RANDOM_GENERATOR_H
// LOVE
#include "RandomGenerator.h"
#include "common/config.h"
#include "common/runtime.h"
namespace love
{
namespace math
{
// Helper functions.
RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx);
int luax_getrandom(lua_State *L, int startidx, double r);
RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx);
int w_RandomGenerator_setSeed(lua_State *L);
int w_RandomGenerator_getSeed(lua_State *L);
int w_RandomGenerator_random(lua_State *L);
int w_RandomGenerator_randomNormal(lua_State *L);
extern "C" int luaopen_randomgenerator(lua_State *L);
} // math
} // love
#endif // LOVE_MATH_WRAP_RANDOM_GENERATOR_H