diff options
author | sanine <sanine.not@pm.me> | 2022-08-18 22:46:40 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-18 22:46:40 -0500 |
commit | 275536b36657744d802866c060654e2b5cd5a5f8 (patch) | |
tree | e685ed8d665fd629c19cbfc39095402d71e349fc | |
parent | 253f1d1ca8b4b81f206e4aeb20afe440a6dae8be (diff) |
implement working windows
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/gl/gl.c | 34 | ||||
-rw-r--r-- | src/gl/gl.h | 9 | ||||
-rw-r--r-- | src/gl/gl.test.c (renamed from src/gl/window.test.c) | 4 | ||||
-rw-r--r-- | src/gl/window.c | 56 | ||||
-rw-r--r-- | src/logging/logging.c | 1 | ||||
-rw-r--r-- | src/main.c | 32 | ||||
-rw-r--r-- | src/test/honey-test.h | 3 |
8 files changed, 134 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8095e9e..99964c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,8 @@ add_subdirectory(${LIB_ROOT}/cglm) set(HONEY_LIB_FILES ${SRC_ROOT}/logging/logging.c + ${SRC_ROOT}/gl/gl.c + ${SRC_ROOT}/gl/window.c ) set(SOURCE_FILES @@ -38,7 +40,7 @@ set(SOURCE_FILES add_executable(honey ${SOURCE_FILES}) -set(LIBRARIES lua5.1 honeysuckle assimp glad cairo m) +set(LIBRARIES lua5.1 honeysuckle assimp cairo m) if (WIN32) set(LIBRARIES ${LIBRARIES} glfw3 opengl32) else() @@ -57,7 +59,7 @@ set(TEST_SOURCES ${SRC_ROOT}/test/honey-test.c ${SRC_ROOT}/logging/logging.test.c - ${SRC_ROOT}/gl/window.test.c + ${SRC_ROOT}/gl/gl.test.c ) add_executable(test EXCLUDE_FROM_ALL ${TEST_SOURCES}) diff --git a/src/gl/gl.c b/src/gl/gl.c new file mode 100644 index 0000000..8613e70 --- /dev/null +++ b/src/gl/gl.c @@ -0,0 +1,34 @@ +#include "gl/glad/glad.h" +#include <GLFW/glfw3.h> +#include <lua.h> +#include <honeysuckle.h> + + +int gl_init(lua_State *L); +int gl_terminate(lua_State *L); + + +void setup_gl(lua_State *L, int honey_index) +{ + hs_create_table(L, + hs_str_cfunc("init", gl_init), + hs_str_cfunc("terminate", gl_terminate) + ); + lua_setfield(L, honey_index, "gl"); +} + + +int gl_init(lua_State *L) +{ + if (!glfwInit()) { + hs_throw_error(L, "failed to initialize GLFW"); + } + return 0; +} + + +int gl_terminate(lua_State *L) +{ + glfwTerminate(); + return 0; +} diff --git a/src/gl/gl.h b/src/gl/gl.h new file mode 100644 index 0000000..2e27851 --- /dev/null +++ b/src/gl/gl.h @@ -0,0 +1,9 @@ +#ifndef HONEY_GL_H +#define HONEY_GL_H + +#include <lua.h> + +void setup_gl(lua_State *L, int honey_index); +void setup_window(lua_State *L, int honey_index); + +#endif diff --git a/src/gl/window.test.c b/src/gl/gl.test.c index b4fb5d4..9bacbf9 100644 --- a/src/gl/window.test.c +++ b/src/gl/gl.test.c @@ -12,7 +12,7 @@ void mock_glfwTerminate_(); #define glfwInit mock_glfwInit_ #define hs_throw_error mock_hs_throw_error_ #define glfwTerminate mock_glfwTerminate_ -#include "gl/window.c" +#include "gl/gl.c" #undef glfwTerminate #undef hs_throw_error #undef glfwInit @@ -102,7 +102,7 @@ void gl_terminate_works() } -void suite_window() +void suite_gl() { lily_run_test(gl_init_succeeds); lily_run_test(gl_init_fails); diff --git a/src/gl/window.c b/src/gl/window.c index bc654f9..8434a5a 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -3,17 +3,61 @@ #include <lua.h> #include <honeysuckle.h> -int gl_init(lua_State *L) + +int window_create(lua_State *L); +int window_destroy(lua_State *L); +int window_should_close(lua_State *L); +int window_poll_events(lua_State *L); + + +void setup_window(lua_State *L, int honey_index) +{ + hs_create_table(L, + hs_str_cfunc("create", window_create), + hs_str_cfunc("destroy", window_destroy), + hs_str_cfunc("shouldClose", window_should_close), + hs_str_cfunc("pollEvents", window_poll_events) + ); + lua_setfield(L, honey_index, "window"); +} + + +int window_create(lua_State *L) +{ + lua_Integer width, height; + char *title; + hs_parse_args(L, hs_int(width), hs_int(height), hs_str(title)); + + GLFWwindow *win = glfwCreateWindow(width, height, title, NULL, NULL); + if (win == NULL) + hs_throw_error(L, "failed to create window"); + lua_pushlightuserdata(L, win); + return 1; +} + + +int window_destroy(lua_State *L) { - if (!glfwInit()) { - hs_throw_error(L, "failed to initialize GLFW"); - } + void *ptr; + hs_parse_args(L, hs_light(ptr)); + GLFWwindow *win = ptr; + glfwDestroyWindow(win); return 0; } -int gl_terminate(lua_State *L) +int window_should_close(lua_State *L) +{ + void *ptr; + hs_parse_args(L, hs_light(ptr)); + GLFWwindow *win = ptr; + lua_pushboolean(L, glfwWindowShouldClose(win)); + return 1; +} + + +int window_poll_events(lua_State *L) { - glfwTerminate(); + glfwPollEvents(); return 0; } diff --git a/src/logging/logging.c b/src/logging/logging.c index 255da4c..db4611a 100644 --- a/src/logging/logging.c +++ b/src/logging/logging.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdarg.h> #include "logging/logging.h" int _honey_log_level = HONEY_WARN; @@ -0,0 +1,32 @@ +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> +#include <honeysuckle.h> +#include "gl/gl.h" + + +int main(int argc, char **argv) +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_createtable(L, 0, 2); + int honey_index = lua_gettop(L); + setup_gl(L, honey_index); + setup_window(L, honey_index); + lua_setglobal(L, "honey"); + + int err = luaL_loadfile(L, "honey.lua"); + if (err != 0) { + printf("cannot open file!\n"); + lua_close(L); + return 0; + } + err = hs_call(L, 0, 0); + if (err != 0) { + const char *err_str = lua_tostring(L, -1); + printf("failed to run: %s\n", err_str); + } + lua_close(L); + return 0; +} diff --git a/src/test/honey-test.h b/src/test/honey-test.h index 5ac5b8f..87eb5e6 100644 --- a/src/test/honey-test.h +++ b/src/test/honey-test.h @@ -29,10 +29,11 @@ void suite_logging(); +void suite_gl(); void suite_window(); #define RUN_TESTS() \ lily_run_suite(suite_logging); \ - lily_run_suite(suite_window); \ + lily_run_suite(suite_gl); \ #endif |