int method_index2_set(lua_State *L) { D3DXMATRIX *m; float* cells; float v; int i, j; lua_getfield(L, 1, "i"); i = (int)lua_tonumber(L, -1) - 1; j = (int)lua_tonumber(L, 2) - 1; v = lua_tonumber(L, 3); if ( i >= 0 && j >= 0 && i < 4 && j < 4 ) { lua_getfield(L, 1, "matrix"); m = luaL_checkudata(L, -1, "D3DXMATRIXForLua"); cells = (float*)m; cells[j*4 + i] = v; } return 0;}int method_index2_get(lua_State *L) { D3DXMATRIX *m; int i, j; lua_getfield(L, 1, "i"); i = (int)lua_tonumber(L, -1) - 1; j = (int)lua_tonumber(L, 2) - 1; if ( i < 0 || j < 0 || i > 3 || j > 3 ) return 0; lua_getfield(L, 1, "matrix"); m = luaL_checkudata(L, -1, "D3DXMATRIXForLua"); float* cells = (float*)m; lua_pushnumber(cells[j*4 + i]); return 1;}int method_index1(lua_State *L) { lua_createtable(L, 0, 2); lua_pushvalue(L, 2); lua_setfield(L, -2, "i"); lua_pushvalue(L, 1); lua_setfield(L, -2, "matrix"); if ( luaL_newmetatable(L, "D3DXMATRIXIndexedOnce") ) { lua_pushcfunction(L, method_index2_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, method_index2_set); lua_setfield(L, -2, "__newindex"); } lua_setmetatable(L, -2); return 1;}void luaMakeMatrix(lua_State *L, D3DXMATRIX** handle){ *handle = (D3DXMATRIX*)lua_newuserdata(L, sizeof(D3DXMATRIX)); if ( luaL_newmetatable(L, "D3DXMATRIXForLua") ) { // Add any other methods you want; I've made indexing for you. lua_pushcfunction(L, method_index1); lua_setfield(L, -2, "__index"); } lua_setmetatable(L, -2);}void luaGetMatrix(lua_State *L, D3DXMATRIX** handle, int pos){ // Throws a Lua error if given stack position is not a D3DXMATRIX. *handle = luaL_checkudata(L, pos, "D3DXMATRIXForLua");}
C usage:
// Create a new matrix, pushing it on the stack and filling a pointer to// the newly-allocated and garbage-collected-for-you-by-lua matrix.D3DXMATRIX* matrix;luaMakeMatrix(L, &matrix);matrix->_11 = matrix->_12 = matrix->_13 = matrix->_14 = 0;// Fill a pointer with the matrix on the lua stack; in this case,// where the matrix is passed as argument 2 to this C implementation// of a lua-protocol function.void befoozle(lua_State *L){ D3DXMATRIX* m; luaGetMatrix(L, &matrix, 2); m->_13 = -m->_13; lua_pushnumber(L, m->_33); return 1;}
Lua usage:
function frobnicate(matrix) matrix[3][1] = 8 return matrix[2][0]end
Untested, and I've assumed, possibly incorrectly, that the float[16] equivalent to a D3DXMATRIX goes [_11, _21, _31, _41, _12, _22, _32... _43, _44]. Just swap i and j in the setter and getter to fix that.
Paypal transfer not required.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.