From 3a14bf97b30e35bf46b8faf7bcc10d0c86539514 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 30 Dec 2021 10:41:24 -0600 Subject: [PATCH] input module and starting physics --- src/input.onyx | 61 ++++++++++++++++++++++++++++++++---------------- src/main.onyx | 52 +++++++++++++++++------------------------ src/physics.onyx | 16 +++++++++++++ src/player.onyx | 9 +++++++ src/world.onyx | 17 -------------- 5 files changed, 88 insertions(+), 67 deletions(-) create mode 100644 src/player.onyx diff --git a/src/input.onyx b/src/input.onyx index c33f6cb..a9a173a 100644 --- a/src/input.onyx +++ b/src/input.onyx @@ -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 diff --git a/src/main.onyx b/src/main.onyx index 44881a0..32effe8 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -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(); diff --git a/src/physics.onyx b/src/physics.onyx index cf7f9ca..bb905b0 100644 --- a/src/physics.onyx +++ b/src/physics.onyx @@ -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 index 0000000..c90c898 --- /dev/null +++ b/src/player.onyx @@ -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 diff --git a/src/world.onyx b/src/world.onyx index 81796ff..a12bc2b 100644 --- a/src/world.onyx +++ b/src/world.onyx @@ -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; -- 2.25.1