wrap_Math.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. R"luastring"--(
  2. -- DO NOT REMOVE THE ABOVE LINE. It is used to load this file as a C++ string.
  3. -- There is a matching delimiter at the bottom of the file.
  4. --[[
  5. Copyright (c) 2006-2022 LOVE Development Team
  6. This software is provided 'as-is', without any express or implied
  7. warranty. In no event will the authors be held liable for any damages
  8. arising from the use of this software.
  9. Permission is granted to anyone to use this software for any purpose,
  10. including commercial applications, and to alter it and redistribute it
  11. freely, subject to the following restrictions:
  12. 1. The origin of this software must not be misrepresented; you must not
  13. claim that you wrote the original software. If you use this software
  14. in a product, an acknowledgment in the product documentation would be
  15. appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be
  17. misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. --]]
  20. local love_math, ffifuncspointer_str = ...
  21. local type, tonumber, error = type, tonumber, error
  22. local floor = math.floor
  23. local min, max = math.min, math.max
  24. local function clamp01(x)
  25. return min(max(x, 0), 1)
  26. end
  27. local rng = love_math._getRandomGenerator()
  28. function love_math.random(l, u)
  29. return rng:random(l, u)
  30. end
  31. function love_math.randomNormal(stddev, mean)
  32. return rng:randomNormal(stddev, mean)
  33. end
  34. function love_math.setRandomSeed(low, high)
  35. return rng:setSeed(low, high)
  36. end
  37. function love_math.getRandomSeed()
  38. return rng:getSeed()
  39. end
  40. function love_math.setRandomState(state)
  41. return rng:setState(state)
  42. end
  43. function love_math.getRandomState()
  44. return rng:getState()
  45. end
  46. function love_math.colorToBytes(r, g, b, a)
  47. if type(r) == "table" then
  48. r, g, b, a = r[1], r[2], r[3], r[4]
  49. end
  50. r = floor(clamp01(r) * 255 + 0.5)
  51. g = floor(clamp01(g) * 255 + 0.5)
  52. b = floor(clamp01(b) * 255 + 0.5)
  53. a = a ~= nil and floor(clamp01(a) * 255 + 0.5) or nil
  54. return r, g, b, a
  55. end
  56. function love_math.colorFromBytes(r, g, b, a)
  57. if type(r) == "table" then
  58. r, g, b, a = r[1], r[2], r[3], r[4]
  59. end
  60. r = clamp01(floor(r + 0.5) / 255)
  61. g = clamp01(floor(g + 0.5) / 255)
  62. b = clamp01(floor(b + 0.5) / 255)
  63. a = a ~= nil and clamp01(floor(a + 0.5) / 255) or nil
  64. return r, g, b, a
  65. end
  66. if type(jit) ~= "table" or not jit.status() then
  67. -- LuaJIT's FFI is *much* slower than LOVE's regular methods when the JIT
  68. -- compiler is disabled.
  69. return
  70. end
  71. local status, ffi = pcall(require, "ffi")
  72. if not status then return end
  73. -- Matches the struct declaration in wrap_Math.cpp.
  74. pcall(ffi.cdef, [[
  75. typedef struct FFI_Math
  76. {
  77. double (*snoise1)(double x);
  78. double (*snoise2)(double x, double y);
  79. double (*snoise3)(double x, double y, double z);
  80. double (*snoise4)(double x, double y, double z, double w);
  81. double (*pnoise1)(double x);
  82. double (*pnoise2)(double x, double y);
  83. double (*pnoise3)(double x, double y, double z);
  84. double (*pnoise4)(double x, double y, double z, double w);
  85. float (*gammaToLinear)(float c);
  86. float (*linearToGamma)(float c);
  87. } FFI_Math;
  88. ]])
  89. local ffifuncs = ffi.cast("FFI_Math **", ffifuncspointer_str)[0]
  90. local love = require("love")
  91. -- Overwrite some regular love.math functions with FFI implementations.
  92. function love_math.noise(x, y, z, w)
  93. love.markDeprecated(2, "love.math.noise", "function", "replaced", "love.math.perlinNoise or love.math.simplexNoise")
  94. if w ~= nil then
  95. return tonumber(ffifuncs.pnoise4(x, y, z, w))
  96. elseif z ~= nil then
  97. return tonumber(ffifuncs.pnoise3(x, y, z))
  98. elseif y ~= nil then
  99. return tonumber(ffifuncs.snoise2(x, y))
  100. else
  101. return tonumber(ffifuncs.snoise1(x))
  102. end
  103. end
  104. function love_math.perlinNoise(x, y, z, w)
  105. if w ~= nil then
  106. return tonumber(ffifuncs.pnoise4(x, y, z, w))
  107. elseif z ~= nil then
  108. return tonumber(ffifuncs.pnoise3(x, y, z))
  109. elseif y ~= nil then
  110. return tonumber(ffifuncs.pnoise2(x, y))
  111. else
  112. return tonumber(ffifuncs.pnoise1(x))
  113. end
  114. end
  115. function love_math.simplexNoise(x, y, z, w)
  116. if w ~= nil then
  117. return tonumber(ffifuncs.snoise4(x, y, z, w))
  118. elseif z ~= nil then
  119. return tonumber(ffifuncs.snoise3(x, y, z))
  120. elseif y ~= nil then
  121. return tonumber(ffifuncs.snoise2(x, y))
  122. else
  123. return tonumber(ffifuncs.snoise1(x))
  124. end
  125. end
  126. local function gammaToLinear(c)
  127. if c ~= nil then
  128. return tonumber(ffifuncs.gammaToLinear(clamp01(c)))
  129. end
  130. return c
  131. end
  132. function love_math.gammaToLinear(r, g, b, a)
  133. if type(r) == "table" then
  134. local t = r
  135. return gammaToLinear(t[1]), gammaToLinear(t[2]), gammaToLinear(t[3]), t[4]
  136. end
  137. return gammaToLinear(r), gammaToLinear(g), gammaToLinear(b), a
  138. end
  139. local function linearToGamma(c)
  140. if c ~= nil then
  141. return tonumber(ffifuncs.linearToGamma(clamp01(c)))
  142. end
  143. return c
  144. end
  145. function love_math.linearToGamma(r, g, b, a)
  146. if type(r) == "table" then
  147. local t = r
  148. return linearToGamma(t[1]), linearToGamma(t[2]), linearToGamma(t[3]), t[4]
  149. end
  150. return linearToGamma(r), linearToGamma(g), linearToGamma(b), a
  151. end
  152. -- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
  153. --)luastring"--"