Refactored and simplified lua stuff a lot.
This commit is contained in:
@@ -9,19 +9,20 @@
|
||||
#include "assert/assert.h"
|
||||
#include "display/camera/camera.h"
|
||||
#include "util/memory.h"
|
||||
#include "script/struct/scriptstruct.h"
|
||||
#include "util/string.h"
|
||||
|
||||
void moduleCamera(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Context cannot be NULL.");
|
||||
|
||||
// Structure
|
||||
scriptStructRegister(
|
||||
context,
|
||||
"camera_mt",
|
||||
moduleCameraGetter,
|
||||
moduleCameraSetter
|
||||
);
|
||||
// Create metatable for camera structure.
|
||||
if(luaL_newmetatable(context->luaState, "camera_mt")) {
|
||||
// Metatable methods
|
||||
lua_pushcfunction(context->luaState, moduleCameraIndex);
|
||||
lua_setfield(context->luaState, -2, "__index");
|
||||
lua_pushcfunction(context->luaState, moduleCameraNewIndex);
|
||||
lua_setfield(context->luaState, -2, "__newindex");
|
||||
lua_pop(context->luaState, 1);
|
||||
}
|
||||
|
||||
// Definitions
|
||||
#define MODULE_CAMERA_SCRIPT_LEN 64
|
||||
@@ -67,9 +68,9 @@ void moduleCamera(scriptcontext_t *context) {
|
||||
);
|
||||
|
||||
// Methods
|
||||
scriptContextRegFunc(context, "cameraCreate", moduleCameraCreate);
|
||||
scriptContextRegFunc(context, "cameraPushMatrix", moduleCameraPushMatrix);
|
||||
scriptContextRegFunc(context, "cameraPopMatrix", moduleCameraPopMatrix);
|
||||
lua_register(context->luaState, "cameraCreate", moduleCameraCreate);
|
||||
lua_register(context->luaState, "cameraPushMatrix", moduleCameraPushMatrix);
|
||||
lua_register(context->luaState, "cameraPopMatrix", moduleCameraPopMatrix);
|
||||
}
|
||||
|
||||
int moduleCameraCreate(lua_State *L) {
|
||||
@@ -84,12 +85,13 @@ int moduleCameraCreate(lua_State *L) {
|
||||
projType = (cameraprojectiontype_t)luaL_checkinteger(L, 1);
|
||||
}
|
||||
|
||||
// Create camera.
|
||||
// Create camera that Lua will own
|
||||
camera_t *cam = (camera_t *)lua_newuserdata(L, sizeof(camera_t));
|
||||
memoryZero(cam, sizeof(camera_t));
|
||||
|
||||
// Push metatable.
|
||||
scriptStructPushMetatable(context, "camera_mt", cam);
|
||||
// Set metatable
|
||||
luaL_getmetatable(L, "camera_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// Init camera
|
||||
switch(projType) {
|
||||
@@ -114,9 +116,11 @@ int moduleCameraCreate(lua_State *L) {
|
||||
|
||||
int moduleCameraPushMatrix(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL.");
|
||||
assertTrue(lua_gettop(L) >= 1, "cameraPushMatrix requires 1 arg.");
|
||||
assertTrue(lua_isuserdata(L, 1), "cameraPushMatrix arg must be userdata.");
|
||||
|
||||
// Camera should be provided (pointer to camera_t).
|
||||
camera_t *cam = *(camera_t **)lua_touserdata(L, 1);
|
||||
camera_t *cam = (camera_t *)luaL_checkudata(L, 1, "camera_mt");
|
||||
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||
|
||||
cameraPushMatrix(cam);
|
||||
@@ -129,64 +133,51 @@ int moduleCameraPopMatrix(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void moduleCameraGetter(
|
||||
const scriptcontext_t *context,
|
||||
const char_t *key,
|
||||
const void *structPtr,
|
||||
scriptvalue_t *outValue
|
||||
) {
|
||||
assertNotNull(context, "Script context cannot be NULL.");
|
||||
assertNotNull(key, "Key cannot be NULL.");
|
||||
assertNotNull(structPtr, "Structure pointer cannot be NULL.");
|
||||
assertNotNull(outValue, "Output value cannot be NULL.");
|
||||
int moduleCameraIndex(lua_State *l) {
|
||||
assertNotNull(l, "Lua state cannot be NULL.");
|
||||
|
||||
camera_t *cam = (camera_t *)structPtr;
|
||||
const char_t *key = luaL_checkstring(l, 2);
|
||||
assertStrLenMin(key, 1, "Key cannot be empty.");
|
||||
|
||||
camera_t *cam = (camera_t *)luaL_checkudata(l, 1, "camera_mt");
|
||||
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||
|
||||
if(stringCompare(key, "near") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->nearClip;
|
||||
return;
|
||||
lua_pushnumber(l, cam->nearClip);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "far") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->farClip;
|
||||
return;
|
||||
lua_pushnumber(l, cam->farClip);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "nearClip") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->nearClip;
|
||||
return;
|
||||
lua_pushnumber(l, cam->nearClip);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "farClip") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->farClip;
|
||||
return;
|
||||
lua_pushnumber(l, cam->farClip);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Perspective relative values
|
||||
if(cam->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) {
|
||||
if(stringCompare(key, "left") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->orthographic.left;
|
||||
return;
|
||||
lua_pushnumber(l, cam->orthographic.left);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "right") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->orthographic.right;
|
||||
return;
|
||||
lua_pushnumber(l, cam->orthographic.right);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "top") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->orthographic.top;
|
||||
return;
|
||||
lua_pushnumber(l, cam->orthographic.top);
|
||||
return 1;
|
||||
} else if(stringCompare(key, "bottom") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->orthographic.bottom;
|
||||
return;
|
||||
lua_pushnumber(l, cam->orthographic.bottom);
|
||||
return 1;
|
||||
}
|
||||
} else if(
|
||||
cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE ||
|
||||
cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED
|
||||
) {
|
||||
if(stringCompare(key, "fov") == 0) {
|
||||
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||
outValue->value.floatValue = cam->perspective.fov;
|
||||
return;
|
||||
lua_pushnumber(l, cam->perspective.fov);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,107 +185,92 @@ void moduleCameraGetter(
|
||||
if(cam->viewType == CAMERA_VIEW_TYPE_MATRIX) {
|
||||
|
||||
} else if(cam->viewType == CAMERA_VIEW_TYPE_LOOKAT) {
|
||||
if(stringCompare(key, "position") == 0) {
|
||||
scriptStructPushMetatable(
|
||||
context,
|
||||
"vec3_mt",
|
||||
(void*)&cam->lookat.position
|
||||
);
|
||||
outValue->type = SCRIPT_VALUE_TYPE_USERDATA;
|
||||
return;
|
||||
}
|
||||
// TODO: Push vec3
|
||||
} else if(cam->viewType == CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT) {
|
||||
|
||||
} else if(cam->viewType == CAMERA_VIEW_TYPE_2D) {
|
||||
|
||||
}
|
||||
|
||||
lua_pushnil(l);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void moduleCameraSetter(
|
||||
const scriptcontext_t *context,
|
||||
const char_t *key,
|
||||
void *structPtr,
|
||||
const scriptvalue_t *inValue
|
||||
) {
|
||||
assertNotNull(context, "Script context cannot be NULL.");
|
||||
assertNotNull(key, "Key cannot be NULL.");
|
||||
assertNotNull(structPtr, "Structure pointer cannot be NULL.");
|
||||
assertNotNull(inValue, "Input value cannot be NULL.");
|
||||
int moduleCameraNewIndex(lua_State *l) {
|
||||
assertNotNull(l, "Lua state cannot be NULL.");
|
||||
|
||||
const char_t *key = luaL_checkstring(l, 2);
|
||||
assertStrLenMin(key, 1, "Key cannot be empty.");
|
||||
|
||||
camera_t *cam = (camera_t *)structPtr;
|
||||
camera_t *cam = (camera_t *)luaL_checkudata(l, 1, "camera_mt");
|
||||
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||
|
||||
if(stringCompare(key, "near") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->nearClip = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->nearClip = (float_t)inValue->value.intValue;
|
||||
if(stringCompare(key, "near") == 0 || stringCompare(key, "nearClip") == 0) {
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera near clip must be a number.");
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "far") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->farClip = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->farClip = (float_t)inValue->value.intValue;
|
||||
cam->nearClip = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stringCompare(key, "far") == 0 || stringCompare(key, "farClip") == 0) {
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera far clip must be a number.");
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "nearClip") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->nearClip = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->nearClip = (float_t)inValue->value.intValue;
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "farClip") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->farClip = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->farClip = (float_t)inValue->value.intValue;
|
||||
}
|
||||
return;
|
||||
cam->farClip = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Perspective relative values
|
||||
if(cam->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) {
|
||||
if(stringCompare(key, "left") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->orthographic.left = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->orthographic.left = (float_t)inValue->value.intValue;
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera orthographic left must be a number.");
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "right") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->orthographic.right = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->orthographic.right = (float_t)inValue->value.intValue;
|
||||
cam->orthographic.left = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stringCompare(key, "right") == 0) {
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera orthographic right must be a number.");
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "top") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->orthographic.top = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->orthographic.top = (float_t)inValue->value.intValue;
|
||||
cam->orthographic.right = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stringCompare(key, "top") == 0) {
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera orthographic top must be a number.");
|
||||
}
|
||||
return;
|
||||
} else if(stringCompare(key, "bottom") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->orthographic.bottom = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->orthographic.bottom = (float_t)inValue->value.intValue;
|
||||
cam->orthographic.top = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stringCompare(key, "bottom") == 0) {
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera orthographic bottom must be a number.");
|
||||
}
|
||||
return;
|
||||
cam->orthographic.bottom = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
} else if(
|
||||
cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE ||
|
||||
cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED
|
||||
) {
|
||||
if(stringCompare(key, "fov") == 0) {
|
||||
if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
|
||||
cam->perspective.fov = inValue->value.floatValue;
|
||||
} else if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
|
||||
cam->perspective.fov = (float_t)inValue->value.intValue;
|
||||
if(!lua_isnumber(l, 3)) {
|
||||
luaL_error(l, "Camera perspective FOV must be a number.");
|
||||
}
|
||||
return;
|
||||
cam->perspective.fov = (float_t)lua_tonumber(l, 3);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
luaL_error(
|
||||
l,
|
||||
"Attempted to set unknown or read-only camera field '%s'.",
|
||||
key
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user