summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-10-24 22:09:34 -0500
committersanine-a <sanine.not@pm.me>2020-10-24 22:09:34 -0500
commitde2fa7938758ff83cabf06baf4c0ac7a230e78bf (patch)
treecf11c155d3c1e432b19f840556065d2b94e41773
parent41ac253f743e464194587c3ecc0f2547c95d924d (diff)
add more window functions and clean up window info access
-rw-r--r--demo/main.lua18
-rw-r--r--src/honey.c9
-rw-r--r--src/main.c5
-rw-r--r--src/window/window.c78
-rw-r--r--src/window/window.h25
5 files changed, 110 insertions, 25 deletions
diff --git a/demo/main.lua b/demo/main.lua
index 6f6439f..64ab0ae 100644
--- a/demo/main.lua
+++ b/demo/main.lua
@@ -1,15 +1,25 @@
-for key, value in pairs(honey.input) do
- print(key, value)
-end
-
local fullscreen = false
local a_func = function(action, data)
+ if (action == 0) then return end
fullscreen = not fullscreen
honey.window.set_fullscreen(fullscreen)
end
+local b_func = function(action, data)
+ if (action == 0) then return end
+ local w, h = honey.window.get_size()
+ print(w, h)
+end
+
+for k,v in pairs(honey.window) do
+ print(k, v)
+end
+
+honey.window.set_title('honey engine demo')
+
honey.input.bind_key(honey.input.key.a, a_func)
+honey.input.bind_key(honey.input.key.b, b_func)
function honey.update(dt)
end
diff --git a/src/honey.c b/src/honey.c
index cfc5bc7..5cf960e 100644
--- a/src/honey.c
+++ b/src/honey.c
@@ -65,18 +65,17 @@ bool honey_setup(lua_State** L)
if (!honey_setup_window(*L))
return false;
-
- lua_getfield(*L, -1, "internal");
- honey_window_information* info = lua_touserdata(*L, -1);
- lua_pop(*L, 1);
lua_setfield(*L, -2, "window");
honey_setup_input(*L);
- glfwSetKeyCallback(info->window, default_honey_keyboard_callback);
lua_setfield(*L, -2, "input");
lua_setglobal(*L, "honey");
+ lua_rawgeti(*L, LUA_REGISTRYINDEX, honey_window_info_ref);
+ honey_window_information* info = lua_touserdata(*L, -1);
+ glfwSetKeyCallback(info->window, default_honey_keyboard_callback);
+
return true;
}
diff --git a/src/main.c b/src/main.c
index 9b83a8f..a49d90b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,11 +12,8 @@ int main(int argc, char** argv)
if (!honey_setup(&L))
return 1;
- lua_getglobal(L, "honey");
- lua_getfield(L, -1, "window");
- lua_getfield(L, -1, "internal");
+ lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
honey_window_information* info = lua_touserdata(L, -1);
- lua_pop(L, 2);
honey_window window = info->window;
char* script;
diff --git a/src/window/window.c b/src/window/window.c
index bb371cd..ff04b5a 100644
--- a/src/window/window.c
+++ b/src/window/window.c
@@ -1,19 +1,25 @@
#include "window.h"
+int honey_window_info_ref = LUA_NOREF;
+
bool honey_setup_window(lua_State* L)
{
- honey_window_information* info = malloc(sizeof(honey_window_information));
- if (info == NULL) {
- fprintf(stderr, "[honey] ERROR: failed to allocate memory for window information!\n");
- return false;
- }
+ 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(640, 480, "honey", NULL, NULL);
+ 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();
@@ -34,11 +40,13 @@ bool honey_setup_window(lua_State* L)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
honey_lua_element window_elements[] = {
- { "internal", HONEY_LIGHTUSERDATA, { .pointer = info } },
{ "set_fullscreen", HONEY_FUNC, { .function = honey_window_set_fullscreen } },
+ { "set_title", HONEY_FUNC, { .function = honey_window_set_title } },
+ { "get_size", HONEY_FUNC, { .function = honey_window_get_size } },
+ { "set_size", HONEY_FUNC, { .function = honey_window_set_size } },
};
- honey_lua_create_table(L, window_elements, 2);
+ honey_lua_create_table(L, window_elements, 4);
return true;
}
@@ -51,11 +59,8 @@ int honey_window_set_fullscreen(lua_State* L)
bool fullscreen = lua_toboolean(L, 1);
- lua_getglobal(L, "honey");
- lua_getfield(L, -1, "window");
- lua_getfield(L, -1, "internal");
+ lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
honey_window_information* info = lua_touserdata(L, -1);
- lua_pop(L, 2);
if (fullscreen) {
glfwGetWindowSize(info->window, &(info->width), &(info->height));
@@ -63,9 +68,58 @@ int honey_window_set_fullscreen(lua_State* L)
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_INT, HONEY_INT))
+ 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;
+}
diff --git a/src/window/window.h b/src/window/window.h
index c0083ba..3d320f7 100644
--- a/src/window/window.h
+++ b/src/window/window.h
@@ -15,6 +15,8 @@ typedef struct {
bool fullscreen;
} honey_window_information;
+extern int honey_window_info_ref;
+
/** @brief Push the various honey.window table to the stack.
*
* @param[in] L The lua state to push to
@@ -33,4 +35,27 @@ bool honey_setup_window(lua_State* L);
*/
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);
+
#endif