diff options
author | sanine <sanine.not@pm.me> | 2022-08-24 00:02:17 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-08-24 00:02:17 -0500 |
commit | 2cb3c3df4099297b0a0554bb482e2de04fe86b5c (patch) | |
tree | 7796b4064c16460d9d603707b5256027649aa8b6 /src | |
parent | 709e1b6e1ce86f8da4fc136747fcefbc6c6057bd (diff) |
add command-line arguments
Diffstat (limited to 'src')
-rw-r--r-- | src/gl/gl.c | 25 | ||||
-rw-r--r-- | src/glm/glm.c | 1 | ||||
-rw-r--r-- | src/main.c | 18 | ||||
-rw-r--r-- | src/options/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/options/options.c | 56 | ||||
-rw-r--r-- | src/options/options.h | 16 |
6 files changed, 119 insertions, 2 deletions
diff --git a/src/gl/gl.c b/src/gl/gl.c index 4639d48..106befe 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -17,6 +17,8 @@ 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); +int gl_enable(lua_State *L); +int gl_disable(lua_State *L); void setup_gl(lua_State *L, int honey_index) { @@ -26,6 +28,8 @@ void setup_gl(lua_State *L, int honey_index) hs_str_cfunc("InitGlad", glad_init), hs_str_cfunc("Terminate", gl_terminate), hs_str_cfunc("GetError", gl_get_error), + hs_str_cfunc("Enable", gl_enable), + hs_str_cfunc("Disable", gl_disable), /******** enums ********/ /* data types */ @@ -41,6 +45,9 @@ void setup_gl(lua_State *L, int honey_index) hs_str_int("INVALID_OPERATION", GL_INVALID_OPERATION), hs_str_int("INVALID_FRAMEBUFFER_OPERATION", GL_INVALID_FRAMEBUFFER_OPERATION), hs_str_int("OUT_OF_MEMORY", GL_OUT_OF_MEMORY), + + /* opengl capabilities */ + hs_str_int("DEPTH_TEST", GL_DEPTH_TEST), ); setup_shader(L, gl_index); @@ -81,3 +88,21 @@ int gl_get_error(lua_State *L) lua_pushinteger(L, glGetError()); return 1; } + + +int gl_enable(lua_State *L) +{ + lua_Integer cap; + hs_parse_args(L, hs_int(cap)); + glEnable(cap); + return 0; +} + + +int gl_disable(lua_State *L) +{ + lua_Integer cap; + hs_parse_args(L, hs_int(cap)); + glDisable(cap); + return 0; +} diff --git a/src/glm/glm.c b/src/glm/glm.c index 525a029..ca6238a 100644 --- a/src/glm/glm.c +++ b/src/glm/glm.c @@ -1,5 +1,6 @@ #include <lua.h> #include <honeysuckle.h> +#include "glm.h" void setup_glm(lua_State *L, int honey_index) @@ -5,13 +5,22 @@ #include "gl/gl.h" #include "image/image.h" #include "glm/glm.h" +#include "options/options.h" int main(int argc, char **argv) { + /* parse command-line options */ + struct honey_options options; + int result = parse_options(&options, argc, argv); + if (result == EXIT_FAILURE) return 1; + else if (result == EXIT_SUCCESS) return 0; + + /* set up lua state */ lua_State *L = luaL_newstate(); luaL_openlibs(L); + /* load honey bindings */ lua_createtable(L, 0, 2); int honey_index = lua_gettop(L); setup_gl(L, honey_index); @@ -20,17 +29,22 @@ int main(int argc, char **argv) setup_glm(L, honey_index); lua_setglobal(L, "honey"); - int err = luaL_loadfile(L, "honey.lua"); + /* load main script */ + int err = luaL_loadfile(L, options.script_file); if (err != 0) { - printf("cannot open file!\n"); + printf("cannot open file '%s'\n", options.script_file); lua_close(L); return 0; } + + /* run */ err = hs_call(L, 0, 0); if (err != 0) { const char *err_str = lua_tostring(L, -1); printf("failed to run: \n%s\n", err_str); } + + /* clean up */ lua_close(L); return 0; } diff --git a/src/options/CMakeLists.txt b/src/options/CMakeLists.txt new file mode 100644 index 0000000..39ed7db --- /dev/null +++ b/src/options/CMakeLists.txt @@ -0,0 +1,5 @@ +project(honey_engine) + +target_sources(honey PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/options.c +) diff --git a/src/options/options.c b/src/options/options.c new file mode 100644 index 0000000..4488837 --- /dev/null +++ b/src/options/options.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <cargs.h> +#include "options.h" + +static struct cag_option opts[] = { + { + .identifier = 's', + .access_letters = "s", + .access_name = "script", + .value_name = "SCRIPT_FILE", + .description = "The filename of the main script. (default: main.lua)" + }, + { + .identifier = 'h', + .access_letters = "h", + .access_name = "help", + .value_name = NULL, + .description = "Shows this help message" + }, +}; + + +void print_help(char *program_name) +{ + printf("usage: %s [OPTIONS]\n", program_name); + cag_option_print(opts, CAG_ARRAY_SIZE(opts), stdout); +} + + +enum outcomes_t parse_options(struct honey_options *options, int argc, char **argv) +{ + /* default values */ + options->script_file = "main.lua"; + + /* parse options */ + char id; + const char *value; + cag_option_context context; + + cag_option_prepare(&context, opts, CAG_ARRAY_SIZE(opts), argc, argv); + while(cag_option_fetch(&context)) { + id = cag_option_get(&context); + switch(id) { + case 's': + options->script_file = cag_option_get_value(&context); + break; + case 'h': + print_help(argv[0]); + return EXIT_SUCCESS; + default: + return EXIT_FAILURE; + } + } + + return CONTINUE_SUCCESS; +} diff --git a/src/options/options.h b/src/options/options.h new file mode 100644 index 0000000..cb07b1a --- /dev/null +++ b/src/options/options.h @@ -0,0 +1,16 @@ +#ifndef HONEY_OPTIONS_H +#define HONEY_OPTIONS_H + +struct honey_options { + const char *script_file; // main entry point +}; + +enum outcomes_t { + CONTINUE_SUCCESS, + EXIT_SUCCESS, + EXIT_FAILURE, +}; + +enum outcomes_t parse_options(struct honey_options *options, int argc, char **argv); + +#endif |