From dccc9bef56d51c5cad13c2568c5a3b97f637fd6e Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 10:45:52 -0500 Subject: refactor: move drawing code into separate file --- src/gl/drawing.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gl/gl.c | 65 +-------------------------------------------------- src/gl/gl.h | 2 ++ src/gl/gl.test.c | 4 ++++ 4 files changed, 78 insertions(+), 64 deletions(-) create mode 100644 src/gl/drawing.c (limited to 'src') diff --git a/src/gl/drawing.c b/src/gl/drawing.c new file mode 100644 index 0000000..a64f56d --- /dev/null +++ b/src/gl/drawing.c @@ -0,0 +1,71 @@ +#include "gl/glad/glad.h" +#include +#include +#include + +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); + +void setup_drawing(lua_State *L, int gl_index) +{ + int primitive_types = hs_create_table(L, + hs_str_int("points", GL_POINTS), + hs_str_int("lines", GL_LINES), + hs_str_int("triangles", GL_TRIANGLES), + ); + + int buffer_masks = hs_create_table(L, + hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT), + hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT), + hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT), + ); + + hs_create_table(L, + 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), + ); + + lua_setfield(L, gl_index, "draw"); +} + +int gl_set_clear_color(lua_State *L) +{ + lua_Number r, g, b, a; + hs_parse_args(L, hs_num(r), hs_num(g), hs_num(b), hs_num(a)); + glClearColor(r, g, b, a); + return 0; +} + + +int gl_clear(lua_State *L) +{ + lua_Integer mask; + hs_parse_args(L, hs_int(mask)); + glClear(mask); + return 0; +} + + +int gl_draw_arrays(lua_State *L) +{ + lua_Integer mode, first, count; + hs_parse_args(L, hs_int(mode), hs_int(first), hs_int(count)); + 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.c b/src/gl/gl.c index d0fb33c..876df9d 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -28,13 +28,6 @@ int gl_vertex_array_bind(lua_State *L); int gl_vertex_attrib_pointer(lua_State *L); int gl_vertex_array_enable_attrib(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); - - void setup_gl(lua_State *L, int honey_index) { int data_types = hs_create_table(L, @@ -62,18 +55,6 @@ void setup_gl(lua_State *L, int honey_index) hs_str_int("dynamicDraw", GL_DYNAMIC_DRAW), ); - int primitive_types = hs_create_table(L, - hs_str_int("points", GL_POINTS), - hs_str_int("lines", GL_LINES), - hs_str_int("triangles", GL_TRIANGLES), - ); - - int buffer_masks = hs_create_table(L, - hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT), - hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT), - hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT), - ); - int gl_index = hs_create_table(L, hs_str_cfunc("init", gl_init), hs_str_cfunc("initGlad", glad_init), @@ -95,18 +76,10 @@ void setup_gl(lua_State *L, int honey_index) hs_str_tbl("bufferTarget", buffer_binding_targets), hs_str_tbl("bufferUsage", buffer_usage_patterns), - - /* drawing */ - 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), ); setup_shader(L, gl_index); + setup_drawing(L, gl_index); lua_setfield(L, honey_index, "gl"); } @@ -248,39 +221,3 @@ int gl_vertex_array_enable_attrib(lua_State *L) glEnableVertexAttribArray(index); return 0; } - - -int gl_set_clear_color(lua_State *L) -{ - lua_Number r, g, b, a; - hs_parse_args(L, hs_num(r), hs_num(g), hs_num(b), hs_num(a)); - glClearColor(r, g, b, a); - return 0; -} - - -int gl_clear(lua_State *L) -{ - lua_Integer mask; - hs_parse_args(L, hs_int(mask)); - glClear(mask); - return 0; -} - - -int gl_draw_arrays(lua_State *L) -{ - lua_Integer mode, first, count; - hs_parse_args(L, hs_int(mode), hs_int(first), hs_int(count)); - 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.h b/src/gl/gl.h index b209957..f384c54 100644 --- a/src/gl/gl.h +++ b/src/gl/gl.h @@ -5,6 +5,8 @@ void setup_gl(lua_State *L, int honey_index); void setup_shader(lua_State *L, int gl_index); +void setup_drawing(lua_State *L, int gl_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 index bab8529..363036a 100644 --- a/src/gl/gl.test.c +++ b/src/gl/gl.test.c @@ -10,11 +10,15 @@ int mock_hs_throw_error_(lua_State *L, const char *str, ...); void mock_glfwTerminate_(); void mock_glBufferData_(int, size_t, const void *, int); +#define DUMMY_FUNCTION(...) + #define glfwInit mock_glfwInit_ #define hs_throw_error mock_hs_throw_error_ #define glfwTerminate mock_glfwTerminate_ #undef glBufferData #define glBufferData mock_glBufferData_ +#define setup_shader DUMMY_FUNCTION +#define setup_drawing DUMMY_FUNCTION #include "gl/gl.c" #undef glBufferData #undef glfwTerminate -- cgit v1.2.1