From: Brendan Hansen Date: Wed, 29 Dec 2021 19:04:02 +0000 (-0600) Subject: random additions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ea1befae76051c331dbf8e1f01fd14b0952e91e6;p=voxel-shooter.git random additions --- diff --git a/run_tree/run.sh b/run_tree/run.sh index b4ccbc5..a9fbfea 100755 --- a/run_tree/run.sh +++ b/run_tree/run.sh @@ -1,2 +1,2 @@ -onyx build -I ../src -o game.wasm && onyxrun game.wasm +onyx build -I ../src -o game.wasm $@ && onyxrun game.wasm diff --git a/src/build.onyx b/src/build.onyx index 310b3d4..3cacaa6 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -16,6 +16,8 @@ #load "world" #load "font" #load "shader" +#load "input" +#load "physics" // Onyx library code #load "stb_truetype" diff --git a/src/camera.onyx b/src/camera.onyx index 070d41b..c8bc72d 100644 --- a/src/camera.onyx +++ b/src/camera.onyx @@ -1,5 +1,8 @@ use package core +@GlobalVariable +camera: Camera; + Camera :: struct { fov: f32; // Radians window_width, window_height: f32; diff --git a/src/chunk.onyx b/src/chunk.onyx index 12395d5..6afef32 100644 --- a/src/chunk.onyx +++ b/src/chunk.onyx @@ -158,7 +158,7 @@ block_highlight: ^Mesh(Chunk_Vertex); chunk_highlight_block :: (x, y, z: f32) { data := 0xf000; - vertex_data := cast(^Chunk_Vertex) alloca(sizeof Chunk_Vertex * 8); + vertex_data := cast(^Chunk_Vertex) alloc.from_stack(sizeof Chunk_Vertex * 8); vertex_data[0] = .{.{x-0.001,y-0.001,z-0.001},data}; vertex_data[1] = .{.{x-0.001,y+1.001,z-0.001},data}; vertex_data[2] = .{.{x+1.001,y+1.001,z-0.001},data}; diff --git a/src/font.onyx b/src/font.onyx index fca42bb..9ba493d 100644 --- a/src/font.onyx +++ b/src/font.onyx @@ -105,11 +105,6 @@ font_make :: (fd: FontDescriptor) -> Font { return font; } -alloca :: macro (size: u32) -> rawptr { - defer __stack_top = ~~(cast(^u8) __stack_top + size); - return __stack_top; -} - font_print :: (font: Font, x, y: f32, format: str, va: ..any) { buf: [1024] u8; msg := conv.str_format_va(buf, format, va); @@ -117,7 +112,7 @@ font_print :: (font: Font, x, y: f32, format: str, va: ..any) { } font_draw :: (font: Font, x, y: f32, msg: str) { - quads: ^stbtt_aligned_quad = alloca(msg.count * sizeof stbtt_aligned_quad); + quads: ^stbtt_aligned_quad = alloc.from_stack(msg.count * sizeof stbtt_aligned_quad); quad_num := 0; x_, y_ := x, y; diff --git a/src/input.onyx b/src/input.onyx new file mode 100644 index 0000000..c33f6cb --- /dev/null +++ b/src/input.onyx @@ -0,0 +1,43 @@ +use package core +use package glfw3 + +#local { + 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 { + + } + } +} + +is_key_down :: (key: u32) { + +} + +is_key_just_down :: (key: u32) { + +} + +is_key_just_up :: (key: u32) { + +} + +handle_button_event :: (button, action, mod: u32) { + +} + +is_button_down :: (button: u32) { + +} + +is_button_just_down :: (button: u32) { + +} diff --git a/src/main.onyx b/src/main.onyx index 825eed6..5467f5e 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -6,8 +6,14 @@ use package stb_truetype use package core.intrinsics.onyx { __initialize } #local runtime :: package runtime -window: GLFWwindow_p; -camera: Camera; +#local { + world: ^World; + world_shader: Shader; + font: Font; + selected_block: Vector3i; + + window: GLFWwindow_p; +} create_window :: () => { #if runtime.OS == runtime.OS_Linux { @@ -24,6 +30,7 @@ create_window :: () => { glfwSwapInterval(1); glfwSetWindowSizeCallback(window, "on_resize"); glfwSetKeyCallback(window, "on_key"); + glfwSetMouseButtonCallback(window, "on_mouse_button"); } #export "on_resize" (window: GLFWwindow_p, width, height: u32) { @@ -40,6 +47,10 @@ create_window :: () => { } } +#export "on_mouse_button" (window: GLFWwindow_p, button, action, mod: u32) { + +} + cursor_grabbed := false; toggle_cursor_grabbed :: () { cursor_grabbed = !cursor_grabbed; @@ -59,11 +70,6 @@ toggle_cursor_grabbed :: () { } } -world: ^World; -world_shader: Shader; -font: Font; -selected_block: Vector3i; - setup_opengl :: () { glInit(glfwGetLoadProcAddress()); @@ -149,7 +155,7 @@ update :: (dt: f32) { } dir: Vector3i; - if !world_ray_cast(.{ camera.position, forward }, 40, null, (_, p) => { + if !ray_cast(.{ camera.position, forward }, 40, null, (_, p) => { return world_get_block(world, p.x, p.y, p.z) != Block_Empty; }, ^selected_block, ^dir) { selected_block = .{0,0,0}; @@ -163,7 +169,7 @@ update :: (dt: f32) { } if glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS { - world_set_block(world, selected_block.x, selected_block.y, selected_block.z, Block_Empty); + world_set_block(world, selected_block.x, selected_block.y, selected_block.z, block_make(0,0,0,1)); } @@ -179,18 +185,28 @@ update :: (dt: f32) { update_world_matrix(); } -debug_screen := true; -game_fps: i32; +#local { + debug_screen := true; + game_fps: i32; +} draw :: () { - glClearColor(.7, .7, .9, 1); + // glClearColor(.7, .7, .9, 1); + glClearColor(0.1, 0.1, 0.1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader_use(world_shader); - glLineWidth(2); world_draw(world); + + glLineWidth(2); chunk_highlight_block(~~selected_block.x, ~~selected_block.y, ~~selected_block.z); + #if false { + aabb_buffer := (cast(^AABB) alloc.from_stack(sizeof [128] AABB))[0 .. 128]; + aabbs := world_get_aabbs(world, camera.position, 5, aabb_buffer); + for^ aabbs do chunk_highlight_block(it.x0, it.y0, it.z0); + } + if debug_screen { ww, wh: i32; glfwGetWindowSize(window, ^ww, ^wh); @@ -227,7 +243,7 @@ main_loop :: () { frame_count = 0; } - update(~~dt); + update(cast(f32) dt); draw(); } } diff --git a/src/physics.onyx b/src/physics.onyx new file mode 100644 index 0000000..cf7f9ca --- /dev/null +++ b/src/physics.onyx @@ -0,0 +1,19 @@ +use package core + +PhysicsBody :: struct { + pos: Vector3; + vel: Vector3; + acc: Vector3; +} + + +physics_body_move :: (use body: ^PhysicsBody, world: ^World) { + aabb_buffer := cast([16] AABB) alloc.from_stack(sizeof [16] AABB); + + aabbs := world_get_aabbs(world, pos, Vector3.mag(vel)); + for aabb: aabbs { + + } +} + + diff --git a/src/world.onyx b/src/world.onyx index 43f2c53..81796ff 100644 --- a/src/world.onyx +++ b/src/world.onyx @@ -52,11 +52,64 @@ 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; + buffer_pos := 0; + + pos := Vector3i.{ ~~math.floor(center.x), ~~math.floor(center.y), ~~math.floor(center.z) }; + rad := cast(i32) math.ceil(radius); + + for x: pos.x-rad .. pos.x+rad+1 { + for y: pos.y-rad .. pos.y+rad+1 { + for z: pos.z-rad .. pos.z+rad+1 { + if world_get_block(world, x, y, z) != Block_Empty { + aabb := AABB.{ + ~~x, ~~y, ~~z, + ~~(x+1), ~~(y+1), ~~(z+1) + }; + + if is_dynamic { + aabbs << aabb; + } else { + buffer[buffer_pos] = aabb; + buffer_pos += 1; + if buffer_pos >= buffer.count do break break break; + } + } + } + } + } + + if is_dynamic do return aabbs; + return buffer[0 .. buffer_pos]; +} + + +@Relocate // to a utils file Ray :: struct { origin, direction: Vector3; } -world_ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) -> bool, out_block: ^Vector3i, out_dir: ^Vector3i) -> bool { +@Relocate // to a utils file +ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) -> bool, out_block: ^Vector3i, out_dir: ^Vector3i) -> bool { pos := Vector3i.{ ~~math.floor(ray.origin.x), ~~math.floor(ray.origin.y), ~~math.floor(ray.origin.z) }; dir := ray.direction; @@ -84,7 +137,7 @@ world_ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) pos.x += step.x; tmax.x += tdelta.x; - *out_dir = Vector3i.{ -step.x, 0, 0 }; + *out_dir = .{ -step.x, 0, 0 }; } else { if tmax.z > radius { @@ -93,7 +146,7 @@ world_ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) pos.z += step.z; tmax.z += tdelta.z; - *out_dir = Vector3i.{ 0, 0, -step.z }; + *out_dir = .{ 0, 0, -step.z }; } } else { @@ -104,7 +157,7 @@ world_ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) pos.y += step.y; tmax.y += tdelta.y; - *out_dir = Vector3i.{ 0, -step.y, 0 }; + *out_dir = .{ 0, -step.y, 0 }; } else { if tmax.z > radius { @@ -113,7 +166,7 @@ world_ray_cast :: (ray: Ray, max_distance: f32, ctx: $T, is_solid: (T, Vector3i) pos.z += step.z; tmax.z += tdelta.z; - *out_dir = Vector3i.{ 0, 0, -step.z }; + *out_dir = .{ 0, 0, -step.z }; } } }