diff options
author | sanine <sanine.not@pm.me> | 2022-08-18 22:03:13 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-18 22:03:13 -0500 |
commit | 253f1d1ca8b4b81f206e4aeb20afe440a6dae8be (patch) | |
tree | d4f7d2d470b956b73f61111f78e199d24e170654 /src/gl | |
parent | a1ac349c22f43ece1dcad7b21ed82dbb434cbf06 (diff) |
add glfwInit and glfwTerminate bindings
Diffstat (limited to 'src/gl')
-rw-r--r-- | src/gl/window.c | 93 | ||||
-rw-r--r-- | src/gl/window.test.c | 446 |
2 files changed, 66 insertions, 473 deletions
diff --git a/src/gl/window.c b/src/gl/window.c index e16b330..bc654f9 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -3,102 +3,17 @@ #include <lua.h> #include <honeysuckle.h> -/* build a table of all possible window hints */ -void create_glfw_window_hints_table(lua_State *L) -{ - /* hint keys */ - hs_create_table(L, - /* window hints */ - hs_str_int("resizable", GLFW_RESIZABLE), - hs_str_int("visible", GLFW_VISIBLE), - hs_str_int("decorated", GLFW_DECORATED), - hs_str_int("focused", GLFW_FOCUSED), - hs_str_int("autoIconify", GLFW_AUTO_ICONIFY), - hs_str_int("floating", GLFW_FLOATING), - hs_str_int("maximized", GLFW_MAXIMIZED), - hs_str_int("centerCursor", GLFW_CENTER_CURSOR), - hs_str_int("transparentFramebuffer", GLFW_TRANSPARENT_FRAMEBUFFER), - hs_str_int("focusOnShow", GLFW_FOCUS_ON_SHOW), - hs_str_int("scaleToMonitor", GLFW_SCALE_TO_MONITOR), - - /* framebuffer hints */ - hs_str_int("redBits", GLFW_RED_BITS), - hs_str_int("greenBits", GLFW_GREEN_BITS), - hs_str_int("blueBits", GLFW_BLUE_BITS), - hs_str_int("alphaBits", GLFW_ALPHA_BITS), - hs_str_int("depthBits", GLFW_DEPTH_BITS), - hs_str_int("stereoscopic", GLFW_STEREO), - hs_str_int("samples", GLFW_SAMPLES), - hs_str_int("srgbCapable", GLFW_SRGB_CAPABLE), - hs_str_int("doubleBuffer", GLFW_DOUBLEBUFFER), - - /* monitor & context hints */ - hs_str_int("refreshRate", GLFW_REFRESH_RATE), - hs_str_int("clientApi", GLFW_CLIENT_API), - hs_str_int("contextCreationApi", GLFW_CONTEXT_CREATION_API), - hs_str_int("contextVersionMajor", GLFW_CONTEXT_VERSION_MAJOR), - hs_str_int("contextVersionMinor", GLFW_CONTEXT_VERSION_MINOR), - hs_str_int("forwardCompatible", GLFW_OPENGL_FORWARD_COMPAT), - hs_str_int("debugContext", GLFW_OPENGL_DEBUG_CONTEXT), - hs_str_int("profile", GLFW_OPENGL_PROFILE), - hs_str_int("contextRobustness", GLFW_CONTEXT_ROBUSTNESS), - hs_str_int("contextReleaseBehavior", GLFW_CONTEXT_RELEASE_BEHAVIOR), - hs_str_int("noError", GLFW_CONTEXT_NO_ERROR) - ); - - /* special hint values */ - hs_create_table(L, - hs_str_int("dontCare", GLFW_DONT_CARE), - - /* client api */ - hs_str_int("glApi", GLFW_OPENGL_API), - hs_str_int("glesApi", GLFW_OPENGL_ES_API), - hs_str_int("noApi", GLFW_NO_API), - - /* context api */ - hs_str_int("nativeApi", GLFW_NATIVE_CONTEXT_API), - hs_str_int("eglApi", GLFW_EGL_CONTEXT_API), - hs_str_int("osMesaApi", GLFW_OSMESA_CONTEXT_API), - - /* robustness */ - hs_str_int("noRobustness", GLFW_NO_ROBUSTNESS), - hs_str_int("noResetNotification", GLFW_NO_RESET_NOTIFICATION), - hs_str_int("loseContextOnReset", GLFW_LOSE_CONTEXT_ON_RESET), - - /* context release */ - hs_str_int("anyBehavior", GLFW_ANY_RELEASE_BEHAVIOR), - hs_str_int("flush", GLFW_RELEASE_BEHAVIOR_FLUSH), - hs_str_int("none", GLFW_RELEASE_BEHAVIOR_NONE), - - /* profile */ - hs_str_int("anyProfile", GLFW_OPENGL_ANY_PROFILE), - hs_str_int("compatabilityProfile", GLFW_OPENGL_COMPAT_PROFILE), - hs_str_int("coreProfile", GLFW_OPENGL_CORE_PROFILE) - ); -} - - int gl_init(lua_State *L) { - if (glfwInit() != GLFW_TRUE) { + if (!glfwInit()) { hs_throw_error(L, "failed to initialize GLFW"); } return 0; } -lua_Integer tointeger(lua_State *L, int index) +int gl_terminate(lua_State *L) { - if (lua_isboolean(L, index)) { - return lua_toboolean(L, index); - } - else if (lua_isnumber(L, index)) { - return lua_tointeger(L, index); - } - else { - hs_throw_error(L, - "expected boolean or number; got %s instead", - lua_typename(L, lua_type(L, index)) - ); - } + glfwTerminate(); + return 0; } diff --git a/src/gl/window.test.c b/src/gl/window.test.c index 6b8e091..b4fb5d4 100644 --- a/src/gl/window.test.c +++ b/src/gl/window.test.c @@ -4,433 +4,111 @@ #include <honeysuckle.h> #include "test/honey-test.h" -int mock_glfwInit(); -int mock_hs_throw_error(lua_State *L, const char *format_string, ...); -#define glfwInit mock_glfwInit -#define hs_throw_error mock_hs_throw_error + +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 glfwInit +#undef glfwTerminate #undef hs_throw_error +#undef glfwInit -lily_mock_t *mock_glfwInit_data = NULL; -int mock_glfwInit() +lily_mock_t *mock_glfwInit = NULL; +int mock_glfwInit_() { - lily_mock_call(mock_glfwInit_data, NULL); + struct lily_mock_arg_t args[] = {}; + lily_mock_store_call(mock_glfwInit, args); int result; - mock_dequeue(mock_glfwInit, int, &result); + lily_get_value(mock_glfwInit, int, &result); return result; } -lily_mock_t *mock_hs_throw_error_data = NULL; -int mock_hs_throw_error(lua_State *L, const char *format_string, ...) -{ - /* to avoid basically just re-implementing printf parsing here, - i am limiting this function to be able to receive strings only */ - - /* count format specifiers */ - char *ptr = strchr(format_string, '%'); - int n_args = 0; - while (ptr != NULL) { - n_args += 1; - ptr = strchr(ptr+1, '%'); - } - /* store arguments */ +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*), &format_string }, - { sizeof(int), &n_args }, + { sizeof(const char *), &str } }; - lily_mock_call(mock_hs_throw_error_data, args); + lily_mock_store_call(mock_hs_throw_error, args); - /* store format arguments */ - va_list vl; - va_start(vl, format_string); - for (int i=0; i<n_args; i++) { - char *str = va_arg(vl, char*); - mock_enqueue(mock_hs_throw_error, char*, str); - } -} + lua_pushstring(L, "some error"); + lua_error(L); + return 0; +} -/* ~~~~~~~~ TESTS ~~~~~~~~ */ -void gl_init_succeeds(); -void gl_init_fail_glfwInit(); -void glfw_window_hints_table(); -void tointeger_parses_bool(); -void tointeger_parses_int(); -void tointeger_fails_other(); -void suite_window() +lily_mock_t *mock_glfwTerminate = NULL; +void mock_glfwTerminate_() { - lily_run_test(gl_init_succeeds); - lily_run_test(gl_init_fail_glfwInit); - lily_run_test(glfw_window_hints_table); - lily_run_test(tointeger_parses_bool); - lily_run_test(tointeger_parses_int); - lily_run_test(tointeger_fails_other); - - CLEAN_MOCK(mock_glfwInit); - CLEAN_MOCK(mock_hs_throw_error); + struct lily_mock_arg_t args[] = {}; + lily_mock_store_call(mock_glfwTerminate, args); } -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - +/* ~~~~~~~~ suite ~~~~~~~~ */ void gl_init_succeeds() { - USE_MOCK(mock_glfwInit); - USE_MOCK(mock_hs_throw_error); - - /* queue success */ - mock_enqueue(mock_glfwInit, int, GLFW_TRUE); - gl_init(NULL); + lily_mock_use(&mock_glfwInit); + lily_mock_use(&mock_hs_throw_error); - lily_assert_int_equal(mock_glfwInit_data->n_calls, 1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 0); -} - - -void gl_init_fail_glfwInit() -{ - USE_MOCK(mock_glfwInit); - USE_MOCK(mock_hs_throw_error); - - /* queue failure */ - mock_enqueue(mock_glfwInit, int, GLFW_FALSE); - gl_init(NULL); - - lily_assert_int_equal(mock_glfwInit_data->n_calls, 1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 1); - - const char *fmt; int argc; - struct lily_mock_arg_t args[] = { - { sizeof(const char*), &fmt }, - { sizeof(int), &argc }, - }; - lily_get_call(mock_hs_throw_error_data, args, 0); + 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_string_equal((char*) fmt, "failed to initialize GLFW"); + 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); } -int get_int(lua_State *L, int table_index, const char *key) +void gl_init_fails() { - lua_getfield(L, table_index, key); - lily_assert_msg(lua_isnumber(L, -1), LILY_LOCATION, "key %s is not a number", key); - int n = lua_tointeger(L, -1); - lua_pop(L, 1); - return n; -} - -#define CHECK_VALUE(str, num) \ - do { \ - int value = get_int(L, 1, str); \ - lily_assert_int_equal(value, num); \ - } while(0) + lily_mock_use(&mock_glfwInit); + lily_mock_use(&mock_hs_throw_error); -void glfw_window_hints_table() -{ lua_State *L = luaL_newstate(); - - lily_assert_int_equal(lua_gettop(L), 0); - create_glfw_window_hints_table(L); - lily_assert_int_equal(lua_gettop(L), 2); - lily_assert_true(lua_istable(L, 1)); - - /* window hints */ - lily_assert_int_equal( - get_int(L, 1, "resizable"), - GLFW_RESIZABLE - ); - - lily_assert_int_equal( - get_int(L, 1, "visible"), - GLFW_VISIBLE - ); - - lily_assert_int_equal( - get_int(L, 1, "decorated"), - GLFW_DECORATED - ); - - lily_assert_int_equal( - get_int(L, 1, "focused"), - GLFW_FOCUSED - ); - - lily_assert_int_equal( - get_int(L, 1, "autoIconify"), - GLFW_AUTO_ICONIFY - ); - - lily_assert_int_equal( - get_int(L, 1, "floating"), - GLFW_FLOATING - ); - - lily_assert_int_equal( - get_int(L, 1, "maximized"), - GLFW_MAXIMIZED - ); - - lily_assert_int_equal( - get_int(L, 1, "centerCursor"), - GLFW_CENTER_CURSOR - ); - - lily_assert_int_equal( - get_int(L, 1, "transparentFramebuffer"), - GLFW_TRANSPARENT_FRAMEBUFFER - ); - - lily_assert_int_equal( - get_int(L, 1, "focusOnShow"), - GLFW_FOCUS_ON_SHOW - ); - - lily_assert_int_equal( - get_int(L, 1, "scaleToMonitor"), - GLFW_SCALE_TO_MONITOR - ); - - - /* framebuffer hints */ - /* (don't expose accumulation or auxiliary buffer hints) */ - lily_assert_int_equal( - get_int(L, 1, "redBits"), - GLFW_RED_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "greenBits"), - GLFW_GREEN_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "blueBits"), - GLFW_BLUE_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "alphaBits"), - GLFW_ALPHA_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "depthBits"), - GLFW_DEPTH_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "stereoscopic"), - GLFW_STEREO - ); - - lily_assert_int_equal( - get_int(L, 1, "samples"), - GLFW_SAMPLES - ); - - lily_assert_int_equal( - get_int(L, 1, "srgbCapable"), - GLFW_SRGB_CAPABLE - ); - - lily_assert_int_equal( - get_int(L, 1, "doubleBuffer"), - GLFW_DOUBLEBUFFER - ); - - - /* monitor & context hints */ - lily_assert_int_equal( - get_int(L, 1, "refreshRate"), - GLFW_REFRESH_RATE - ); - - lily_assert_int_equal( - get_int(L, 1, "clientApi"), - GLFW_CLIENT_API - ); - - lily_assert_int_equal( - get_int(L, 1, "contextCreationApi"), - GLFW_CONTEXT_CREATION_API - ); - - lily_assert_int_equal( - get_int(L, 1, "contextVersionMajor"), - GLFW_CONTEXT_VERSION_MAJOR - ); - - lily_assert_int_equal( - get_int(L, 1, "contextVersionMinor"), - GLFW_CONTEXT_VERSION_MINOR - ); - - lily_assert_int_equal( - get_int(L, 1, "forwardCompatible"), - GLFW_OPENGL_FORWARD_COMPAT - ); - - lily_assert_int_equal( - get_int(L, 1, "debugContext"), - GLFW_OPENGL_DEBUG_CONTEXT - ); - - lily_assert_int_equal( - get_int(L, 1, "profile"), - GLFW_OPENGL_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 1, "contextRobustness"), - GLFW_CONTEXT_ROBUSTNESS - ); - - lily_assert_int_equal( - get_int(L, 1, "contextReleaseBehavior"), - GLFW_CONTEXT_RELEASE_BEHAVIOR - ); - - lily_assert_int_equal( - get_int(L, 1, "noError"), - GLFW_CONTEXT_NO_ERROR - ); - - - /* special hint values */ - - lily_assert_int_equal( - get_int(L, 2, "dontCare"), - GLFW_DONT_CARE - ); - - /* client api */ - lily_assert_int_equal( - get_int(L, 2, "glApi"), - GLFW_OPENGL_API - ); - - lily_assert_int_equal( - get_int(L, 2, "glesApi"), - GLFW_OPENGL_ES_API - ); - - lily_assert_int_equal( - get_int(L, 2, "noApi"), - GLFW_NO_API - ); - - /* context api */ - lily_assert_int_equal( - get_int(L, 2, "nativeApi"), - GLFW_NATIVE_CONTEXT_API - ); - - lily_assert_int_equal( - get_int(L, 2, "eglApi"), - GLFW_EGL_CONTEXT_API - ); - - lily_assert_int_equal( - get_int(L, 2, "osMesaApi"), - GLFW_OSMESA_CONTEXT_API - ); - - /* robustness */ - lily_assert_int_equal( - get_int(L, 2, "noRobustness"), - GLFW_NO_ROBUSTNESS - ); - - lily_assert_int_equal( - get_int(L, 2, "noResetNotification"), - GLFW_NO_RESET_NOTIFICATION - ); - - lily_assert_int_equal( - get_int(L, 2, "loseContextOnReset"), - GLFW_LOSE_CONTEXT_ON_RESET - ); - - /* release */ - lily_assert_int_equal( - get_int(L, 2, "anyBehavior"), - GLFW_ANY_RELEASE_BEHAVIOR - ); - - lily_assert_int_equal( - get_int(L, 2, "flush"), - GLFW_RELEASE_BEHAVIOR_FLUSH - ); - - lily_assert_int_equal( - get_int(L, 2, "none"), - GLFW_RELEASE_BEHAVIOR_NONE - ); - - /* profile */ - lily_assert_int_equal( - get_int(L, 2, "anyProfile"), - GLFW_OPENGL_ANY_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 2, "compatabilityProfile"), - GLFW_OPENGL_COMPAT_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 2, "coreProfile"), - GLFW_OPENGL_CORE_PROFILE - ); - + lily_store_value(mock_glfwInit, int, 0); + lua_pushcfunction(L, gl_init); + int err = lua_pcall(L, 0, 0, 0); lua_close(L); -} - -void tointeger_parses_bool() -{ - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); - - lua_pushboolean(L, 0); - lily_assert_false(lua_toboolean(L, -1)); - lily_assert_int_equal(tointeger(L, -1), 0); - - lua_pushboolean(L, 1); - lily_assert_true(lua_toboolean(L, -1)); - lily_assert_int_equal(tointeger(L, -1), 1); - - lua_close(L); + lily_assert_int_equal(err, LUA_ERRRUN); + lily_assert_int_equal(mock_hs_throw_error->n_calls, 1); } -void tointeger_parses_int() +void gl_terminate_works() { - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); - - lua_pushinteger(L, 234); - lua_pushinteger(L, 55555); - - lily_assert_int_equal(tointeger(L, -2), 234); - lily_assert_int_equal(tointeger(L, -1), 55555); + 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 tointeger_fails_other() +void suite_window() { - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); + lily_run_test(gl_init_succeeds); + lily_run_test(gl_init_fails); + lily_run_test(gl_terminate_works); - lua_pushstring(L, "hey there babe"); - tointeger(L, -1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 1); + lily_mock_destroy(mock_glfwInit); + lily_mock_destroy(mock_hs_throw_error); + lily_mock_destroy(mock_glfwTerminate); } |