Added lerp, lerp_color, approach, and multiapproach lua functions.

This commit is contained in:
Kyzentun
2014-10-23 00:13:13 -06:00
parent 2e7fabc7e7
commit ca4eb65b61
8 changed files with 125 additions and 3 deletions
+5
View File
@@ -266,6 +266,11 @@ void LuaFunc_Register_##func( lua_State *L ); \
void LuaFunc_Register_##func( lua_State *L ) { lua_register( L, #func, LuaFunc_##func ); } \
REGISTER_WITH_LUA_FUNCTION( LuaFunc_Register_##func );
#define LUAFUNC_REGISTER_COMMON(func_name) \
void LuaFunc_Register_##func_name(lua_State* L); \
void LuaFunc_Register_##func_name(lua_State* L) { lua_register(L, #func_name, LuaFunc_##func_name); } \
REGISTER_WITH_LUA_FUNCTION(LuaFunc_Register_##func_name);
#endif
/*
+23 -2
View File
@@ -74,6 +74,14 @@ RString RageColor::NormalizeColorString( RString sColor )
return c.ToString();
}
void lerp_rage_color(RageColor& out, RageColor const& a, RageColor const& b, float t)
{
out.b= lerp(t, a.b, b.b);
out.g= lerp(t, a.g, b.g);
out.r= lerp(t, a.r, b.r);
out.a= lerp(t, a.a, b.a);
}
void WeightedAvergeOfRSVs(RageSpriteVertex& average_out, RageSpriteVertex const& rsv1, RageSpriteVertex const& rsv2, float percent_between)
{
average_out.p= lerp(percent_between, rsv1.p, rsv2.p);
@@ -207,8 +215,21 @@ int LuaFunc_color( lua_State *L )
c.PushTable( L );
return 1;
}
void LuaFunc_Register_color( lua_State *L ) { lua_register( L, "color", LuaFunc_color ); }
REGISTER_WITH_LUA_FUNCTION( LuaFunc_Register_color );
LUAFUNC_REGISTER_COMMON(color);
int LuaFunc_lerp_color(lua_State *L)
{
// Args: percent, color, color
// Returns: color
float percent= FArg(1);
RageColor a, b, c;
a.FromStack(L, 2);
b.FromStack(L, 3);
lerp_rage_color(c, a, b, percent);
c.PushTable(L);
return 1;
}
LUAFUNC_REGISTER_COMMON(lerp_color);
/*
* Copyright (c) 2006 Glenn Maynard
+1
View File
@@ -342,6 +342,7 @@ struct RageSpriteVertex // has color
RageVector2 t; // texture coordinates
};
void lerp_rage_color(RageColor& out, RageColor const& a, RageColor const& b, float t);
void WeightedAvergeOfRSVs(RageSpriteVertex& average_out, RageSpriteVertex const& rsv1, RageSpriteVertex const& rsv2, float percent_between);
struct RageModelVertex // doesn't have color. Relies on material color
+70
View File
@@ -2329,6 +2329,76 @@ LuaFunction( PrettyPercent, PrettyPercent( FArg(1), FArg(2) ) );
//LuaFunction( IsHexVal, IsHexVal( SArg(1) ) );
static bool UndocumentedFeature( RString s ){ sm_crash(s); return true; }
LuaFunction( UndocumentedFeature, UndocumentedFeature(SArg(1)) );
LuaFunction( lerp, lerp(FArg(1), FArg(2), FArg(3)) );
void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind);
void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind, int process_index)
{
#define TONUMBER_NICE(dest, num_name, index) \
if(!lua_isnumber(L, index)) \
{ \
luaL_error(L, "approach: " #num_name " for approach %d is not a number.", process_index); \
} \
dest= lua_tonumber(L, index);
float val= 0;
float goal= 0;
float speed= 0;
TONUMBER_NICE(val, current, valind);
TONUMBER_NICE(goal, goal, goalind);
TONUMBER_NICE(speed, speed, speedind);
#undef TONUMBER_NICE
if(speed < 0)
{
luaL_error(L, "approach: speed %d is negative.", process_index);
}
fapproach(val, goal, speed);
lua_pushnumber(L, val);
}
int LuaFunc_approach(lua_State* L);
int LuaFunc_approach(lua_State* L)
{
// Args: current, goal, speed
// Returns: new_current
luafunc_approach_internal(L, 1, 2, 3, 1);
return 1;
}
LUAFUNC_REGISTER_COMMON(approach);
int LuaFunc_multiapproach(lua_State* L);
int LuaFunc_multiapproach(lua_State* L)
{
// Args: {currents}, {goals}, {speeds}
// Returns: {currents}
// Modifies the values in {currents} in place.
if(lua_gettop(L) != 3)
{
luaL_error(L, "multiapproach: A table of current values, a table of goal values, and a table of speeds must be passed.");
}
size_t currents_len= lua_objlen(L, 1);
size_t goals_len= lua_objlen(L, 2);
size_t speeds_len= lua_objlen(L, 3);
if(currents_len != goals_len || currents_len != speeds_len)
{
luaL_error(L, "multiapproach: There must be the same number of current values, goal values, and speeds.");
}
if(!lua_istable(L, 1) || !lua_istable(L, 2) || !lua_istable(L, 3))
{
luaL_error(L, "multiapproach: current, goal, and speed must all be tables.");
}
for(size_t i= 1; i <= currents_len; ++i)
{
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 2, i);
lua_rawgeti(L, 3, i);
luafunc_approach_internal(L, -3, -2, -1, i);
lua_rawseti(L, 1, i);
lua_pop(L, 3);
}
lua_pushvalue(L, 1);
return 1;
}
LUAFUNC_REGISTER_COMMON(multiapproach);
/*
* Copyright (c) 2001-2005 Chris Danford, Glenn Maynard