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
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);
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;
}
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;
}
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;
}
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();
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;
--- /dev/null
+
+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
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;