From ecde0c8110da1c1f94cce7f6a1fa9406d04eca69 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 19 Aug 2022 00:39:04 -0500 Subject: add several buffer-related functions --- CMakeLists.txt | 4 +++- src/gl/gl.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/gl/gl.test.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 99964c3..2da7f20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ add_subdirectory(${LIB_ROOT}/cglm) set(HONEY_LIB_FILES ${SRC_ROOT}/logging/logging.c + ${SRC_ROOT}/gl/glad/glad.c ${SRC_ROOT}/gl/gl.c ${SRC_ROOT}/gl/window.c ) @@ -59,6 +60,7 @@ set(TEST_SOURCES ${SRC_ROOT}/test/honey-test.c ${SRC_ROOT}/logging/logging.test.c + ${SRC_ROOT}/gl/glad/glad.c ${SRC_ROOT}/gl/gl.test.c ) @@ -66,4 +68,4 @@ add_executable(test EXCLUDE_FROM_ALL ${TEST_SOURCES}) set_target_properties(test PROPERTIES C_STANDARD 99 CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") -target_link_libraries(test lua5.1 honeysuckle) +target_link_libraries(test lua5.1 honeysuckle dl) diff --git a/src/gl/gl.c b/src/gl/gl.c index 8613e70..7b07027 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -1,18 +1,36 @@ +#include #include "gl/glad/glad.h" #include #include #include +/* needs to be here because glad uses macros to define glBufferData */ +#ifdef HONEY_TEST_H +#undef glBufferData +#define glBufferData mock_glBufferData_ +#endif + int gl_init(lua_State *L); int gl_terminate(lua_State *L); +int gl_create_buffer(lua_State *L); +int gl_bind_buffer(lua_State *L); +int gl_buffer_data(lua_State *L); void setup_gl(lua_State *L, int honey_index) { + int buffer_binding_targets = hs_create_table(L, + hs_str_int("arrayBuffer", GL_ARRAY_BUFFER), + ); + hs_create_table(L, hs_str_cfunc("init", gl_init), - hs_str_cfunc("terminate", gl_terminate) + hs_str_cfunc("terminate", gl_terminate), + hs_str_cfunc("createBuffer", gl_create_buffer), + hs_str_cfunc("bindBuffer", gl_bind_buffer), + hs_str_tbl("bufferTarget", buffer_binding_targets), + hs_str_cfunc("bufferData", gl_buffer_data), ); lua_setfield(L, honey_index, "gl"); } @@ -32,3 +50,48 @@ int gl_terminate(lua_State *L) glfwTerminate(); return 0; } + + +int gl_create_buffer(lua_State *L) +{ + int buf; + glGenBuffers(1, &buf); + lua_pushinteger(L, buf); + return 1; +} + + +int gl_bind_buffer(lua_State *L) +{ + lua_Integer buf, target; + hs_parse_args(L, hs_int(buf), hs_int(target)); + glBindBuffer(buf, target); + return 0; +} + + +int gl_buffer_data(lua_State *L) +{ + lua_Integer target, usage; + int table; + hs_parse_args(L, hs_int(target), hs_tbl(table), hs_int(usage)); + + /* build raw buffer */ + size_t len = lua_objlen(L, table); + lua_Number *buf = malloc(len * sizeof(lua_Number)); + if (buf == NULL) + hs_throw_error(L, "failed to allocate intermediary buffer"); + for (int i=0; in_calls, 1); + int target; size_t size; int usage; + struct lily_mock_arg_t args[] = { + { sizeof(int), &target }, + { sizeof(size_t), &size }, + { sizeof(int), &usage } + }; + lily_mock_get_call(mock_glBufferData, args, 0); + lily_assert_int_equal(target, GL_ARRAY_BUFFER); + lily_assert_int_equal(size, 3*sizeof(lua_Number)); + lily_assert_int_equal(usage, GL_STATIC_DRAW); + + lua_Number n; + lily_get_value(mock_glBufferData, lua_Number, &n); + lily_assert_float_equal(n, 22, 0.1); + lily_get_value(mock_glBufferData, lua_Number, &n); + lily_assert_float_equal(n, 33, 0.1); + lily_get_value(mock_glBufferData, lua_Number, &n); + lily_assert_float_equal(n, 44, 0.1); +} + + void suite_gl() { lily_run_test(gl_init_succeeds); lily_run_test(gl_init_fails); lily_run_test(gl_terminate_works); + lily_run_test(gl_buffer_data_works); lily_mock_destroy(mock_glfwInit); lily_mock_destroy(mock_hs_throw_error); lily_mock_destroy(mock_glfwTerminate); + lily_mock_destroy(mock_glBufferData); } -- cgit v1.2.1