From 275536b36657744d802866c060654e2b5cd5a5f8 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 18 Aug 2022 22:46:40 -0500 Subject: implement working windows --- src/gl/gl.c | 34 +++++++++++++++ src/gl/gl.h | 9 ++++ src/gl/gl.test.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/gl/window.c | 56 ++++++++++++++++++++++--- src/gl/window.test.c | 114 --------------------------------------------------- 5 files changed, 207 insertions(+), 120 deletions(-) create mode 100644 src/gl/gl.c create mode 100644 src/gl/gl.h create mode 100644 src/gl/gl.test.c delete mode 100644 src/gl/window.test.c (limited to 'src/gl') 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 +#include +#include + + +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 + +void setup_gl(lua_State *L, int honey_index); +void setup_window(lua_State *L, int honey_index); + +#endif diff --git a/src/gl/gl.test.c b/src/gl/gl.test.c new file mode 100644 index 0000000..9bacbf9 --- /dev/null +++ b/src/gl/gl.test.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include "test/honey-test.h" + + +int mock_glfwInit_(void); +int mock_hs_throw_error_(lua_State *L, const char *str); +void mock_glfwTerminate_(); + +#define glfwInit mock_glfwInit_ +#define hs_throw_error mock_hs_throw_error_ +#define glfwTerminate mock_glfwTerminate_ +#include "gl/gl.c" +#undef glfwTerminate +#undef hs_throw_error +#undef glfwInit + + +lily_mock_t *mock_glfwInit = NULL; +int mock_glfwInit_() +{ + struct lily_mock_arg_t args[] = {}; + lily_mock_store_call(mock_glfwInit, args); + + int result; + lily_get_value(mock_glfwInit, int, &result); + return result; +} + + +lily_mock_t *mock_hs_throw_error = NULL; +int mock_hs_throw_error_(lua_State *L, const char *str) +{ + struct lily_mock_arg_t args[] = { + { sizeof(const char *), &str } + }; + lily_mock_store_call(mock_hs_throw_error, args); + + lua_pushstring(L, "some error"); + lua_error(L); + + return 0; +} + + +lily_mock_t *mock_glfwTerminate = NULL; +void mock_glfwTerminate_() +{ + struct lily_mock_arg_t args[] = {}; + lily_mock_store_call(mock_glfwTerminate, args); +} + + +/* ~~~~~~~~ suite ~~~~~~~~ */ + +void gl_init_succeeds() +{ + lily_mock_use(&mock_glfwInit); + lily_mock_use(&mock_hs_throw_error); + + lua_State *L = luaL_newstate(); + lily_store_value(mock_glfwInit, int, 1); + lua_pushcfunction(L, gl_init); + int err = lua_pcall(L, 0, 0, 0); + lua_close(L); + + lily_assert_int_equal(err, 0); + lily_assert_int_equal(mock_glfwInit->n_calls, 1); + lily_assert_int_equal(mock_hs_throw_error->n_calls, 0); +} + + +void gl_init_fails() +{ + lily_mock_use(&mock_glfwInit); + lily_mock_use(&mock_hs_throw_error); + + lua_State *L = luaL_newstate(); + lily_store_value(mock_glfwInit, int, 0); + lua_pushcfunction(L, gl_init); + int err = lua_pcall(L, 0, 0, 0); + lua_close(L); + + lily_assert_int_equal(err, LUA_ERRRUN); + lily_assert_int_equal(mock_hs_throw_error->n_calls, 1); +} + + +void gl_terminate_works() +{ + lily_mock_use(&mock_glfwTerminate); + + lua_State *L = luaL_newstate(); + lua_pushcfunction(L, gl_terminate); + int err = lua_pcall(L, 0, 0, 0); + lua_close(L); + + lily_assert_int_equal(err, 0); + lily_assert_int_equal(mock_glfwTerminate->n_calls, 1); +} + + +void suite_gl() +{ + lily_run_test(gl_init_succeeds); + lily_run_test(gl_init_fails); + lily_run_test(gl_terminate_works); + + lily_mock_destroy(mock_glfwInit); + lily_mock_destroy(mock_hs_throw_error); + lily_mock_destroy(mock_glfwTerminate); +} 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 #include -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/gl/window.test.c b/src/gl/window.test.c deleted file mode 100644 index b4fb5d4..0000000 --- a/src/gl/window.test.c +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include -#include -#include -#include "test/honey-test.h" - - -int mock_glfwInit_(void); -int mock_hs_throw_error_(lua_State *L, const char *str); -void mock_glfwTerminate_(); - -#define glfwInit mock_glfwInit_ -#define hs_throw_error mock_hs_throw_error_ -#define glfwTerminate mock_glfwTerminate_ -#include "gl/window.c" -#undef glfwTerminate -#undef hs_throw_error -#undef glfwInit - - -lily_mock_t *mock_glfwInit = NULL; -int mock_glfwInit_() -{ - struct lily_mock_arg_t args[] = {}; - lily_mock_store_call(mock_glfwInit, args); - - int result; - lily_get_value(mock_glfwInit, int, &result); - return result; -} - - -lily_mock_t *mock_hs_throw_error = NULL; -int mock_hs_throw_error_(lua_State *L, const char *str) -{ - struct lily_mock_arg_t args[] = { - { sizeof(const char *), &str } - }; - lily_mock_store_call(mock_hs_throw_error, args); - - lua_pushstring(L, "some error"); - lua_error(L); - - return 0; -} - - -lily_mock_t *mock_glfwTerminate = NULL; -void mock_glfwTerminate_() -{ - struct lily_mock_arg_t args[] = {}; - lily_mock_store_call(mock_glfwTerminate, args); -} - - -/* ~~~~~~~~ suite ~~~~~~~~ */ - -void gl_init_succeeds() -{ - lily_mock_use(&mock_glfwInit); - lily_mock_use(&mock_hs_throw_error); - - lua_State *L = luaL_newstate(); - lily_store_value(mock_glfwInit, int, 1); - lua_pushcfunction(L, gl_init); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, 0); - lily_assert_int_equal(mock_glfwInit->n_calls, 1); - lily_assert_int_equal(mock_hs_throw_error->n_calls, 0); -} - - -void gl_init_fails() -{ - lily_mock_use(&mock_glfwInit); - lily_mock_use(&mock_hs_throw_error); - - lua_State *L = luaL_newstate(); - lily_store_value(mock_glfwInit, int, 0); - lua_pushcfunction(L, gl_init); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, LUA_ERRRUN); - lily_assert_int_equal(mock_hs_throw_error->n_calls, 1); -} - - -void gl_terminate_works() -{ - lily_mock_use(&mock_glfwTerminate); - - lua_State *L = luaL_newstate(); - lua_pushcfunction(L, gl_terminate); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, 0); - lily_assert_int_equal(mock_glfwTerminate->n_calls, 1); -} - - -void suite_window() -{ - lily_run_test(gl_init_succeeds); - lily_run_test(gl_init_fails); - lily_run_test(gl_terminate_works); - - lily_mock_destroy(mock_glfwInit); - lily_mock_destroy(mock_hs_throw_error); - lily_mock_destroy(mock_glfwTerminate); -} -- cgit v1.2.1