|
@@ -22,7 +22,30 @@ misrepresented as being the original software.
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
--]]
|
|
--]]
|
|
|
|
|
|
-local math, ffifuncspointer = ...
|
|
|
|
|
|
+local love_math, ffifuncspointer = ...
|
|
|
|
+
|
|
|
|
+local type, tonumber, error = type, tonumber, error
|
|
|
|
+local floor = math.floor
|
|
|
|
+
|
|
|
|
+local _random = love_math._random
|
|
|
|
+
|
|
|
|
+local function getrandom(r, l, u)
|
|
|
|
+ if u ~= nil then
|
|
|
|
+ if type(r) ~= "number" then error("bad argument #1 to 'random' (number expected)", 2) end
|
|
|
|
+ if type(l) ~= "number" then error("bad argument #2 to 'random' (number expected)", 2) end
|
|
|
|
+ return floor(r * (u - l + 1)) + l
|
|
|
|
+ elseif l ~= nil then
|
|
|
|
+ if type(l) ~= "number" then error("bad argument #1 to 'random' (number expected)", 2) end
|
|
|
|
+ return floor(r * l) + 1
|
|
|
|
+ else
|
|
|
|
+ return r
|
|
|
|
+ end
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+function love_math.random(l, u)
|
|
|
|
+ local r = _random()
|
|
|
|
+ return getrandom(r, l, u)
|
|
|
|
+end
|
|
|
|
|
|
if type(jit) ~= "table" or not jit.status() then
|
|
if type(jit) ~= "table" or not jit.status() then
|
|
-- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT
|
|
-- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT
|
|
@@ -33,12 +56,12 @@ end
|
|
local status, ffi = pcall(require, "ffi")
|
|
local status, ffi = pcall(require, "ffi")
|
|
if not status then return end
|
|
if not status then return end
|
|
|
|
|
|
-local type, tonumber = type, tonumber
|
|
|
|
-
|
|
|
|
-- Matches the struct declaration in wrap_Math.cpp.
|
|
-- Matches the struct declaration in wrap_Math.cpp.
|
|
pcall(ffi.cdef, [[
|
|
pcall(ffi.cdef, [[
|
|
typedef struct FFI_Math
|
|
typedef struct FFI_Math
|
|
{
|
|
{
|
|
|
|
+ double (*random)(void);
|
|
|
|
+
|
|
float (*noise1)(float x);
|
|
float (*noise1)(float x);
|
|
float (*noise2)(float x, float y);
|
|
float (*noise2)(float x, float y);
|
|
float (*noise3)(float x, float y, float z);
|
|
float (*noise3)(float x, float y, float z);
|
|
@@ -54,7 +77,12 @@ local ffifuncs = ffi.cast("FFI_Math *", ffifuncspointer)
|
|
|
|
|
|
-- Overwrite some regular love.math functions with FFI implementations.
|
|
-- Overwrite some regular love.math functions with FFI implementations.
|
|
|
|
|
|
-function math.noise(x, y, z, w)
|
|
|
|
|
|
+function love_math.random(l, u)
|
|
|
|
+ local r = tonumber(ffifuncs.random())
|
|
|
|
+ return getrandom(r, l, u)
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+function love_math.noise(x, y, z, w)
|
|
if w ~= nil then
|
|
if w ~= nil then
|
|
return tonumber(ffifuncs.noise4(x, y, z, w))
|
|
return tonumber(ffifuncs.noise4(x, y, z, w))
|
|
elseif z ~= nil then
|
|
elseif z ~= nil then
|
|
@@ -73,7 +101,7 @@ local function gammaToLinear(c)
|
|
return c
|
|
return c
|
|
end
|
|
end
|
|
|
|
|
|
-function math.gammaToLinear(r, g, b, a)
|
|
|
|
|
|
+function love_math.gammaToLinear(r, g, b, a)
|
|
if type(r) == "table" then
|
|
if type(r) == "table" then
|
|
local t = r
|
|
local t = r
|
|
return gammaToLinear(t[1]), gammaToLinear(t[2]), gammaToLinear(t[3]), t[4]
|
|
return gammaToLinear(t[1]), gammaToLinear(t[2]), gammaToLinear(t[3]), t[4]
|
|
@@ -88,7 +116,7 @@ local function linearToGamma(c)
|
|
return c
|
|
return c
|
|
end
|
|
end
|
|
|
|
|
|
-function math.linearToGamma(r, g, b, a)
|
|
|
|
|
|
+function love_math.linearToGamma(r, g, b, a)
|
|
if type(r) == "table" then
|
|
if type(r) == "table" then
|
|
local t = r
|
|
local t = r
|
|
return linearToGamma(t[1]), linearToGamma(t[2]), linearToGamma(t[3]), t[4]
|
|
return linearToGamma(t[1]), linearToGamma(t[2]), linearToGamma(t[3]), t[4]
|