From 65f1870eaa68cd18f2136bd112c042728855af03 Mon Sep 17 00:00:00 2001
From: sanine-a <sanine.not@pm.me>
Date: Sun, 29 Nov 2020 16:26:12 -0600
Subject: refactor all files to use new argument parsing and table building
 functions

---
 CMakeLists.txt     | 16 +++++-----
 src/common.h       | 16 ++++++++++
 src/glm_bindings.h | 12 --------
 src/honey.c        |  5 ---
 src/mesh.c         | 13 ++++----
 src/primitives.c   | 11 +++----
 src/shader.c       | 12 ++++----
 src/texture.c      | 19 +++++-------
 src/window.c       | 89 +++++++++++++++++++++++++++++-------------------------
 9 files changed, 97 insertions(+), 96 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0d96e64..336ff16 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,16 +16,16 @@ add_library(stb_image src/stb_image/stb_image.c)
 set(SOURCE_FILES
   src/main.c
   src/glm_bindings.c
-  src/camera/camera.c
+  src/camera.c
   src/honey.c
-  src/input/input.c
+  src/input.c
   src/honey_lua.c
-  src/light/light.c
-  src/mesh/mesh.c
-  src/primitives/primitives.c
-  src/shader/shader.c
-  src/texture/texture.c
-  src/window/window.c)
+  src/light.c
+  src/mesh.c
+  src/primitives.c
+  src/shader.c
+  src/texture.c
+  src/window.c)
   
 add_executable(honey ${SOURCE_FILES})
 if (WIN32)
diff --git a/src/common.h b/src/common.h
index f9bc64b..7eac236 100644
--- a/src/common.h
+++ b/src/common.h
@@ -72,6 +72,22 @@ typedef enum {
   HONEY_N_ERRORS
 } honey_result;
 
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+typedef enum { VEC3,
+	       VEC4,
+	       MAT3,
+	       MAT4
+} honey_glm_array_type;
+
+typedef struct {
+  honey_glm_array_type type;
+  unsigned int size;
+  float* data;
+} honey_glm_array;
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
 honey_result honey_format_string(char** string,
                                  char* format_string,
                                  ...);
diff --git a/src/glm_bindings.h b/src/glm_bindings.h
index d47f153..f6ec104 100644
--- a/src/glm_bindings.h
+++ b/src/glm_bindings.h
@@ -12,18 +12,6 @@ extern int honey_glm_vec4_mt_ref;
 extern int honey_glm_mat3_mt_ref;
 extern int honey_glm_mat4_mt_ref;
 
-typedef enum { VEC3,
-	       VEC4,
-	       MAT3,
-	       MAT4
-} honey_glm_array_type;
-
-typedef struct {
-  honey_glm_array_type type;
-  unsigned int size;
-  float* data;
-} honey_glm_array;
-
 /** @brief Push the honey glm binding functions to the lua stack.
  *
  * @returns Nothing.
diff --git a/src/honey.c b/src/honey.c
index 673cd49..122c736 100644
--- a/src/honey.c
+++ b/src/honey.c
@@ -117,7 +117,6 @@ bool honey_setup(lua_State** L)
 
     if (!honey_setup_window(*L))
         return false;
-    lua_setfield(*L, -2, "window");
 
     honey_setup_input(*L);
     lua_setfield(*L, -2, "input");
@@ -126,16 +125,12 @@ bool honey_setup(lua_State** L)
     lua_setfield(*L, -2, "glm");
 
     honey_setup_shader(*L);
-    lua_setfield(*L, -2, "shader");
 
     honey_setup_mesh(*L);
-    lua_setfield(*L, -2, "mesh");
 
     honey_setup_primitives(*L);
-    lua_setfield(*L, -2, "primitives");
 
     honey_setup_texture(*L);
-    lua_setfield(*L, -2, "texture");
 
     lua_pushcfunction(*L, honey_exit);
     lua_setfield(*L, -2, "exit");
diff --git a/src/mesh.c b/src/mesh.c
index 68e39d7..b06fd68 100644
--- a/src/mesh.c
+++ b/src/mesh.c
@@ -21,13 +21,12 @@ static int honey_mesh_lua_delete(lua_State* L)
 
 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);
+    honey_lua_create_table
+	(L, 3,
+	 HONEY_FUNCTION, "load",  honey_mesh_load,
+	 HONEY_FUNCTION, "draw",  honey_mesh_lua_draw,
+	 HONEY_FUNCTION, "delete",  honey_mesh_lua_delete);
+    lua_setfield(L, -2, "mesh");
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
diff --git a/src/primitives.c b/src/primitives.c
index d499248..c2f6c9e 100644
--- a/src/primitives.c
+++ b/src/primitives.c
@@ -33,12 +33,11 @@ static int honey_mesh_lua_cube(lua_State* L)
 
 void honey_setup_primitives(lua_State* L)
 {
-    honey_lua_element primitive_elements[] = {
-        { "plane", HONEY_FUNCTION, { .function = honey_mesh_lua_plane } },
-        { "cube", HONEY_FUNCTION, { .function = honey_mesh_lua_cube } },
-    };
-
-    honey_lua_create_table(L, primitive_elements, 2);
+    honey_lua_create_table
+	(L, 2,
+	 HONEY_FUNCTION, "plane", honey_mesh_lua_plane,
+	 HONEY_FUNCTION, "cube",  honey_mesh_lua_cube);
+    lua_setfield(L, -2, "primitives");
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
diff --git a/src/shader.c b/src/shader.c
index 6e393c2..a05bd0c 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -60,13 +60,13 @@ int honey_shader_new(lua_State* L)
     }
 
     int program = glCreateProgram();
-    glAttachShader(shader, vertex_shader);
-    glAttachShader(shader, fragment_shader);
-    glLinkProgram(shader);
+    glAttachShader(program, vertex_shader);
+    glAttachShader(program, fragment_shader);
+    glLinkProgram(program);
 
-    glGetShaderiv(shader, GL_LINK_STATUS, &success);
+    glGetShaderiv(program, GL_LINK_STATUS, &success);
     if (!success) {
-        glGetShaderInfoLog(shader, 1024, NULL, error);
+        glGetShaderInfoLog(program, 1024, NULL, error);
 	honey_lua_throw_error(L, "error linking shader program: %s",
 			      error);
     }
@@ -214,7 +214,7 @@ int honey_shader_set_mat3(lua_State* L)
 
 int honey_shader_set_mat4(lua_State* L)
 {
-    int *shader; char* name; float* array;
+    int *shader; char* name; honey_glm_array* array;
     honey_lua_parse_arguments
 	(L, 1, 3,
 	 HONEY_USERDATA, &shader,
diff --git a/src/texture.c b/src/texture.c
index 37e38e9..7388d50 100644
--- a/src/texture.c
+++ b/src/texture.c
@@ -97,15 +97,14 @@ static int honey_lua_framebuffer_new(lua_State* L)
 
 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);
+    honey_lua_create_table
+	(L, 5,
+	 HONEY_FUNCTION, "new",  honey_lua_texture_new,
+	 HONEY_FUNCTION, "new_framebuffer",  honey_lua_framebuffer_new,
+	 HONEY_FUNCTION, "create",  honey_lua_texture_create,
+	 HONEY_FUNCTION, "load",  honey_lua_texture_load,
+	 HONEY_FUNCTION, "use",  honey_lua_texture_use);
+    lua_setfield(L, -2, "texture");
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@@ -158,8 +157,6 @@ static void generate_texture(honey_texture* texture,
     default:
         break;
     }
-    
-    return HONEY_OK;
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
diff --git a/src/window.c b/src/window.c
index 6a1fe6f..ffcaee7 100644
--- a/src/window.c
+++ b/src/window.c
@@ -104,18 +104,19 @@ bool honey_setup_window(lua_State* L)
     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);
+    honey_lua_create_table
+	(L, 8,
+	 HONEY_FUNCTION, "set_fullscreen",  honey_window_set_fullscreen,
+	 HONEY_FUNCTION, "set_title",       honey_window_set_title,
+	 HONEY_FUNCTION, "get_size",        honey_window_get_size,
+	 HONEY_FUNCTION, "set_size",        honey_window_set_size,
+	 HONEY_FUNCTION, "resize_bind",     honey_window_resize_bind,
+	 HONEY_FUNCTION, "resize_unbind",   honey_window_resize_unbind,
+	 HONEY_FUNCTION, "focus_bind",      honey_window_focus_bind,
+	 HONEY_FUNCTION, "focus_unbind",    honey_window_focus_unbind);
+
+    lua_setfield(L, -2, "window");
+
     return true;
 }
 
@@ -123,10 +124,8 @@ bool honey_setup_window(lua_State* L)
 
 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);
+    bool fullscreen;
+    honey_lua_parse_arguments(L, 1, 1, HONEY_BOOLEAN, &fullscreen);
 
     lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
     honey_window_information* info = lua_touserdata(L, -1);
@@ -150,10 +149,8 @@ int honey_window_set_fullscreen(lua_State* L)
 
 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);
+    char* title;
+    honey_lua_parse_arguments(L, 1, 1, HONEY_STRING, &title);
 
     lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
     honey_window_information* info = lua_touserdata(L, -1);
@@ -180,11 +177,11 @@ int honey_window_get_size(lua_State* L)
 
 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);
+    int width, height;
+    honey_lua_parse_arguments
+	(L, 1, 2,
+	 HONEY_INTEGER, &width,
+	 HONEY_INTEGER, &height);
 
     lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
     honey_window_information* info = lua_touserdata(L, -1);
@@ -197,13 +194,20 @@ int honey_window_set_size(lua_State* L)
 
 int honey_window_resize_bind(lua_State* L)
 {
-    if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY))
-        lua_error(L);
+    int choice = honey_lua_parse_arguments
+	(L, 2,
+	 1, HONEY_FUNCTION,
+	 2, HONEY_FUNCTION, HONEY_ANY);
+
+    honey_window_resize_unbind(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);
+
+    if (choice == 1) {
+	lua_pushvalue(L, 2);
+	honey_window_resize_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+    }
 
     return 0;
 }
@@ -216,13 +220,11 @@ int honey_window_resize_unbind(lua_State* L)
     int data = honey_window_resize_callback_data_ref;
 
     if (callback != LUA_NOREF) {
-        lua_pushnil(L);
-        lua_rawseti(L, LUA_REGISTRYINDEX, callback);
+	luaL_unref(L, LUA_REGISTRYINDEX, callback);
     }
 
     if (data != LUA_NOREF && data != LUA_REFNIL) {
-        lua_pushnil(L);
-        lua_rawseti(L, LUA_REGISTRYINDEX, callback);
+	luaL_unref(L, LUA_REGISTRYINDEX, data);
     }
 
     honey_window_resize_callback_ref = LUA_NOREF;
@@ -234,13 +236,20 @@ int honey_window_resize_unbind(lua_State* L)
 
 int honey_window_focus_bind(lua_State* L)
 {
-    if (!honey_lua_validate_types(L, 2, HONEY_FUNCTION, HONEY_ANY))
-        lua_error(L);
+    int choice = honey_lua_parse_arguments
+	(L, 2,
+	 1, HONEY_FUNCTION,
+	 2, HONEY_FUNCTION, HONEY_ANY);
+
+    honey_window_focus_unbind(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);
+
+    if (choice == 1) {
+	lua_pushvalue(L, 2);
+	honey_window_focus_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+    }
 
     return 0;
 }
@@ -253,13 +262,11 @@ int honey_window_focus_unbind(lua_State* L)
     int data = honey_window_focus_callback_data_ref;
 
     if (callback != LUA_NOREF) {
-        lua_pushnil(L);
-        lua_rawseti(L, LUA_REGISTRYINDEX, callback);
+	luaL_unref(L, LUA_REGISTRYINDEX, callback);
     }
 
     if (data != LUA_NOREF && data != LUA_REFNIL) {
-        lua_pushnil(L);
-        lua_rawseti(L, LUA_REGISTRYINDEX, callback);
+        luaL_unref(L, LUA_REGISTRYINDEX, callback);
     }
 
     honey_window_focus_callback_ref = LUA_NOREF;
-- 
cgit v1.2.1