diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 2d5b97388..c7a685dc2 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -503,6 +503,7 @@ StringMap::Entry typeEntries[] = // Math {"RandomGenerator", MATH_RANDOM_GENERATOR_ID}, + {"BezierCurve", MATH_BEZIER_CURVE_ID}, // Audio {"Source", AUDIO_SOURCE_ID}, diff --git a/src/common/types.h b/src/common/types.h index af38c2ebc..b538c5976 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -60,6 +60,7 @@ enum Type // Math MATH_RANDOM_GENERATOR_ID, + MATH_BEZIER_CURVE_ID, // Audio AUDIO_SOURCE_ID, @@ -136,6 +137,7 @@ const bits IMAGE_COMPRESSED_DATA_T = (bits(1) << IMAGE_COMPRESSED_DATA_ID) | DAT // Math. const bits MATH_RANDOM_GENERATOR_T = (bits(1) << MATH_RANDOM_GENERATOR_ID) | OBJECT_T; +const bits MATH_BEZIER_CURVE_T = (bits(1) << MATH_BEZIER_CURVE_ID) | OBJECT_T; // Audio. const bits AUDIO_SOURCE_T = (bits(1) << AUDIO_SOURCE_ID) | OBJECT_T; diff --git a/src/modules/math/MathModule.cpp b/src/modules/math/MathModule.cpp index bab274d05..445d69d89 100644 --- a/src/modules/math/MathModule.cpp +++ b/src/modules/math/MathModule.cpp @@ -21,6 +21,7 @@ // LOVE #include "MathModule.h" #include "common/Vector.h" +#include "BezierCurve.h" // STL #include @@ -95,6 +96,11 @@ RandomGenerator *Math::newRandomGenerator() return new RandomGenerator(); } +BezierCurve *Math::newBezierCurve(const vector &points) +{ + return new BezierCurve(points); +} + vector Math::triangulate(const vector &polygon) { if (polygon.size() < 3) diff --git a/src/modules/math/MathModule.h b/src/modules/math/MathModule.h index 2a97a7783..51cb062ba 100644 --- a/src/modules/math/MathModule.h +++ b/src/modules/math/MathModule.h @@ -26,6 +26,7 @@ // LOVE #include "common/Module.h" #include "common/math.h" +#include "common/Vector.h" #include "common/int.h" // Noise @@ -39,6 +40,8 @@ namespace love namespace math { +class BezierCurve; + class Math : public Module { private: @@ -95,6 +98,11 @@ public: **/ RandomGenerator *newRandomGenerator(); + /** + * Creates a new bezier curve. + **/ + BezierCurve *newBezierCurve(const std::vector &points); + virtual const char *getName() const { return "love.math"; diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 0af23edf2..fa901ef56 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -20,7 +20,9 @@ #include "wrap_Math.h" #include "wrap_RandomGenerator.h" +#include "wrap_BezierCurve.h" #include "MathModule.h" +#include "BezierCurve.h" #include #include @@ -74,6 +76,44 @@ int w_newRandomGenerator(lua_State *L) return 1; } +int w_newBezierCurve(lua_State *L) +{ + std::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 = luaL_checknumber(L, -2); + v.y = 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 = luaL_checknumber(L, i); + v.y = luaL_checknumber(L, i+1); + points.push_back(v); + } + } + + BezierCurve *curve = Math::instance.newBezierCurve(points); + luax_newtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, (void *)curve); + return 1; +} + int w_triangulate(lua_State *L) { std::vector vertices; @@ -228,6 +268,7 @@ static const luaL_Reg functions[] = { "random", w_random }, { "randomnormal", w_randomnormal }, { "newRandomGenerator", w_newRandomGenerator }, + { "newBezierCurve", w_newBezierCurve }, { "triangulate", w_triangulate }, { "isConvex", w_isConvex }, { "noise", w_noise }, @@ -237,6 +278,7 @@ static const luaL_Reg functions[] = static const lua_CFunction types[] = { luaopen_randomgenerator, + luaopen_beziercurve, 0 }; diff --git a/src/modules/math/wrap_Math.h b/src/modules/math/wrap_Math.h index 369827e77..63b61abd5 100644 --- a/src/modules/math/wrap_Math.h +++ b/src/modules/math/wrap_Math.h @@ -34,6 +34,7 @@ int w_randomseed(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);