diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gl/gl.c | 47 | ||||
-rw-r--r-- | src/gl/gl.test.c | 16 | ||||
-rw-r--r-- | src/gl/window.c | 72 | ||||
-rw-r--r-- | src/gl/window.test.c | 24 | ||||
-rw-r--r-- | src/test/honey-test.h | 1 |
5 files changed, 147 insertions, 13 deletions
diff --git a/src/gl/gl.c b/src/gl/gl.c index 485d412..581cf39 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -15,6 +15,7 @@ int gl_init(lua_State *L); int glad_init(lua_State *L); int gl_terminate(lua_State *L); +int gl_get_error(lua_State *L); /* buffers */ int gl_create_buffer(lua_State *L); @@ -38,6 +39,7 @@ int gl_program_link(lua_State *L); int gl_program_use(lua_State *L); /* drawing */ +int gl_set_viewport(lua_State *L); int gl_draw_arrays(lua_State *L); int gl_set_clear_color(lua_State *L); int gl_clear(lua_State *L); @@ -45,6 +47,15 @@ int gl_clear(lua_State *L); void setup_gl(lua_State *L, int honey_index) { + int error_types = hs_create_table(L, + hs_str_int("noError", GL_NO_ERROR), + hs_str_int("invalidEnum", GL_INVALID_ENUM), + hs_str_int("invalidValue", GL_INVALID_VALUE), + hs_str_int("invalidOperation", GL_INVALID_OPERATION), + hs_str_int("invalidFramebufferOperation", GL_INVALID_FRAMEBUFFER_OPERATION), + hs_str_int("outOfMemory", GL_OUT_OF_MEMORY), + ); + int buffer_binding_targets = hs_create_table(L, hs_str_int("arrayBuffer", GL_ARRAY_BUFFER), ); @@ -76,6 +87,9 @@ void setup_gl(lua_State *L, int honey_index) hs_str_cfunc("init", gl_init), hs_str_cfunc("initGlad", glad_init), hs_str_cfunc("terminate", gl_terminate), + hs_str_cfunc("getError", gl_get_error), + + hs_str_tbl("errorType", error_types), /* buffer */ hs_str_cfunc("createBuffer", gl_create_buffer), @@ -107,6 +121,7 @@ void setup_gl(lua_State *L, int honey_index) hs_str_cfunc("drawArrays", gl_draw_arrays), hs_str_cfunc("setClearColor", gl_set_clear_color), hs_str_cfunc("clear", gl_clear), + hs_str_cfunc("setViewport", gl_set_viewport), hs_str_tbl("primitiveType", primitive_types), hs_str_tbl("bufferMask", buffer_masks), @@ -139,6 +154,13 @@ int gl_terminate(lua_State *L) } +int gl_get_error(lua_State *L) +{ + lua_pushinteger(L, glGetError()); + return 1; +} + + int gl_create_buffer(lua_State *L) { int buf; @@ -150,9 +172,9 @@ int gl_create_buffer(lua_State *L) int gl_bind_buffer(lua_State *L) { - lua_Integer buf, target; - hs_parse_args(L, hs_int(buf), hs_int(target)); - glBindBuffer(buf, target); + lua_Integer target, buf; + hs_parse_args(L, hs_int(target), hs_int(buf)); + glBindBuffer(target, buf); return 0; } @@ -174,9 +196,13 @@ int gl_buffer_data(lua_State *L) hs_throw_error(L, "all table items must be numbers (failed at index %d)", i); } buf[i] = lua_tonumber(L, -1); + printf(" [%d] %f\n", i, buf[i]); lua_pop(L, 1); } + printf("target: %d\n", target); + printf("GL_ARRAY_BUFFER: %d\n", GL_ARRAY_BUFFER); + /* call */ glBufferData(target, len*sizeof(float), buf, usage); free(buf); @@ -292,9 +318,11 @@ int gl_vertex_attrib_pointer(lua_State *L) lua_Integer index, size, stride, offset; bool normalized; hs_parse_args(L, hs_int(index), hs_int(size), hs_bool(normalized), hs_int(stride), hs_int(offset)); - glVertexAttribPointer(index, size, GL_FLOAT, + printf("normalized: %d\n", normalized); + /*glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride*sizeof(float), - (void*) (offset*sizeof(float))); + (void*) (offset*sizeof(float)));*/ + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); return 0; } @@ -333,3 +361,12 @@ int gl_draw_arrays(lua_State *L) glDrawArrays(mode, first, count); return 0; } + + +int gl_set_viewport(lua_State *L) +{ + lua_Integer x, y, w, h; + hs_parse_args(L, hs_int(x), hs_int(y), hs_int(w), hs_int(h)); + glViewport(x, y, w, h); + return 0; +} diff --git a/src/gl/gl.test.c b/src/gl/gl.test.c index aa24fd2..9782caf 100644 --- a/src/gl/gl.test.c +++ b/src/gl/gl.test.c @@ -67,10 +67,10 @@ void mock_glBufferData_(int target, size_t size, const void *data, int usage) }; lily_mock_store_call(mock_glBufferData, args); - size_t count = size/sizeof(lua_Number); - lua_Number *numbers = data; + size_t count = size/sizeof(float); + float *numbers = data; for (int i=0; i<count; i++) { - lily_store_value(mock_glBufferData, lua_Number, numbers[i]); + lily_store_value(mock_glBufferData, float, numbers[i]); } } @@ -151,15 +151,15 @@ void gl_buffer_data_works() }; 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(size, 3*sizeof(float)); lily_assert_int_equal(usage, GL_STATIC_DRAW); - lua_Number n; - lily_get_value(mock_glBufferData, lua_Number, &n); + float n; + lily_get_value(mock_glBufferData, float, &n); lily_assert_float_equal(n, 22, 0.1); - lily_get_value(mock_glBufferData, lua_Number, &n); + lily_get_value(mock_glBufferData, float, &n); lily_assert_float_equal(n, 33, 0.1); - lily_get_value(mock_glBufferData, lua_Number, &n); + lily_get_value(mock_glBufferData, float, &n); lily_assert_float_equal(n, 44, 0.1); } diff --git a/src/gl/window.c b/src/gl/window.c index cb30d3f..5ed9427 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -1,31 +1,74 @@ +#include <stdlib.h> #include "gl/glad/glad.h" #include <GLFW/glfw3.h> #include <lua.h> #include <honeysuckle.h> +struct window_data { + lua_State *L; + int framebuffer_size_callback; +}; + +struct window_data * create_window_data(lua_State *L) +{ + struct window_data *wdata = malloc(sizeof(struct window_data)); + if (wdata == NULL) + return NULL; + wdata->L = L; + wdata->framebuffer_size_callback = LUA_NOREF; +} int window_create(lua_State *L); int window_destroy(lua_State *L); int window_make_context_current(lua_State *L); +int window_set_hint(lua_State *L); int window_should_close(lua_State *L); int window_poll_events(lua_State *L); int window_swap_buffers(lua_State *L); +int window_set_framebuffer_size_callback(lua_State *L); void setup_window(lua_State *L, int honey_index) { + int hint_types = hs_create_table(L, + hs_str_int("contextVersionMajor", GLFW_CONTEXT_VERSION_MAJOR), + hs_str_int("contextVersionMinor", GLFW_CONTEXT_VERSION_MINOR), + hs_str_int("openGlProfile", GLFW_OPENGL_PROFILE), + ); + + int profile_types = hs_create_table(L, + hs_str_int("openGlCoreProfile", GLFW_OPENGL_CORE_PROFILE), + ); + hs_create_table(L, hs_str_cfunc("create", window_create), hs_str_cfunc("destroy", window_destroy), hs_str_cfunc("makeContextCurrent", window_make_context_current), + hs_str_cfunc("setHint", window_set_hint), hs_str_cfunc("shouldClose", window_should_close), hs_str_cfunc("pollEvents", window_poll_events), hs_str_cfunc("swapBuffers", window_swap_buffers), + hs_str_cfunc("setFramebufferSizeCallback", window_set_framebuffer_size_callback), + + hs_str_tbl("hintType", hint_types), + hs_str_tbl("profileType", profile_types), ); lua_setfield(L, honey_index, "window"); } +static void framebuffer_size_callback_(GLFWwindow *win, int width, int height) +{ + struct window_data *wdata = glfwGetWindowUserPointer(win); + if (wdata->framebuffer_size_callback != LUA_NOREF) { + hs_rload(wdata->L, wdata->framebuffer_size_callback); + lua_pushlightuserdata(wdata->L, win); + lua_pushinteger(wdata->L, width); + lua_pushinteger(wdata->L, height); + hs_call(wdata->L, 3, 0); + } +} + int window_create(lua_State *L) { lua_Integer width, height; @@ -35,6 +78,12 @@ int window_create(lua_State *L) GLFWwindow *win = glfwCreateWindow(width, height, title, NULL, NULL); if (win == NULL) hs_throw_error(L, "failed to create window"); + + struct window_data *wdata = create_window_data(L); + glfwSetWindowUserPointer(win, wdata); + + glfwSetFramebufferSizeCallback(win, framebuffer_size_callback_); + lua_pushlightuserdata(L, win); return 1; } @@ -60,6 +109,15 @@ int window_make_context_current(lua_State *L) } +int window_set_hint(lua_State *L) +{ + lua_Integer hint, value; + hs_parse_args(L, hs_int(hint), hs_int(value)); + glfwWindowHint(hint, value); + return 0; +} + + int window_should_close(lua_State *L) { void *ptr; @@ -85,3 +143,17 @@ int window_swap_buffers(lua_State *L) glfwSwapBuffers(win); return 0; } + + +int window_set_framebuffer_size_callback(lua_State *L) +{ + void *ptr; + int func; + hs_parse_args(L, hs_light(ptr), hs_func(func)); + GLFWwindow *win = ptr; + struct window_data *wdata = glfwGetWindowUserPointer(win); + + lua_pushvalue(L, func); + wdata->framebuffer_size_callback = hs_rstore(L); + return 0; +} diff --git a/src/gl/window.test.c b/src/gl/window.test.c new file mode 100644 index 0000000..505c876 --- /dev/null +++ b/src/gl/window.test.c @@ -0,0 +1,24 @@ +#include <stdlib.h> +#include <lua.h> +#include <honeysuckle.h> +#include "test/honey-test.h" + +#include "window.c" + + +void create_window_data_works() +{ + lua_State *L = luaL_newstate(); + struct window_data *wdata = create_window_data(L); + + lily_assert_ptr_equal(L, wdata->L); + lily_assert_int_equal(wdata->framebuffer_size_callback, LUA_NOREF); + lua_close(L); + free(wdata); +} + + +void suite_window() +{ + lily_run_test(create_window_data_works); +} diff --git a/src/test/honey-test.h b/src/test/honey-test.h index 87eb5e6..30730d5 100644 --- a/src/test/honey-test.h +++ b/src/test/honey-test.h @@ -35,5 +35,6 @@ void suite_window(); #define RUN_TESTS() \ lily_run_suite(suite_logging); \ lily_run_suite(suite_gl); \ + lily_run_suite(suite_window); \ #endif |