From f835c917ca3b4eafba5fecc8952844f89fbc2e9b Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 2 Nov 2021 13:49:01 -0500 Subject: [PATCH] window resizing --- src/gfx.c | 1 + src/heartbreak_system.c | 6 +++++- tests/simp.onyx | 11 +++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/gfx.c b/src/gfx.c index 9d1ea2c..f81ffa7 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -187,5 +187,6 @@ void gfx_immediate_renderer_update_window_size(ImmediateRenderer *ir, f32 width, -(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1, }; + glViewport(0, 0, width, height); glUniformMatrix4fv(ir->simple_shader.view_uniform, 1, false, camera_matrix); } \ No newline at end of file diff --git a/src/heartbreak_system.c b/src/heartbreak_system.c index a0a3861..eebbf16 100644 --- a/src/heartbreak_system.c +++ b/src/heartbreak_system.c @@ -3,6 +3,10 @@ #define HEARTBREAK_MODULE_NAME system +static void __glfw_window_size_callback(GLFWwindow *w, i32 new_width, i32 new_height) { + gfx_immediate_renderer_update_window_size(&renderer, (f32) new_width, (f32) new_height); +} + HEARTBREAK_DEF(init, (), (WASM_I32)) { if (!glfwInit()) { bh_printf("Failed to initialize GLFW.\n"); @@ -20,6 +24,7 @@ HEARTBREAK_DEF(init, (), (WASM_I32)) { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwMakeContextCurrent(glfw_window); + glfwSetWindowSizeCallback(glfw_window, __glfw_window_size_callback); glfwSwapInterval(1); @@ -27,7 +32,6 @@ HEARTBREAK_DEF(init, (), (WASM_I32)) { i32 width, height; glfwGetWindowSize(glfw_window, &width, &height); - glViewport(0, 0, width, height); gfx_immediate_renderer_update_window_size(&renderer, (f32) width, (f32) height); results->data[0] = WASM_I32_VAL(1); diff --git a/tests/simp.onyx b/tests/simp.onyx index 7181bcb..fcd5397 100644 --- a/tests/simp.onyx +++ b/tests/simp.onyx @@ -26,15 +26,22 @@ update :: (dt: f32) { } draw :: () { - hb.graphics.setClearColor(math.sin(t), 0, math.cos(t), 1); + hb.graphics.setClearColor(0.1, 0.1, 0.1, 1); hb.graphics.clear(); hb.graphics.setColor(0, 1, 0); hb.graphics.rectangle(.Fill, 0, 0, 100, 100); hb.graphics.setColor(1, 1, 0); hb.graphics.rectangle(.Fill, 100, 100, 200, 100); + + mx := hb.input.mouseGetX(); + my := hb.input.mouseGetY(); + hb.graphics.rectangle(.Fill, ~~mx, ~~my, 100, 100); + + w := cast(f32) hb.window.getWidth(); + h := cast(f32) hb.window.getHeight(); hb.graphics.setColor(1, 1, 1); - hb.graphics.rectangle(.Fill, 700, 500, 100, 100); + hb.graphics.rectangle(.Fill, w - 100, h - 100, 100, 100); } main :: (_) => hb.run(.{ init, update, draw }); \ No newline at end of file -- 2.25.1