wrap_Math.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-2021 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. float (*noise1)(double x);
  78. float (*noise2)(double x, double y);
  79. float (*noise3)(double x, double y, double z);
  80. float (*noise4)(double x, double y, double z, double w);
  81. float (*gammaToLinear)(float c);
  82. float (*linearToGamma)(float c);
  83. } FFI_Math;
  84. ]])
  85. local ffifuncs = ffi.cast("FFI_Math **", ffifuncspointer_str)[0]
  86. -- Overwrite some regular love.math functions with FFI implementations.
  87. function love_math.noise(x, y, z, w)
  88. if w ~= nil then
  89. return tonumber(ffifuncs.noise4(x, y, z, w))
  90. elseif z ~= nil then
  91. return tonumber(ffifuncs.noise3(x, y, z))
  92. elseif y ~= nil then
  93. return tonumber(ffifuncs.noise2(x, y))
  94. else
  95. return tonumber(ffifuncs.noise1(x))
  96. end
  97. end
  98. local function gammaToLinear(c)
  99. if c ~= nil then
  100. return tonumber(ffifuncs.gammaToLinear(clamp01(c)))
  101. end
  102. return c
  103. end
  104. function love_math.gammaToLinear(r, g, b, a)
  105. if type(r) == "table" then
  106. local t = r
  107. return gammaToLinear(t[1]), gammaToLinear(t[2]), gammaToLinear(t[3]), t[4]
  108. end
  109. return gammaToLinear(r), gammaToLinear(g), gammaToLinear(b), a
  110. end
  111. local function linearToGamma(c)
  112. if c ~= nil then
  113. return tonumber(ffifuncs.linearToGamma(clamp01(c)))
  114. end
  115. return c
  116. end
  117. function love_math.linearToGamma(r, g, b, a)
  118. if type(r) == "table" then
  119. local t = r
  120. return linearToGamma(t[1]), linearToGamma(t[2]), linearToGamma(t[3]), t[4]
  121. end
  122. return linearToGamma(r), linearToGamma(g), linearToGamma(b), a
  123. end
  124. -- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
  125. --)luastring"--"