Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-Mutating Vectors #731

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions etc/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ function lovr.run()
lovr.graphics.present()
end
if lovr.headset then lovr.headset.submit() end
if lovr.math then lovr.math.drain() end
end
end

Expand Down Expand Up @@ -270,8 +269,6 @@ function lovr.errhand(message)
lovr.graphics.present()
end
end

lovr.math.drain()
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,13 @@ void luax_optcolor(lua_State* L, int index, float color[4]) {
break;
case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA: {
VectorType type;
float* v = luax_tovector(L, index, &type);
if (type == V_VEC3) {
int vectorType;
float* v = luax_tovector(L, index, &vectorType);
if (vectorType == V_VEC3) {
memcpy(color, v, 3 * sizeof(float));
color[3] = 1.f;
break;
} else if (type == V_VEC4) {
} else if (vectorType == V_VEC4) {
memcpy(color, v, 4 * sizeof(float));
break;
}
Expand Down
17 changes: 13 additions & 4 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,19 @@ struct ColoredString* luax_checkcoloredstrings(lua_State* L, int index, uint32_t
#endif

#ifndef LOVR_DISABLE_MATH
#include "math/math.h" // TODO
float* luax_tovector(lua_State* L, int index, VectorType* type);
float* luax_checkvector(lua_State* L, int index, VectorType type, const char* expected);
float* luax_newtempvector(lua_State* L, VectorType type);
enum {
V_NONE,
V_VEC2,
V_VEC3,
V_VEC4,
V_QUAT,
V_MAT4,
MAX_VECTOR_TYPES
};

float* luax_tovector(lua_State* L, int index, int* type);
float* luax_checkvector(lua_State* L, int index, int type, const char* expected);
float* luax_newvector(lua_State* L, int type);
int luax_readvec2(lua_State* L, int index, float* v, const char* expected);
int luax_readvec3(lua_State* L, int index, float* v, const char* expected);
int luax_readvec4(lua_State* L, int index, float* v, const char* expected);
Expand Down
15 changes: 8 additions & 7 deletions src/api/l_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ void luax_checkvariant(lua_State* L, int index, Variant* variant) {
/* fallthrough */

case LUA_TLIGHTUSERDATA: {
VectorType type;
float* v = luax_tovector(L, index, &type);
int vtype;
float* v = luax_tovector(L, index, &vtype);
if (v) {
if (type == V_MAT4) {
if (vtype == V_MAT4) {
variant->type = TYPE_MATRIX;
variant->value.matrix.data = malloc(16 * sizeof(float));
lovrAssert(variant->value.matrix.data, "Out of memory");
memcpy(variant->value.matrix.data, v, 16 * sizeof(float));
break;
} else {
variant->type = TYPE_VECTOR;
variant->value.vector.type = type;
memcpy(variant->value.vector.data, v, (type == V_VEC2 ? 2 : 4) * sizeof(float));
variant->value.vector.type = vtype;
size_t components = vtype == V_VEC2 ? 2 : (vtype == V_VEC3 ? 3 : 4);
memcpy(variant->value.vector.data, v, components * sizeof(float));
break;
}
} else if (lua_type(L, index) == LUA_TLIGHTUSERDATA) {
Expand All @@ -125,8 +126,8 @@ int luax_pushvariant(lua_State* L, Variant* variant) {
case TYPE_MINISTRING: lua_pushlstring(L, variant->value.ministring.data, variant->value.ministring.length); return 1;
case TYPE_POINTER: lua_pushlightuserdata(L, variant->value.pointer); return 1;
case TYPE_OBJECT: _luax_pushtype(L, variant->value.object.type, hash64(variant->value.object.type, strlen(variant->value.object.type)), variant->value.object.pointer); return 1;
case TYPE_VECTOR: memcpy(luax_newtempvector(L, variant->value.vector.type), variant->value.vector.data, (variant->value.vector.type == V_VEC2 ? 2 : 4) * sizeof(float)); return 1;
case TYPE_MATRIX: memcpy(luax_newtempvector(L, V_MAT4), variant->value.vector.data, 16 * sizeof(float)); return 1;
case TYPE_VECTOR: memcpy(luax_newvector(L, variant->value.vector.type), variant->value.vector.data, (variant->value.vector.type == V_VEC2 ? 2 : 4) * sizeof(float)); return 1;
case TYPE_MATRIX: memcpy(luax_newvector(L, V_MAT4), variant->value.vector.data, 16 * sizeof(float)); return 1;
default: return 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_graphics_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void luax_checkfieldv(lua_State* L, int index, int type, void* data) {
DataPointer p = { .raw = data };
uint32_t n = typeComponents[type];
lovrCheck(n > 1, "Expected number for scalar data type, got vector");
VectorType vectorType;
int vectorType;
float* v = luax_tovector(L, index, &vectorType);
lovrCheck(v, "Expected vector, got non-vector userdata");
if (n >= TYPE_MAT2 && n <= TYPE_MAT4) {
Expand Down
6 changes: 3 additions & 3 deletions src/api/l_graphics_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ static int l_lovrModelSetNodeTransform(lua_State* L) {
Model* model = luax_checktype(L, 1, Model);
uint32_t node = luax_checknodeindex(L, 2, lovrModelGetInfo(model)->data);
int index = 3;
VectorType type;
int vtype;
float position[3], scale[3], rotation[4];
float* m = luax_tovector(L, index, &type);
if (m && type == V_MAT4) {
float* m = luax_tovector(L, index, &vtype);
if (m && vtype == V_MAT4) {
mat4_getPosition(m, position);
mat4_getScale(m, scale);
mat4_getOrientation(m, rotation);
Expand Down
8 changes: 4 additions & 4 deletions src/api/l_graphics_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ static int l_lovrPassGetViewPose(lua_State* L) {
static int l_lovrPassSetViewPose(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
uint32_t view = luaL_checkinteger(L, 2) - 1;
VectorType type;
float* p = luax_tovector(L, 3, &type);
if (p && type == V_MAT4) {
int vtype;
float* p = luax_tovector(L, 3, &vtype);
if (p && vtype == V_MAT4) {
float matrix[16];
mat4_init(matrix, p);
bool inverted = lua_toboolean(L, 4);
Expand Down Expand Up @@ -876,7 +876,7 @@ static int l_lovrPassSphere(lua_State* L) {

static bool luax_checkendpoints(lua_State* L, int index, float transform[16], bool center) {
float *v, *u;
VectorType t1, t2;
int t1, t2;
if ((v = luax_tovector(L, index + 0, &t1)) == NULL || t1 != V_VEC3) return false;
if ((u = luax_tovector(L, index + 1, &t2)) == NULL || t2 != V_VEC3) return false;
float radius = luax_optfloat(L, index + 2, 1.);
Expand Down