mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Cleaned up the code for love.math.random and related functions.
This commit is contained in:
@@ -39,45 +39,10 @@ namespace love
|
||||
namespace math
|
||||
{
|
||||
|
||||
int w__random(lua_State *L)
|
||||
int w__getRandomGenerator(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, Math::instance.random());
|
||||
return 1;
|
||||
}
|
||||
|
||||
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_setRandomSeed(lua_State *L)
|
||||
{
|
||||
luax_catchexcept(L, [&](){ Math::instance.setRandomSeed(luax_checkrandomseed(L, 1)); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getRandomSeed(lua_State *L)
|
||||
{
|
||||
RandomGenerator::Seed s = Math::instance.getRandomSeed();
|
||||
lua_pushnumber(L, (lua_Number) s.b32.low);
|
||||
lua_pushnumber(L, (lua_Number) s.b32.high);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_setRandomState(lua_State *L)
|
||||
{
|
||||
luax_catchexcept(L, [&](){ Math::instance.setRandomState(luax_checkstring(L, 1)); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getRandomState(lua_State *L)
|
||||
{
|
||||
luax_pushstring(L, Math::instance.getRandomState());
|
||||
RandomGenerator *t = Math::instance.getRandomGenerator();
|
||||
luax_pushtype(L, MATH_RANDOM_GENERATOR_ID, t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -423,8 +388,6 @@ int w_decompress(lua_State *L)
|
||||
// C functions in a struct, necessary for the FFI versions of math functions.
|
||||
struct FFI_Math
|
||||
{
|
||||
double (*random)(void);
|
||||
|
||||
float (*noise1)(float x);
|
||||
float (*noise2)(float x, float y);
|
||||
float (*noise3)(float x, float y, float z);
|
||||
@@ -436,11 +399,6 @@ struct FFI_Math
|
||||
|
||||
static FFI_Math ffifuncs =
|
||||
{
|
||||
[](void) -> double // random
|
||||
{
|
||||
return Math::instance.random();
|
||||
},
|
||||
|
||||
[](float x) -> float // noise1
|
||||
{
|
||||
return Math::instance.noise(x);
|
||||
@@ -471,12 +429,9 @@ static FFI_Math ffifuncs =
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "_random", w__random }, // love.math.random is inside wrap_Math.lua.
|
||||
{ "randomNormal", w_randomNormal },
|
||||
{ "setRandomSeed", w_setRandomSeed },
|
||||
{ "getRandomSeed", w_getRandomSeed },
|
||||
{ "setRandomState", w_setRandomState },
|
||||
{ "getRandomState", w_getRandomState },
|
||||
// love.math.random etc. are defined in wrap_Math.lua.
|
||||
|
||||
{ "_getRandomGenerator", w__getRandomGenerator },
|
||||
{ "newRandomGenerator", w_newRandomGenerator },
|
||||
{ "newBezierCurve", w_newBezierCurve },
|
||||
{ "triangulate", w_triangulate },
|
||||
@@ -510,7 +465,7 @@ extern "C" int luaopen_love_math(lua_State *L)
|
||||
|
||||
int n = luax_register_module(L, w);
|
||||
|
||||
// Execute wrap_Event.lua, sending the math table and ffifuncs pointer as args.
|
||||
// Execute wrap_Math.lua, sending the math table and ffifuncs pointer as args.
|
||||
luaL_loadbuffer(L, math_lua, sizeof(math_lua), "wrap_Math.lua");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_pushlightuserdata(L, &ffifuncs);
|
||||
|
||||
Reference in New Issue
Block a user