summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c186
1 files changed, 37 insertions, 149 deletions
diff --git a/src/main.c b/src/main.c
index 465e746..14037fb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,144 +1,42 @@
#include "honey.h"
-struct options {
- bool verbose;
- bool run;
- bool has_update;
- bool has_draw;
- char* scriptdir;
-};
-
-bool has_function(lua_State* L, char* field)
-{
- lua_getglobal(L, "honey");
- lua_getfield(L, -1, field);
- if (lua_isfunction(L, -1))
- return true;
- return false;
-}
-
-bool parse_options(struct options* opt, int argc, char** argv)
-{
- opt->verbose = false;
- opt->run = false;
-
- opterr = 0;
- int c;
-
- while ((c = getopt(argc, argv, "vh")) != -1) {
- switch (c) {
- case 'v':
- opt->verbose = true;
- break;
-
- case 'h':
- // print help
- break;
-
- case '?':
- if (isprint (optopt))
- fprintf (stderr, "Unknown option `-%c'.\n", optopt);
- else
- fprintf (stderr,
- "Unknown option character `\\x%x'.\n",
- optopt);
- return false;
-
- default:
- return false;
- }
- }
-
- if (optind < argc) {
- opt->run = true;
- opt->scriptdir = argv[optind];
- }
-
- return true;
-}
-
-GLFWwindow* setup_window(int width, int height, char* title)
-{
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
- GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
- if (window == NULL) {
- fprintf(stderr, "ERROR: failed to create window!\n");
- glfwTerminate();
- return NULL;
- }
-
- glfwMakeContextCurrent(window);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
-
- if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
- fprintf(stderr, "ERROR: failed to initialize GLAD!\n");
- glfwTerminate();
- return NULL;
- }
-
- honey_setup_keyboard();
- glfwSetKeyCallback(window, default_honey_keyboard_callback);
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- return window;
-}
-
-// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
int main(int argc, char** argv)
{
- struct options opt;
- if (!parse_options(&opt, argc, argv)) {
- fprintf(stderr, "ERROR: failed to parse command line options!\n");
- return 1;
- }
-
- if (!opt.run)
+ honey_options opts;
+ if (!honey_parse_options(&opts, argc, argv)) {
return 0;
+ }
- printf("%d\n", LUA_NOREF);
-
- GLFWwindow* window = setup_window(480, 320, "honey-engine");
-
- lua_State* L = luaL_newstate();
- glfwSetWindowUserPointer(window, L);
- luaL_openlibs(L);
+ honey_window window;
+ lua_State* L;
- lua_createtable(L, 0, 1);
-
- honey_setup_input(L);
- lua_setfield(L, -2, "input");
+ if (!honey_setup(&L, &window))
+ return 1;
- lua_setglobal(L, "honey");
-
- size_t dirlen = strlen(opt.scriptdir) + 10;
- char* scriptfile = malloc(sizeof(char) * dirlen);
- if (scriptfile == NULL) {
- fprintf(stderr, "ERROR: failed to allocate memory for script file string!\n");
+ char* script;
+ honey_result res = honey_format_string(&script,
+ "%s/main.lua",
+ opts.script_directory);
+ if (res != HONEY_OK) {
+ fprintf(stderr, "[honey] FATAL: could not allocate space for script filename!");
return 1;
}
-
- snprintf(scriptfile, dirlen, "%s/main.lua", opt.scriptdir);
- if (luaL_loadfile(L, scriptfile) == 0) {
- if (lua_pcall(L, 0, 1, 0) == 0) {
- lua_pop(L, lua_gettop(L));
+ if (luaL_loadfile(L, script) == 0) {
+ if (!lua_pcall(L, 0, 1, 0) == 0) {
+ const char* error = lua_tostring(L, -1);
+ fprintf(stderr, "[honey] ERROR: %s\n", error);
+ return 1;
}
}
else {
- fprintf(stderr, "ERROR: failed to open %s!\n", scriptfile);
+ fprintf(stderr, "ERROR: failed to open %s!\n", script);
return 1;
}
- opt.has_update = has_function(L, "update");
- opt.has_draw = has_function(L, "draw");
-
+ int update_callback = honey_get_callback(L, "update");
+ int draw_callback = honey_get_callback(L, "draw");
+
float prevTime = 0;
float currentTime = 0;
float dt;
@@ -149,36 +47,26 @@ int main(int argc, char** argv)
prevTime = currentTime;
glfwPollEvents();
- lua_getglobal(L, "honey");
-
- if (opt.has_update) {
- lua_getfield(L, -1, "update");
- if (lua_isfunction(L, -1)) {
- lua_pushnumber(L, dt);
- int result = lua_pcall(L, 1, 0, 0);
- if (result != 0) {
- glfwSetWindowShouldClose(window, true);
- }
- }
- else {
- lua_pop(L, 1);
+ if (update_callback != LUA_NOREF) {
+ lua_rawgeti(L, LUA_REGISTRYINDEX, update_callback);
+ lua_pushnumber(L, dt);
+ int result = lua_pcall(L, 1, 0, 0);
+ if (result != 0) {
+ const char* error = lua_tostring(L, -1);
+ fprintf(stderr, "[honey] ERROR: %s\n", error);
+ glfwSetWindowShouldClose(window, true);
}
}
- if (opt.has_draw) {
- lua_getfield(L, -1, "draw");
- if (lua_isfunction(L, -1)) {
- int result = lua_pcall(L, 0, 0, 0);
- if (result != 0) {
- glfwSetWindowShouldClose(window, true);
- }
- }
- else {
- lua_pop(L, 1);
+ if (draw_callback != LUA_NOREF) {
+ lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback);
+ int result = lua_pcall(L, 0, 0, 0);
+ if (result != 0) {
+ const char* error = lua_tostring(L, -1);
+ fprintf(stderr, "[honey] ERROR: %s\n", error);
+ glfwSetWindowShouldClose(window, true);
}
}
-
- lua_pop(L, 1);
}
lua_close(L);