diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-27 14:33:25 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-27 14:33:25 -0500 |
commit | 3ef1112d085a101aa55d11ff894f1fb515d29c27 (patch) | |
tree | 32095f981cdfedbf6a8791b834f8ab5c82f8646e | |
parent | acc262b019206d09ce0d297f7b185862fe432601 (diff) |
add mesh lua functions and refactor lua type names
-rw-r--r-- | src/cglm_bindings.c | 88 | ||||
-rw-r--r-- | src/common.h | 27 | ||||
-rw-r--r-- | src/honey_lua.c | 131 | ||||
-rw-r--r-- | src/input/input.c | 272 | ||||
-rw-r--r-- | src/mesh/mesh.c | 152 | ||||
-rw-r--r-- | src/mesh/mesh.h | 38 | ||||
-rw-r--r-- | src/primitives/primitives.c | 356 | ||||
-rw-r--r-- | src/shader/shader.c | 28 | ||||
-rw-r--r-- | src/window/window.c | 24 |
9 files changed, 623 insertions, 493 deletions
diff --git a/src/cglm_bindings.c b/src/cglm_bindings.c index 10c44cc..300b85b 100644 --- a/src/cglm_bindings.c +++ b/src/cglm_bindings.c @@ -3,54 +3,54 @@ void honey_setup_cglm(lua_State* L) { honey_lua_element vec3_elements[] = { - { "dot", HONEY_FUNC, { .function = honey_cglm_vec3_dot } }, - { "cross", HONEY_FUNC, { .function = honey_cglm_vec3_cross } }, - { "square_norm", HONEY_FUNC, { .function = honey_cglm_vec3_square_norm } }, - { "norm", HONEY_FUNC, { .function = honey_cglm_vec3_norm } }, + { "dot", HONEY_FUNCTION, { .function = honey_cglm_vec3_dot } }, + { "cross", HONEY_FUNCTION, { .function = honey_cglm_vec3_cross } }, + { "square_norm", HONEY_FUNCTION, { .function = honey_cglm_vec3_square_norm } }, + { "norm", HONEY_FUNCTION, { .function = honey_cglm_vec3_norm } }, }; honey_lua_element vec4_elements[] = { - { "dot", HONEY_FUNC, { .function = honey_cglm_vec4_dot } }, - { "norm2", HONEY_FUNC, { .function = honey_cglm_vec4_norm2 } }, - { "norm", HONEY_FUNC, { .function = honey_cglm_vec4_norm } }, - { "add", HONEY_FUNC, { .function = honey_cglm_vec4_add } }, - { "adds", HONEY_FUNC, { .function = honey_cglm_vec4_adds } }, - { "mul", HONEY_FUNC, { .function = honey_cglm_vec4_mul } }, - { "muls", HONEY_FUNC, { .function = honey_cglm_vec4_muls } }, - { "normalize", HONEY_FUNC, { .function = honey_cglm_vec4_normalize } }, - { "distance", HONEY_FUNC, { .function = honey_cglm_vec4_distance } }, - { "lerp", HONEY_FUNC, { .function = honey_cglm_vec4_lerp } }, + { "dot", HONEY_FUNCTION, { .function = honey_cglm_vec4_dot } }, + { "norm2", HONEY_FUNCTION, { .function = honey_cglm_vec4_norm2 } }, + { "norm", HONEY_FUNCTION, { .function = honey_cglm_vec4_norm } }, + { "add", HONEY_FUNCTION, { .function = honey_cglm_vec4_add } }, + { "adds", HONEY_FUNCTION, { .function = honey_cglm_vec4_adds } }, + { "mul", HONEY_FUNCTION, { .function = honey_cglm_vec4_mul } }, + { "muls", HONEY_FUNCTION, { .function = honey_cglm_vec4_muls } }, + { "normalize", HONEY_FUNCTION, { .function = honey_cglm_vec4_normalize } }, + { "distance", HONEY_FUNCTION, { .function = honey_cglm_vec4_distance } }, + { "lerp", HONEY_FUNCTION, { .function = honey_cglm_vec4_lerp } }, }; honey_lua_element mat4_elements[] = { - { "identity", HONEY_FUNC, { .function = honey_cglm_mat4_identity } }, - { "pick3", HONEY_FUNC, { .function = honey_cglm_mat4_pick3 } }, - { "mul", HONEY_FUNC, { .function = honey_cglm_mat4_mul } }, - { "muls", HONEY_FUNC, { .function = honey_cglm_mat4_muls } }, - { "mulv", HONEY_FUNC, { .function = honey_cglm_mat4_mulv } }, - { "trans", HONEY_FUNC, { .function = honey_cglm_mat4_trans } }, - { "det", HONEY_FUNC, { .function = honey_cglm_mat4_det } }, - { "trace", HONEY_FUNC, { .function = honey_cglm_mat4_trace } }, - { "inv", HONEY_FUNC, { .function = honey_cglm_mat4_inv } }, - { "inv_fast", HONEY_FUNC, { .function = honey_cglm_mat4_inv_fast } }, + { "identity", HONEY_FUNCTION, { .function = honey_cglm_mat4_identity } }, + { "pick3", HONEY_FUNCTION, { .function = honey_cglm_mat4_pick3 } }, + { "mul", HONEY_FUNCTION, { .function = honey_cglm_mat4_mul } }, + { "muls", HONEY_FUNCTION, { .function = honey_cglm_mat4_muls } }, + { "mulv", HONEY_FUNCTION, { .function = honey_cglm_mat4_mulv } }, + { "trans", HONEY_FUNCTION, { .function = honey_cglm_mat4_trans } }, + { "det", HONEY_FUNCTION, { .function = honey_cglm_mat4_det } }, + { "trace", HONEY_FUNCTION, { .function = honey_cglm_mat4_trace } }, + { "inv", HONEY_FUNCTION, { .function = honey_cglm_mat4_inv } }, + { "inv_fast", HONEY_FUNCTION, { .function = honey_cglm_mat4_inv_fast } }, }; honey_lua_element affine_elements[] = { - { "translate", HONEY_FUNC, { .function = honey_cglm_translate } }, - { "scale", HONEY_FUNC, { .function = honey_cglm_scale } }, - { "rotate", HONEY_FUNC, { .function = honey_cglm_rotate } }, + { "translate", HONEY_FUNCTION, { .function = honey_cglm_translate } }, + { "scale", HONEY_FUNCTION, { .function = honey_cglm_scale } }, + { "rotate", HONEY_FUNCTION, { .function = honey_cglm_rotate } }, }; honey_lua_element camera_elements[] = { - { "perspective", HONEY_FUNC, { .function = honey_cglm_perspective } }, - { "orthographic", HONEY_FUNC, { .function = honey_cglm_orthographic } }, + { "perspective", HONEY_FUNCTION, { .function = honey_cglm_perspective } }, + { "orthographic", HONEY_FUNCTION, { .function = honey_cglm_orthographic } }, }; honey_lua_element cglm_elements[] = { - { "new_array_zero", HONEY_FUNC, { .function = honey_cglm_new_array_zero } }, - { "set_value", HONEY_FUNC, { .function = honey_cglm_array_set_value } }, - { "get_value", HONEY_FUNC, { .function = honey_cglm_array_get_value } }, - { "copy_array", HONEY_FUNC, { .function = honey_cglm_array_copy } }, + { "new_array_zero", HONEY_FUNCTION, { .function = honey_cglm_new_array_zero } }, + { "set_value", HONEY_FUNCTION, { .function = honey_cglm_array_set_value } }, + { "get_value", HONEY_FUNCTION, { .function = honey_cglm_array_get_value } }, + { "copy_array", HONEY_FUNCTION, { .function = honey_cglm_array_copy } }, { "vec3", HONEY_TABLE, { .table = { 4, vec3_elements } } }, { "vec4", HONEY_TABLE, { .table = { 10, vec4_elements } } }, { "mat4", HONEY_TABLE, { .table = { 10, mat4_elements } } }, @@ -65,7 +65,7 @@ void honey_setup_cglm(lua_State* L) int honey_cglm_new_array_zero(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_INT)) + if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) lua_error(L); int size = lua_tointeger(L, 1); @@ -79,7 +79,7 @@ int honey_cglm_new_array_zero(lua_State* L) int honey_cglm_array_set_value(lua_State* L) { - if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_INT, HONEY_NUM)) + if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_INTEGER, HONEY_NUMBER)) lua_error(L); float* array = lua_touserdata(L, 1); @@ -94,7 +94,7 @@ int honey_cglm_array_set_value(lua_State* L) int honey_cglm_array_get_value(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INT)) + if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INTEGER)) lua_error(L); float* array = lua_touserdata(L, 1); @@ -107,7 +107,7 @@ int honey_cglm_array_get_value(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_array_copy(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INT)) + if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INTEGER)) lua_error(L); float* array = lua_touserdata(L, 1); @@ -253,7 +253,7 @@ int honey_cglm_vec4_add(lua_State* L) int honey_cglm_vec4_adds(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUM, HONEY_USERDATA)) + if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) lua_error(L); float a = lua_tonumber(L, 1); @@ -287,7 +287,7 @@ int honey_cglm_vec4_mul(lua_State* L) int honey_cglm_vec4_muls(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUM, HONEY_USERDATA)) + if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) lua_error(L); float a = lua_tonumber(L, 1); @@ -334,7 +334,7 @@ int honey_cglm_vec4_distance(lua_State* L) int honey_cglm_vec4_lerp(lua_State* L) { - if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_USERDATA, HONEY_NUM)) + if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_USERDATA, HONEY_NUMBER)) lua_error(L); float* a = lua_touserdata(L, 1); @@ -400,7 +400,7 @@ int honey_cglm_mat4_mul(lua_State* L) int honey_cglm_mat4_muls(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUM, HONEY_USERDATA)) + if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) lua_error(L); float a = lua_tonumber(L, 1); @@ -543,7 +543,7 @@ int honey_cglm_scale(lua_State* L) int honey_cglm_rotate(lua_State* L) { if (!honey_lua_validate_types(L, 4, HONEY_USERDATA, HONEY_USERDATA, - HONEY_USERDATA, HONEY_NUM)) + HONEY_USERDATA, HONEY_NUMBER)) lua_error(L); float* matrix = lua_touserdata(L, 1); @@ -566,8 +566,8 @@ int honey_cglm_perspective(lua_State* L) { if (!honey_lua_validate_types(L, 5, HONEY_USERDATA, - HONEY_NUM, HONEY_NUM, - HONEY_NUM, HONEY_NUM)) + HONEY_NUMBER, HONEY_NUMBER, + HONEY_NUMBER, HONEY_NUMBER)) lua_error(L); float* matrix = lua_touserdata(L, 1); diff --git a/src/common.h b/src/common.h index c87992a..e74e824 100644 --- a/src/common.h +++ b/src/common.h @@ -109,11 +109,11 @@ honey_result honey_format_string(char** string, /* lua binding functions */ typedef enum { - HONEY_BOOL, - HONEY_INT, - HONEY_NUM, + HONEY_BOOLEAN, + HONEY_INTEGER, + HONEY_NUMBER, HONEY_STRING, - HONEY_FUNC, + HONEY_FUNCTION, HONEY_TABLE, HONEY_NIL, HONEY_USERDATA, @@ -121,6 +121,25 @@ typedef enum { HONEY_ANY } honey_lua_type; +/** @brief Get arguments from a function, checking to ensure the types match. + * + * If a function should accept a variable list of arguments, but you still wish to ensure + * correct types, use honey_lua_validate_types() and throw an error only if all of your + * possiblities fail to match. + * + * Note that this function will check for correct types of HONEY_TABLE, HONEY_NIL, and + * HONEY_FUNCTION, but does not expect a pointer to them. It performs no check for + * HONEY_ANY, and also does not expect a pointer. + * + * @param[in] L The lua state to parse arguments from. + * @param[in] n The number of arguments to parse. + * @param[in] ... Variadic list of alternating types and pointers to store the type. + * + * @returns Nothing, but throws a lua_error if a type mismatch is detected. + */ +void honey_lua_parse_arguments(lua_State* L, int n, ...); + + /** @brief Check that a functions' arguments are of the correct type. * * @param[in] L The lua state to validate. diff --git a/src/honey_lua.c b/src/honey_lua.c index 1d1375f..f427969 100644 --- a/src/honey_lua.c +++ b/src/honey_lua.c @@ -28,6 +28,123 @@ honey_result honey_format_string(char** string, /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +static void honey_lua_arg_error(lua_State* L, + char* expected_type, + int position) +{ + char* error_message; + honey_result result; + char* got_type = lua_typename(L, lua_type(L, position)); + result = honey_format_string(&error_message, + "bad argument in position %d: " + "expected %s, but got %s instead.", + position, + expected_type, + got_type); + if (result != HONEY_OK) { + lua_pushstring(L, "error allocating memory for error message :("); + } + else { + lua_pushstring(L, error_message); + free(error_message); + } + lua_error(L); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_lua_parse_arguments(lua_State* L, int n, ...) +{ + va_list args; + va_start(args, n); + + for (int i=1; i<=n; i++) { + honey_lua_type expected_type = va_arg(args, honey_lua_type); + + switch (expected_type) { + case HONEY_BOOLEAN: + if (!lua_isboolean(L, i)) + honey_lua_arg_error(L, "boolean", i); + { + bool* result = va_arg(args, bool*); + *result = lua_toboolean(L, i); + } + break; + + case HONEY_INTEGER: + if (!lua_isnumber(L, i)) + honey_lua_arg_error(L, "integer", i); + { + int* result = va_arg(args, int*); + *result = lua_tointeger(L, i); + } + break; + + case HONEY_NUMBER: + if (!lua_isnumber(L, i)) + honey_lua_arg_error(L, "number", i); + { + float* result = va_arg(args, float*); + *result = lua_tonumber(L, i); + } + break; + + case HONEY_STRING: + if (!lua_isstring(L, i)) + honey_lua_arg_error(L, "string", i); + { + char** result = va_arg(args, char**); + *result = lua_tostring(L, i); + } + break; + + case HONEY_TABLE: + if (!lua_istable(L, i)) + honey_lua_arg_error(L, "table", i); + break; + + case HONEY_FUNCTION: + if (!lua_isfunction(L, i)) + honey_lua_arg_error(L, "function", i); + break; + + case HONEY_NIL: + if (!lua_isnil(L, i)) + honey_lua_arg_error(L, "nil", i); + break; + + case HONEY_USERDATA: + if (!lua_isuserdata(L, i)) + honey_lua_arg_error(L, "userdata", i); + { + void** result = va_arg(args, void**); + *result = lua_touserdata(L, i); + } + break; + + case HONEY_LIGHTUSERDATA: + if (!lua_isuserdata(L, i)) + honey_lua_arg_error(L, "light userdata", i); + { + void** result = va_arg(args, void**); + *result = lua_touserdata(L, i); + } + break; + + case HONEY_ANY: + break; + + default: + /* should never get here! */ + break; + } + } + + va_end(args); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + bool honey_lua_validate_types(lua_State* L, unsigned int n_types, ...) @@ -41,7 +158,7 @@ bool honey_lua_validate_types(lua_State* L, char* error_message; switch(expected_type) { - case HONEY_BOOL: + case HONEY_BOOLEAN: if (!lua_isboolean(L, i+1)) { result = honey_format_string(&error_message, "Expected boolean in position %d", @@ -56,8 +173,8 @@ bool honey_lua_validate_types(lua_State* L, } break; - case HONEY_INT: - case HONEY_NUM: + case HONEY_INTEGER: + case HONEY_NUMBER: if (!lua_isnumber(L, i+1)) { result = honey_format_string(&error_message, "Expected number in position %d", @@ -87,7 +204,7 @@ bool honey_lua_validate_types(lua_State* L, } break; - case HONEY_FUNC: + case HONEY_FUNCTION: if (!lua_isfunction(L, i+1)) { result = honey_format_string(&error_message, "Expected function in position %d", @@ -193,11 +310,11 @@ void honey_lua_create_table(lua_State* L, void honey_lua_push_element(lua_State* L, honey_lua_element element) { switch(element.type) { - case HONEY_INT: + case HONEY_INTEGER: lua_pushinteger(L, element.data.integer); break; - case HONEY_NUM: + case HONEY_NUMBER: lua_pushnumber(L, element.data.number); break; @@ -205,7 +322,7 @@ void honey_lua_push_element(lua_State* L, honey_lua_element element) lua_pushstring(L, element.data.string); break; - case HONEY_FUNC: + case HONEY_FUNCTION: lua_pushcfunction(L, element.data.function); break; diff --git a/src/input/input.c b/src/input/input.c index aa472b4..a0ccbdc 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -30,144 +30,144 @@ void honey_setup_input(lua_State* L) glfwSetCursorPosCallback(info->window, honey_glfw_mouse_movement_callback); honey_lua_element key_elements[] = { - { "unknown", HONEY_INT, { HONEY_KEY_UNKNOWN } }, - { "space", HONEY_INT, { HONEY_KEY_SPACE } }, - { "apostrophe", HONEY_INT, { HONEY_KEY_APOSTROPHE } }, - { "comma", HONEY_INT, { HONEY_KEY_COMMA } }, - { "minus", HONEY_INT, { HONEY_KEY_MINUS } }, - { "period", HONEY_INT, { HONEY_KEY_PERIOD } }, - { "slash", HONEY_INT, { HONEY_KEY_SLASH } }, - { "0", HONEY_INT, { HONEY_KEY_0 } }, - { "1", HONEY_INT, { HONEY_KEY_1 } }, - { "2", HONEY_INT, { HONEY_KEY_2 } }, - { "3", HONEY_INT, { HONEY_KEY_3 } }, - { "4", HONEY_INT, { HONEY_KEY_4 } }, - { "5", HONEY_INT, { HONEY_KEY_5 } }, - { "6", HONEY_INT, { HONEY_KEY_6 } }, - { "7", HONEY_INT, { HONEY_KEY_7 } }, - { "8", HONEY_INT, { HONEY_KEY_8 } }, - { "9", HONEY_INT, { HONEY_KEY_9 } }, - { "semicolon", HONEY_INT, { HONEY_KEY_SEMICOLON } }, - { "equal", HONEY_INT, { HONEY_KEY_EQUAL } }, - { "a", HONEY_INT, { HONEY_KEY_A } }, - { "b", HONEY_INT, { HONEY_KEY_B } }, - { "c", HONEY_INT, { HONEY_KEY_C } }, - { "d", HONEY_INT, { HONEY_KEY_D } }, - { "e", HONEY_INT, { HONEY_KEY_E } }, - { "f", HONEY_INT, { HONEY_KEY_F } }, - { "g", HONEY_INT, { HONEY_KEY_G } }, - { "h", HONEY_INT, { HONEY_KEY_H } }, - { "i", HONEY_INT, { HONEY_KEY_I } }, - { "j", HONEY_INT, { HONEY_KEY_J } }, - { "k", HONEY_INT, { HONEY_KEY_K } }, - { "l", HONEY_INT, { HONEY_KEY_L } }, - { "m", HONEY_INT, { HONEY_KEY_M } }, - { "n", HONEY_INT, { HONEY_KEY_N } }, - { "o", HONEY_INT, { HONEY_KEY_O } }, - { "p", HONEY_INT, { HONEY_KEY_P } }, - { "q", HONEY_INT, { HONEY_KEY_Q } }, - { "r", HONEY_INT, { HONEY_KEY_R } }, - { "s", HONEY_INT, { HONEY_KEY_S } }, - { "t", HONEY_INT, { HONEY_KEY_T } }, - { "u", HONEY_INT, { HONEY_KEY_U } }, - { "v", HONEY_INT, { HONEY_KEY_V } }, - { "w", HONEY_INT, { HONEY_KEY_W } }, - { "x", HONEY_INT, { HONEY_KEY_X } }, - { "y", HONEY_INT, { HONEY_KEY_Y } }, - { "z", HONEY_INT, { HONEY_KEY_Z } }, - { "left_bracket", HONEY_INT, { HONEY_KEY_LEFT_BRACKET } }, - { "backslash", HONEY_INT, { HONEY_KEY_BACKSLASH } }, - { "right_bracket", HONEY_INT, { HONEY_KEY_RIGHT_BRACKET } }, - { "grave_accent", HONEY_INT, { HONEY_KEY_GRAVE_ACCENT } }, - { "world_1", HONEY_INT, { HONEY_KEY_WORLD_1 } }, - { "world_2", HONEY_INT, { HONEY_KEY_WORLD_2 } }, - { "escape", HONEY_INT, { HONEY_KEY_ESCAPE } }, - { "enter", HONEY_INT, { HONEY_KEY_ENTER } }, - { "tab", HONEY_INT, { HONEY_KEY_TAB } }, - { "backspace", HONEY_INT, { HONEY_KEY_BACKSPACE } }, - { "insert", HONEY_INT, { HONEY_KEY_INSERT } }, - { "delete", HONEY_INT, { HONEY_KEY_DELETE } }, - { "right", HONEY_INT, { HONEY_KEY_RIGHT } }, - { "left", HONEY_INT, { HONEY_KEY_LEFT } }, - { "down", HONEY_INT, { HONEY_KEY_DOWN } }, - { "up", HONEY_INT, { HONEY_KEY_UP } }, - { "page_up", HONEY_INT, { HONEY_KEY_PAGE_UP } }, - { "page_down", HONEY_INT, { HONEY_KEY_PAGE_DOWN } }, - { "home", HONEY_INT, { HONEY_KEY_HOME } }, - { "end", HONEY_INT, { HONEY_KEY_END } }, - { "caps_lock", HONEY_INT, { HONEY_KEY_CAPS_LOCK } }, - { "scroll_lock", HONEY_INT, { HONEY_KEY_SCROLL_LOCK } }, - { "num_lock", HONEY_INT, { HONEY_KEY_NUM_LOCK } }, - { "print_screen", HONEY_INT, { HONEY_KEY_PRINT_SCREEN } }, - { "pause", HONEY_INT, { HONEY_KEY_PAUSE } }, - { "f1", HONEY_INT, { HONEY_KEY_F1 } }, - { "f2", HONEY_INT, { HONEY_KEY_F2 } }, - { "f3", HONEY_INT, { HONEY_KEY_F3 } }, - { "f4", HONEY_INT, { HONEY_KEY_F4 } }, - { "f5", HONEY_INT, { HONEY_KEY_F5 } }, - { "f6", HONEY_INT, { HONEY_KEY_F6 } }, - { "f7", HONEY_INT, { HONEY_KEY_F7 } }, - { "f8", HONEY_INT, { HONEY_KEY_F8 } }, - { "f9", HONEY_INT, { HONEY_KEY_F9 } }, - { "f10", HONEY_INT, { HONEY_KEY_F10 } }, - { "f11", HONEY_INT, { HONEY_KEY_F11 } }, - { "f12", HONEY_INT, { HONEY_KEY_F12 } }, - { "f13", HONEY_INT, { HONEY_KEY_F13 } }, - { "f14", HONEY_INT, { HONEY_KEY_F14 } }, - { "f15", HONEY_INT, { HONEY_KEY_F15 } }, - { "f16", HONEY_INT, { HONEY_KEY_F16 } }, - { "f17", HONEY_INT, { HONEY_KEY_F17 } }, - { "f18", HONEY_INT, { HONEY_KEY_F18 } }, - { "f19", HONEY_INT, { HONEY_KEY_F19 } }, - { "f20", HONEY_INT, { HONEY_KEY_F20 } }, - { "f21", HONEY_INT, { HONEY_KEY_F21 } }, - { "f22", HONEY_INT, { HONEY_KEY_F22 } }, - { "f23", HONEY_INT, { HONEY_KEY_F23 } }, - { "f24", HONEY_INT, { HONEY_KEY_F24 } }, - { "f25", HONEY_INT, { HONEY_KEY_F25 } }, - { "kp_0", HONEY_INT, { HONEY_KEY_KP_0 } }, - { "kp_1", HONEY_INT, { HONEY_KEY_KP_1 } }, - { "kp_2", HONEY_INT, { HONEY_KEY_KP_2 } }, - { "kp_3", HONEY_INT, { HONEY_KEY_KP_3 } }, - { "kp_4", HONEY_INT, { HONEY_KEY_KP_4 } }, - { "kp_5", HONEY_INT, { HONEY_KEY_KP_5 } }, - { "kp_6", HONEY_INT, { HONEY_KEY_KP_6 } }, - { "kp_7", HONEY_INT, { HONEY_KEY_KP_7 } }, - { "kp_8", HONEY_INT, { HONEY_KEY_KP_8 } }, - { "kp_9", HONEY_INT, { HONEY_KEY_KP_9 } }, - { "kp_decimal", HONEY_INT, { HONEY_KEY_KP_DECIMAL } }, - { "kp_divide", HONEY_INT, { HONEY_KEY_KP_DIVIDE } }, - { "kp_multiply", HONEY_INT, { HONEY_KEY_KP_MULTIPLY } }, - { "kp_subtract", HONEY_INT, { HONEY_KEY_KP_SUBTRACT } }, - { "kp_add", HONEY_INT, { HONEY_KEY_KP_ADD } }, - { "kp_enter", HONEY_INT, { HONEY_KEY_KP_ENTER } }, - { "kp_equal", HONEY_INT, { HONEY_KEY_KP_EQUAL } }, - { "left_shift", HONEY_INT, { HONEY_KEY_LEFT_SHIFT } }, - { "left_control", HONEY_INT, { HONEY_KEY_LEFT_CONTROL } }, - { "left_alt", HONEY_INT, { HONEY_KEY_LEFT_ALT } }, - { "left_super", HONEY_INT, { HONEY_KEY_LEFT_SUPER } }, - { "right_shift", HONEY_INT, { HONEY_KEY_RIGHT_SHIFT } }, - { "right_control", HONEY_INT, { HONEY_KEY_RIGHT_CONTROL } }, - { "right_alt", HONEY_INT, { HONEY_KEY_RIGHT_ALT } }, - { "right_super", HONEY_INT, { HONEY_KEY_RIGHT_SUPER } }, - { "menu", HONEY_INT, { HONEY_KEY_MENU } }, - { "is_down", HONEY_FUNC, { .function = honey_key_down } }, - { "bind", HONEY_FUNC, { .function = honey_key_bind } }, - { "unbind", HONEY_FUNC, { .function = honey_key_unbind } }, - { "unbind_all", HONEY_FUNC, { .function = honey_key_unbind_all } }, + { "unknown", HONEY_INTEGER, { HONEY_KEY_UNKNOWN } }, + { "space", HONEY_INTEGER, { HONEY_KEY_SPACE } }, + { "apostrophe", HONEY_INTEGER, { HONEY_KEY_APOSTROPHE } }, + { "comma", HONEY_INTEGER, { HONEY_KEY_COMMA } }, + { "minus", HONEY_INTEGER, { HONEY_KEY_MINUS } }, + { "period", HONEY_INTEGER, { HONEY_KEY_PERIOD } }, + { "slash", HONEY_INTEGER, { HONEY_KEY_SLASH } }, + { "0", HONEY_INTEGER, { HONEY_KEY_0 } }, + { "1", HONEY_INTEGER, { HONEY_KEY_1 } }, + { "2", HONEY_INTEGER, { HONEY_KEY_2 } }, + { "3", HONEY_INTEGER, { HONEY_KEY_3 } }, + { "4", HONEY_INTEGER, { HONEY_KEY_4 } }, + { "5", HONEY_INTEGER, { HONEY_KEY_5 } }, + { "6", HONEY_INTEGER, { HONEY_KEY_6 } }, + { "7", HONEY_INTEGER, { HONEY_KEY_7 } }, + { "8", HONEY_INTEGER, { HONEY_KEY_8 } }, + { "9", HONEY_INTEGER, { HONEY_KEY_9 } }, + { "semicolon", HONEY_INTEGER, { HONEY_KEY_SEMICOLON } }, + { "equal", HONEY_INTEGER, { HONEY_KEY_EQUAL } }, + { "a", HONEY_INTEGER, { HONEY_KEY_A } }, + { "b", HONEY_INTEGER, { HONEY_KEY_B } }, + { "c", HONEY_INTEGER, { HONEY_KEY_C } }, + { "d", HONEY_INTEGER, { HONEY_KEY_D } }, + { "e", HONEY_INTEGER, { HONEY_KEY_E } }, + { "f", HONEY_INTEGER, { HONEY_KEY_F } }, + { "g", HONEY_INTEGER, { HONEY_KEY_G } }, + { "h", HONEY_INTEGER, { HONEY_KEY_H } }, + { "i", HONEY_INTEGER, { HONEY_KEY_I } }, + { "j", HONEY_INTEGER, { HONEY_KEY_J } }, + { "k", HONEY_INTEGER, { HONEY_KEY_K } }, + { "l", HONEY_INTEGER, { HONEY_KEY_L } }, + { "m", HONEY_INTEGER, { HONEY_KEY_M } }, + { "n", HONEY_INTEGER, { HONEY_KEY_N } }, + { "o", HONEY_INTEGER, { HONEY_KEY_O } }, + { "p", HONEY_INTEGER, { HONEY_KEY_P } }, + { "q", HONEY_INTEGER, { HONEY_KEY_Q } }, + { "r", HONEY_INTEGER, { HONEY_KEY_R } }, + { "s", HONEY_INTEGER, { HONEY_KEY_S } }, + { "t", HONEY_INTEGER, { HONEY_KEY_T } }, + { "u", HONEY_INTEGER, { HONEY_KEY_U } }, + { "v", HONEY_INTEGER, { HONEY_KEY_V } }, + { "w", HONEY_INTEGER, { HONEY_KEY_W } }, + { "x", HONEY_INTEGER, { HONEY_KEY_X } }, + { "y", HONEY_INTEGER, { HONEY_KEY_Y } }, + { "z", HONEY_INTEGER, { HONEY_KEY_Z } }, + { "left_bracket", HONEY_INTEGER, { HONEY_KEY_LEFT_BRACKET } }, + { "backslash", HONEY_INTEGER, { HONEY_KEY_BACKSLASH } }, + { "right_bracket", HONEY_INTEGER, { HONEY_KEY_RIGHT_BRACKET } }, + { "grave_accent", HONEY_INTEGER, { HONEY_KEY_GRAVE_ACCENT } }, + { "world_1", HONEY_INTEGER, { HONEY_KEY_WORLD_1 } }, + { "world_2", HONEY_INTEGER, { HONEY_KEY_WORLD_2 } }, + { "escape", HONEY_INTEGER, { HONEY_KEY_ESCAPE } }, + { "enter", HONEY_INTEGER, { HONEY_KEY_ENTER } }, + { "tab", HONEY_INTEGER, { HONEY_KEY_TAB } }, + { "backspace", HONEY_INTEGER, { HONEY_KEY_BACKSPACE } }, + { "insert", HONEY_INTEGER, { HONEY_KEY_INSERT } }, + { "delete", HONEY_INTEGER, { HONEY_KEY_DELETE } }, + { "right", HONEY_INTEGER, { HONEY_KEY_RIGHT } }, + { "left", HONEY_INTEGER, { HONEY_KEY_LEFT } }, + { "down", HONEY_INTEGER, { HONEY_KEY_DOWN } }, + { "up", HONEY_INTEGER, { HONEY_KEY_UP } }, + { "page_up", HONEY_INTEGER, { HONEY_KEY_PAGE_UP } }, + { "page_down", HONEY_INTEGER, { HONEY_KEY_PAGE_DOWN } }, + { "home", HONEY_INTEGER, { HONEY_KEY_HOME } }, + { "end", HONEY_INTEGER, { HONEY_KEY_END } }, + { "caps_lock", HONEY_INTEGER, { HONEY_KEY_CAPS_LOCK } }, + { "scroll_lock", HONEY_INTEGER, { HONEY_KEY_SCROLL_LOCK } }, + { "num_lock", HONEY_INTEGER, { HONEY_KEY_NUM_LOCK } }, + { "print_screen", HONEY_INTEGER, { HONEY_KEY_PRINT_SCREEN } }, + { "pause", HONEY_INTEGER, { HONEY_KEY_PAUSE } }, + { "f1", HONEY_INTEGER, { HONEY_KEY_F1 } }, + { "f2", HONEY_INTEGER, { HONEY_KEY_F2 } }, + { "f3", HONEY_INTEGER, { HONEY_KEY_F3 } }, + { "f4", HONEY_INTEGER, { HONEY_KEY_F4 } }, + { "f5", HONEY_INTEGER, { HONEY_KEY_F5 } }, + { "f6", HONEY_INTEGER, { HONEY_KEY_F6 } }, + { "f7", HONEY_INTEGER, { HONEY_KEY_F7 } }, + { "f8", HONEY_INTEGER, { HONEY_KEY_F8 } }, + { "f9", HONEY_INTEGER, { HONEY_KEY_F9 } }, + { "f10", HONEY_INTEGER, { HONEY_KEY_F10 } }, + { "f11", HONEY_INTEGER, { HONEY_KEY_F11 } }, + { "f12", HONEY_INTEGER, { HONEY_KEY_F12 } }, + { "f13", HONEY_INTEGER, { HONEY_KEY_F13 } }, + { "f14", HONEY_INTEGER, { HONEY_KEY_F14 } }, + { "f15", HONEY_INTEGER, { HONEY_KEY_F15 } }, + { "f16", HONEY_INTEGER, { HONEY_KEY_F16 } }, + { "f17", HONEY_INTEGER, { HONEY_KEY_F17 } }, + { "f18", HONEY_INTEGER, { HONEY_KEY_F18 } }, + { "f19", HONEY_INTEGER, { HONEY_KEY_F19 } }, + { "f20", HONEY_INTEGER, { HONEY_KEY_F20 } }, + { "f21", HONEY_INTEGER, { HONEY_KEY_F21 } }, + { "f22", HONEY_INTEGER, { HONEY_KEY_F22 } }, + { "f23", HONEY_INTEGER, { HONEY_KEY_F23 } }, + { "f24", HONEY_INTEGER, { HONEY_KEY_F24 } }, + { "f25", HONEY_INTEGER, { HONEY_KEY_F25 } }, + { "kp_0", HONEY_INTEGER, { HONEY_KEY_KP_0 } }, + { "kp_1", HONEY_INTEGER, { HONEY_KEY_KP_1 } }, + { "kp_2", HONEY_INTEGER, { HONEY_KEY_KP_2 } }, + { "kp_3", HONEY_INTEGER, { HONEY_KEY_KP_3 } }, + { "kp_4", HONEY_INTEGER, { HONEY_KEY_KP_4 } }, + { "kp_5", HONEY_INTEGER, { HONEY_KEY_KP_5 } }, + { "kp_6", HONEY_INTEGER, { HONEY_KEY_KP_6 } }, + { "kp_7", HONEY_INTEGER, { HONEY_KEY_KP_7 } }, + { "kp_8", HONEY_INTEGER, { HONEY_KEY_KP_8 } }, + { "kp_9", HONEY_INTEGER, { HONEY_KEY_KP_9 } }, + { "kp_decimal", HONEY_INTEGER, { HONEY_KEY_KP_DECIMAL } }, + { "kp_divide", HONEY_INTEGER, { HONEY_KEY_KP_DIVIDE } }, + { "kp_multiply", HONEY_INTEGER, { HONEY_KEY_KP_MULTIPLY } }, + { "kp_subtract", HONEY_INTEGER, { HONEY_KEY_KP_SUBTRACT } }, + { "kp_add", HONEY_INTEGER, { HONEY_KEY_KP_ADD } }, + { "kp_enter", HONEY_INTEGER, { HONEY_KEY_KP_ENTER } }, + { "kp_equal", HONEY_INTEGER, { HONEY_KEY_KP_EQUAL } }, + { "left_shift", HONEY_INTEGER, { HONEY_KEY_LEFT_SHIFT } }, + { "left_control", HONEY_INTEGER, { HONEY_KEY_LEFT_CONTROL } }, + { "left_alt", HONEY_INTEGER, { HONEY_KEY_LEFT_ALT } }, + { "left_super", HONEY_INTEGER, { HONEY_KEY_LEFT_SUPER } }, + { "right_shift", HONEY_INTEGER, { HONEY_KEY_RIGHT_SHIFT } }, + { "right_control", HONEY_INTEGER, { HONEY_KEY_RIGHT_CONTROL } }, + { "right_alt", HONEY_INTEGER, { HONEY_KEY_RIGHT_ALT } }, + { "right_super", HONEY_INTEGER, { HONEY_KEY_RIGHT_SUPER } }, + { "menu", HONEY_INTEGER, { HONEY_KEY_MENU } }, + { "is_down", HONEY_FUNCTION, { .function = honey_key_down } }, + { "bind", HONEY_FUNCTION, { .function = honey_key_bind } }, + { "unbind", HONEY_FUNCTION, { .function = honey_key_unbind } }, + { "unbind_all", HONEY_FUNCTION, { .function = honey_key_unbind_all } }, }; honey_lua_element mouse_mode_elements[] = { - { "normal", HONEY_INT, { .integer = HONEY_MOUSE_MODE_NORMAL } }, - { "hidden", HONEY_INT, { .integer = HONEY_MOUSE_MODE_HIDDEN } }, - { "disabled", HONEY_INT, { .integer = HONEY_MOUSE_MODE_DISABLED } }, + { "normal", HONEY_INTEGER, { .integer = HONEY_MOUSE_MODE_NORMAL } }, + { "hidden", HONEY_INTEGER, { .integer = HONEY_MOUSE_MODE_HIDDEN } }, + { "disabled", HONEY_INTEGER, { .integer = HONEY_MOUSE_MODE_DISABLED } }, }; honey_lua_element mouse_elements[] = { { "mode", HONEY_TABLE, { .table = { 3, mouse_mode_elements } } }, - { "set_mode", HONEY_FUNC, { .function = honey_mouse_set_mode } }, - { "bind_movement", HONEY_FUNC, { .function = honey_mouse_movement_bind } }, - { "unbind_movement", HONEY_FUNC, { .function = honey_mouse_movement_unbind } }, + { "set_mode", HONEY_FUNCTION, { .function = honey_mouse_set_mode } }, + { "bind_movement", HONEY_FUNCTION, { .function = honey_mouse_movement_bind } }, + { "unbind_movement", HONEY_FUNCTION, { .function = honey_mouse_movement_unbind } }, }; honey_lua_element input_elements[] = { @@ -201,12 +201,12 @@ int honey_key_bind(lua_State* L) /* lua params: (int) key, (func) callback, (arb) data */ int n_args = lua_gettop(L); if (n_args == 3) { - if (!honey_lua_validate_types(L, 3, HONEY_INT, HONEY_FUNC, HONEY_ANY)) { + if (!honey_lua_validate_types(L, 3, HONEY_INTEGER, HONEY_FUNCTION, HONEY_ANY)) { lua_error(L); } } else if (n_args == 2) { - if (!honey_lua_validate_types(L, 2, HONEY_INT, HONEY_FUNC)) { + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_FUNCTION)) { lua_error(L); } } @@ -242,7 +242,7 @@ int honey_key_bind(lua_State* L) int honey_key_unbind(lua_State* L) { /* lua params: (int) key */ - if (!honey_lua_validate_types(L, 1, HONEY_INT)) + if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) lua_error(L); int key = lua_tointeger(L, 1); @@ -275,7 +275,7 @@ int honey_key_unbind_all(lua_State* L) int honey_mouse_set_mode(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_INT)) + if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) lua_error(L); int cursor_mode = lua_tointeger(L, 1); @@ -298,7 +298,7 @@ int honey_mouse_set_mode(lua_State* L) int honey_mouse_movement_bind(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_FUNC, HONEY_ANY)) + if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) lua_error(L); honey_mouse_movement_unbind(L); /* avoid memory leaks! */ diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c index 99210eb..55de82c 100644 --- a/src/mesh/mesh.c +++ b/src/mesh/mesh.c @@ -1,86 +1,90 @@ #include "mesh.h" -honey_result honey_mesh_new(honey_mesh* mesh, - float* vertices, - unsigned int n_vertices, - unsigned int n_attributes, - unsigned int* attribute_sizes, - unsigned int* indices, - unsigned int n_indices) { - if (vertices == NULL || n_vertices == 0) { - return HONEY_MESH_BAD_VERTEX_DATA; - } - if (indices == NULL || n_indices == 0) { - return HONEY_MESH_BAD_INDEX_DATA; - } - - unsigned int vertex_size = 0; - for (int i=0; i<n_attributes; i++) { - vertex_size += attribute_sizes[i]; - } - - (*mesh).vertices = malloc(vertex_size*n_vertices * sizeof(float)); - if ((*mesh).vertices == NULL) { - return HONEY_MEMORY_ALLOCATION_ERROR; - } - memcpy((*mesh).vertices, vertices, vertex_size*n_vertices*sizeof(float)); - - (*mesh).indices = malloc(n_indices * sizeof(unsigned int)); - if ((*mesh).indices == NULL) { - return HONEY_MEMORY_ALLOCATION_ERROR; - } - memcpy((*mesh).indices, indices, n_indices * sizeof(unsigned int)); - - (*mesh).n_vertices = n_vertices; - (*mesh).n_indices = n_indices; - - glGenVertexArrays(1, &((*mesh).vertex_array)); - glGenBuffers(1, &((*mesh).vertex_buffer)); - glGenBuffers(1, &((*mesh).element_buffer)); - - glBindVertexArray((*mesh).vertex_array); - - glBindBuffer(GL_ARRAY_BUFFER, (*mesh).vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertex_size*n_vertices*sizeof(float), (*mesh).vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*mesh).element_buffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, n_indices * sizeof(unsigned int), (*mesh).indices, GL_STATIC_DRAW); - - /* set up vertex attributes */ - unsigned int offset = 0; - for (int i=0; i<n_attributes; i++) { - glEnableVertexAttribArray(i); - glVertexAttribPointer(i, - attribute_sizes[i], - GL_FLOAT, - GL_FALSE, - vertex_size*sizeof(float), - (void*) (offset*sizeof(float))); - offset += attribute_sizes[i]; - } - - glBindVertexArray(0); - - return HONEY_OK; +int honey_mesh_new(lua_State* L) +{ + float* vertices; + unsigned int* indices; + int n_vertices, n_indices; + honey_lua_parse_arguments(L, 5, + HONEY_USERDATA, &vertices, + HONEY_USERDATA, &indices, + HONEY_TABLE, + HONEY_INTEGER, &n_vertices, + HONEY_INTEGER, &n_indices); + + size_t n_attributes = lua_objlen(L, 3); + unsigned int* attribute_sizes = malloc(n_attributes * sizeof(unsigned int)); + if (attribute_sizes == NULL) { + lua_pushstring(L, "failed to allocate memory for vertex attributes"); + lua_error(L); + } + + unsigned int vertex_size = 0; + for (int i=0; i<n_attributes; i++) { + lua_rawgeti(L, 2, i+1); + attribute_sizes[i] = lua_tointeger(L, -1); + vertex_size += attribute_sizes[i]; + } + lua_pop(L, n_attributes); + + honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + mesh->n_indices = n_indices; + + glGenVertexArrays(1, &mesh->vertex_array); + glGenBuffers(1, &mesh->vertex_buffer); + glGenBuffers(1, &mesh->element_buffer); + + glBindVertexArray(mesh->vertex_array); + + glBindBuffer(GL_ARRAY_BUFFER, mesh->vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, + vertex_size * n_vertices * sizeof(float), + vertices, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->element_buffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, + n_indices * sizeof(unsigned int), + indices, GL_STATIC_DRAW); + + unsigned int offset = 0; + for (int i=0; i<n_attributes; i++) { + glEnableVertexAttribArray(i); + glVertexAttribPointer(i, + attribute_sizes[i], + GL_FLOAT, GL_FALSE, + vertex_size * sizeof(float), + (void*) (offset * sizeof(float))); + offset += attribute_sizes[i]; + } + + glBindVertexArray(0); + return 1; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void honey_mesh_draw(honey_mesh mesh, int shader) { - glUseProgram(shader); +int honey_mesh_draw(lua_State* L) +{ + honey_mesh* mesh; + int shader; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &mesh, + HONEY_INTEGER, &shader); + + glUseProgram(shader); - glBindVertexArray(mesh.vertex_array); - glDrawElements(GL_TRIANGLES, mesh.n_indices, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + glBindVertexArray(mesh->vertex_array); + glDrawElements(GL_TRIANGLES, mesh->n_indices, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void honey_mesh_delete(honey_mesh mesh) { - free(mesh.vertices); - free(mesh.indices); - - glDeleteVertexArrays(1, &(mesh.vertex_array)); - glDeleteBuffers(1, &(mesh.vertex_buffer)); - glDeleteBuffers(1, &(mesh.element_buffer)); +int honey_mesh_delete(lua_State* L) +{ + honey_mesh* mesh; + honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh); + glDeleteVertexArrays(1, &(mesh->vertex_array)); + glDeleteBuffers(1, &(mesh->vertex_buffer)); + glDeleteBuffers(1, &(mesh->element_buffer)); } diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h index 6392faa..a82db52 100644 --- a/src/mesh/mesh.h +++ b/src/mesh/mesh.h @@ -12,46 +12,36 @@ #include "../shader/shader.h" typedef struct { - float* vertices; - unsigned int n_vertices; - unsigned int* indices; - unsigned int n_indices; - unsigned int vertex_array, vertex_buffer, element_buffer; + unsigned int n_vertices, n_indices; + unsigned int vertex_array, vertex_buffer, element_buffer; } honey_mesh; /** @brief Create a new mesh from vertex and index arrays. * - * Note that this function creates copies of the vertex and index arrays, - * so you can deallocate those immediately. + * This function copies the data, so you can safely permit vertices and + * indices to be garbage collected after calling it. * - * @param[out] mesh Pointer to the destination honey_mesh struct - * @param[in] vertices Array of floats representing the vertices - * @param[in] n_attributes The number of attributes per vertex - * @param[in] attribute_sizes An array containing for each attribute how many floats it contains - * @param[in] n_vertices The number of vertices (NOT the number of floats in the vertex array) - * @param[in] indices Array of vertex indices - * @param[in] n_indices The number of elements in the index array + * @param[in] vertices Userdata array containing the vertices. + * @param[in] indices Userdata array defining the mesh faces. + * @param[in] attributes Lua table containing the attribute sizes. + * @param[in] n_vertices Integer number of vertices. + * @param[in] n_indices Integer number of indices. + * + * @returns Userdata containing the mesh's OpenGL handles. */ -honey_result honey_mesh_new(honey_mesh* mesh, - float* vertices, - unsigned int n_vertices, - unsigned int n_attributes, - unsigned int* attribute_sizes, - unsigned int* indices, - unsigned int n_indices); +int honey_mesh_new(lua_State* L); /** @brief Draw a mesh on screen. * * @param[in] mesh The mesh to draw * @param[in] shader The shader to use when drawing the mesh */ -void honey_mesh_draw(honey_mesh mesh, - int shader); +int honey_mesh_draw(lua_State* L); /** @brief Delete a mesh. * * @param[in] mesh The mesh to delete */ -void honey_mesh_delete(honey_mesh mesh); +int honey_mesh_delete(lua_State* L); #endif diff --git a/src/primitives/primitives.c b/src/primitives/primitives.c index efc4a0f..0d8f209 100644 --- a/src/primitives/primitives.c +++ b/src/primitives/primitives.c @@ -1,180 +1,180 @@ #include "primitives.h" -honey_result honey_mesh_new_textured_plane(honey_mesh* mesh, - float width, - float height) { - float x0 = 0; - float y0 = 0; - float x1 = width; - float y1 = height; - - float vertices[] = { - /* position normal uv */ - x0, y0, 0, 0, 0, 1, 0, 0, - x1, y0, 0, 0, 0, 1, 1, 0, - x0, y1, 0, 0, 0, 1, 0, 1, - x1, y1, 0, 0, 0, 1, 1, 1 }; - - unsigned int indices[] = { - 0, 1, 2, - 1, 2, 3 }; - - unsigned int attrib_sizes[] = { 3, 3, 2 }; - - honey_result result = honey_mesh_new(mesh, - vertices, 4, - 3, attrib_sizes, - indices, 6); - return result; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -honey_result honey_mesh_new_cube(honey_mesh* mesh, - float width, - float height, - float depth) { - float x0 = 0; - float y0 = 0; - float z0 = 0; - - float x1 = width; - float y1 = height; - float z1 = depth; - - float vertices[] = { - /* position normal tex coord */ - /* back face */ - x0, y0, z0, 0, 0, -1, - x1, y0, z0, 0, 0, -1, - x0, y1, z0, 0, 0, -1, - x1, y1, z0, 0, 0, -1, - - /* front face */ - x0, y0, z1, 0, 0, 1, - x1, y0, z1, 0, 0, 1, - x0, y1, z1, 0, 0, 1, - x1, y1, z1, 0, 0, 1, - - /* left face */ - x0, y0, z0, -1, 0, 0, - x0, y1, z0, -1, 0, 0, - x0, y0, z1, -1, 0, 0, - x0, y1, z1, -1, 0, 0, - - /* right face */ - x1, y0, z0, 1, 0, 0, - x1, y1, z0, 1, 0, 0, - x1, y0, z1, 1, 0, 0, - x1, y1, z1, 1, 0, 0, - - /* bottom face */ - x0, y0, z0, 0, -1, 0, - x1, y0, z0, 0, -1, 0, - x0, y0, z1, 0, -1, 0, - x1, y0, z1, 0, -1, 0, - - /* top face */ - x0, y1, z0, 0, 1, 0, - x1, y1, z0, 0, 1, 0, - x0, y1, z1, 0, 1, 0, - x1, y1, z1, 0, 1, 0 }; - - unsigned int indices[] = { - 0, 1, 2, - 1, 2, 3, - 4, 5, 6, - 5, 6, 7, - 8, 9, 10, - 9, 10, 11, - 12, 13, 14, - 13, 14, 15, - 16, 17, 18, - 17, 18, 19, - 20, 21, 22, - 21, 22, 23 }; - - unsigned int attrib_sizes[] = { 3, 3 }; - - honey_result result = honey_mesh_new(mesh, - vertices, 24, - 2, attrib_sizes, - indices, 36); - - return result; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -honey_result honey_mesh_new_textured_cube(honey_mesh* mesh, - float width, - float height, - float depth) { - float x0 = 0; - float y0 = 0; - float z0 = 0; - - float x1 = width; - float y1 = height; - float z1 = depth; - - float vertices[] = { - /* position normal tex coord */ - /* back face */ - x0, y0, z0, 0, 0, -1, 0, 0, - x1, y0, z0, 0, 0, -1, 1, 0, - x0, y1, z0, 0, 0, -1, 0, 1, - x1, y1, z0, 0, 0, -1, 1, 1, - - /* front face */ - x0, y0, z1, 0, 0, 1, 0, 0, - x1, y0, z1, 0, 0, 1, 1, 0, - x0, y1, z1, 0, 0, 1, 0, 1, - x1, y1, z1, 0, 0, 1, 1, 1, - - /* left face */ - x0, y0, z0, -1, 0, 0, 0, 0, - x0, y1, z0, -1, 0, 0, 1, 0, - x0, y0, z1, -1, 0, 0, 0, 1, - x0, y1, z1, -1, 0, 0, 1, 1, - - /* right face */ - x1, y0, z0, 1, 0, 0, 0, 0, - x1, y1, z0, 1, 0, 0, 1, 0, - x1, y0, z1, 1, 0, 0, 0, 1, - x1, y1, z1, 1, 0, 0, 1, 1, - - /* bottom face */ - x0, y0, z0, 0, -1, 0, 0, 0, - x1, y0, z0, 0, -1, 0, 1, 0, - x0, y0, z1, 0, -1, 0, 0, 1, - x1, y0, z1, 0, -1, 0, 1, 1, - - /* top face */ - x0, y1, z0, 0, 1, 0, 0, 0, - x1, y1, z0, 0, 1, 0, 1, 0, - x0, y1, z1, 0, 1, 0, 0, 1, - x1, y1, z1, 0, 1, 0, 1, 1 }; - - unsigned int indices[] = { - 0, 1, 2, - 1, 2, 3, - 4, 5, 6, - 5, 6, 7, - 8, 9, 10, - 9, 10, 11, - 12, 13, 14, - 13, 14, 15, - 16, 17, 18, - 17, 18, 19, - 20, 21, 22, - 21, 22, 23 }; - - unsigned int attrib_sizes[] = { 3, 3, 2 }; - - honey_result result; - result = honey_mesh_new(mesh, vertices, 24, - 3, attrib_sizes, - indices, 36); - return result; -} +//honey_result honey_mesh_new_textured_plane(honey_mesh* mesh, +// float width, +// float height) { +// float x0 = 0; +// float y0 = 0; +// float x1 = width; +// float y1 = height; +// +// float vertices[] = { +// /* position normal uv */ +// x0, y0, 0, 0, 0, 1, 0, 0, +// x1, y0, 0, 0, 0, 1, 1, 0, +// x0, y1, 0, 0, 0, 1, 0, 1, +// x1, y1, 0, 0, 0, 1, 1, 1 }; +// +// unsigned int indices[] = { +// 0, 1, 2, +// 1, 2, 3 }; +// +// unsigned int attrib_sizes[] = { 3, 3, 2 }; +// +// honey_result result = honey_mesh_new(mesh, +// vertices, 4, +// 3, attrib_sizes, +// indices, 6); +// return result; +//} +// +///* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +// +//honey_result honey_mesh_new_cube(honey_mesh* mesh, +// float width, +// float height, +// float depth) { +// float x0 = 0; +// float y0 = 0; +// float z0 = 0; +// +// float x1 = width; +// float y1 = height; +// float z1 = depth; +// +// float vertices[] = { +// /* position normal tex coord */ +// /* back face */ +// x0, y0, z0, 0, 0, -1, +// x1, y0, z0, 0, 0, -1, +// x0, y1, z0, 0, 0, -1, +// x1, y1, z0, 0, 0, -1, +// +// /* front face */ +// x0, y0, z1, 0, 0, 1, +// x1, y0, z1, 0, 0, 1, +// x0, y1, z1, 0, 0, 1, +// x1, y1, z1, 0, 0, 1, +// +// /* left face */ +// x0, y0, z0, -1, 0, 0, +// x0, y1, z0, -1, 0, 0, +// x0, y0, z1, -1, 0, 0, +// x0, y1, z1, -1, 0, 0, +// +// /* right face */ +// x1, y0, z0, 1, 0, 0, +// x1, y1, z0, 1, 0, 0, +// x1, y0, z1, 1, 0, 0, +// x1, y1, z1, 1, 0, 0, +// +// /* bottom face */ +// x0, y0, z0, 0, -1, 0, +// x1, y0, z0, 0, -1, 0, +// x0, y0, z1, 0, -1, 0, +// x1, y0, z1, 0, -1, 0, +// +// /* top face */ +// x0, y1, z0, 0, 1, 0, +// x1, y1, z0, 0, 1, 0, +// x0, y1, z1, 0, 1, 0, +// x1, y1, z1, 0, 1, 0 }; +// +// unsigned int indices[] = { +// 0, 1, 2, +// 1, 2, 3, +// 4, 5, 6, +// 5, 6, 7, +// 8, 9, 10, +// 9, 10, 11, +// 12, 13, 14, +// 13, 14, 15, +// 16, 17, 18, +// 17, 18, 19, +// 20, 21, 22, +// 21, 22, 23 }; +// +// unsigned int attrib_sizes[] = { 3, 3 }; +// +// honey_result result = honey_mesh_new(mesh, +// vertices, 24, +// 2, attrib_sizes, +// indices, 36); +// +// return result; +//} +// +///* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +// +//honey_result honey_mesh_new_textured_cube(honey_mesh* mesh, +// float width, +// float height, +// float depth) { +// float x0 = 0; +// float y0 = 0; +// float z0 = 0; +// +// float x1 = width; +// float y1 = height; +// float z1 = depth; +// +// float vertices[] = { +// /* position normal tex coord */ +// /* back face */ +// x0, y0, z0, 0, 0, -1, 0, 0, +// x1, y0, z0, 0, 0, -1, 1, 0, +// x0, y1, z0, 0, 0, -1, 0, 1, +// x1, y1, z0, 0, 0, -1, 1, 1, +// +// /* front face */ +// x0, y0, z1, 0, 0, 1, 0, 0, +// x1, y0, z1, 0, 0, 1, 1, 0, +// x0, y1, z1, 0, 0, 1, 0, 1, +// x1, y1, z1, 0, 0, 1, 1, 1, +// +// /* left face */ +// x0, y0, z0, -1, 0, 0, 0, 0, +// x0, y1, z0, -1, 0, 0, 1, 0, +// x0, y0, z1, -1, 0, 0, 0, 1, +// x0, y1, z1, -1, 0, 0, 1, 1, +// +// /* right face */ +// x1, y0, z0, 1, 0, 0, 0, 0, +// x1, y1, z0, 1, 0, 0, 1, 0, +// x1, y0, z1, 1, 0, 0, 0, 1, +// x1, y1, z1, 1, 0, 0, 1, 1, +// +// /* bottom face */ +// x0, y0, z0, 0, -1, 0, 0, 0, +// x1, y0, z0, 0, -1, 0, 1, 0, +// x0, y0, z1, 0, -1, 0, 0, 1, +// x1, y0, z1, 0, -1, 0, 1, 1, +// +// /* top face */ +// x0, y1, z0, 0, 1, 0, 0, 0, +// x1, y1, z0, 0, 1, 0, 1, 0, +// x0, y1, z1, 0, 1, 0, 0, 1, +// x1, y1, z1, 0, 1, 0, 1, 1 }; +// +// unsigned int indices[] = { +// 0, 1, 2, +// 1, 2, 3, +// 4, 5, 6, +// 5, 6, 7, +// 8, 9, 10, +// 9, 10, 11, +// 12, 13, 14, +// 13, 14, 15, +// 16, 17, 18, +// 17, 18, 19, +// 20, 21, 22, +// 21, 22, 23 }; +// +// unsigned int attrib_sizes[] = { 3, 3, 2 }; +// +// honey_result result; +// result = honey_mesh_new(mesh, vertices, 24, +// 3, attrib_sizes, +// indices, 36); +// return result; +//} diff --git a/src/shader/shader.c b/src/shader/shader.c index 3deb5b8..d733c60 100644 --- a/src/shader/shader.c +++ b/src/shader/shader.c @@ -3,15 +3,15 @@ void honey_setup_shader(lua_State* L) { honey_lua_element shader_elements[] = { - { "new", HONEY_FUNC, { .function = honey_shader_new } }, - { "use", HONEY_FUNC, { .function = honey_shader_use } }, - { "set_int", HONEY_FUNC, { .function = honey_shader_set_int } }, - { "set_float", HONEY_FUNC, { .function = honey_shader_set_float } }, - { "set_vec3", HONEY_FUNC, { .function = honey_shader_set_vec3 } }, - { "set_vec4", HONEY_FUNC, { .function = honey_shader_set_vec4 } }, - { "set_mat3", HONEY_FUNC, { .function = honey_shader_set_mat3 } }, - { "set_mat4", HONEY_FUNC, { .function = honey_shader_set_mat4 } }, - { "delete", HONEY_FUNC, { .function = honey_shader_delete } }, + { "new", HONEY_FUNCTION, { .function = honey_shader_new } }, + { "use", HONEY_FUNCTION, { .function = honey_shader_use } }, + { "set_int", HONEY_FUNCTION, { .function = honey_shader_set_int } }, + { "set_float", HONEY_FUNCTION, { .function = honey_shader_set_float } }, + { "set_vec3", HONEY_FUNCTION, { .function = honey_shader_set_vec3 } }, + { "set_vec4", HONEY_FUNCTION, { .function = honey_shader_set_vec4 } }, + { "set_mat3", HONEY_FUNCTION, { .function = honey_shader_set_mat3 } }, + { "set_mat4", HONEY_FUNCTION, { .function = honey_shader_set_mat4 } }, + { "delete", HONEY_FUNCTION, { .function = honey_shader_delete } }, }; honey_lua_create_table(L, shader_elements, 9); @@ -93,7 +93,7 @@ int honey_shader_new(lua_State* L) int honey_shader_use(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_INT)) + if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) lua_error(L); int shader = lua_tointeger(L, 1); @@ -105,7 +105,7 @@ int honey_shader_use(lua_State* L) int honey_shader_set_int(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_INT, HONEY_STRING, HONEY_INT)) + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_INTEGER)) lua_error(L); int shader = lua_tointeger(L, 1); @@ -123,7 +123,7 @@ int honey_shader_set_int(lua_State* L) int honey_shader_set_float(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_INT, HONEY_STRING, HONEY_NUM)) + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_NUMBER)) lua_error(L); int shader = lua_tointeger(L, 1); @@ -143,7 +143,7 @@ static void get_array(lua_State* L, unsigned int* location, float** value) { - if (!honey_lua_validate_types(L, 2, HONEY_INT, HONEY_STRING, HONEY_USERDATA)) + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_USERDATA)) lua_error(L); int shader = lua_tointeger(L, 1); @@ -206,7 +206,7 @@ int honey_shader_set_mat4(lua_State* L) int honey_shader_delete(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_INT)) + if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) lua_error(L); int shader = lua_tointeger(L, 1); diff --git a/src/window/window.c b/src/window/window.c index da1ddc9..263bd45 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -99,14 +99,14 @@ bool honey_setup_window(lua_State* L) honey_lua_element window_elements[] = { - { "set_fullscreen", HONEY_FUNC, { .function = honey_window_set_fullscreen } }, - { "set_title", HONEY_FUNC, { .function = honey_window_set_title } }, - { "get_size", HONEY_FUNC, { .function = honey_window_get_size } }, - { "set_size", HONEY_FUNC, { .function = honey_window_set_size } }, - { "resize_bind", HONEY_FUNC, { .function = honey_window_resize_bind } }, - { "resize_unbind", HONEY_FUNC, { .function = honey_window_resize_unbind } }, - { "focus_bind", HONEY_FUNC, { .function = honey_window_focus_bind } }, - { "focus_unbind", HONEY_FUNC, { .function = honey_window_focus_unbind } }, + { "set_fullscreen", HONEY_FUNCTION, { .function = honey_window_set_fullscreen } }, + { "set_title", HONEY_FUNCTION, { .function = honey_window_set_title } }, + { "get_size", HONEY_FUNCTION, { .function = honey_window_get_size } }, + { "set_size", HONEY_FUNCTION, { .function = honey_window_set_size } }, + { "resize_bind", HONEY_FUNCTION, { .function = honey_window_resize_bind } }, + { "resize_unbind", HONEY_FUNCTION, { .function = honey_window_resize_unbind } }, + { "focus_bind", HONEY_FUNCTION, { .function = honey_window_focus_bind } }, + { "focus_unbind", HONEY_FUNCTION, { .function = honey_window_focus_unbind } }, }; honey_lua_create_table(L, window_elements, 8); @@ -117,7 +117,7 @@ bool honey_setup_window(lua_State* L) int honey_window_set_fullscreen(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_BOOL)) + if (!honey_lua_validate_types(L, 1, HONEY_BOOLEAN)) lua_error(L); bool fullscreen = lua_toboolean(L, 1); @@ -174,7 +174,7 @@ int honey_window_get_size(lua_State* L) int honey_window_set_size(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_INT, HONEY_INT)) + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_INTEGER)) lua_error(L); int width = lua_tointeger(L, 1); @@ -191,7 +191,7 @@ int honey_window_set_size(lua_State* L) int honey_window_resize_bind(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_FUNC, HONEY_ANY)) + if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) lua_error(L); lua_pushvalue(L, 1); @@ -228,7 +228,7 @@ int honey_window_resize_unbind(lua_State* L) int honey_window_focus_bind(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_FUNC, HONEY_ANY)) + if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) lua_error(L); lua_pushvalue(L, 1); |