Created
June 25, 2015 15:48
-
-
Save notcake/30012af1a25bdaf3d92f to your computer and use it in GitHub Desktop.
Garry's Mod Lua SetUnpacked and Unpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GarrysMod/Lua/Interface.h" | |
#include "mathlib/vector.h" | |
#include "mathlib/vmatrix.h" | |
#include <stdio.h> | |
using namespace GarrysMod::Lua; | |
static int Angle_SetUnpacked (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::ANGLE); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *angle = (vec_t *)userdata->data; | |
for (int i = 0; i < 3; i++) | |
{ | |
angle [i] = (vec_t)LUA->GetNumber (i + 2); | |
} | |
return 0; | |
} | |
static int Angle_Unpack (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::ANGLE); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *angle = (vec_t *)userdata->data; | |
for (int i = 0; i < 3; i++) | |
{ | |
LUA->PushNumber (angle [i]); | |
} | |
return 3; | |
} | |
static int Vector_SetUnpacked (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::VECTOR); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *vector = (vec_t *)userdata->data; | |
for (int i = 0; i < 3; i++) | |
{ | |
vector [i] = (vec_t)LUA->GetNumber (i + 2); | |
} | |
return 0; | |
} | |
static int Vector_Unpack (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::VECTOR); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *vector = (vec_t *)userdata->data; | |
for (int i = 0; i < 3; i++) | |
{ | |
LUA->PushNumber (vector [i]); | |
} | |
return 3; | |
} | |
static int VMatrix_SetUnpacked (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::MATRIX); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *vmatrix = (vec_t *)userdata->data; | |
for (int i = 0; i < 16; i++) | |
{ | |
vmatrix [i] = (vec_t)LUA->GetNumber (i + 2); | |
} | |
return 0; | |
} | |
static int VMatrix_Unpack (lua_State *state) | |
{ | |
LUA->CheckType (1, Type::MATRIX); | |
UserData *userdata = (UserData *)LUA->GetUserdata (1); | |
vec_t *vmatrix = (vec_t *)userdata->data; | |
for (int i = 0; i < 16; i++) | |
{ | |
LUA->PushNumber (vmatrix [i]); | |
} | |
return 16; | |
} | |
GMOD_MODULE_OPEN () | |
{ | |
// _R.Angle | |
{ | |
LUA->PushSpecial (GarrysMod::Lua::SPECIAL_REG); | |
LUA->GetField (-1, "Angle"); | |
// .SetUnpacked = Angle_SetUnpacked | |
LUA->PushCFunction (Angle_SetUnpacked); | |
LUA->SetField (-2, "SetUnpacked"); | |
// .Unpack = Angle_Unpack | |
LUA->PushCFunction (Angle_Unpack); | |
LUA->SetField (-2, "Unpack"); | |
LUA->Pop(); | |
} | |
// _R.Vector | |
{ | |
LUA->PushSpecial (GarrysMod::Lua::SPECIAL_REG); | |
LUA->GetField (-1, "Vector"); | |
// .SetUnpacked = Vector_SetUnpacked | |
LUA->PushCFunction (Vector_SetUnpacked); | |
LUA->SetField (-2, "SetUnpacked"); | |
// .Unpack = Vector_Unpack | |
LUA->PushCFunction (Vector_Unpack); | |
LUA->SetField (-2, "Unpack"); | |
LUA->Pop(); | |
} | |
// _R.VMatrix | |
{ | |
LUA->PushSpecial (GarrysMod::Lua::SPECIAL_REG); | |
LUA->GetField (-1, "VMatrix"); | |
// .SetUnpacked = VMatrix_SetUnpacked | |
LUA->PushCFunction (VMatrix_SetUnpacked); | |
LUA->SetField (-2, "SetUnpacked"); | |
// .Unpack = VMatrix_Unpack | |
LUA->PushCFunction (VMatrix_Unpack); | |
LUA->SetField (-2, "Unpack"); | |
LUA->Pop(); | |
} | |
return 0; | |
} | |
GMOD_MODULE_CLOSE () | |
{ | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- x, y, z = Vector:Unpack () | |
-- p, y, r = Angle:Unpack () | |
-- m11, m12, m13, m14, | |
-- m21, m22, m23, m24, | |
-- m31, m32, m33, m34, | |
-- m41, m42, m43, m44 = VMatrix:Unpack () | |
-- Vector:SetPacked (x, y, z) | |
-- Angle:SetPacked (p, y, r) | |
-- VMatrix:SetPacked ( | |
-- m11, m12, m13, m14, | |
-- m21, m22, m23, m24, | |
-- m31, m32, m33, m34, | |
-- m41, m42, m43, m44 | |
-- ) | |
local _R = debug.getregistry () | |
local function Profile (name, f, ...) | |
local t0 = SysTime () | |
for i = 1, 1000 do | |
f (...) | |
end | |
local duration = SysTime () - t0 | |
MsgN (string.format ("%s (1000x) took %.3f ms", name, duration * 1000)) | |
end | |
-- Vector | |
local Vector___index = _R.Vector.__index | |
local Vector___newindex = _R.Vector.__newindex | |
local Vector_SetUnpacked = _R.Vector.SetUnpacked | |
local Vector_Unpack = _R.Vector.Unpack | |
local function VectorGetElementsUsingIndexer (v) | |
return Vector___index (v, 1), Vector___index (v, 2), Vector___index (v, 3) | |
end | |
local function VectorGetElementsUsingUnpack (v) | |
return Vector_Unpack (v) | |
end | |
local function VectorSetElementsUsingIndexer (v, x, y, z) | |
Vector___newindex (v, 1, x) | |
Vector___newindex (v, 2, y) | |
Vector___newindex (v, 3, z) | |
return v | |
end | |
local function VectorSetElementsUsingSetUnpacked (v, x, y, z) | |
Vector_SetUnpacked (v, x, y, z) | |
return v | |
end | |
Profile ("VectorGetElementsUsingIndexer", VectorGetElementsUsingIndexer, Vector ()) | |
Profile ("VectorGetElementsUsingUnpack", VectorGetElementsUsingUnpack, Vector ()) | |
Profile ("VectorSetElementsUsingIndexer", VectorSetElementsUsingIndexer, Vector (), 1, 2, 3) | |
Profile ("VectorSetElementsUsingSetUnpacked", VectorSetElementsUsingSetUnpacked, Vector (), 1, 2, 3) | |
-- Matrix | |
local VMatrix_GetField = _R.VMatrix.GetField | |
local VMatrix_SetField = _R.VMatrix.SetField | |
local VMatrix_SetUnpacked = _R.VMatrix.SetUnpacked | |
local VMatrix_ToTable = _R.VMatrix.ToTable | |
local VMatrix_Unpack = _R.VMatrix.Unpack | |
local function MatrixGetElementsUsingGetField (m) | |
return VMatrix_GetField (m, 1, 1), VMatrix_GetField (m, 1, 2), VMatrix_GetField (m, 1, 3), VMatrix_GetField (m, 1, 4), | |
VMatrix_GetField (m, 2, 1), VMatrix_GetField (m, 2, 2), VMatrix_GetField (m, 2, 3), VMatrix_GetField (m, 2, 4), | |
VMatrix_GetField (m, 3, 1), VMatrix_GetField (m, 3, 2), VMatrix_GetField (m, 3, 3), VMatrix_GetField (m, 3, 4), | |
VMatrix_GetField (m, 4, 1), VMatrix_GetField (m, 4, 2), VMatrix_GetField (m, 4, 3), VMatrix_GetField (m, 4, 4) | |
end | |
local function MatrixGetElementsUsingToTable (m) | |
return VMatrix_ToTable (m) | |
end | |
local function MatrixGetElementsUsingUnpack (m) | |
return VMatrix_Unpack (m) | |
end | |
local function MatrixSetElementsUsingSetField (m, | |
m11, m12, m13, m14, | |
m21, m22, m23, m24, | |
m31, m32, m33, m34, | |
m41, m42, m43, m44) | |
VMatrix_SetField (m, 1, 1, m11) VMatrix_SetField (m, 1, 2, m12) VMatrix_SetField (m, 1, 3, m13) VMatrix_SetField (m, 1, 4, m14) | |
VMatrix_SetField (m, 2, 1, m21) VMatrix_SetField (m, 2, 2, m22) VMatrix_SetField (m, 2, 3, m23) VMatrix_SetField (m, 2, 4, m24) | |
VMatrix_SetField (m, 3, 1, m31) VMatrix_SetField (m, 3, 2, m32) VMatrix_SetField (m, 3, 3, m33) VMatrix_SetField (m, 3, 4, m34) | |
VMatrix_SetField (m, 4, 1, m41) VMatrix_SetField (m, 4, 2, m42) VMatrix_SetField (m, 4, 3, m43) VMatrix_SetField (m, 4, 4, m44) | |
return m | |
end | |
local function MatrixSetElementsUsingSetUnpacked (m, | |
m11, m12, m13, m14, | |
m21, m22, m23, m24, | |
m31, m32, m33, m34, | |
m41, m42, m43, m44) | |
VMatrix_SetUnpacked (m, | |
m11, m12, m13, m14, | |
m21, m22, m23, m24, | |
m31, m32, m33, m34, | |
m41, m42, m43, m44 | |
) | |
return m | |
end | |
Profile ("MatrixGetElementsUsingGetField", MatrixGetElementsUsingGetField, Matrix ()) | |
Profile ("MatrixGetElementsUsingToTable", MatrixGetElementsUsingToTable, Matrix ()) | |
Profile ("MatrixGetElementsUsingUnpack", MatrixGetElementsUsingUnpack, Matrix ()) | |
Profile ("MatrixSetElementsUsingSetField", MatrixSetElementsUsingSetField, Matrix (), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) | |
Profile ("MatrixSetElementsUsingSetUnpacked", MatrixSetElementsUsingSetUnpacked, Matrix (), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VectorGetElementsUsingIndexer (1000x) took 1.199 ms | |
VectorGetElementsUsingUnpack (1000x) took 0.468 ms | |
VectorSetElementsUsingIndexer (1000x) took 0.987 ms | |
VectorSetElementsUsingSetUnpacked (1000x) took 0.499 ms | |
MatrixGetElementsUsingGetField (1000x) took 4.262 ms | |
MatrixGetElementsUsingToTable (1000x) took 4.860 ms | |
MatrixGetElementsUsingUnpack (1000x) took 0.570 ms | |
MatrixSetElementsUsingSetField (1000x) took 4.254 ms | |
MatrixSetElementsUsingSetUnpacked (1000x) took 0.588 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment