diff --git a/SDK/src/NDK/LuaAPI_Renderer.cpp b/SDK/src/NDK/LuaAPI_Renderer.cpp index c088a5151..5c801748a 100644 --- a/SDK/src/NDK/LuaAPI_Renderer.cpp +++ b/SDK/src/NDK/LuaAPI_Renderer.cpp @@ -7,14 +7,6 @@ #include #include -#include - -// WIP: I don't know where to put it... -template T ranged_cast(long long a, T high = std::numeric_limits::max(), T low = std::numeric_limits::min()) -{ - return static_cast(std::min(static_cast(high), std::max(a, static_cast(low)))); -} - namespace Ndk { void LuaAPI::Register_Renderer(Nz::LuaInstance& instance) @@ -41,7 +33,7 @@ namespace Ndk unsigned int argCount = std::min(lua.GetStackTop(), 2U); if (argCount == 1 && lua.IsOfType(1, Nz::LuaType_Number)) - memory = abstractImage->GetMemoryUsage(ranged_cast(lua.CheckInteger(1))); + memory = abstractImage->GetMemoryUsage(Nz::ranged_cast(lua.CheckInteger(1))); else memory = abstractImage->GetMemoryUsage(); @@ -81,15 +73,15 @@ namespace Ndk virtual bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) */ - unsigned int srcWidth = ranged_cast(lua.CheckInteger(2)); + unsigned int srcWidth = Nz::ranged_cast(lua.CheckInteger(2)); unsigned int srcHeight = 0; Nz::UInt8 level = 0; if (argCount >= 3) { - srcHeight = ranged_cast(lua.CheckInteger(3)); + srcHeight = Nz::ranged_cast(lua.CheckInteger(3)); if (argCount >= 4) - level = ranged_cast(lua.CheckInteger(3)); + level = Nz::ranged_cast(lua.CheckInteger(3)); } rValue = abstractImage->Update(pixels, srcWidth, srcHeight, level); @@ -107,12 +99,12 @@ namespace Ndk if (argCount >= 3) { - srcWidth = ranged_cast(lua.CheckInteger(3)); + srcWidth = Nz::ranged_cast(lua.CheckInteger(3)); if (argCount >= 4) { - srcHeight = ranged_cast(lua.CheckInteger(4)); + srcHeight = Nz::ranged_cast(lua.CheckInteger(4)); if (argCount >= 5) - level = ranged_cast(lua.CheckInteger(5)); + level = Nz::ranged_cast(lua.CheckInteger(5)); } } rValue = abstractImage->Update(pixels, box, srcWidth, srcHeight, level); @@ -131,15 +123,15 @@ namespace Ndk if (argCount >= 3) { - z = ranged_cast(lua.CheckInteger(3)); + z = Nz::ranged_cast(lua.CheckInteger(3)); if (argCount >= 4) { - srcWidth = ranged_cast(lua.CheckInteger(4)); + srcWidth = Nz::ranged_cast(lua.CheckInteger(4)); if (argCount >= 5) { - srcHeight = ranged_cast(lua.CheckInteger(5)); + srcHeight = Nz::ranged_cast(lua.CheckInteger(5)); if (argCount >= 6) - level = ranged_cast(lua.CheckInteger(6)); + level = Nz::ranged_cast(lua.CheckInteger(6)); } } } diff --git a/include/Nazara/Lua/LuaInstance.hpp b/include/Nazara/Lua/LuaInstance.hpp index 0cb1f809d..1c6d6b9f2 100644 --- a/include/Nazara/Lua/LuaInstance.hpp +++ b/include/Nazara/Lua/LuaInstance.hpp @@ -15,6 +15,7 @@ #include #include #include +#include struct lua_Debug; struct lua_State; @@ -189,6 +190,14 @@ namespace Nz lua_State* m_state; unsigned int m_level; }; + + // Performs a 'ranged cast' to type T, i.e. a cast guaranteed to end up within the numerical limits of the type (or the limits provided in argument), + // without over- or under-flowing. + // Values beyond that range will be truncated to the type minimum (resp. maximum) if they are greater (resp. lesser) than the initial value. + template T ranged_cast(long long a, T high = std::numeric_limits::max(), T low = std::numeric_limits::min()) + { + return static_cast(std::min(static_cast(high), std::max(a, static_cast(low)))); + } } #include