diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-25 11:09:47 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-25 11:09:47 -0500 |
commit | 8dbfbdc929c2321f23b50754eda8fbcdba00ad03 (patch) | |
tree | eed76380aaf660f0e7910e987579ca6f1e920525 | |
parent | 1a55ea2d22436359f8e11061e203cf9e8849a114 (diff) |
move main loop into honey_run
-rw-r--r-- | src/honey.c | 67 | ||||
-rw-r--r-- | src/honey.h | 5 | ||||
-rw-r--r-- | src/main.c | 62 |
3 files changed, 64 insertions, 70 deletions
diff --git a/src/honey.c b/src/honey.c index c3b4856..da55ca6 100644 --- a/src/honey.c +++ b/src/honey.c @@ -80,20 +80,71 @@ bool honey_setup(lua_State** L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void honey_run(honey_window window) { - /*float prevTime = 0; +bool honey_run(lua_State* L, honey_options opts) { + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); + honey_window_information* info = lua_touserdata(L, -1); + honey_window window = info->window; + + 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 false; + } + + if (luaL_loadfile(L, script) == 0) { + if (!honey_lua_pcall(L, 0, 1) == 0) { + const char* error = lua_tostring(L, -1); + fprintf(stderr, "[honey] ERROR: %s\n", error); + return false; + } + } + else { + fprintf(stderr, "ERROR: failed to open %s!\n", script); + return false; + } + + int update_callback = honey_get_callback(L, "update"); + int draw_callback = honey_get_callback(L, "draw"); + + float prevTime = 0; float currentTime = 0; float dt; - float draw_dt = 0; - - while(!glfwWindowShouldClose(window)) { + + while (!glfwWindowShouldClose(window)) { currentTime = (float) glfwGetTime(); dt = currentTime - prevTime; prevTime = currentTime; + glfwPollEvents(); + + if (update_callback != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, update_callback); + lua_pushnumber(L, dt); + int result = honey_lua_pcall(L, 1, 0); + if (result != 0) { + const char* error = lua_tostring(L, -1); + fprintf(stderr, "[honey] ERROR: %s\n", error); + glfwSetWindowShouldClose(window, true); + } + } - honey_update_callback(dt); - honey_draw_callback(); - }*/ + if (draw_callback != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback); + int result = honey_lua_pcall(L, 0, 0); + if (result != 0) { + const char* error = lua_tostring(L, -1); + fprintf(stderr, "[honey] ERROR: %s\n", error); + glfwSetWindowShouldClose(window, true); + } + } + } + + lua_close(L); + + glfwTerminate(); + return true; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ diff --git a/src/honey.h b/src/honey.h index 974136a..caa92e7 100644 --- a/src/honey.h +++ b/src/honey.h @@ -53,9 +53,10 @@ bool honey_setup(lua_State** L); /** @brief The main game loop. * - * @param[in] window The window the game is running in, created with honey_setup() + * @param[in] L The lua state honey was initialized in. + * @param[in] opts The honey_options struct previously populated by honey_parse_options(). */ -void honey_run(honey_window window); +bool honey_run(lua_State* L, honey_options opts); /** @brief Get a registry reference to a given honey callback. * @@ -12,68 +12,10 @@ int main(int argc, char** argv) if (!honey_setup(&L)) return 1; - lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref); - honey_window_information* info = lua_touserdata(L, -1); - honey_window window = info->window; + bool success = honey_run(L, opts); - 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!"); + if (!success) return 1; - } - - if (luaL_loadfile(L, script) == 0) { - if (!honey_lua_pcall(L, 0, 1) == 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", script); - return 1; - } - - int update_callback = honey_get_callback(L, "update"); - int draw_callback = honey_get_callback(L, "draw"); - - float prevTime = 0; - float currentTime = 0; - float dt; - - while (!glfwWindowShouldClose(window)) { - currentTime = (float) glfwGetTime(); - dt = currentTime - prevTime; - prevTime = currentTime; - glfwPollEvents(); - - if (update_callback != LUA_NOREF) { - lua_rawgeti(L, LUA_REGISTRYINDEX, update_callback); - lua_pushnumber(L, dt); - int result = honey_lua_pcall(L, 1, 0); - if (result != 0) { - const char* error = lua_tostring(L, -1); - fprintf(stderr, "[honey] ERROR: %s\n", error); - glfwSetWindowShouldClose(window, true); - } - } - - if (draw_callback != LUA_NOREF) { - lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback); - int result = honey_lua_pcall(L, 0, 0); - if (result != 0) { - const char* error = lua_tostring(L, -1); - fprintf(stderr, "[honey] ERROR: %s\n", error); - glfwSetWindowShouldClose(window, true); - } - } - } - - lua_close(L); - glfwTerminate(); return 0; } |