diff options
Diffstat (limited to 'src/gl/shader.c')
-rw-r--r-- | src/gl/shader.c | 95 |
1 files changed, 46 insertions, 49 deletions
diff --git a/src/gl/shader.c b/src/gl/shader.c index 412fe2b..d78b1ef 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -1,7 +1,8 @@ +#include <stdbool.h> #include <glad/glad.h> #include <GLFW/glfw3.h> #include <lua.h> -#include <honeysuckle.h> +#include <lauxlib.h> #include "util/util.h" int gl_create_shader(lua_State *L); @@ -25,41 +26,42 @@ int gl_uniform_matrix_4fv(lua_State *L); void setup_shader(lua_State *L, int gl_index) { - int tbl = hs_create_table(L, + struct honey_tbl_t tbl[] = { /* functions */ - hs_str_cfunc("CreateShader", gl_create_shader), - hs_str_cfunc("ShaderSource", gl_shader_set_source), - hs_str_cfunc("CompileShader", gl_shader_compile), - hs_str_cfunc("DeleteShader", gl_shader_delete), + H_FUNC("CreateShader", gl_create_shader), + H_FUNC("ShaderSource", gl_shader_set_source), + H_FUNC("CompileShader", gl_shader_compile), + H_FUNC("DeleteShader", gl_shader_delete), - hs_str_cfunc("CreateProgram", gl_program_create), - hs_str_cfunc("AttachShader", gl_program_attach_shader), - hs_str_cfunc("LinkProgram", gl_program_link), - hs_str_cfunc("UseProgram", gl_program_use), + H_FUNC("CreateProgram", gl_program_create), + H_FUNC("AttachShader", gl_program_attach_shader), + H_FUNC("LinkProgram", gl_program_link), + H_FUNC("UseProgram", gl_program_use), - hs_str_cfunc("GetUniformLocation", gl_uniform_get_location), - hs_str_cfunc("Uniform1i", gl_uniform_1i), - hs_str_cfunc("Uniform1f", gl_uniform_1i), - hs_str_cfunc("Uniform3f", gl_uniform_1i), - hs_str_cfunc("Uniform4f", gl_uniform_4f), + H_FUNC("GetUniformLocation", gl_uniform_get_location), + H_FUNC("Uniform1i", gl_uniform_1i), + H_FUNC("Uniform1f", gl_uniform_1i), + H_FUNC("Uniform3f", gl_uniform_1i), + H_FUNC("Uniform4f", gl_uniform_4f), - hs_str_cfunc("UniformMatrix4fv", gl_uniform_matrix_4fv), + H_FUNC("UniformMatrix4fv", gl_uniform_matrix_4fv), /******** enums ********/ /* shader types */ - hs_str_int("VERTEX_SHADER", GL_VERTEX_SHADER), - hs_str_int("FRAGMENT_SHADER", GL_FRAGMENT_SHADER), - ); + H_INT("VERTEX_SHADER", GL_VERTEX_SHADER), + H_INT("FRAGMENT_SHADER", GL_FRAGMENT_SHADER), - append_table(L, gl_index, tbl); + H_END + }; + create_table(L, tbl); + append_table(L, gl_index, lua_gettop(L)); lua_pop(L, 1); } int gl_create_shader(lua_State *L) { - lua_Integer type; - hs_parse_args(L, hs_int(type)); + lua_Integer type = luaL_checkinteger(L, 1); lua_Integer shader = glCreateShader(type); lua_pushinteger(L, shader); return 1; @@ -68,9 +70,8 @@ int gl_create_shader(lua_State *L) int gl_shader_set_source(lua_State *L) { - lua_Integer shader; - char *code; - hs_parse_args(L, hs_int(shader), hs_str(code)); + lua_Integer shader = luaL_checkinteger(L, 1); + const char *code = luaL_checkstring(L, 2); glShaderSource(shader, 1, (const GLchar * const*)&code, NULL); return 0; } @@ -78,14 +79,13 @@ int gl_shader_set_source(lua_State *L) int gl_shader_compile(lua_State *L) { - lua_Integer shader; - hs_parse_args(L, hs_int(shader)); + lua_Integer shader = luaL_checkinteger(L, 1); glCompileShader(shader); int success; char log[1024]; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(shader, 1024, NULL, log); - hs_throw_error(L, "shader compilation failed: %s", log); + luaL_error(L, "shader compilation failed: %s", log); } return 0; } @@ -93,8 +93,7 @@ int gl_shader_compile(lua_State *L) int gl_shader_delete(lua_State *L) { - lua_Integer shader; - hs_parse_args(L, hs_int(shader)); + lua_Integer shader = luaL_checkinteger(L, 1); glDeleteShader(shader); return 0; } @@ -111,7 +110,8 @@ int gl_program_create(lua_State *L) int gl_program_attach_shader(lua_State *L) { lua_Integer program, shader; - hs_parse_args(L, hs_int(program), hs_int(shader)), + program = luaL_checkinteger(L, 1); + shader = luaL_checkinteger(L, 2); glAttachShader(program, shader); return 0; } @@ -119,14 +119,13 @@ int gl_program_attach_shader(lua_State *L) int gl_program_link(lua_State *L) { - lua_Integer program; - hs_parse_args(L, hs_int(program)); + lua_Integer program = luaL_checkinteger(L, 1); glLinkProgram(program); int success; char log[1024]; glGetProgramiv(program, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(program, 1024, NULL, log); - hs_throw_error(L, "shader linking failed: %s", log); + luaL_error(L, "shader linking failed: %s", log); } return 0; } @@ -134,8 +133,7 @@ int gl_program_link(lua_State *L) int gl_program_use(lua_State *L) { - lua_Integer program; - hs_parse_args(L, hs_int(program)); + lua_Integer program = luaL_checkinteger(L, 1); glUseProgram(program); return 0; } @@ -143,9 +141,8 @@ int gl_program_use(lua_State *L) int gl_uniform_get_location(lua_State *L) { - lua_Integer program; - char *name; - hs_parse_args(L, hs_int(program), hs_str(name)); + lua_Integer program = luaL_checkinteger(L, 1); + const char *name = luaL_checkstring(L, 2); int location = glGetUniformLocation(program, (const GLchar*)name); lua_pushinteger(L, location); return 1; @@ -155,7 +152,8 @@ int gl_uniform_get_location(lua_State *L) int gl_uniform_1i(lua_State *L) { lua_Integer location, v0; - hs_parse_args(L, hs_int(location), hs_int(v0)); + location = luaL_checkinteger(L, 1); + v0 = luaL_checkinteger(L, 2); glUniform1i(location, v0); return 0; } @@ -183,9 +181,11 @@ int gl_uniform_3f(lua_State *L) int gl_uniform_4f(lua_State *L) { - lua_Integer location; - lua_Number v0, v1, v2, v3; - hs_parse_args(L, hs_int(location), hs_num(v0), hs_num(v1), hs_num(v2), hs_num(v3)); + lua_Integer location = luaL_checkinteger(L, 1); + lua_Number v0 = luaL_checknumber(L, 2); + lua_Number v1 = luaL_checknumber(L, 3); + lua_Number v2 = luaL_checknumber(L, 4); + lua_Number v3 = luaL_checknumber(L, 5); glUniform4f(location, v0, v1, v2, v3); return 0; } @@ -193,12 +193,9 @@ int gl_uniform_4f(lua_State *L) int gl_uniform_matrix_4fv(lua_State *L) { - lua_Integer location; - bool transpose; - void *ptr; - hs_parse_args(L, hs_int(location), hs_bool(transpose), hs_user(ptr)); - - float *value = ptr; + lua_Integer location = luaL_checkinteger(L, 1); + bool transpose = lua_toboolean(L, 2); + float *value = lua_touserdata(L, 3); glUniformMatrix4fv(location, 1, transpose, value); return 0; } |