From 140666204191b218b72274d8d14921c89a6631fd Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 29 Nov 2020 15:16:42 -0600 Subject: refactor: eliminate src subdirectories for honey files --- src/camera.c | 137 +++++ src/camera.h | 132 +++++ src/camera/camera.c | 137 ----- src/camera/camera.h | 132 ----- src/common.h | 23 +- src/glm_bindings.c | 2 +- src/honey.h | 16 +- src/honey_lua.c | 273 ++++++--- src/input.c | 8 +- src/input.h | 4 +- src/input/input.c | 1355 ------------------------------------------- src/input/input.h | 229 -------- src/light.c | 20 + src/light.h | 33 ++ src/light/light.c | 20 - src/light/light.h | 33 -- src/lua/honey_lua.h | 46 -- src/mesh.c | 232 ++++++++ src/mesh.h | 62 ++ src/mesh/mesh.c | 232 -------- src/mesh/mesh.h | 62 -- src/primitives.c | 223 +++++++ src/primitives.h | 62 ++ src/primitives/primitives.c | 223 ------- src/primitives/primitives.h | 62 -- src/shader.c | 194 +++++++ src/shader.h | 102 ++++ src/shader/shader.c | 198 ------- src/shader/shader.h | 100 ---- src/texture.c | 268 +++++++++ src/texture.h | 121 ++++ src/texture/texture.c | 268 --------- src/texture/texture.h | 121 ---- src/window.c | 268 +++++++++ src/window.h | 86 +++ src/window/window.c | 268 --------- src/window/window.h | 86 --- 37 files changed, 2167 insertions(+), 3671 deletions(-) create mode 100644 src/camera.c create mode 100644 src/camera.h delete mode 100644 src/camera/camera.c delete mode 100644 src/camera/camera.h delete mode 100644 src/input/input.c delete mode 100644 src/input/input.h create mode 100644 src/light.c create mode 100644 src/light.h delete mode 100644 src/light/light.c delete mode 100644 src/light/light.h delete mode 100644 src/lua/honey_lua.h create mode 100644 src/mesh.c create mode 100644 src/mesh.h delete mode 100644 src/mesh/mesh.c delete mode 100644 src/mesh/mesh.h create mode 100644 src/primitives.c create mode 100644 src/primitives.h delete mode 100644 src/primitives/primitives.c delete mode 100644 src/primitives/primitives.h create mode 100644 src/shader.c create mode 100644 src/shader.h delete mode 100644 src/shader/shader.c delete mode 100644 src/shader/shader.h create mode 100644 src/texture.c create mode 100644 src/texture.h delete mode 100644 src/texture/texture.c delete mode 100644 src/texture/texture.h create mode 100644 src/window.c create mode 100644 src/window.h delete mode 100644 src/window/window.c delete mode 100644 src/window/window.h diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..44f480d --- /dev/null +++ b/src/camera.c @@ -0,0 +1,137 @@ +#include "camera.h" + +void honey_camera_new(honey_camera* camera, + vec3 position, + vec3 angle, + enum honey_camera_projection projection_type, + float aspect_ratio, + float near, float far, + float fov, + float left, float right, float top, float bottom) { + glm_vec3_copy(position, camera->position); + glm_vec3_copy(angle, camera->angle); + + camera->projection_type = projection_type; + + camera->aspect_ratio = aspect_ratio; + camera->near = near; + camera->far = far; + + if (projection_type == HONEY_PERSPECTIVE) { + camera->fov = fov; + } + else if (projection_type == HONEY_ORTHOGRAPHIC) { + camera->ortho_left = left; + camera->ortho_right = right; + camera->ortho_top = top; + camera->ortho_bottom = bottom; + } + + honey_camera_calculate_view(camera); + honey_camera_calculate_projection(camera); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_new_perspective(honey_camera* camera, + vec3 position, + vec3 angle, + float aspect_ratio, + float near, float far, + float fov) { + honey_camera_new(camera, + position, angle, + HONEY_PERSPECTIVE, + aspect_ratio, near, far, fov, + 0, 0, 0, 0); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_new_orthographic(honey_camera* camera, + vec3 position, + vec3 angle, + float near, float far, + float left, float right, float top, float bottom) { + honey_camera_new(camera, + position, angle, + HONEY_ORTHOGRAPHIC, + 0, + near, far, + 0, + left, right, top, bottom); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_calculate_look_direction(honey_camera* camera) { + float pitch = camera->angle[0]; + float yaw = camera->angle[1]; + + float x = cos(pitch) * cos(yaw); + float y = sin(pitch); + float z = cos(pitch) * sin(yaw); + + camera->look_direction[0] = x; + camera->look_direction[1] = y; + camera->look_direction[2] = z; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_calculate_up(honey_camera* camera) { + float pitch = camera->angle[0]; + float yaw = camera->angle[1]; + float roll = camera->angle[2]; + + camera->up[0] = 0; + camera->up[1] = 1; + camera->up[2] = 0; + + mat3 rot3; + mat4 rot4; + glm_mat4_identity(rot4); + glm_rotate(rot4, roll, camera->look_direction); + glm_mat4_pick3(rot4, rot3); + + glm_mat3_mulv(rot3, camera->up, camera->up); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_calculate_right(honey_camera* camera) { + glm_vec3_cross(camera->up, camera->look_direction, camera->right); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_calculate_view(honey_camera* camera) { + honey_camera_calculate_look_direction(camera); + honey_camera_calculate_up(camera); + honey_camera_calculate_right(camera); + + glm_look(camera->position, camera->look_direction, camera->up, camera->view); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_camera_calculate_projection(honey_camera* camera) { + if (camera->projection_type == HONEY_PERSPECTIVE) { + glm_mat4_identity(camera->projection); + glm_perspective(camera->fov, + camera->aspect_ratio, + camera->near, + camera->far, + camera->projection); + } + else if (camera->projection_type == HONEY_ORTHOGRAPHIC) { + glm_mat4_identity(camera->projection); + glm_ortho(camera->ortho_left, + camera->ortho_right, + camera->ortho_bottom, + camera->ortho_top, + camera->near, + camera->far, + camera->projection); + } +} diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 0000000..b3b9eed --- /dev/null +++ b/src/camera.h @@ -0,0 +1,132 @@ +#ifndef HONEY_CAMERA_H +#define HONEY_CAMERA_H + +/** @file camera.h + * + * @brief Define the basic honey_camera struct, associated functions, + * and common camera variants. + */ + +#include "common.h" + +enum honey_camera_projection { + HONEY_PERSPECTIVE, + HONEY_ORTHOGRAPHIC, +}; + +typedef struct { + vec3 position; + vec3 angle; /* pitch, yaw, roll */ + vec3 look_direction; + vec3 up; + vec3 right; + mat4 view; + mat4 projection; + enum honey_camera_projection projection_type; + float aspect_ratio; + float near, far; + float fov; + float ortho_left; + float ortho_right; + float ortho_top; + float ortho_bottom; +} honey_camera; + +/** @brief Create a new camera. + * + * The full camera creation function. Most of the time, you will probably use either + * honey_camera_new_perspective() or honey_camera_new_projection() instead. + * + * @param[out] camera Pointer to the destination honey_camera. + * @param[in] position The position of the camera. + * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. + * @param[in] projection_type The type of projection to use. + * @param[in] aspect_ratio The aspect ratio of the camera. Set to zero if using orthographic projection. + * @param[in] near The distance of the near plane. + * @param[in] far The distance of the far plane. + * @param[in] fov The field of view. Set to zero if using orthographic projection. + * @param[in] left The leftmost distance. Set to zero if using perspective projection. + * @param[in] right The rightmost distance. Set to zero if using perspective projection. + * @param[in] top The uppermost distance. Set to zero if using perspective projection. + * @param[in] bottom The lowest distance. Set to zero if using perspective projection. + */ +void honey_camera_new(honey_camera* camera, + vec3 position, + vec3 angle, + enum honey_camera_projection projection_type, + float aspect_ratio, + float near, float far, + float fov, + float left, float right, float top, float bottom); + +/** @brief Create a camera with a perspective projection matrix. + * + * @param[out] camera Pointer to the destination honey_camera. + * @param[in] position The position of the camera. + * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. + * @param[in] aspect_ratio The aspect ratio of the camera. + * @param[in] near The distance of the near plane. + * @param[in] far The distance of the far plane. + * @param[in] fov The field of view. + */ +void honey_camera_new_perspective(honey_camera* camera, + vec3 position, + vec3 angle, + float aspect_ratio, + float near, float far, + float fov); + +/** @brief Create a camera with an orthographic projection matrix. + * + * @param[out] camera Pointer to the destination honey_camera. + * @param[in] position The position of the camera. + * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. + * @param[in] near The distance of the near plane. + * @param[in] far The distance of the far plane. + * @param[in] left The leftmost distance. + * @param[in] right The rightmost distance. + * @param[in] top The uppermost distance. + * @param[in] bottom The lowest distance. + */ +void honey_camera_new_orthographic(honey_camera* camera, + vec3 position, + vec3 angle, + float near, float far, + float left, float right, float top, float bottom); + +/** @brief (Re-)Calculate a camera's look_direction. + * + * @param[in] Pointer to the camera to re-calculate. + */ +void honey_camera_calculate_look_direction(honey_camera* camera); + +/** @brief (Re-)Calculate a camera's up vector. + * + * @param[in] Pointer to the camera to re-calculate. + */ +void honey_camera_calculate_up(honey_camera* camera); + +/** @brief (Re-)Calculate a camera's right vector. + * + * @param[in] Pointer to the camera to re-calculate. + */ +void honey_camera_calculate_right(honey_camera* camera); + +/** @brief (Re-)Calculate a camera's view matrix. + * + * This function need only be called when the camera has been moved in some way. + * + * @param[in] camera Pointer to the camera to re-calculate. + */ +void honey_camera_calculate_view(honey_camera* camera); + +/** @brief (Re-)Calculate a camera's projection matrix. + * + * This function need only be called when the projection has changes in some way. + * Most commonly, this would be changing the FOV. + * + * @param[in] camera Pointer to the camera to re-calculate. + */ +void honey_camera_calculate_projection(honey_camera* camera); + +#endif diff --git a/src/camera/camera.c b/src/camera/camera.c deleted file mode 100644 index 44f480d..0000000 --- a/src/camera/camera.c +++ /dev/null @@ -1,137 +0,0 @@ -#include "camera.h" - -void honey_camera_new(honey_camera* camera, - vec3 position, - vec3 angle, - enum honey_camera_projection projection_type, - float aspect_ratio, - float near, float far, - float fov, - float left, float right, float top, float bottom) { - glm_vec3_copy(position, camera->position); - glm_vec3_copy(angle, camera->angle); - - camera->projection_type = projection_type; - - camera->aspect_ratio = aspect_ratio; - camera->near = near; - camera->far = far; - - if (projection_type == HONEY_PERSPECTIVE) { - camera->fov = fov; - } - else if (projection_type == HONEY_ORTHOGRAPHIC) { - camera->ortho_left = left; - camera->ortho_right = right; - camera->ortho_top = top; - camera->ortho_bottom = bottom; - } - - honey_camera_calculate_view(camera); - honey_camera_calculate_projection(camera); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_new_perspective(honey_camera* camera, - vec3 position, - vec3 angle, - float aspect_ratio, - float near, float far, - float fov) { - honey_camera_new(camera, - position, angle, - HONEY_PERSPECTIVE, - aspect_ratio, near, far, fov, - 0, 0, 0, 0); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_new_orthographic(honey_camera* camera, - vec3 position, - vec3 angle, - float near, float far, - float left, float right, float top, float bottom) { - honey_camera_new(camera, - position, angle, - HONEY_ORTHOGRAPHIC, - 0, - near, far, - 0, - left, right, top, bottom); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_calculate_look_direction(honey_camera* camera) { - float pitch = camera->angle[0]; - float yaw = camera->angle[1]; - - float x = cos(pitch) * cos(yaw); - float y = sin(pitch); - float z = cos(pitch) * sin(yaw); - - camera->look_direction[0] = x; - camera->look_direction[1] = y; - camera->look_direction[2] = z; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_calculate_up(honey_camera* camera) { - float pitch = camera->angle[0]; - float yaw = camera->angle[1]; - float roll = camera->angle[2]; - - camera->up[0] = 0; - camera->up[1] = 1; - camera->up[2] = 0; - - mat3 rot3; - mat4 rot4; - glm_mat4_identity(rot4); - glm_rotate(rot4, roll, camera->look_direction); - glm_mat4_pick3(rot4, rot3); - - glm_mat3_mulv(rot3, camera->up, camera->up); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_calculate_right(honey_camera* camera) { - glm_vec3_cross(camera->up, camera->look_direction, camera->right); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_calculate_view(honey_camera* camera) { - honey_camera_calculate_look_direction(camera); - honey_camera_calculate_up(camera); - honey_camera_calculate_right(camera); - - glm_look(camera->position, camera->look_direction, camera->up, camera->view); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_camera_calculate_projection(honey_camera* camera) { - if (camera->projection_type == HONEY_PERSPECTIVE) { - glm_mat4_identity(camera->projection); - glm_perspective(camera->fov, - camera->aspect_ratio, - camera->near, - camera->far, - camera->projection); - } - else if (camera->projection_type == HONEY_ORTHOGRAPHIC) { - glm_mat4_identity(camera->projection); - glm_ortho(camera->ortho_left, - camera->ortho_right, - camera->ortho_bottom, - camera->ortho_top, - camera->near, - camera->far, - camera->projection); - } -} diff --git a/src/camera/camera.h b/src/camera/camera.h deleted file mode 100644 index b8d3b61..0000000 --- a/src/camera/camera.h +++ /dev/null @@ -1,132 +0,0 @@ -#ifndef HONEY_CAMERA_H -#define HONEY_CAMERA_H - -/** @file camera.h - * - * @brief Define the basic honey_camera struct, associated functions, - * and common camera variants. - */ - -#include "../common.h" - -enum honey_camera_projection { - HONEY_PERSPECTIVE, - HONEY_ORTHOGRAPHIC, -}; - -typedef struct { - vec3 position; - vec3 angle; /* pitch, yaw, roll */ - vec3 look_direction; - vec3 up; - vec3 right; - mat4 view; - mat4 projection; - enum honey_camera_projection projection_type; - float aspect_ratio; - float near, far; - float fov; - float ortho_left; - float ortho_right; - float ortho_top; - float ortho_bottom; -} honey_camera; - -/** @brief Create a new camera. - * - * The full camera creation function. Most of the time, you will probably use either - * honey_camera_new_perspective() or honey_camera_new_projection() instead. - * - * @param[out] camera Pointer to the destination honey_camera. - * @param[in] position The position of the camera. - * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. - * @param[in] projection_type The type of projection to use. - * @param[in] aspect_ratio The aspect ratio of the camera. Set to zero if using orthographic projection. - * @param[in] near The distance of the near plane. - * @param[in] far The distance of the far plane. - * @param[in] fov The field of view. Set to zero if using orthographic projection. - * @param[in] left The leftmost distance. Set to zero if using perspective projection. - * @param[in] right The rightmost distance. Set to zero if using perspective projection. - * @param[in] top The uppermost distance. Set to zero if using perspective projection. - * @param[in] bottom The lowest distance. Set to zero if using perspective projection. - */ -void honey_camera_new(honey_camera* camera, - vec3 position, - vec3 angle, - enum honey_camera_projection projection_type, - float aspect_ratio, - float near, float far, - float fov, - float left, float right, float top, float bottom); - -/** @brief Create a camera with a perspective projection matrix. - * - * @param[out] camera Pointer to the destination honey_camera. - * @param[in] position The position of the camera. - * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. - * @param[in] aspect_ratio The aspect ratio of the camera. - * @param[in] near The distance of the near plane. - * @param[in] far The distance of the far plane. - * @param[in] fov The field of view. - */ -void honey_camera_new_perspective(honey_camera* camera, - vec3 position, - vec3 angle, - float aspect_ratio, - float near, float far, - float fov); - -/** @brief Create a camera with an orthographic projection matrix. - * - * @param[out] camera Pointer to the destination honey_camera. - * @param[in] position The position of the camera. - * @param[in] angle The Euler angles (pitch, yaw, roll) of the camera. - * @param[in] near The distance of the near plane. - * @param[in] far The distance of the far plane. - * @param[in] left The leftmost distance. - * @param[in] right The rightmost distance. - * @param[in] top The uppermost distance. - * @param[in] bottom The lowest distance. - */ -void honey_camera_new_orthographic(honey_camera* camera, - vec3 position, - vec3 angle, - float near, float far, - float left, float right, float top, float bottom); - -/** @brief (Re-)Calculate a camera's look_direction. - * - * @param[in] Pointer to the camera to re-calculate. - */ -void honey_camera_calculate_look_direction(honey_camera* camera); - -/** @brief (Re-)Calculate a camera's up vector. - * - * @param[in] Pointer to the camera to re-calculate. - */ -void honey_camera_calculate_up(honey_camera* camera); - -/** @brief (Re-)Calculate a camera's right vector. - * - * @param[in] Pointer to the camera to re-calculate. - */ -void honey_camera_calculate_right(honey_camera* camera); - -/** @brief (Re-)Calculate a camera's view matrix. - * - * This function need only be called when the camera has been moved in some way. - * - * @param[in] camera Pointer to the camera to re-calculate. - */ -void honey_camera_calculate_view(honey_camera* camera); - -/** @brief (Re-)Calculate a camera's projection matrix. - * - * This function need only be called when the projection has changes in some way. - * Most commonly, this would be changing the FOV. - * - * @param[in] camera Pointer to the camera to re-calculate. - */ -void honey_camera_calculate_projection(honey_camera* camera); - -#endif diff --git a/src/common.h b/src/common.h index 6ae1c40..f9bc64b 100644 --- a/src/common.h +++ b/src/common.h @@ -107,11 +107,6 @@ typedef enum { HONEY_ANY } honey_lua_type; -typedef struct { - honey_lua_type type; - void* ptr; -} honey_lua_argument_pair; - /** @brief Get arguments from a function, checking to ensure the types match. * * Each argument type should be specified as [# of args], type, ptr, type, ptr..., @@ -137,7 +132,7 @@ typedef struct { * @returns The zero-indexed index of the actual argument option used. Throws an * error if no options matched the provided arguments. */ -int honey_lua_parse_arguments(lua_State* L, int n, ...); +int honey_lua_parse_arguments(lua_State* L, unsigned int n, ...); /** @brief Wrap C objects for lua. */ typedef struct honey_lua_element { @@ -166,17 +161,25 @@ typedef struct honey_lua_element { void honey_lua_push_element(lua_State* L, honey_lua_element element); -/** @brief Create a lua table populated with various elements. +/** @brief Create and populate a lua table. + * + * This function expects a list of the form `type, name, element, type, name, element...`. + * In the case of a sub-table, the format is + * `type, name, n_elements, subtype, subname, subelement...`. + * This can be nested as deeply as one wishes. + * + * As userdata cannot be pushed from C, attempting to push HONEY_USERDATA will result in + * an error. * * @param[in] L The lua state to push the table to. - * @param[in] elements Array of elements to populate the table. * @param[in] n_elements The number of elements in the array. + * @param[in] ... Variadic list of table elements. * * @returns Nothing. */ void honey_lua_create_table(lua_State* L, - honey_lua_element* elements, - unsigned int n_elements); + unsigned int n_elements, + ...); /** @brief Get the traceback for use after an error. */ diff --git a/src/glm_bindings.c b/src/glm_bindings.c index cfd85e5..9fdd7f2 100644 --- a/src/glm_bindings.c +++ b/src/glm_bindings.c @@ -7,7 +7,7 @@ int honey_glm_mat4_mt_ref = LUA_NOREF; void honey_setup_glm(lua_State* L) { - honey_lua_create_table(L, NULL, 0); + lua_pushnil(L); } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/honey.h b/src/honey.h index 0ca3d52..5401a47 100644 --- a/src/honey.h +++ b/src/honey.h @@ -8,15 +8,15 @@ #include "common.h" -#include "camera/camera.h" +#include "camera.h" #include "glm_bindings.h" -#include "input/input.h" -#include "light/light.h" -#include "mesh/mesh.h" -#include "primitives/primitives.h" -#include "shader/shader.h" -#include "texture/texture.h" -#include "window/window.h" +#include "input.h" +#include "light.h" +#include "mesh.h" +#include "primitives.h" +#include "shader.h" +#include "texture.h" +#include "window.h" /** @struct Helper struct to wrap command-line options. */ typedef struct { diff --git a/src/honey_lua.c b/src/honey_lua.c index 7730809..2469d37 100644 --- a/src/honey_lua.c +++ b/src/honey_lua.c @@ -2,14 +2,31 @@ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * - * Lua binding helper function definitions + * Helper structs + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +struct argument_pair { + honey_lua_type type; + void* ptr; +}; + +struct argument_list { + unsigned int length; + struct argument_pair* args; +}; + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Lua binding helper function declarations * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* string must be able to hold at least 16 characters. */ -static void type_to_string(char* string, - honey_lua_type type); +static int type_to_string(char* string, + honey_lua_type type); static bool check_argument(lua_State* L, honey_lua_type type, @@ -111,90 +128,198 @@ static void honey_lua_arg_error(lua_State* L, /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -int honey_lua_parse_arguments(lua_State* L, int n, ...) +static bool check_arg_list(lua_State* L, + struct argument_list arg_list) { - va_list args; - va_start(args, n); + struct argument_pair* args = arg_list.args; + for (int i=0; iwindow, honey_glfw_keyboard_callback); - glfwSetCursorPosCallback(info->window, honey_glfw_mouse_movement_callback); - - honey_lua_element key_elements[] = { - { "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_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_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[] = { - { "key", HONEY_TABLE, { .table={ HONEY_N_KEYS+4, key_elements } } }, - { "mouse", HONEY_TABLE, { .table={ 4, mouse_elements } } }, - }; - - honey_lua_create_table(L, input_elements, 2); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_key_down(lua_State* L) -{ - if (!lua_isnumber(L, 1)) - return 0; - - int key = lua_tointeger(L, 1); - if (key < 0 || key >= HONEY_N_KEYS) - lua_pushboolean(L, false); - else - lua_pushboolean(L, honey_key_states[key]); - - return 1; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -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_INTEGER, HONEY_FUNCTION, HONEY_ANY)) { - lua_error(L); - } - } - else if (n_args == 2) { - if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_FUNCTION)) { - lua_error(L); - } - } - else { - char* error_message; - honey_format_string(&error_message, "expected 2 or 3 arguments; received %d instead", n_args); - lua_pushstring(L, error_message); - free(error_message); - lua_error(L); - } - - int key = lua_tointeger(L, 1); - lua_pushvalue(L, 2); - int callback = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 3); - int data = luaL_ref(L, LUA_REGISTRYINDEX); - - /* avoid potential memory leak */ - lua_pushcfunction(L, honey_key_unbind); - lua_pushinteger(L, key); - honey_lua_pcall(L, 1, 0); - - if (key >= 0 && key < HONEY_N_KEYS) { - honey_key_callbacks[key] = callback; - honey_key_callbacks_data[key] = data; - } - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_key_unbind(lua_State* L) -{ - /* lua params: (int) key */ - if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) - lua_error(L); - - int key = lua_tointeger(L, 1); - - if (key >=0 && key < HONEY_N_KEYS) { - honey_key_callbacks[key] = LUA_NOREF; - honey_key_callbacks_data[key] = LUA_NOREF; - } - else { - char* error_message; - honey_format_string(&error_message, "'%d' is not a valid keycode", key); - lua_pushstring(L, error_message); - free(error_message); - lua_error(L); - } - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_key_unbind_all(lua_State* L) -{ - for (int i=0; iwindow, GLFW_CURSOR, cursor_mode); - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_mouse_movement_bind(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) - lua_error(L); - - honey_mouse_movement_unbind(L); /* avoid memory leaks! */ - - lua_pushvalue(L, 1); - honey_mouse_movement_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - honey_mouse_movement_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_mouse_movement_unbind(lua_State* L) -{ - int callback = honey_mouse_movement_callback_ref; - int data = honey_mouse_movement_callback_data_ref; - - if (callback != LUA_NOREF || callback != LUA_REFNIL) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, callback); - } - - if (data != LUA_NOREF || data != LUA_REFNIL) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, data); - } - - honey_mouse_movement_callback_ref = LUA_NOREF; - honey_mouse_movement_callback_data_ref = LUA_NOREF; - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void honey_glfw_mouse_movement_callback(honey_window window, - double x_pos, double y_pos) -{ - int callback = honey_mouse_movement_callback_ref; - int data = honey_mouse_movement_callback_data_ref; - lua_State* L = glfwGetWindowUserPointer(window); - - if (callback == LUA_NOREF || callback == LUA_REFNIL) - return; - - lua_rawgeti(L, LUA_REGISTRYINDEX, callback); - lua_pushnumber(L, x_pos); - lua_pushnumber(L, y_pos); - - if (data == LUA_NOREF || data == LUA_REFNIL) - lua_pushnil(L); - else - lua_rawgeti(L, LUA_REGISTRYINDEX, data); - - honey_lua_pcall(L, 3, 0); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void execute_lua_key_callback(lua_State* L, int callback, int action, int data) -{ - if (callback != LUA_NOREF) { - lua_rawgeti(L, LUA_REGISTRYINDEX, callback); - lua_pushinteger(L, action); - if (data != LUA_REFNIL) - lua_rawgeti(L, LUA_REGISTRYINDEX, data); - else - lua_pushnil(L); - honey_lua_pcall(L, 2, 0); - } -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void honey_glfw_keyboard_callback(honey_window window, - int key, int scancode, - int action, int mods) -{ - int callback, data; - lua_State* L = glfwGetWindowUserPointer(window); - switch (key) { - case GLFW_KEY_UNKNOWN: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_UNKNOWN] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_UNKNOWN] = 0; } - callback = honey_key_callbacks[HONEY_KEY_UNKNOWN]; - data = honey_key_callbacks_data[HONEY_KEY_UNKNOWN]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_SPACE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_SPACE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_SPACE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_SPACE]; - data = honey_key_callbacks_data[HONEY_KEY_SPACE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_APOSTROPHE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_APOSTROPHE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_APOSTROPHE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_APOSTROPHE]; - data = honey_key_callbacks_data[HONEY_KEY_APOSTROPHE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_COMMA: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_COMMA] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_COMMA] = 0; } - callback = honey_key_callbacks[HONEY_KEY_COMMA]; - data = honey_key_callbacks_data[HONEY_KEY_COMMA]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_MINUS: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_MINUS] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_MINUS] = 0; } - callback = honey_key_callbacks[HONEY_KEY_MINUS]; - data = honey_key_callbacks_data[HONEY_KEY_MINUS]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_PERIOD: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_PERIOD] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_PERIOD] = 0; } - callback = honey_key_callbacks[HONEY_KEY_PERIOD]; - data = honey_key_callbacks_data[HONEY_KEY_PERIOD]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_SLASH: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_SLASH] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_SLASH] = 0; } - callback = honey_key_callbacks[HONEY_KEY_SLASH]; - data = honey_key_callbacks_data[HONEY_KEY_SLASH]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_0: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_0] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_0] = 0; } - callback = honey_key_callbacks[HONEY_KEY_0]; - data = honey_key_callbacks_data[HONEY_KEY_0]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_1: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_1] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_1] = 0; } - callback = honey_key_callbacks[HONEY_KEY_1]; - data = honey_key_callbacks_data[HONEY_KEY_1]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_2: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_2] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_2] = 0; } - callback = honey_key_callbacks[HONEY_KEY_2]; - data = honey_key_callbacks_data[HONEY_KEY_2]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_3: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_3] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_3] = 0; } - callback = honey_key_callbacks[HONEY_KEY_3]; - data = honey_key_callbacks_data[HONEY_KEY_3]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_4: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_4] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_4] = 0; } - callback = honey_key_callbacks[HONEY_KEY_4]; - data = honey_key_callbacks_data[HONEY_KEY_4]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_5: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_5] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_5] = 0; } - callback = honey_key_callbacks[HONEY_KEY_5]; - data = honey_key_callbacks_data[HONEY_KEY_5]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_6: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_6] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_6] = 0; } - callback = honey_key_callbacks[HONEY_KEY_6]; - data = honey_key_callbacks_data[HONEY_KEY_6]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_7: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_7] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_7] = 0; } - callback = honey_key_callbacks[HONEY_KEY_7]; - data = honey_key_callbacks_data[HONEY_KEY_7]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_8: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_8] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_8] = 0; } - callback = honey_key_callbacks[HONEY_KEY_8]; - data = honey_key_callbacks_data[HONEY_KEY_8]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_9: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_9] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_9] = 0; } - callback = honey_key_callbacks[HONEY_KEY_9]; - data = honey_key_callbacks_data[HONEY_KEY_9]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_SEMICOLON: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_SEMICOLON] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_SEMICOLON] = 0; } - callback = honey_key_callbacks[HONEY_KEY_SEMICOLON]; - data = honey_key_callbacks_data[HONEY_KEY_SEMICOLON]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_EQUAL: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_EQUAL] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_EQUAL] = 0; } - callback = honey_key_callbacks[HONEY_KEY_EQUAL]; - data = honey_key_callbacks_data[HONEY_KEY_EQUAL]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_A: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_A] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_A] = 0; } - callback = honey_key_callbacks[HONEY_KEY_A]; - data = honey_key_callbacks_data[HONEY_KEY_A]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_B: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_B] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_B] = 0; } - callback = honey_key_callbacks[HONEY_KEY_B]; - data = honey_key_callbacks_data[HONEY_KEY_B]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_C: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_C] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_C] = 0; } - callback = honey_key_callbacks[HONEY_KEY_C]; - data = honey_key_callbacks_data[HONEY_KEY_C]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_D: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_D] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_D] = 0; } - callback = honey_key_callbacks[HONEY_KEY_D]; - data = honey_key_callbacks_data[HONEY_KEY_D]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_E: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_E] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_E] = 0; } - callback = honey_key_callbacks[HONEY_KEY_E]; - data = honey_key_callbacks_data[HONEY_KEY_E]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F]; - data = honey_key_callbacks_data[HONEY_KEY_F]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_G: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_G] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_G] = 0; } - callback = honey_key_callbacks[HONEY_KEY_G]; - data = honey_key_callbacks_data[HONEY_KEY_G]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_H: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_H] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_H] = 0; } - callback = honey_key_callbacks[HONEY_KEY_H]; - data = honey_key_callbacks_data[HONEY_KEY_H]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_I: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_I] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_I] = 0; } - callback = honey_key_callbacks[HONEY_KEY_I]; - data = honey_key_callbacks_data[HONEY_KEY_I]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_J: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_J] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_J] = 0; } - callback = honey_key_callbacks[HONEY_KEY_J]; - data = honey_key_callbacks_data[HONEY_KEY_J]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_K: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_K] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_K] = 0; } - callback = honey_key_callbacks[HONEY_KEY_K]; - data = honey_key_callbacks_data[HONEY_KEY_K]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_L: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_L] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_L] = 0; } - callback = honey_key_callbacks[HONEY_KEY_L]; - data = honey_key_callbacks_data[HONEY_KEY_L]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_M: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_M] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_M] = 0; } - callback = honey_key_callbacks[HONEY_KEY_M]; - data = honey_key_callbacks_data[HONEY_KEY_M]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_N: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_N] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_N] = 0; } - callback = honey_key_callbacks[HONEY_KEY_N]; - data = honey_key_callbacks_data[HONEY_KEY_N]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_O: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_O] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_O] = 0; } - callback = honey_key_callbacks[HONEY_KEY_O]; - data = honey_key_callbacks_data[HONEY_KEY_O]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_P: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_P] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_P] = 0; } - callback = honey_key_callbacks[HONEY_KEY_P]; - data = honey_key_callbacks_data[HONEY_KEY_P]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_Q: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_Q] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_Q] = 0; } - callback = honey_key_callbacks[HONEY_KEY_Q]; - data = honey_key_callbacks_data[HONEY_KEY_Q]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_R: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_R] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_R] = 0; } - callback = honey_key_callbacks[HONEY_KEY_R]; - data = honey_key_callbacks_data[HONEY_KEY_R]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_S: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_S] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_S] = 0; } - callback = honey_key_callbacks[HONEY_KEY_S]; - data = honey_key_callbacks_data[HONEY_KEY_S]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_T: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_T] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_T] = 0; } - callback = honey_key_callbacks[HONEY_KEY_T]; - data = honey_key_callbacks_data[HONEY_KEY_T]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_U: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_U] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_U] = 0; } - callback = honey_key_callbacks[HONEY_KEY_U]; - data = honey_key_callbacks_data[HONEY_KEY_U]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_V: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_V] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_V] = 0; } - callback = honey_key_callbacks[HONEY_KEY_V]; - data = honey_key_callbacks_data[HONEY_KEY_V]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_W: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_W] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_W] = 0; } - callback = honey_key_callbacks[HONEY_KEY_W]; - data = honey_key_callbacks_data[HONEY_KEY_W]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_X: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_X] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_X] = 0; } - callback = honey_key_callbacks[HONEY_KEY_X]; - data = honey_key_callbacks_data[HONEY_KEY_X]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_Y: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_Y] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_Y] = 0; } - callback = honey_key_callbacks[HONEY_KEY_Y]; - data = honey_key_callbacks_data[HONEY_KEY_Y]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_Z: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_Z] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_Z] = 0; } - callback = honey_key_callbacks[HONEY_KEY_Z]; - data = honey_key_callbacks_data[HONEY_KEY_Z]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT_BRACKET: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT_BRACKET] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT_BRACKET] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT_BRACKET]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT_BRACKET]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_BACKSLASH: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_BACKSLASH] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_BACKSLASH] = 0; } - callback = honey_key_callbacks[HONEY_KEY_BACKSLASH]; - data = honey_key_callbacks_data[HONEY_KEY_BACKSLASH]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT_BRACKET: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT_BRACKET] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT_BRACKET] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT_BRACKET]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT_BRACKET]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_GRAVE_ACCENT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_GRAVE_ACCENT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_GRAVE_ACCENT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_GRAVE_ACCENT]; - data = honey_key_callbacks_data[HONEY_KEY_GRAVE_ACCENT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_WORLD_1: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_WORLD_1] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_WORLD_1] = 0; } - callback = honey_key_callbacks[HONEY_KEY_WORLD_1]; - data = honey_key_callbacks_data[HONEY_KEY_WORLD_1]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_WORLD_2: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_WORLD_2] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_WORLD_2] = 0; } - callback = honey_key_callbacks[HONEY_KEY_WORLD_2]; - data = honey_key_callbacks_data[HONEY_KEY_WORLD_2]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_ESCAPE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_ESCAPE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_ESCAPE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_ESCAPE]; - data = honey_key_callbacks_data[HONEY_KEY_ESCAPE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_ENTER: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_ENTER] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_ENTER] = 0; } - callback = honey_key_callbacks[HONEY_KEY_ENTER]; - data = honey_key_callbacks_data[HONEY_KEY_ENTER]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_TAB: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_TAB] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_TAB] = 0; } - callback = honey_key_callbacks[HONEY_KEY_TAB]; - data = honey_key_callbacks_data[HONEY_KEY_TAB]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_BACKSPACE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_BACKSPACE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_BACKSPACE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_BACKSPACE]; - data = honey_key_callbacks_data[HONEY_KEY_BACKSPACE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_INSERT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_INSERT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_INSERT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_INSERT]; - data = honey_key_callbacks_data[HONEY_KEY_INSERT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_DELETE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_DELETE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_DELETE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_DELETE]; - data = honey_key_callbacks_data[HONEY_KEY_DELETE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_DOWN: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_DOWN] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_DOWN] = 0; } - callback = honey_key_callbacks[HONEY_KEY_DOWN]; - data = honey_key_callbacks_data[HONEY_KEY_DOWN]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_UP: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_UP] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_UP] = 0; } - callback = honey_key_callbacks[HONEY_KEY_UP]; - data = honey_key_callbacks_data[HONEY_KEY_UP]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_PAGE_UP: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_PAGE_UP] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_PAGE_UP] = 0; } - callback = honey_key_callbacks[HONEY_KEY_PAGE_UP]; - data = honey_key_callbacks_data[HONEY_KEY_PAGE_UP]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_PAGE_DOWN: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_PAGE_DOWN] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_PAGE_DOWN] = 0; } - callback = honey_key_callbacks[HONEY_KEY_PAGE_DOWN]; - data = honey_key_callbacks_data[HONEY_KEY_PAGE_DOWN]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_HOME: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_HOME] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_HOME] = 0; } - callback = honey_key_callbacks[HONEY_KEY_HOME]; - data = honey_key_callbacks_data[HONEY_KEY_HOME]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_END: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_END] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_END] = 0; } - callback = honey_key_callbacks[HONEY_KEY_END]; - data = honey_key_callbacks_data[HONEY_KEY_END]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_CAPS_LOCK: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_CAPS_LOCK] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_CAPS_LOCK] = 0; } - callback = honey_key_callbacks[HONEY_KEY_CAPS_LOCK]; - data = honey_key_callbacks_data[HONEY_KEY_CAPS_LOCK]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_SCROLL_LOCK: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_SCROLL_LOCK] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_SCROLL_LOCK] = 0; } - callback = honey_key_callbacks[HONEY_KEY_SCROLL_LOCK]; - data = honey_key_callbacks_data[HONEY_KEY_SCROLL_LOCK]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_NUM_LOCK: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_NUM_LOCK] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_NUM_LOCK] = 0; } - callback = honey_key_callbacks[HONEY_KEY_NUM_LOCK]; - data = honey_key_callbacks_data[HONEY_KEY_NUM_LOCK]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_PRINT_SCREEN: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_PRINT_SCREEN] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_PRINT_SCREEN] = 0; } - callback = honey_key_callbacks[HONEY_KEY_PRINT_SCREEN]; - data = honey_key_callbacks_data[HONEY_KEY_PRINT_SCREEN]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_PAUSE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_PAUSE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_PAUSE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_PAUSE]; - data = honey_key_callbacks_data[HONEY_KEY_PAUSE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F1: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F1] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F1] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F1]; - data = honey_key_callbacks_data[HONEY_KEY_F1]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F2: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F2] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F2] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F2]; - data = honey_key_callbacks_data[HONEY_KEY_F2]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F3: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F3] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F3] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F3]; - data = honey_key_callbacks_data[HONEY_KEY_F3]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F4: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F4] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F4] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F4]; - data = honey_key_callbacks_data[HONEY_KEY_F4]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F5: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F5] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F5] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F5]; - data = honey_key_callbacks_data[HONEY_KEY_F5]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F6: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F6] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F6] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F6]; - data = honey_key_callbacks_data[HONEY_KEY_F6]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F7: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F7] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F7] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F7]; - data = honey_key_callbacks_data[HONEY_KEY_F7]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F8: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F8] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F8] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F8]; - data = honey_key_callbacks_data[HONEY_KEY_F8]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F9: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F9] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F9] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F9]; - data = honey_key_callbacks_data[HONEY_KEY_F9]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F10: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F10] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F10] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F10]; - data = honey_key_callbacks_data[HONEY_KEY_F10]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F11: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F11] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F11] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F11]; - data = honey_key_callbacks_data[HONEY_KEY_F11]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F12: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F12] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F12] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F12]; - data = honey_key_callbacks_data[HONEY_KEY_F12]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F13: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F13] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F13] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F13]; - data = honey_key_callbacks_data[HONEY_KEY_F13]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F14: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F14] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F14] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F14]; - data = honey_key_callbacks_data[HONEY_KEY_F14]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F15: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F15] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F15] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F15]; - data = honey_key_callbacks_data[HONEY_KEY_F15]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F16: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F16] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F16] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F16]; - data = honey_key_callbacks_data[HONEY_KEY_F16]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F17: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F17] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F17] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F17]; - data = honey_key_callbacks_data[HONEY_KEY_F17]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F18: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F18] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F18] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F18]; - data = honey_key_callbacks_data[HONEY_KEY_F18]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F19: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F19] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F19] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F19]; - data = honey_key_callbacks_data[HONEY_KEY_F19]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F20: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F20] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F20] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F20]; - data = honey_key_callbacks_data[HONEY_KEY_F20]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F21: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F21] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F21] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F21]; - data = honey_key_callbacks_data[HONEY_KEY_F21]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F22: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F22] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F22] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F22]; - data = honey_key_callbacks_data[HONEY_KEY_F22]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F23: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F23] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F23] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F23]; - data = honey_key_callbacks_data[HONEY_KEY_F23]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F24: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F24] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F24] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F24]; - data = honey_key_callbacks_data[HONEY_KEY_F24]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_F25: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_F25] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_F25] = 0; } - callback = honey_key_callbacks[HONEY_KEY_F25]; - data = honey_key_callbacks_data[HONEY_KEY_F25]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_0: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_0] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_0] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_0]; - data = honey_key_callbacks_data[HONEY_KEY_KP_0]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_1: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_1] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_1] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_1]; - data = honey_key_callbacks_data[HONEY_KEY_KP_1]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_2: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_2] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_2] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_2]; - data = honey_key_callbacks_data[HONEY_KEY_KP_2]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_3: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_3] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_3] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_3]; - data = honey_key_callbacks_data[HONEY_KEY_KP_3]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_4: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_4] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_4] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_4]; - data = honey_key_callbacks_data[HONEY_KEY_KP_4]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_5: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_5] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_5] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_5]; - data = honey_key_callbacks_data[HONEY_KEY_KP_5]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_6: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_6] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_6] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_6]; - data = honey_key_callbacks_data[HONEY_KEY_KP_6]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_7: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_7] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_7] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_7]; - data = honey_key_callbacks_data[HONEY_KEY_KP_7]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_8: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_8] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_8] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_8]; - data = honey_key_callbacks_data[HONEY_KEY_KP_8]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_9: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_9] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_9] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_9]; - data = honey_key_callbacks_data[HONEY_KEY_KP_9]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_DECIMAL: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_DECIMAL] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_DECIMAL] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_DECIMAL]; - data = honey_key_callbacks_data[HONEY_KEY_KP_DECIMAL]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_DIVIDE: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_DIVIDE] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_DIVIDE] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_DIVIDE]; - data = honey_key_callbacks_data[HONEY_KEY_KP_DIVIDE]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_MULTIPLY: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_MULTIPLY] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_MULTIPLY] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_MULTIPLY]; - data = honey_key_callbacks_data[HONEY_KEY_KP_MULTIPLY]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_SUBTRACT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_SUBTRACT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_SUBTRACT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_SUBTRACT]; - data = honey_key_callbacks_data[HONEY_KEY_KP_SUBTRACT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_ADD: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_ADD] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_ADD] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_ADD]; - data = honey_key_callbacks_data[HONEY_KEY_KP_ADD]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_ENTER: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_ENTER] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_ENTER] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_ENTER]; - data = honey_key_callbacks_data[HONEY_KEY_KP_ENTER]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_KP_EQUAL: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_KP_EQUAL] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_KP_EQUAL] = 0; } - callback = honey_key_callbacks[HONEY_KEY_KP_EQUAL]; - data = honey_key_callbacks_data[HONEY_KEY_KP_EQUAL]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT_SHIFT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT_SHIFT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT_SHIFT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT_SHIFT]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT_SHIFT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT_CONTROL: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT_CONTROL] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT_CONTROL] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT_CONTROL]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT_CONTROL]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT_ALT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT_ALT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT_ALT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT_ALT]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT_ALT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_LEFT_SUPER: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_LEFT_SUPER] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_LEFT_SUPER] = 0; } - callback = honey_key_callbacks[HONEY_KEY_LEFT_SUPER]; - data = honey_key_callbacks_data[HONEY_KEY_LEFT_SUPER]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT_SHIFT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT_SHIFT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT_SHIFT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT_SHIFT]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT_SHIFT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT_CONTROL: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT_CONTROL] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT_CONTROL] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT_CONTROL]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT_CONTROL]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT_ALT: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT_ALT] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT_ALT] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT_ALT]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT_ALT]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_RIGHT_SUPER: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_RIGHT_SUPER] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_RIGHT_SUPER] = 0; } - callback = honey_key_callbacks[HONEY_KEY_RIGHT_SUPER]; - data = honey_key_callbacks_data[HONEY_KEY_RIGHT_SUPER]; - execute_lua_key_callback(L, callback, action, data); - break; - - case GLFW_KEY_MENU: - if (action == HONEY_KEY_PRESS) { honey_key_states[HONEY_KEY_MENU] = 1; } - if (action == HONEY_KEY_RELEASE) { honey_key_states[HONEY_KEY_MENU] = 0; } - callback = honey_key_callbacks[HONEY_KEY_MENU]; - data = honey_key_callbacks_data[HONEY_KEY_MENU]; - execute_lua_key_callback(L, callback, action, data); - break; - - default: - /* do nothing */ - break; - } -} diff --git a/src/input/input.h b/src/input/input.h deleted file mode 100644 index 7fd3293..0000000 --- a/src/input/input.h +++ /dev/null @@ -1,229 +0,0 @@ -#ifndef HONEY_INPUT_H -#define HONEY_INPUT_H - -/** @file input.h - * - * @brief Wrap GLFW input functions for honey. - */ - -#include "../common.h" - -#define HONEY_KEY_UNKNOWN 0 -#define HONEY_KEY_SPACE 1 -#define HONEY_KEY_APOSTROPHE 2 -#define HONEY_KEY_COMMA 3 -#define HONEY_KEY_MINUS 4 -#define HONEY_KEY_PERIOD 5 -#define HONEY_KEY_SLASH 6 -#define HONEY_KEY_0 7 -#define HONEY_KEY_1 8 -#define HONEY_KEY_2 9 -#define HONEY_KEY_3 10 -#define HONEY_KEY_4 11 -#define HONEY_KEY_5 12 -#define HONEY_KEY_6 13 -#define HONEY_KEY_7 14 -#define HONEY_KEY_8 15 -#define HONEY_KEY_9 16 -#define HONEY_KEY_SEMICOLON 17 -#define HONEY_KEY_EQUAL 18 -#define HONEY_KEY_A 19 -#define HONEY_KEY_B 20 -#define HONEY_KEY_C 21 -#define HONEY_KEY_D 22 -#define HONEY_KEY_E 23 -#define HONEY_KEY_F 24 -#define HONEY_KEY_G 25 -#define HONEY_KEY_H 26 -#define HONEY_KEY_I 27 -#define HONEY_KEY_J 28 -#define HONEY_KEY_K 29 -#define HONEY_KEY_L 30 -#define HONEY_KEY_M 31 -#define HONEY_KEY_N 32 -#define HONEY_KEY_O 33 -#define HONEY_KEY_P 34 -#define HONEY_KEY_Q 35 -#define HONEY_KEY_R 36 -#define HONEY_KEY_S 37 -#define HONEY_KEY_T 38 -#define HONEY_KEY_U 39 -#define HONEY_KEY_V 40 -#define HONEY_KEY_W 41 -#define HONEY_KEY_X 42 -#define HONEY_KEY_Y 43 -#define HONEY_KEY_Z 44 -#define HONEY_KEY_LEFT_BRACKET 45 -#define HONEY_KEY_BACKSLASH 46 -#define HONEY_KEY_RIGHT_BRACKET 47 -#define HONEY_KEY_GRAVE_ACCENT 48 -#define HONEY_KEY_WORLD_1 49 -#define HONEY_KEY_WORLD_2 50 -#define HONEY_KEY_ESCAPE 51 -#define HONEY_KEY_ENTER 52 -#define HONEY_KEY_TAB 53 -#define HONEY_KEY_BACKSPACE 54 -#define HONEY_KEY_INSERT 55 -#define HONEY_KEY_DELETE 56 -#define HONEY_KEY_RIGHT 57 -#define HONEY_KEY_LEFT 58 -#define HONEY_KEY_DOWN 59 -#define HONEY_KEY_UP 60 -#define HONEY_KEY_PAGE_UP 61 -#define HONEY_KEY_PAGE_DOWN 62 -#define HONEY_KEY_HOME 63 -#define HONEY_KEY_END 64 -#define HONEY_KEY_CAPS_LOCK 65 -#define HONEY_KEY_SCROLL_LOCK 66 -#define HONEY_KEY_NUM_LOCK 67 -#define HONEY_KEY_PRINT_SCREEN 68 -#define HONEY_KEY_PAUSE 69 -#define HONEY_KEY_F1 70 -#define HONEY_KEY_F2 71 -#define HONEY_KEY_F3 72 -#define HONEY_KEY_F4 73 -#define HONEY_KEY_F5 74 -#define HONEY_KEY_F6 75 -#define HONEY_KEY_F7 76 -#define HONEY_KEY_F8 77 -#define HONEY_KEY_F9 78 -#define HONEY_KEY_F10 79 -#define HONEY_KEY_F11 80 -#define HONEY_KEY_F12 81 -#define HONEY_KEY_F13 82 -#define HONEY_KEY_F14 83 -#define HONEY_KEY_F15 84 -#define HONEY_KEY_F16 85 -#define HONEY_KEY_F17 86 -#define HONEY_KEY_F18 87 -#define HONEY_KEY_F19 88 -#define HONEY_KEY_F20 89 -#define HONEY_KEY_F21 90 -#define HONEY_KEY_F22 91 -#define HONEY_KEY_F23 92 -#define HONEY_KEY_F24 93 -#define HONEY_KEY_F25 94 -#define HONEY_KEY_KP_0 95 -#define HONEY_KEY_KP_1 96 -#define HONEY_KEY_KP_2 97 -#define HONEY_KEY_KP_3 98 -#define HONEY_KEY_KP_4 99 -#define HONEY_KEY_KP_5 100 -#define HONEY_KEY_KP_6 101 -#define HONEY_KEY_KP_7 102 -#define HONEY_KEY_KP_8 103 -#define HONEY_KEY_KP_9 104 -#define HONEY_KEY_KP_DECIMAL 105 -#define HONEY_KEY_KP_DIVIDE 106 -#define HONEY_KEY_KP_MULTIPLY 107 -#define HONEY_KEY_KP_SUBTRACT 108 -#define HONEY_KEY_KP_ADD 109 -#define HONEY_KEY_KP_ENTER 110 -#define HONEY_KEY_KP_EQUAL 111 -#define HONEY_KEY_LEFT_SHIFT 112 -#define HONEY_KEY_LEFT_CONTROL 113 -#define HONEY_KEY_LEFT_ALT 114 -#define HONEY_KEY_LEFT_SUPER 115 -#define HONEY_KEY_RIGHT_SHIFT 116 -#define HONEY_KEY_RIGHT_CONTROL 117 -#define HONEY_KEY_RIGHT_ALT 118 -#define HONEY_KEY_RIGHT_SUPER 119 -#define HONEY_KEY_MENU 120 - -#define HONEY_N_KEYS 121 - -#define HONEY_KEY_PRESS GLFW_PRESS -#define HONEY_KEY_RELEASE GLFW_RELEASE - -#define HONEY_MOUSE_MODE_NORMAL GLFW_CURSOR_NORMAL -#define HONEY_MOUSE_MODE_HIDDEN GLFW_CURSOR_HIDDEN -#define HONEY_MOUSE_MODE_DISABLED GLFW_CURSOR_DISABLED - -unsigned int honey_key_states[HONEY_N_KEYS]; -static int honey_key_callbacks[HONEY_N_KEYS]; -static int honey_key_callbacks_data[HONEY_N_KEYS]; - -extern int honey_mouse_movement_callback_ref; -extern int honey_mouse_movement_callback_data_ref; - -/** @brief Initializes Honey's internal keyboard states. - * - * This function is called by honey_setup, so you shouldn't need - * to manually call it in most cases. - */ -void honey_setup_keyboard(); - -/* @brief Place a lua table containing the enum values for GLFW keys on the stack. - * - * @param L The lua state to place the table on the top of the stack. - * - * @returns Nothing. - */ -void honey_setup_input(lua_State* L); - -/** @brief Check if a key is down. - * - * This function accepts a single integer as input and returns TRUE if the integer - * refers to a valid key and is pressed down, and false otherwise. - * - * @param[in] L The lua state. - * - * @return 1 on success and 0 on failure. - */ -int honey_key_down(lua_State* L); - -/** @brief Bind a callback to a key. - * - * The callback must have signature (void*, int), where the first argument is supplied by the - * void* supplied with this function, and the int is one of HONEY_KEY_PRESS or HONEY_KEY_RELEASE, - * depending on the action that triggered the callback. - * - * @param[in] key The key to bind the callback to. - * @param[in] callback The callback function to bind. - * @param[in] data Data to pass to the callback function upon triggering. - */ -int honey_key_bind(lua_State* L); - -/** @brief Unbind a key callback. - * - * THIS FUNCTION DOES NOT FREE MEMORY! If you allocated any, be sure to free it yourself! - * - * @param[in] key The key to unbind a callback from. - */ -int honey_key_unbind(lua_State* L); - -/** @brief Unbind all key callbacks. - * - * THIS FUNCTION DOES NOT FREE MEMORY! If you allocated any, be sure to free it yourself! - */ -int honey_key_unbind_all(lua_State* L); - -/** @brief Set the cursor mode. - * - * @param[in] mode The mouse mode (from honey.input.mouse.mode) to use. - * - * @returns Nothing. - */ -int honey_mouse_set_mode(lua_State* L); - -/** Bind a callback to mouse movement. - * - * The callback should be of the form cb(xpos, ypos, data), - * where xpos and ypos are the coordinates of the mouse relative to the - * upper left corner of the window, and data is the data parameter passed - * to this function. - * - * @param[in] callback A callback as described above. - * @param[in] data Auxilliary data to pass to the callback. - * - * @returns Nothing. - */ -int honey_mouse_movement_bind(lua_State* L); - -/** Unbind any callback that may be attached to mouse movement. - * - * @returns Nothing. - */ -int honey_mouse_movement_unbind(lua_State* L); - -#endif diff --git a/src/light.c b/src/light.c new file mode 100644 index 0000000..839d747 --- /dev/null +++ b/src/light.c @@ -0,0 +1,20 @@ +#include "light.h" + +void honey_point_light_new(honey_point_light* light, + float x, float y, float z, + float r, float g, float b, + float constant, + float linear, + float quadratic) { + light->position[0] = x; + light->position[1] = y; + light->position[2] = z; + + light->color[0] = r; + light->color[1] = g; + light->color[2] = b; + + light->constant = constant; + light->linear = linear; + light->quadratic = quadratic; +} diff --git a/src/light.h b/src/light.h new file mode 100644 index 0000000..93fcfd1 --- /dev/null +++ b/src/light.h @@ -0,0 +1,33 @@ +#ifndef HONEY_LIGHT_H +#define HONEY_LIGHT_H + +#include "common.h" + +#define HONEY_MAX_LIGHT_NAME_LENGTH 64 + +typedef struct { + vec3 position; + vec3 color; + + float constant; + float linear; + float quadratic; +} honey_point_light; + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +typedef struct { + vec3 direction; + vec3 color; +} honey_directional_light; + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_point_light_new(honey_point_light* light, + float x, float y, float z, + float r, float g, float b, + float constant, + float linear, + float quadratic); + +#endif diff --git a/src/light/light.c b/src/light/light.c deleted file mode 100644 index 839d747..0000000 --- a/src/light/light.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "light.h" - -void honey_point_light_new(honey_point_light* light, - float x, float y, float z, - float r, float g, float b, - float constant, - float linear, - float quadratic) { - light->position[0] = x; - light->position[1] = y; - light->position[2] = z; - - light->color[0] = r; - light->color[1] = g; - light->color[2] = b; - - light->constant = constant; - light->linear = linear; - light->quadratic = quadratic; -} diff --git a/src/light/light.h b/src/light/light.h deleted file mode 100644 index dfb6979..0000000 --- a/src/light/light.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef HONEY_LIGHT_H -#define HONEY_LIGHT_H - -#include "../common.h" - -#define HONEY_MAX_LIGHT_NAME_LENGTH 64 - -typedef struct { - vec3 position; - vec3 color; - - float constant; - float linear; - float quadratic; -} honey_point_light; - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -typedef struct { - vec3 direction; - vec3 color; -} honey_directional_light; - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_point_light_new(honey_point_light* light, - float x, float y, float z, - float r, float g, float b, - float constant, - float linear, - float quadratic); - -#endif diff --git a/src/lua/honey_lua.h b/src/lua/honey_lua.h deleted file mode 100644 index 90fc2c7..0000000 --- a/src/lua/honey_lua.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef HONEY_LUA_H -#define HONEY_LUA_H - -#include "../common.h" - -typedef enum { - HONEY_INT, - HONEY_NUM, - HONEY_STRING, - HONEY_FUNC -} honey_lua_type; - -typedef struct { - char* name; - honey_lua_type type; - union { - int integer; - double number; - char* string; - int (*function)(lua_State*); - } data; -} honey_lua_element; - -/** @brief Push an element to the lua stack. - * - * @param[in] L The lua state to push the element to. - * @param[in] element The honey_lua_element to push to the stack. - * - * @returns Nothing. - */ -void honey_lua_push_element(lua_State* L, - honey_lua_element element); - -/** @brief Create a lua table populated with various elements. - * - * @param[in] L The lua state to push the table to. - * @param[in] elements Array of elements to populate the table. - * @param[in] n_elements The number of elements in the array. - * - * @returns Nothing. - */ -void honey_lua_create_table(lua_State* L, - honey_lua_element* elements, - unsigned int n_elements); - -#endif diff --git a/src/mesh.c b/src/mesh.c new file mode 100644 index 0000000..68e39d7 --- /dev/null +++ b/src/mesh.c @@ -0,0 +1,232 @@ +#include "mesh.h" + +static int honey_mesh_lua_draw(lua_State* L) +{ + honey_mesh* mesh; + int shader; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &mesh, + HONEY_INTEGER, &shader); + honey_mesh_draw(*mesh, shader); + return 0; +} + +static int honey_mesh_lua_delete(lua_State* L) +{ + honey_mesh* mesh; + honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh); + honey_mesh_delete(*mesh); + return 0; +} + +void honey_setup_mesh(lua_State* L) +{ + honey_lua_element mesh_elements[] = { + { "load", HONEY_FUNCTION, { .function = honey_mesh_load } }, + { "draw", HONEY_FUNCTION, { .function = honey_mesh_lua_draw } }, + { "delete", HONEY_FUNCTION, { .function = honey_mesh_lua_delete } }, + }; + + honey_lua_create_table(L, mesh_elements, 2); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +static honey_mesh assimp_to_honey_mesh(struct aiMesh* mesh, + struct aiScene* scene) +{ + unsigned int vertex_step = 6; + bool mesh_has_uvs = false; + unsigned int n_vertices = mesh->mNumVertices; + + if (mesh->mTextureCoords[0]) { + mesh_has_uvs = true; + vertex_step = 8; + } + + float* vertices = malloc(sizeof(float) * vertex_step * n_vertices); + for (int i=0; imVertices[i].x; + vertices[j+1] = mesh->mVertices[i].y; + vertices[j+2] = mesh->mVertices[i].z; + + /* normals */ + vertices[j+3] = mesh->mNormals[i].x; + vertices[j+4] = mesh->mNormals[i].y; + vertices[j+5] = mesh->mNormals[i].z; + + /* uvs? */ + if (mesh_has_uvs) { + vertices[j+6] = mesh->mTextureCoords[0][i].x; + vertices[j+7] = mesh->mTextureCoords[0][i].y; + } + } + + unsigned int n_indices = mesh->mNumFaces*3; + unsigned int* indices = malloc(sizeof(unsigned int) * n_indices); + for (int i=0; imNumFaces; i++) { + int j = 3*i; + struct aiFace face = mesh->mFaces[i]; + indices[j+0] = face.mIndices[0]; + indices[j+1] = face.mIndices[1]; + indices[j+2] = face.mIndices[2]; + } + + honey_mesh result; + + if (mesh_has_uvs) { + unsigned int n_attributes = 3; + unsigned int attribute_sizes[] = { 3, 3, 2 }; + honey_mesh_new(&result, + vertices, n_vertices, + n_attributes, attribute_sizes, + indices, n_indices); + } + else { + unsigned int n_attributes = 2; + unsigned int attribute_sizes[] = { 3, 3 }; + honey_mesh_new(&result, + vertices, n_vertices, + n_attributes, attribute_sizes, + indices, n_indices); + } + + free(vertices); + free(indices); + + return result; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +static void process_nodes_recursively(lua_State* L, + struct aiScene* scene, + struct aiNode* node, + int* n_meshes) +{ + for (int i=0; imNumMeshes; i++) { + honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + struct aiMesh* assimp_mesh = scene->mMeshes[node->mMeshes[i]]; + *mesh = assimp_to_honey_mesh(assimp_mesh, scene); + lua_rawseti(L, -2, *n_meshes); + (*n_meshes)++; + } + + for (int i=0; imNumChildren; i++) { + process_nodes_recursively(L, scene, node->mChildren[i], n_meshes); + } +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_mesh_load(lua_State* L) +{ + char* filename; + honey_lua_parse_arguments(L, 1, HONEY_STRING, &filename); + + int n_meshes = 1; + + struct aiScene* scene = aiImportFile(filename, + aiProcess_Triangulate | + aiProcess_FlipUVs); + if (scene == NULL) { + char* error; + honey_format_string(&error, "could not open file '%s'", filename); + lua_pushstring(L, error); + free(error); + lua_error(L); + } + + if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || + scene->mRootNode == NULL) { + char* error; + honey_format_string(&error, "could not read mesh(es) in '%s'", filename); + lua_pushstring(L, error); + free(error); + lua_error(L); + } + + lua_createtable(L, 0, 0); + + process_nodes_recursively(L, scene, scene->mRootNode, &n_meshes); + + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +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; imNumVertices; - - if (mesh->mTextureCoords[0]) { - mesh_has_uvs = true; - vertex_step = 8; - } - - float* vertices = malloc(sizeof(float) * vertex_step * n_vertices); - for (int i=0; imVertices[i].x; - vertices[j+1] = mesh->mVertices[i].y; - vertices[j+2] = mesh->mVertices[i].z; - - /* normals */ - vertices[j+3] = mesh->mNormals[i].x; - vertices[j+4] = mesh->mNormals[i].y; - vertices[j+5] = mesh->mNormals[i].z; - - /* uvs? */ - if (mesh_has_uvs) { - vertices[j+6] = mesh->mTextureCoords[0][i].x; - vertices[j+7] = mesh->mTextureCoords[0][i].y; - } - } - - unsigned int n_indices = mesh->mNumFaces*3; - unsigned int* indices = malloc(sizeof(unsigned int) * n_indices); - for (int i=0; imNumFaces; i++) { - int j = 3*i; - struct aiFace face = mesh->mFaces[i]; - indices[j+0] = face.mIndices[0]; - indices[j+1] = face.mIndices[1]; - indices[j+2] = face.mIndices[2]; - } - - honey_mesh result; - - if (mesh_has_uvs) { - unsigned int n_attributes = 3; - unsigned int attribute_sizes[] = { 3, 3, 2 }; - honey_mesh_new(&result, - vertices, n_vertices, - n_attributes, attribute_sizes, - indices, n_indices); - } - else { - unsigned int n_attributes = 2; - unsigned int attribute_sizes[] = { 3, 3 }; - honey_mesh_new(&result, - vertices, n_vertices, - n_attributes, attribute_sizes, - indices, n_indices); - } - - free(vertices); - free(indices); - - return result; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void process_nodes_recursively(lua_State* L, - struct aiScene* scene, - struct aiNode* node, - int* n_meshes) -{ - for (int i=0; imNumMeshes; i++) { - honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); - struct aiMesh* assimp_mesh = scene->mMeshes[node->mMeshes[i]]; - *mesh = assimp_to_honey_mesh(assimp_mesh, scene); - lua_rawseti(L, -2, *n_meshes); - (*n_meshes)++; - } - - for (int i=0; imNumChildren; i++) { - process_nodes_recursively(L, scene, node->mChildren[i], n_meshes); - } -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_mesh_load(lua_State* L) -{ - char* filename; - honey_lua_parse_arguments(L, 1, HONEY_STRING, &filename); - - int n_meshes = 1; - - struct aiScene* scene = aiImportFile(filename, - aiProcess_Triangulate | - aiProcess_FlipUVs); - if (scene == NULL) { - char* error; - honey_format_string(&error, "could not open file '%s'", filename); - lua_pushstring(L, error); - free(error); - lua_error(L); - } - - if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || - scene->mRootNode == NULL) { - char* error; - honey_format_string(&error, "could not read mesh(es) in '%s'", filename); - lua_pushstring(L, error); - free(error); - lua_error(L); - } - - lua_createtable(L, 0, 0); - - process_nodes_recursively(L, scene, scene->mRootNode, &n_meshes); - - return 1; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -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; iid = texture_id; + texture->width = width; + texture->height = height; + + switch(format) { + case GL_RED: + texture->type = GREY; + texture->channels = 1; + break; + + case GL_RGB: + texture->type = RGB; + texture->channels = 3; + break; + + case GL_RGBA: + texture->type = RGBA; + texture->channels = 4; + break; + + case GL_DEPTH_COMPONENT: + texture->type = DEPTH; + texture->channels = 1; + break; + + default: + break; + } + + return HONEY_OK; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_new_greyscale(honey_texture* texture, + int height, int width, + unsigned char* data) +{ + generate_texture(texture, width, height, GL_RED, GL_UNSIGNED_BYTE, data); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_new_rgb(honey_texture* texture, + int height, int width, + unsigned char* data) +{ + generate_texture(texture, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_new_rgba(honey_texture* texture, + int height, int width, + unsigned char* data) +{ + generate_texture(texture, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_new_depth(honey_texture* texture, + int height, int width, + float* data) +{ + generate_texture(texture, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, data); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +enum honey_texture_result honey_texture_load(honey_texture* texture, + char* texture_path) +{ + int width, height, channels; + unsigned char* image_data = stbi_load(texture_path, &width, &height, &channels, 0); + if (image_data == NULL) { + return TEXTURE_FAILED; + } + + switch(channels) { + case 1: + honey_texture_new_greyscale(texture, width, height, image_data); + break; + + case 3: + honey_texture_new_rgb(texture, width, height, image_data); + break; + + case 4: + honey_texture_new_rgba(texture, width, height, image_data); + break; + + default: + return TEXTURE_CHANNEL_ERROR; + } + + glGenerateMipmap(GL_TEXTURE_2D); + stbi_image_free(image_data); + + return TEXTURE_OK; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_use(honey_texture texture, int texture_unit) { + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, texture.id); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_texture_framebuffer_object_new(unsigned int* destination, + honey_texture* draw, + honey_texture* depth, + int width, int height) +{ + glBindTexture(GL_TEXTURE_2D, 0); + + glGenFramebuffers(1, destination); + glBindFramebuffer(GL_FRAMEBUFFER, *destination); + + if (draw != NULL) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, draw->id, 0); + else { + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + } + + if (depth != NULL) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth->id, 0); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + printf("framebuffer is not complete!\n"); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} diff --git a/src/texture.h b/src/texture.h new file mode 100644 index 0000000..aed6057 --- /dev/null +++ b/src/texture.h @@ -0,0 +1,121 @@ +#ifndef HONEY_TEXTURE_H +#define HONEY_TEXTURE_H + +/** @file texture.h + * + *@brief Defines the honey_texture struct and associated functions. +*/ + +#include "common.h" + +enum honey_texture_result { + TEXTURE_OK, + TEXTURE_FAILED, + TEXTURE_CHANNEL_ERROR, + N_TEXTURE_RESULTS }; + +typedef struct { + unsigned int id; + enum { + GREY, + RGB, + RGBA, + DEPTH + } type; + int width; + int height; + int channels; +} honey_texture; + +/** @brief Place the honey.texture bindings as a table on the stack. */ +void honey_setup_texture(lua_State* L); + +/** @brief Create a greyscale texture. + * + * @param[out] texture Pointer to the destination texture. + * @param[in] width The width in pixels of the texture to create. + * @param[in] height The height in pixels of the texture to create. + * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. + * + * @returns Nothing. + */ +void honey_texture_new_greyscale(honey_texture* texture, + int width, int height, + unsigned char* data); + +/** @brief Create an RGB texture. + * + * @param[out] texture Pointer to the destination texture. + * @param[in] width The width in pixels of the texture to create. + * @param[in] height The height in pixels of the texture to create. + * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. + * + * @returns Nothing. + */ +void honey_texture_new_rgb(honey_texture* texture, + int width, int height, + unsigned char* data); + +/** @brief Create an RGBA texture. + * + * @param[out] texture Pointer to the destination texture. + * @param[in] width The width in pixels of the texture to create. + * @param[in] height The height in pixels of the texture to create. + * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. + * + * @returns Nothing. + */ +void honey_texture_new_rgba(honey_texture* texture, + int width, int height, + unsigned char* data); + +/** @brief Create a depth texture. + * + * @param[out] texture Pointer to the destination texture. + * @param[in] width The width in pixels of the texture to create. + * @param[in] height The height in pixels of the texture to create. + * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. + * + * @returns Nothing. + */ +void honey_texture_new_depth(honey_texture* texture, + int width, int height, + float* data); + +/** @brief Load a texture from disk. + * + * @param[out] texture Pointer to the destination texture + * @param[in] texture_path Path to the location of the texture + * @param[in] alpha_channel Set to true if the target image contains an alpha channel + * + * @return Success or failure type + */ +enum honey_texture_result honey_texture_load(honey_texture* texture, + char* texture_path); + +/** @brief Load a texture into a texture unit. + * + * @param[in] texture The texture to use + * @param[in] texture_unit The texture unit to put the texture in + */ +void honey_texture_use(honey_texture texture, int texture_unit); + +/** @brief Create a framebuffer object. + * + * You must specify at least one of draw and depth; otherwise, the framebuffer will + * be incomplete and fail. + * + * @param[out] destination Pointer to store the resulting OpenGL handle in. + * @param[in] draw Pointer to a texture to draw to. + * @param[in] depth Pointer to a depth texture. + * @param[in] width The width in pixels of the FBO. + * @param[in] height The height in pixels of the FBO. + * + * @returns Nothing. + */ +void honey_texture_framebuffer_object_new(unsigned int* destination, + honey_texture* draw, + honey_texture* depth, + int width, int height); + +#endif diff --git a/src/texture/texture.c b/src/texture/texture.c deleted file mode 100644 index 37e38e9..0000000 --- a/src/texture/texture.c +++ /dev/null @@ -1,268 +0,0 @@ -#include "texture.h" - -static int honey_lua_texture_new(lua_State* L) -{ - honey_texture* texture = lua_newuserdata(L, sizeof(honey_texture)); - return 1; -} - -static int honey_lua_texture_create(lua_State* L) -{ - honey_texture* texture; - int width, height; - char* type; - honey_lua_parse_arguments(L, 4, - HONEY_USERDATA, &texture, - HONEY_STRING, &type, - HONEY_INTEGER, &width, - HONEY_INTEGER, &height); - - if (strcmp(type, "greyscale") == 0) - honey_texture_new_greyscale(texture, width, height, NULL); - else if (strcmp(type, "rgb") == 0) - honey_texture_new_rgb(texture, width, height, NULL); - else if (strcmp(type, "rgba") == 0) - honey_texture_new_rgba(texture, width, height, NULL); - else if (strcmp(type, "depth") == 0) - honey_texture_new_depth(texture, width, height, NULL); - else { - char* error; - honey_format_string(&error, - "unknown texture type '%s'", - type); - lua_pushstring(L, error); - free(error); - lua_error(L); - } - return 0; -} - -static int honey_lua_texture_load(lua_State* L) -{ - honey_texture* texture; - char* texture_path; - honey_lua_parse_arguments(L, 3, - HONEY_USERDATA, &texture, - HONEY_STRING, &texture_path); - enum honey_texture_result result = honey_texture_load(texture, texture_path); - if (result != TEXTURE_OK) { - char* error; - honey_format_string(&error, - "failed to load '%s'", - texture_path); - lua_pushstring(L, error); - free(error); - lua_error(L); - } - - return 0; -} - -static int honey_lua_texture_use(lua_State* L) -{ - honey_texture* texture; - int texture_unit; - honey_lua_parse_arguments(L, 2, - HONEY_USERDATA, &texture, - HONEY_INTEGER, &texture_unit); - honey_texture_use(*texture, texture_unit); - return 0; -} - -static int honey_lua_framebuffer_new(lua_State* L) -{ - honey_texture* draw, *depth; - if (lua_isuserdata(L, 1)) - draw = lua_touserdata(L, 1); - else - draw = NULL; - - if (lua_isuserdata(L, 2)) - depth = lua_touserdata(L, 2); - else - depth = NULL; - - int width, height; - honey_lua_parse_arguments(L, 4, HONEY_ANY, HONEY_ANY, - HONEY_INTEGER, &width, - HONEY_INTEGER, &height); - - unsigned int framebuffer; - honey_texture_framebuffer_object_new(&framebuffer, - draw, depth, - width, height); - lua_pushinteger(L, framebuffer); - return 1; -} - -void honey_setup_texture(lua_State* L) -{ - honey_lua_element texture_elements[] = { - { "new", HONEY_FUNCTION, { .function = honey_lua_texture_new } }, - { "new_framebuffer", HONEY_FUNCTION, { .function = honey_lua_framebuffer_new } }, - { "create", HONEY_FUNCTION, { .function = honey_lua_texture_create } }, - { "load", HONEY_FUNCTION, { .function = honey_lua_texture_load } }, - { "use", HONEY_FUNCTION, { .function = honey_lua_texture_use } }, - }; - - honey_lua_create_table(L, texture_elements, 5); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void generate_texture(honey_texture* texture, - int width, int height, - int format, int type, - void* data) -{ - unsigned int texture_id; - glGenTextures(1, &texture_id); - glBindTexture(GL_TEXTURE_2D, texture_id); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glTexImage2D(GL_TEXTURE_2D, 0, - format, - width, height, 0, - format, - type, data); - - texture->id = texture_id; - texture->width = width; - texture->height = height; - - switch(format) { - case GL_RED: - texture->type = GREY; - texture->channels = 1; - break; - - case GL_RGB: - texture->type = RGB; - texture->channels = 3; - break; - - case GL_RGBA: - texture->type = RGBA; - texture->channels = 4; - break; - - case GL_DEPTH_COMPONENT: - texture->type = DEPTH; - texture->channels = 1; - break; - - default: - break; - } - - return HONEY_OK; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_new_greyscale(honey_texture* texture, - int height, int width, - unsigned char* data) -{ - generate_texture(texture, width, height, GL_RED, GL_UNSIGNED_BYTE, data); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_new_rgb(honey_texture* texture, - int height, int width, - unsigned char* data) -{ - generate_texture(texture, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_new_rgba(honey_texture* texture, - int height, int width, - unsigned char* data) -{ - generate_texture(texture, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_new_depth(honey_texture* texture, - int height, int width, - float* data) -{ - generate_texture(texture, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, data); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -enum honey_texture_result honey_texture_load(honey_texture* texture, - char* texture_path) -{ - int width, height, channels; - unsigned char* image_data = stbi_load(texture_path, &width, &height, &channels, 0); - if (image_data == NULL) { - return TEXTURE_FAILED; - } - - switch(channels) { - case 1: - honey_texture_new_greyscale(texture, width, height, image_data); - break; - - case 3: - honey_texture_new_rgb(texture, width, height, image_data); - break; - - case 4: - honey_texture_new_rgba(texture, width, height, image_data); - break; - - default: - return TEXTURE_CHANNEL_ERROR; - } - - glGenerateMipmap(GL_TEXTURE_2D); - stbi_image_free(image_data); - - return TEXTURE_OK; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_use(honey_texture texture, int texture_unit) { - glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, texture.id); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void honey_texture_framebuffer_object_new(unsigned int* destination, - honey_texture* draw, - honey_texture* depth, - int width, int height) -{ - glBindTexture(GL_TEXTURE_2D, 0); - - glGenFramebuffers(1, destination); - glBindFramebuffer(GL_FRAMEBUFFER, *destination); - - if (draw != NULL) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, draw->id, 0); - else { - glDrawBuffer(GL_NONE); - glReadBuffer(GL_NONE); - } - - if (depth != NULL) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth->id, 0); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - printf("framebuffer is not complete!\n"); - - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} diff --git a/src/texture/texture.h b/src/texture/texture.h deleted file mode 100644 index 9dc0308..0000000 --- a/src/texture/texture.h +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef HONEY_TEXTURE_H -#define HONEY_TEXTURE_H - -/** @file texture.h - * - *@brief Defines the honey_texture struct and associated functions. -*/ - -#include "../common.h" - -enum honey_texture_result { - TEXTURE_OK, - TEXTURE_FAILED, - TEXTURE_CHANNEL_ERROR, - N_TEXTURE_RESULTS }; - -typedef struct { - unsigned int id; - enum { - GREY, - RGB, - RGBA, - DEPTH - } type; - int width; - int height; - int channels; -} honey_texture; - -/** @brief Place the honey.texture bindings as a table on the stack. */ -void honey_setup_texture(lua_State* L); - -/** @brief Create a greyscale texture. - * - * @param[out] texture Pointer to the destination texture. - * @param[in] width The width in pixels of the texture to create. - * @param[in] height The height in pixels of the texture to create. - * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. - * - * @returns Nothing. - */ -void honey_texture_new_greyscale(honey_texture* texture, - int width, int height, - unsigned char* data); - -/** @brief Create an RGB texture. - * - * @param[out] texture Pointer to the destination texture. - * @param[in] width The width in pixels of the texture to create. - * @param[in] height The height in pixels of the texture to create. - * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. - * - * @returns Nothing. - */ -void honey_texture_new_rgb(honey_texture* texture, - int width, int height, - unsigned char* data); - -/** @brief Create an RGBA texture. - * - * @param[out] texture Pointer to the destination texture. - * @param[in] width The width in pixels of the texture to create. - * @param[in] height The height in pixels of the texture to create. - * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. - * - * @returns Nothing. - */ -void honey_texture_new_rgba(honey_texture* texture, - int width, int height, - unsigned char* data); - -/** @brief Create a depth texture. - * - * @param[out] texture Pointer to the destination texture. - * @param[in] width The width in pixels of the texture to create. - * @param[in] height The height in pixels of the texture to create. - * @param[in] data The data to populate the texture with, or NULL to leave it unpopulated. - * - * @returns Nothing. - */ -void honey_texture_new_depth(honey_texture* texture, - int width, int height, - float* data); - -/** @brief Load a texture from disk. - * - * @param[out] texture Pointer to the destination texture - * @param[in] texture_path Path to the location of the texture - * @param[in] alpha_channel Set to true if the target image contains an alpha channel - * - * @return Success or failure type - */ -enum honey_texture_result honey_texture_load(honey_texture* texture, - char* texture_path); - -/** @brief Load a texture into a texture unit. - * - * @param[in] texture The texture to use - * @param[in] texture_unit The texture unit to put the texture in - */ -void honey_texture_use(honey_texture texture, int texture_unit); - -/** @brief Create a framebuffer object. - * - * You must specify at least one of draw and depth; otherwise, the framebuffer will - * be incomplete and fail. - * - * @param[out] destination Pointer to store the resulting OpenGL handle in. - * @param[in] draw Pointer to a texture to draw to. - * @param[in] depth Pointer to a depth texture. - * @param[in] width The width in pixels of the FBO. - * @param[in] height The height in pixels of the FBO. - * - * @returns Nothing. - */ -void honey_texture_framebuffer_object_new(unsigned int* destination, - honey_texture* draw, - honey_texture* depth, - int width, int height); - -#endif diff --git a/src/window.c b/src/window.c new file mode 100644 index 0000000..6a1fe6f --- /dev/null +++ b/src/window.c @@ -0,0 +1,268 @@ +#include "window.h" + +int honey_window_info_ref = LUA_NOREF; +int honey_window_resize_callback_ref = LUA_NOREF; +int honey_window_resize_callback_data_ref = LUA_NOREF; +int honey_window_focus_callback_ref = LUA_NOREF; +int honey_window_focus_callback_data_ref = LUA_NOREF; + +static void honey_glfw_window_resize_callback(honey_window window, + int width, int height) +{ + lua_State* L = glfwGetWindowUserPointer(window); + + int callback = honey_window_resize_callback_ref; + int data = honey_window_resize_callback_data_ref; + + if (callback == LUA_NOREF) + return; + + lua_rawgeti(L, LUA_REGISTRYINDEX, callback); + + lua_pushinteger(L, width); + lua_pushinteger(L, height); + + if (data == LUA_NOREF || data == LUA_REFNIL) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, data); + + honey_lua_pcall(L, 3, 0); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +static void honey_glfw_window_focus_callback(honey_window window, + int focused) +{ + lua_State* L = glfwGetWindowUserPointer(window); + + int callback = honey_window_focus_callback_ref; + int data = honey_window_focus_callback_data_ref; + + if (callback == LUA_NOREF) + return; + + lua_rawgeti(L, LUA_REGISTRYINDEX, callback); + + lua_pushboolean(L, focused); + + if (data == LUA_NOREF || data == LUA_REFNIL) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, data); + + honey_lua_pcall(L, 2, 0); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +bool honey_setup_window(lua_State* L) +{ + honey_window_information* info = lua_newuserdata(L, sizeof(honey_window_information)); + honey_window_info_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + info->window = glfwCreateWindow(HONEY_WINDOW_DEFAULT_WIDTH, + HONEY_WINDOW_DEFAULT_HEIGHT, + "honey", NULL, NULL); + + info->width = HONEY_WINDOW_DEFAULT_WIDTH; + info->height = HONEY_WINDOW_DEFAULT_WIDTH; + info->fullscreen = false; + + if (info->window == NULL) { + fprintf(stderr, "[honey] ERROR: failed to create window!\n"); + glfwTerminate(); + return false; + } + /* store lua state in window, so it's accessible from GLFW callbacks */ + glfwSetWindowUserPointer(info->window, L); + glfwMakeContextCurrent(info->window); + + if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { + fprintf(stderr, "[honey] ERROR: failed to initialize GLAD!\n"); + glfwTerminate(); + return false; + } + + // Enable blending + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // Enable depth testing + glEnable(GL_DEPTH_TEST); + + // Enable face culling + glEnable(GL_CULL_FACE); + + glfwSetWindowSizeCallback(info->window, honey_glfw_window_resize_callback); + glfwSetWindowFocusCallback(info->window, honey_glfw_window_focus_callback); + + + honey_lua_element window_elements[] = { + { "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); + return true; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_set_fullscreen(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_BOOLEAN)) + lua_error(L); + + bool fullscreen = lua_toboolean(L, 1); + + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); + honey_window_information* info = lua_touserdata(L, -1); + + if (fullscreen) { + glfwGetWindowSize(info->window, &(info->width), &(info->height)); + + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + const GLFWvidmode* mode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(info->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); + info->fullscreen = true; + } + else { + glfwSetWindowMonitor(info->window, NULL, 20, 20, info->width, info->height, 0); + info->fullscreen = false; + } + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_set_title(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_STRING)) + lua_error(L); + + const char* title = lua_tostring(L, 1); + + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); + honey_window_information* info = lua_touserdata(L, -1); + + glfwSetWindowTitle(info->window, title); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_get_size(lua_State* L) +{ + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); + honey_window_information* info = lua_touserdata(L, -1); + + int width, height; + glfwGetWindowSize(info->window, &width, &height); + lua_pushinteger(L, width); + lua_pushinteger(L, height); + return 2; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_set_size(lua_State* L) +{ + if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_INTEGER)) + lua_error(L); + + int width = lua_tointeger(L, 1); + int height = lua_tointeger(L, 2); + + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); + honey_window_information* info = lua_touserdata(L, -1); + + glfwSetWindowSize(info->window, width, height); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_resize_bind(lua_State* L) +{ + if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) + lua_error(L); + + lua_pushvalue(L, 1); + honey_window_resize_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + honey_window_resize_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_resize_unbind(lua_State* L) +{ + int callback = honey_window_resize_callback_ref; + int data = honey_window_resize_callback_data_ref; + + if (callback != LUA_NOREF) { + lua_pushnil(L); + lua_rawseti(L, LUA_REGISTRYINDEX, callback); + } + + if (data != LUA_NOREF && data != LUA_REFNIL) { + lua_pushnil(L); + lua_rawseti(L, LUA_REGISTRYINDEX, callback); + } + + honey_window_resize_callback_ref = LUA_NOREF; + honey_window_resize_callback_data_ref = LUA_NOREF; + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_focus_bind(lua_State* L) +{ + if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) + lua_error(L); + + lua_pushvalue(L, 1); + honey_window_focus_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + honey_window_focus_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_focus_unbind(lua_State* L) +{ + int callback = honey_window_focus_callback_ref; + int data = honey_window_focus_callback_data_ref; + + if (callback != LUA_NOREF) { + lua_pushnil(L); + lua_rawseti(L, LUA_REGISTRYINDEX, callback); + } + + if (data != LUA_NOREF && data != LUA_REFNIL) { + lua_pushnil(L); + lua_rawseti(L, LUA_REGISTRYINDEX, callback); + } + + honey_window_focus_callback_ref = LUA_NOREF; + honey_window_focus_callback_data_ref = LUA_NOREF; + return 0; +} diff --git a/src/window.h b/src/window.h new file mode 100644 index 0000000..fb5c036 --- /dev/null +++ b/src/window.h @@ -0,0 +1,86 @@ +/** @file */ + +#ifndef HONEY_WINDOW_H +#define HONEY_WINDOW_H + +#include "common.h" + +#define HONEY_WINDOW_DEFAULT_WIDTH 640 +#define HONEY_WINDOW_DEFAULT_HEIGHT 480 + +/** @brief Push the various honey.window table to the stack. + * + * @param[in] L The lua state to push to + * @param[in] window The window created by honey_setup() + * + * @returns Nothing. + */ +bool honey_setup_window(lua_State* L); + +/** @brief Set whether or not the window is fullscreen. + * + * Lua parameters: + * @param[in] fullscreen Boolean set to true if the window is to be fullscreen and false otherwise. + * + * @returns Nothing. + */ +int honey_window_set_fullscreen(lua_State* L); + +/** @brief Set the title of the window. + * + * @param[in] title String containing the desired window title. + * + * @returns Nothing. + */ +int honey_window_set_title(lua_State* L); + +/** @brief Get the current size of the window. + * + * @returns width, height numbers representing the window size in pixels. + */ +int honey_window_get_size(lua_State* L); + +/** @brief Set the current size of the window. + * + * @param[in] width Integer of the desired width in pixels. + * @param[in] height Integer of the desired height in pixels. + * + * @returns Nothing. + */ +int honey_window_set_size(lua_State* L); + +/** @brief Bind a call back to the window resize. + * + * @param[in] callback The callback function to call on a window resize. + * + * @returns Nothing. + */ +int honey_window_resize_bind(lua_State* L); + +/** @brief Unbind any callback that may be attached to the window resize. + * + * @returns Nothing. + */ +int honey_window_resize_unbind(lua_State* L); + +/** @brief Bind a callback to the window changing focus. + * + * The supplied callback function should be of the form + * function(boolean, data). The boolean is true if the window + * is gaining focus, and false if it is losing focus. The data is + * just the data parameter passed to this function. + * + * @param callback The callback function to call on a window resize. + * @param data Data to send to the callback. + * + * @returns Nothing. + */ +int honey_window_focus_bind(lua_State* L); + +/** @brief Unbind any callback that may be attached to the window focus. + * + * @returns Nothing. + */ +int honey_window_focus_unbind(lua_State* L); + +#endif diff --git a/src/window/window.c b/src/window/window.c deleted file mode 100644 index 6a1fe6f..0000000 --- a/src/window/window.c +++ /dev/null @@ -1,268 +0,0 @@ -#include "window.h" - -int honey_window_info_ref = LUA_NOREF; -int honey_window_resize_callback_ref = LUA_NOREF; -int honey_window_resize_callback_data_ref = LUA_NOREF; -int honey_window_focus_callback_ref = LUA_NOREF; -int honey_window_focus_callback_data_ref = LUA_NOREF; - -static void honey_glfw_window_resize_callback(honey_window window, - int width, int height) -{ - lua_State* L = glfwGetWindowUserPointer(window); - - int callback = honey_window_resize_callback_ref; - int data = honey_window_resize_callback_data_ref; - - if (callback == LUA_NOREF) - return; - - lua_rawgeti(L, LUA_REGISTRYINDEX, callback); - - lua_pushinteger(L, width); - lua_pushinteger(L, height); - - if (data == LUA_NOREF || data == LUA_REFNIL) - lua_pushnil(L); - else - lua_rawgeti(L, LUA_REGISTRYINDEX, data); - - honey_lua_pcall(L, 3, 0); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void honey_glfw_window_focus_callback(honey_window window, - int focused) -{ - lua_State* L = glfwGetWindowUserPointer(window); - - int callback = honey_window_focus_callback_ref; - int data = honey_window_focus_callback_data_ref; - - if (callback == LUA_NOREF) - return; - - lua_rawgeti(L, LUA_REGISTRYINDEX, callback); - - lua_pushboolean(L, focused); - - if (data == LUA_NOREF || data == LUA_REFNIL) - lua_pushnil(L); - else - lua_rawgeti(L, LUA_REGISTRYINDEX, data); - - honey_lua_pcall(L, 2, 0); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -bool honey_setup_window(lua_State* L) -{ - honey_window_information* info = lua_newuserdata(L, sizeof(honey_window_information)); - honey_window_info_ref = luaL_ref(L, LUA_REGISTRYINDEX); - - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - info->window = glfwCreateWindow(HONEY_WINDOW_DEFAULT_WIDTH, - HONEY_WINDOW_DEFAULT_HEIGHT, - "honey", NULL, NULL); - - info->width = HONEY_WINDOW_DEFAULT_WIDTH; - info->height = HONEY_WINDOW_DEFAULT_WIDTH; - info->fullscreen = false; - - if (info->window == NULL) { - fprintf(stderr, "[honey] ERROR: failed to create window!\n"); - glfwTerminate(); - return false; - } - /* store lua state in window, so it's accessible from GLFW callbacks */ - glfwSetWindowUserPointer(info->window, L); - glfwMakeContextCurrent(info->window); - - if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { - fprintf(stderr, "[honey] ERROR: failed to initialize GLAD!\n"); - glfwTerminate(); - return false; - } - - // Enable blending - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - // Enable depth testing - glEnable(GL_DEPTH_TEST); - - // Enable face culling - glEnable(GL_CULL_FACE); - - glfwSetWindowSizeCallback(info->window, honey_glfw_window_resize_callback); - glfwSetWindowFocusCallback(info->window, honey_glfw_window_focus_callback); - - - honey_lua_element window_elements[] = { - { "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); - return true; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_set_fullscreen(lua_State* L) -{ - if (!honey_lua_validate_types(L, 1, HONEY_BOOLEAN)) - lua_error(L); - - bool fullscreen = lua_toboolean(L, 1); - - lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); - honey_window_information* info = lua_touserdata(L, -1); - - if (fullscreen) { - glfwGetWindowSize(info->window, &(info->width), &(info->height)); - - GLFWmonitor* monitor = glfwGetPrimaryMonitor(); - const GLFWvidmode* mode = glfwGetVideoMode(monitor); - glfwSetWindowMonitor(info->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); - info->fullscreen = true; - } - else { - glfwSetWindowMonitor(info->window, NULL, 20, 20, info->width, info->height, 0); - info->fullscreen = false; - } - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_set_title(lua_State* L) -{ - if (!honey_lua_validate_types(L, 1, HONEY_STRING)) - lua_error(L); - - const char* title = lua_tostring(L, 1); - - lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); - honey_window_information* info = lua_touserdata(L, -1); - - glfwSetWindowTitle(info->window, title); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_get_size(lua_State* L) -{ - lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); - honey_window_information* info = lua_touserdata(L, -1); - - int width, height; - glfwGetWindowSize(info->window, &width, &height); - lua_pushinteger(L, width); - lua_pushinteger(L, height); - return 2; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_set_size(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_INTEGER)) - lua_error(L); - - int width = lua_tointeger(L, 1); - int height = lua_tointeger(L, 2); - - lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); - honey_window_information* info = lua_touserdata(L, -1); - - glfwSetWindowSize(info->window, width, height); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_resize_bind(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) - lua_error(L); - - lua_pushvalue(L, 1); - honey_window_resize_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - honey_window_resize_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_resize_unbind(lua_State* L) -{ - int callback = honey_window_resize_callback_ref; - int data = honey_window_resize_callback_data_ref; - - if (callback != LUA_NOREF) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, callback); - } - - if (data != LUA_NOREF && data != LUA_REFNIL) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, callback); - } - - honey_window_resize_callback_ref = LUA_NOREF; - honey_window_resize_callback_data_ref = LUA_NOREF; - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_focus_bind(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY)) - lua_error(L); - - lua_pushvalue(L, 1); - honey_window_focus_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - honey_window_focus_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_window_focus_unbind(lua_State* L) -{ - int callback = honey_window_focus_callback_ref; - int data = honey_window_focus_callback_data_ref; - - if (callback != LUA_NOREF) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, callback); - } - - if (data != LUA_NOREF && data != LUA_REFNIL) { - lua_pushnil(L); - lua_rawseti(L, LUA_REGISTRYINDEX, callback); - } - - honey_window_focus_callback_ref = LUA_NOREF; - honey_window_focus_callback_data_ref = LUA_NOREF; - return 0; -} diff --git a/src/window/window.h b/src/window/window.h deleted file mode 100644 index 3ac982a..0000000 --- a/src/window/window.h +++ /dev/null @@ -1,86 +0,0 @@ -/** @file */ - -#ifndef HONEY_WINDOW_H -#define HONEY_WINDOW_H - -#include "../common.h" - -#define HONEY_WINDOW_DEFAULT_WIDTH 640 -#define HONEY_WINDOW_DEFAULT_HEIGHT 480 - -/** @brief Push the various honey.window table to the stack. - * - * @param[in] L The lua state to push to - * @param[in] window The window created by honey_setup() - * - * @returns Nothing. - */ -bool honey_setup_window(lua_State* L); - -/** @brief Set whether or not the window is fullscreen. - * - * Lua parameters: - * @param[in] fullscreen Boolean set to true if the window is to be fullscreen and false otherwise. - * - * @returns Nothing. - */ -int honey_window_set_fullscreen(lua_State* L); - -/** @brief Set the title of the window. - * - * @param[in] title String containing the desired window title. - * - * @returns Nothing. - */ -int honey_window_set_title(lua_State* L); - -/** @brief Get the current size of the window. - * - * @returns width, height numbers representing the window size in pixels. - */ -int honey_window_get_size(lua_State* L); - -/** @brief Set the current size of the window. - * - * @param[in] width Integer of the desired width in pixels. - * @param[in] height Integer of the desired height in pixels. - * - * @returns Nothing. - */ -int honey_window_set_size(lua_State* L); - -/** @brief Bind a call back to the window resize. - * - * @param[in] callback The callback function to call on a window resize. - * - * @returns Nothing. - */ -int honey_window_resize_bind(lua_State* L); - -/** @brief Unbind any callback that may be attached to the window resize. - * - * @returns Nothing. - */ -int honey_window_resize_unbind(lua_State* L); - -/** @brief Bind a callback to the window changing focus. - * - * The supplied callback function should be of the form - * function(boolean, data). The boolean is true if the window - * is gaining focus, and false if it is losing focus. The data is - * just the data parameter passed to this function. - * - * @param callback The callback function to call on a window resize. - * @param data Data to send to the callback. - * - * @returns Nothing. - */ -int honey_window_focus_bind(lua_State* L); - -/** @brief Unbind any callback that may be attached to the window focus. - * - * @returns Nothing. - */ -int honey_window_focus_unbind(lua_State* L); - -#endif -- cgit v1.2.1