diff options
author | sanine-a <sanine.not@pm.me> | 2020-05-20 22:40:44 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-05-20 22:40:44 -0500 |
commit | c3a41085272644709d891bf04137a6c2bd901230 (patch) | |
tree | 24a6b7e82af426428741f3f9cf2976509e9769c4 | |
parent | c6ec37cd355b313083af5be3435162224020fe5e (diff) |
add overridable update() and draw() callbacks
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | demo.c | 105 | ||||
-rw-r--r-- | include/honey.h | 17 | ||||
-rw-r--r-- | src/honey.c | 66 | ||||
-rw-r--r-- | src/honey_setup.c | 28 | ||||
-rw-r--r-- | src/run_callbacks.c | 3 |
6 files changed, 141 insertions, 80 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8bf34bb..c78eafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ add_executable(honey_engine_demo demo.c) set(CMAKE_C_FLAGS "-g") -add_library(honey src/honey_setup.c src/texture.c src/shader.c src/mesh.c) +add_library(honey src/honey.c src/texture.c src/shader.c src/mesh.c) add_library(glad src/glad.c) add_library(stb_image src/stb_image.c) @@ -1,5 +1,7 @@ #include "include/honey.h" +honey_window window; + unsigned int screen_width = 640; unsigned int screen_height = 480; @@ -13,6 +15,13 @@ float cameraPitch = 0; float cameraYaw = 0; const float cameraMouseSensitivity = 0.1; +honey_mesh cube; +honey_shader shader; +honey_texture container; +honey_texture happy_face; + +mat4 model, view, projection; + bool wireframe = false; bool fKeyDown = false; @@ -56,7 +65,11 @@ void mouseCallback(GLFWwindow* window, double x, double y) { /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void processInput(GLFWwindow* window, float dt) { +void update(float dt) { + glfwPollEvents(); + + glm_rotate_x(model, glm_rad(10*dt), model); + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } @@ -96,27 +109,57 @@ void processInput(GLFWwindow* window, float dt) { } } -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void draw() { + glClearColor(0.4f, 0.4f, 0.4f, 1.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + if (wireframe) { + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + } + else { + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + + glm_perspective_default(((float)screen_width)/screen_height, projection); + honey_shader_set_matrix_4fv(shader, "projection", (float*) projection); + + vec3 cameraDirection; + glm_vec3_add(cameraPosition, cameraFacing, cameraDirection); + glm_lookat(cameraPosition, cameraDirection, cameraUp, view); + honey_shader_set_matrix_4fv(shader, "view", (float*) view); + + honey_shader_set_matrix_4fv(shader, "model", (float*) model); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, container.texture_id); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, happy_face.texture_id); + + honey_mesh_draw(cube, shader); + + glfwSwapBuffers(window); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int main() { - honey_window window = honey_setup(screen_width, screen_height, "hello, world!"); + window = honey_setup(screen_width, screen_height, "hello, world!"); honey_set_resize_callback(window, framebufferResizeCallback); honey_set_mouse_move_callback(window, mouseCallback); - /* load box texture */ - honey_texture box; - if (honey_texture_new(&box, "container.jpg", false) != TEXTURE_OK) { + /* load container texture */ + if (honey_texture_new(&container, "container.jpg", false) != TEXTURE_OK) { return 1; } /* load happy face texture */ - honey_texture happy_face; if (honey_texture_new(&happy_face, "happy.png", true) != TEXTURE_OK) { return 1; } - honey_shader shader; if (honey_shader_load(&shader, "demo.vs", "demo.fs") != SHADER_OK) { return 1; } @@ -146,7 +189,6 @@ int main() { 0, 1, 4, 1, 4, 5 }; - honey_mesh cube; unsigned int attribute_sizes[] = { 3, 3, 2 }; /* position, color, texture coordinate */ enum honey_mesh_result result = honey_mesh_new(&cube, vertices, 8, 3, attribute_sizes, @@ -160,7 +202,6 @@ int main() { honey_shader_set_int(shader, "boxTexture", 0); honey_shader_set_int(shader, "happyTexture", 1); - mat4 model, view, projection; glm_mat4_identity(model); glm_rotate_x(model, glm_rad(-55), model); honey_shader_set_matrix_4fv(shader, "model", (float*) model); @@ -177,48 +218,10 @@ int main() { /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - float prevTime = 0; - float currentTime = 0; - float dt = 0; + honey_set_update_callback(&update); + honey_set_draw_callback(&draw); - while(!glfwWindowShouldClose(window)) { - currentTime = (float) glfwGetTime(); - dt = currentTime - prevTime; - prevTime = currentTime; - - processInput(window, dt); - - glClearColor(0.4f, 0.4f, 0.4f, 1.0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - if (wireframe) { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } - else { - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - } - - glm_perspective_default(((float)screen_width)/screen_height, projection); - honey_shader_set_matrix_4fv(shader, "projection", (float*) projection); - - vec3 cameraDirection; - glm_vec3_add(cameraPosition, cameraFacing, cameraDirection); - glm_lookat(cameraPosition, cameraDirection, cameraUp, view); - honey_shader_set_matrix_4fv(shader, "view", (float*) view); - - glm_rotate_x(model, glm_rad(10*dt), model); - honey_shader_set_matrix_4fv(shader, "model", (float*) model); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, box.texture_id); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, happy_face.texture_id); - - honey_mesh_draw(cube, shader); - - glfwSwapBuffers(window); - glfwPollEvents(); - } + honey_run(window); honey_mesh_delete(cube); honey_shader_delete(shader); diff --git a/include/honey.h b/include/honey.h index 9b048c0..7ae7789 100644 --- a/include/honey.h +++ b/include/honey.h @@ -1,6 +1,7 @@ #ifndef HONEY_ENGINE_H #define HONEY_ENGINE_H +/** @file Defines the basic loading and callback functions. */ #include "common.h" #include "mesh.h" #include "shader.h" @@ -8,8 +9,24 @@ typedef GLFWwindow* honey_window; +/** @brief Initialize Honey. + * + * @param[in] screen_width The desired width of the screen in pixels + * @param[in] screen_height The desired height of the screen in pixels + * @param[in] window_title Title to use for the window. + */ honey_window honey_setup(int screen_width, int screen_height, char* window_title); +static void (*honey_update_callback)(float dt); +static void (*honey_draw_callback)(); +static float honey_draw_dt = 0.16; + +void honey_set_update_callback(void (*update_callback)(float)); +void honey_set_draw_callback(void (*draw_callback)()); +void honey_set_fps(unsigned int); + +void honey_run(honey_window window); + #define honey_set_resize_callback glfwSetFramebufferSizeCallback #define honey_set_mouse_move_callback glfwSetCursorPosCallback 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" + + |