window resizing
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 2 Nov 2021 18:49:01 +0000 (13:49 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 2 Nov 2021 18:49:01 +0000 (13:49 -0500)
src/gfx.c
src/heartbreak_system.c
tests/simp.onyx

index 9d1ea2c400a85886f07bf27a65e9f6857b51159e..f81ffa70da2ed8ae27bce6c3ce259444b4f07f84 100644 (file)
--- 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
index a0a386168fddd54a47a68afb521f68b2a5c37ed5..eebbf162db7bf240ee62d18581792300274db94c 100644 (file)
@@ -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);
index 7181bcb15199a061c71d970e86f0784216785072..fcd539745920b2007e8480f104bef5908fb498af 100644 (file)
@@ -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