input module and starting physics
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Dec 2021 16:41:24 +0000 (10:41 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Dec 2021 16:41:24 +0000 (10:41 -0600)
src/input.onyx
src/main.onyx
src/physics.onyx
src/player.onyx [new file with mode: 0644]
src/world.onyx

index c33f6cbb0a4c2d0ed86c2fa0f9b448d832e6aecf..a9a173a4a94134d48a2eeed994f9f325a991c686 100644 (file)
@@ -2,42 +2,63 @@ use package core
 use package glfw3
 
 #local {
+       keys_this_frame: [..] u32
        keys_last_frame: [..] u32 
-       last_buttons: [3] bool
-}
-
-handle_key_event :: (key, scancode, action, mod: u32) {
-       switch action {
-               case GLFW_PRESS, GLFW_REPEAT {
 
-               }
-
-               case GLFW_RELEASE {
-
-               }
-       }       
+       buttons_this_frame: [8] bool
+       buttons_last_frame: [8] bool
 }
 
-is_key_down :: (key: u32) {
-
+input_update :: () {
+    glfwGetCursorPos(window, ^mouse_x, ^mouse_y);
 }
 
-is_key_just_down :: (key: u32) {
+input_post_update :: () {
+       array.clear(^keys_last_frame);
+       for keys_this_frame do keys_last_frame << it;
 
+       for 8 do buttons_last_frame[it] = buttons_this_frame[it];
+
+       last_mouse_x = mouse_x;
+       last_mouse_y = mouse_y;
 }
 
-is_key_just_up :: (key: u32) {
+handle_key_event :: (key, scancode, action, mod: u32) {
+       if action == GLFW_PRESS {
+               keys_this_frame << key;
+       }
 
+       if action == GLFW_RELEASE {
+               array.remove(^keys_this_frame, key);
+       }
 }
 
+is_key_down      :: (key) => array.contains(keys_this_frame, key);
+is_key_just_down :: (key) => array.contains(keys_this_frame, key) && !array.contains(keys_last_frame, key);
+is_key_just_up   :: (key) => !array.contains(keys_this_frame, key) && array.contains(keys_last_frame, key);
+
 handle_button_event :: (button, action, mod: u32) {
+       if action == GLFW_PRESS {
+               buttons_this_frame[button] = true;
+       }
 
+       if action == GLFW_RELEASE {
+               buttons_this_frame[button] = false;
+       }
 }
 
-is_button_down :: (button: u32) {
+is_button_down      :: (button) => buttons_this_frame[button];
+is_button_just_down :: (button) => buttons_this_frame[button] && !buttons_last_frame[button];
+is_button_just_up   :: (button) => !buttons_this_frame[button] && buttons_last_frame[button];
 
-}
+#local {
+       last_mouse_x: f64;
+       last_mouse_y: f64;
 
-is_button_just_down :: (button: u32) {
-       
+       mouse_x: f64;
+       mouse_y: f64;
 }
+
+mouse_get_delta :: () -> (f64, f64) {
+       return mouse_x - last_mouse_x, mouse_y - last_mouse_y;
+}
\ No newline at end of file
index 44881a00f2c96c6ae3e5d37d0f266e0e2038253e..32effe83f99db953f0f6646ff1335db3a0ff1b63 100644 (file)
@@ -11,10 +11,11 @@ use package core.intrinsics.onyx { __initialize }
     world_shader: Shader;
     font: Font;
     selected_block: Vector3i;
-
-    window: GLFWwindow_p;
 }
 
+@GlobalVariable
+window: GLFWwindow_p;
+
 create_window :: () => {
     #if runtime.OS == runtime.OS_Linux {
         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -45,10 +46,12 @@ create_window :: () => {
         if cursor_grabbed do toggle_cursor_grabbed();
         else do glfwSetWindowShouldClose(window, true);
     }
+
+    handle_key_event(key, scancode, action, mod);
 }
 
 #export "on_mouse_button" (window: GLFWwindow_p, button, action, mod: u32) {
-
+    handle_button_event(button, action, mod);
 }
 
 cursor_grabbed := false;
@@ -109,31 +112,25 @@ setup_opengl :: () {
 }
 
 update :: (dt: f32) {
-    #persist last_mouse_x: f64;
-    #persist last_mouse_y: f64;
-
-    mx, my: f64;
-    glfwGetCursorPos(window, ^mx, ^my);
-    defer {
-        last_mouse_x = mx;
-        last_mouse_y = my;
-    }
+    input_update();
+    defer input_post_update();
 
     if !cursor_grabbed {
-        if glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_TRUE {
+        if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) {
             toggle_cursor_grabbed();
         }
         return;
     }
 
-    camera.y_rot += ~~(last_mouse_x - mx) / 400.0f;
-    camera.x_rot += ~~(my - last_mouse_y) / 400.0f;
+    mdx, mdy := mouse_get_delta();
+    camera.y_rot += ~~(-mdx / 400);
+    camera.x_rot += ~~( mdy / 400);
     while camera.y_rot >= 2 * math.PI do camera.y_rot -= 2 * math.PI;
     while camera.y_rot < 0 do camera.y_rot += 2 * math.PI;
     camera.x_rot = math.clamp(camera.x_rot, -math.PI / 2 + 0.1, math.PI / 2 - 0.1);
 
     speed := 7 * dt;
-    if glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_LEFT_CONTROL) {
         speed = 15 * dt;
     }
 
@@ -141,16 +138,16 @@ update :: (dt: f32) {
     facing := forward;
     facing.y = 0;
     facing = Vector3.norm(facing);
-    if glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_W) {
         camera.position += facing * speed;
     }
-    if glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_S) {
         camera.position -= facing * speed;
     }
-    if glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_D) {
         camera.position += Vector3.norm(Vector3.cross(facing, .{0,1,0})) * speed;
     }
-    if glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_A) {
         camera.position -= Vector3.norm(Vector3.cross(facing, .{0,1,0})) * speed;
     }
 
@@ -161,25 +158,20 @@ update :: (dt: f32) {
         selected_block = .{0,0,0};
     }
 
-    if glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_SPACE) {
         camera.position.y += 7 * dt;
     }
-    if glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS {
+    if is_key_down(GLFW_KEY_LEFT_SHIFT) {
         camera.position.y -= 7 * dt;
     }
 
-    if glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS {
+    if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) {
         world_set_block(world, selected_block.x, selected_block.y, selected_block.z, block_make(0,0,0,1));
     }
     
 
-    {
-        #persist last_debug_key := 0;
-        debug_key := glfwGetKey(window, GLFW_KEY_F7);
-        if debug_key == GLFW_RELEASE && last_debug_key == GLFW_PRESS {
-            debug_screen = !debug_screen;
-        }
-        last_debug_key = debug_key;
+    if is_key_just_down(GLFW_KEY_F7) {
+        debug_screen = !debug_screen;
     }
 
     update_world_matrix();
index cf7f9caf0e070f29ae703857e0aae80ee69682c7..bb905b0accc04dedac6496f95d5f556f522c98ac 100644 (file)
@@ -1,5 +1,21 @@
 use package core
 
+AABB :: struct {
+    x0, y0, z0, x1, y1, z1: f32;
+
+    intersects :: (a1, a2: AABB) -> bool {
+        return a1.x0 < a2.x1 && a1.x1 > a2.x0
+            && a1.y0 < a2.y1 && a1.y1 > a2.y0
+            && a1.z0 < a2.z1 && a1.z1 > a2.z0;
+    }
+
+    contains :: (a: AABB, v: Vector3) -> bool {
+        return a.x0 < v.x && v.x < a.x1
+            && a.y0 < v.y && v.y < a.y1
+            && a.z0 < v.z && v.z < a.z1;
+    }
+}
+
 PhysicsBody :: struct {
        pos: Vector3;
        vel: Vector3;
diff --git a/src/player.onyx b/src/player.onyx
new file mode 100644 (file)
index 0000000..c90c898
--- /dev/null
@@ -0,0 +1,9 @@
+
+Player :: struct {
+    camera: ^Camera; // Should the camera exist on the player? Or should the player just control the camera?
+    body:   PhysicsBody;
+}
+
+player_update :: (player: ^Player) {
+
+}
\ No newline at end of file
index 81796ff79314173fe267ef04546facfcc364c643..a12bc2be0fde436cbedc0683018405417ef6d94c 100644 (file)
@@ -52,23 +52,6 @@ world_set_block :: (world: ^World, x, y, z: i32, block: Block) {
     chunk_set(chunk, x % Chunk_Size, y % Chunk_Size, z % Chunk_Size, block);
 }
 
-@Relocate // to a utils file
-AABB :: struct {
-    x0, y0, z0, x1, y1, z1: f32;
-
-    intersects :: (a1, a2: AABB) -> bool {
-        return a1.x0 < a2.x1 && a1.x1 > a2.x0
-            && a1.y0 < a2.y1 && a1.y1 > a2.y0
-            && a1.z0 < a2.z1 && a1.z1 > a2.z0;
-    }
-
-    contains :: (a: AABB, v: Vector3) -> bool {
-        return a.x0 < v.x && v.x < a.x1
-            && a.y0 < v.y && v.y < a.y1
-            && a.z0 < v.z && v.z < a.z1;
-    }
-}
-
 world_get_aabbs :: (use world: ^World, center: Vector3, radius: f32, buffer: [] AABB = .[]) -> [] AABB {
     is_dynamic := buffer.count == 0;
     aabbs: [..] AABB;