summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-05-20 22:40:44 -0500
committersanine-a <sanine.not@pm.me>2020-05-20 22:40:44 -0500
commitc3a41085272644709d891bf04137a6c2bd901230 (patch)
tree24a6b7e82af426428741f3f9cf2976509e9769c4 /src
parentc6ec37cd355b313083af5be3435162224020fe5e (diff)
add overridable update() and draw() callbacks
Diffstat (limited to 'src')
-rw-r--r--src/honey.c66
-rw-r--r--src/honey_setup.c28
-rw-r--r--src/run_callbacks.c3
3 files changed, 69 insertions, 28 deletions
diff --git a/src/honey.c b/src/honey.c
new file mode 100644
index 0000000..c55ddc2
--- /dev/null
+++ b/src/honey.c
@@ -0,0 +1,66 @@
+#include "include/honey.h"
+
+static void default_honey_update_callback(float dt) {}
+static void default_honey_draw_callback() {}
+
+honey_window honey_setup(int screen_width, int screen_height, char* window_title) {
+ honey_update_callback = &default_honey_update_callback;
+ honey_draw_callback = &default_honey_draw_callback;
+
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+
+ honey_window window = glfwCreateWindow(screen_width, screen_height, window_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;
+ }
+
+ return window;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_set_update_callback(void (*update_callback)(float)) {
+ honey_update_callback = update_callback;
+}
+
+void honey_set_draw_callback(void (*draw_callback)()) {
+ honey_draw_callback = draw_callback;
+}
+
+void honey_set_fps(unsigned int fps) {
+ honey_draw_dt = 1.0f / fps;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_run(honey_window window) {
+ float prevTime = 0;
+ float currentTime = 0;
+ float dt;
+ float draw_dt = 0;
+
+ while(!glfwWindowShouldClose(window)) {
+ currentTime = (float) glfwGetTime();
+ dt = currentTime - prevTime;
+ prevTime = currentTime;
+
+ honey_update_callback(dt);
+ honey_draw_callback();
+ }
+}
+
+
diff --git a/src/honey_setup.c b/src/honey_setup.c
deleted file mode 100644
index 247c325..0000000
--- a/src/honey_setup.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#include "include/honey.h"
-
-honey_window honey_setup(int screen_width, int screen_height, char* window_title) {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
- honey_window window = glfwCreateWindow(screen_width, screen_height, window_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;
- }
-
- return window;
-}
-
-
diff --git a/src/run_callbacks.c b/src/run_callbacks.c
new file mode 100644
index 0000000..f6b1379
--- /dev/null
+++ b/src/run_callbacks.c
@@ -0,0 +1,3 @@
+#include "honey.h"
+
+