From 4b49259b80ca1f2a748f2d767fb1c260246af49b Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 7 Apr 2022 15:55:50 -0500 Subject: [PATCH] code cleanup; removing uses of 'memory' package --- lib/onyx-net | 2 +- src/client/connect_menu.onyx | 18 +++++++++--------- src/client/game.onyx | 12 +++++++++--- src/client/gfx/immediate.onyx | 2 +- src/client/gfx/ui.onyx | 16 +++++++++------- src/client/main.onyx | 13 ++++++++++++- src/client/net.onyx | 33 +++++++++++++++++++++++++++++++++ src/client/world/chunk.onyx | 34 +++++++--------------------------- src/client/world/physics.onyx | 2 +- src/client/world/player.onyx | 8 ++++---- src/client/world/world.onyx | 10 +++++----- src/config.onyx.template | 5 +++++ src/server/build.onyx | 2 +- src/server/server.onyx | 6 ++++-- src/shared/packets.onyx | 15 ++++++++++++++- src/shared/world.onyx | 22 ++++++++++++++++++++++ 16 files changed, 137 insertions(+), 63 deletions(-) create mode 100644 src/shared/world.onyx diff --git a/lib/onyx-net b/lib/onyx-net index 27c896c..d851e9c 160000 --- a/lib/onyx-net +++ b/lib/onyx-net @@ -1 +1 @@ -Subproject commit 27c896ce946092882f9665b1ec39f6a3d74c5094 +Subproject commit d851e9c7762bba2c40e9ea0e4fdeb4e935009406 diff --git a/src/client/connect_menu.onyx b/src/client/connect_menu.onyx index ab7bf39..976a629 100644 --- a/src/client/connect_menu.onyx +++ b/src/client/connect_menu.onyx @@ -42,13 +42,13 @@ Connect_Menu :: struct { tx := (ww - 200) / 2; ty := (wh - 160) / 2; - textbox_list_start(); - draw_textbox(.{tx, ty+0, 200, 40}, ^ip_addr, "IP Address"); - draw_textbox(.{tx, ty+40, 200, 40}, ^port, "Port"); - draw_textbox(.{tx, ty+80, 200, 40}, ^name, "Name"); - textbox_list_end(); + ui.textbox_list_start(); + ui.draw_textbox(.{tx, ty+0, 200, 40}, ^ip_addr, "IP Address"); + ui.draw_textbox(.{tx, ty+40, 200, 40}, ^port, "Port"); + ui.draw_textbox(.{tx, ty+80, 200, 40}, ^name, "Name"); + ui.textbox_list_end(); - if draw_button(.{tx, ty+120, 200, 40}, "Join") { + if ui.draw_button(.{tx, ty+120, 200, 40}, "Join") { join(_); } } @@ -72,9 +72,9 @@ Connecting_Menu :: struct { } leave :: (_: rawptr) { - array.free(^ip_addr); - array.free(^port); - array.free(^name); + delete(^ip_addr); + delete(^port); + delete(^name); } update :: (_: rawptr, dt: f32) { diff --git a/src/client/game.onyx b/src/client/game.onyx index 1907f92..565a56e 100644 --- a/src/client/game.onyx +++ b/src/client/game.onyx @@ -21,6 +21,7 @@ player_shader: Shader; Game_State :: struct { init :: game_init; + deinit :: game_deinit; update :: game_update; draw :: game_draw; } @@ -45,6 +46,11 @@ game_init :: (_: rawptr) { Meshes.init(); } +game_deinit :: (_: rawptr) { + net_send_disconnect(); + net_disconnect(); +} + game_update :: (_: rawptr, dt: f32) { net_pulse(); @@ -95,7 +101,7 @@ game_draw :: (_: rawptr) { // A very hacky wait to have the click the capture mouse. immediate_push_scissor(0, 0, 0, 0); if !cursor_grabbed { - if draw_button(.{ 0, 0, ~~ww, ~~wh }, "") { + if ui.draw_button(.{ 0, 0, ~~ww, ~~wh }, "") { toggle_cursor_grabbed(); } } @@ -120,7 +126,7 @@ draw_scene :: () { shader_use(player_shader); shader_set_uniform(player_shader, #cstr "color", Color.{1,0,0}); for^ other_players.entries { - update_model_matrix(it.value.position - .{0,0.8,0}, .{0.5, 1, 0.5}); + update_model_matrix(it.value.position - .{0,0.8,0}, .{0.3, 1, 0.3}); mesh_draw(Meshes.white_box); } } @@ -145,7 +151,7 @@ draw_chat :: () { } #persist pending_message: [..] u8; - if draw_textbox(.{0, ~~(wh - 32), 400, 32}, ^pending_message) == .Enter_Pressed { + if ui.draw_textbox(.{0, ~~(wh - 32), 400, 32}, ^pending_message) == .Enter_Pressed { if pending_message.count > 0 { net_send_chat_message(string.alloc_copy(pending_message)); array.clear(^pending_message); diff --git a/src/client/gfx/immediate.onyx b/src/client/gfx/immediate.onyx index d1c764e..4adb269 100644 --- a/src/client/gfx/immediate.onyx +++ b/src/client/gfx/immediate.onyx @@ -4,7 +4,7 @@ use package opengles use package glfw3 immediate_init :: () { - memory.alloc_slice(^vertex_data, Maximum_Vertex_Count); + vertex_data = make([] Immediate_Vertex, Maximum_Vertex_Count); vertex_count = 0; imgui_shader = shader_make(Shader_Path); diff --git a/src/client/gfx/ui.onyx b/src/client/gfx/ui.onyx index 48e7b9e..c9d7a4e 100644 --- a/src/client/gfx/ui.onyx +++ b/src/client/gfx/ui.onyx @@ -1,14 +1,16 @@ // // Very simple immediate mode UI // +package ui use package core use package opengles use package glfw3 +use package main UI_Id :: u32 -ui_end_frame :: () { +end_frame :: () { hot_item_depth_needed = hot_item_depth; if !hot_item_was_set do set_hot_item(0); hot_item_depth = 0; @@ -59,7 +61,7 @@ draw_button :: (use r: Rect, text: str, theme := ^default_button_theme, site := animation_state := get_animation(hash); mx, my := mouse_get_position(); - contains := Rect.contains(r, .{~~mx, ~~my}); + contains := r->contains(.{~~mx, ~~my}); if is_active_item(hash) { renew_active_item(hash); @@ -157,7 +159,7 @@ draw_slider :: (use r: Rect, value: ^$T, min_value: T, max_value: T, theme := ^d } } - if Rect.contains(r, .{ ~~mx, ~~my }) { + if r->contains(.{ ~~mx, ~~my }) { set_hot_item(hash); } @@ -259,7 +261,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th text_x := x + border_width; text_y := y + font.em + (h - text_height) / 2; - contains := Rect.contains(r, .{~~mx, ~~my}); + contains := r->contains(.{~~mx, ~~my}); if is_hot_item(hash) && !is_active_item(hash) { if is_button_down(GLFW_MOUSE_BUTTON_LEFT) && contains { @@ -485,7 +487,7 @@ draw_checkbox :: (use r: Rect, value: ^bool, text: str, theme := ^default_checkb animation_state := get_animation(hash); mx, my := mouse_get_position(); - contains := Rect.contains(r, .{~~mx, ~~my}); + contains := r->contains(.{~~mx, ~~my}); if is_active_item(hash) { renew_active_item(hash); @@ -576,7 +578,7 @@ draw_radio :: (use r: Rect, value: ^$T, set_to: T, text: str, theme := ^default_ animation_state := get_animation(hash); mx, my := mouse_get_position(); - contains := Rect.contains(r, .{~~mx, ~~my}); + contains := r->contains(.{~~mx, ~~my}); if is_active_item(hash) { renew_active_item(hash); @@ -650,7 +652,7 @@ scrolling_region_start :: (r: Rect, max_y_scroll := 10000.0f, site := #callsite, state = ^scroll_states[hash]; } - contains := Rect.contains(r, .{~~mx, ~~my}); + contains := r->contains(.{~~mx, ~~my}); if contains { scroll_delta := mouse_get_scroll_vector(); state.xscroll -= scroll_delta.x * 20; diff --git a/src/client/main.onyx b/src/client/main.onyx index e013ecc..0369bd3 100644 --- a/src/client/main.onyx +++ b/src/client/main.onyx @@ -5,6 +5,10 @@ use package opengles use package core.intrinsics.onyx { __initialize } #local runtime :: package runtime +#package { + ui :: package ui +} + State :: struct { data: rawptr; @@ -79,6 +83,12 @@ init :: () { update_window_matrix(); } +deinit :: () { + while state_stack.count > 0 { + pop_game_state(); + } +} + create_window :: () => { #if runtime.compiler_os == .Linux { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -135,7 +145,7 @@ draw :: () { immediate_flush(); input_post_update(); - ui_end_frame(); + ui.end_frame(); glfwSwapBuffers(window); } @@ -175,6 +185,7 @@ main :: (args) => { init(); main_loop(); + deinit(); } diff --git a/src/client/net.onyx b/src/client/net.onyx index 1afa509..3ae5481 100644 --- a/src/client/net.onyx +++ b/src/client/net.onyx @@ -99,6 +99,17 @@ net_handle_packet :: (packet_data: [] u8) { player.velocity = moved.velocity; player.facing = moved.facing; } + + case .Block_Updates { + block_updates := cast(^packets.Block_Updates) packet; + + update_count := block_updates.update_count; + updates := ([] packets.Block_Updates.Update).{ ~~^block_updates.updates, ~~update_count }; + for updates { + pos := it.position; + world_set_block(world, pos.x, pos.y, pos.z, it.new_block); + } + } } } @@ -119,6 +130,11 @@ net_send_connect :: (name: str) { net_send_disconnect :: () { disconnect := new(packets.Disconnect); disconnect.player_id = player_id; + + p := new(onet.Packet); + p.data = .{ ~~disconnect, sizeof typeof *disconnect }; + p.free_data = true; + onet.peer_send(peer, 0, p); } net_send_chat_message :: (message: str) { @@ -148,6 +164,23 @@ net_send_movement_update :: (position: Vector3, velocity: Vector3, facing: Vecto onet.peer_send(peer, 0, p); } +net_send_block_updates :: (updates: [] packets.Block_Updates.Update) { + size := sizeof packets.Block_Updates + updates.count * sizeof packets.Block_Updates.Update; + block_updates := cast(^packets.Block_Updates) calloc(size); + block_updates.type = .Block_Updates; + block_updates.update_count = ~~updates.count; + + block_update := cast(^packets.Block_Updates.Update) ^block_updates.updates; + for updates.count { + block_update[it].position = updates[it].position; + block_update[it].new_block = updates[it].new_block; + } + + p := new(onet.Packet); + p.data = .{ ~~block_updates, size }; + p.free_data = true; + onet.peer_send(peer, 0, p); +} Remote_Player :: struct { name: str; diff --git a/src/client/world/chunk.onyx b/src/client/world/chunk.onyx index bba0ce1..8e49480 100644 --- a/src/client/world/chunk.onyx +++ b/src/client/world/chunk.onyx @@ -1,27 +1,6 @@ use package core use package opengles -Block :: #distinct u32; -#operator == macro (b1, b2: Block) => cast(u32) b1 == cast(u32) b2; -#operator != macro (b1, b2: Block) => cast(u32) b1 != cast(u32) b2; - -Block_Empty :: cast(Block) 0; - -Block_Options :: struct { - texture_enabled := true; -} - -block_make :: (red, green, blue: f32, brightness: f32, options := Block_Options.{}) -> Block { - r := cast(u32) (red * 15.0f); - g := cast(u32) (green * 15.0f); - b := cast(u32) (blue * 15.0f); - i := cast(u32) (brightness * 15.0f); - - tex := 1 if options.texture_enabled else 0; - - return ~~((tex << 16) | (i << 12) | (b << 8) | (g << 4) | r); -} - Chunk_Vertex :: struct { position : Vector3; texture : Vector2; @@ -54,15 +33,16 @@ chunk_make :: (x, y, z: i32, allocator := context.allocator) -> ^Chunk { ~~(y * Chunk_Size), ~~(z * Chunk_Size), }; - memory.alloc_slice(^chunk.blocks, Chunk_Size * Chunk_Size * Chunk_Size); - memory.fill_slice(chunk.blocks, Block_Empty); + + chunk.blocks = make([] Block, Chunk_Size*Chunk_Size*Chunk_Size); + array.fill(chunk.blocks, Block_Empty); return chunk; } chunk_free :: (chunk: ^Chunk) { chunk_destroy_mesh(chunk); - memory.free_slice(^chunk.blocks); - cfree(chunk); + delete(^chunk.blocks); + delete(chunk); } #local in_chunk_bounds :: macro (x, y, z: i32) -> bool { @@ -142,8 +122,8 @@ chunk_build_mesh :: (use chunk: ^Chunk) { mesh = null; } - verticies := array.make(Chunk_Vertex, capacity=512); - defer array.free(^verticies); + verticies := make([..] Chunk_Vertex, 512); + defer delete(^verticies); chunk_foreach(chunk) { if block == Block_Empty do continue; diff --git a/src/client/world/physics.onyx b/src/client/world/physics.onyx index 746e39d..2c725c2 100644 --- a/src/client/world/physics.onyx +++ b/src/client/world/physics.onyx @@ -39,7 +39,7 @@ physics_simulate :: (use body: ^PhysicsBody, dt: f32, world: ^World) { body_aabb := get_collision_object(new_pos); for^ aabbs { - if AABB.intersects(body_aabb, *it) { + if body_aabb->intersects(*it) { if delta.y < 0 do on_ground = true; return 0; } diff --git a/src/client/world/player.onyx b/src/client/world/player.onyx index b2a3beb..2814107 100644 --- a/src/client/world/player.onyx +++ b/src/client/world/player.onyx @@ -34,7 +34,7 @@ player_update_controls :: (use player: ^Player, dt: f32) { forward := camera_get_forward(camera); facing := forward; facing.y = 0; - facing = Vector3.norm(facing); + facing = facing->norm(); xz_vel := Vector3.{0,0,0}; if is_key_down(GLFW_KEY_W) do xz_vel += facing; @@ -57,7 +57,7 @@ player_update_physics :: (use player: ^Player, dt: f32) { xz_vel := direction_to_move; - if Vector3.mag(xz_vel) > 0 do xz_vel = Vector3.norm(xz_vel) * speed; + if xz_vel->mag() > 0 do xz_vel = xz_vel->norm() * speed; body.vel.x = xz_vel.x; body.vel.z = xz_vel.z; @@ -92,12 +92,12 @@ player_update :: (use player: ^Player, dt: f32) { } if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) { - world_set_block(world, selected_block.x, selected_block.y, selected_block.z, Block_Empty); + net_send_block_updates(.[.{selected_block, Block_Empty}]); } if is_button_just_down(GLFW_MOUSE_BUTTON_RIGHT) { target := selected_block + dir; - world_set_block(world, target.x, target.y, target.z, block_make(1,0,0,1)); + net_send_block_updates(.[.{target, block_make(1,0,0,1)}]); } } diff --git a/src/client/world/world.onyx b/src/client/world/world.onyx index 397b380..2bff142 100644 --- a/src/client/world/world.onyx +++ b/src/client/world/world.onyx @@ -16,8 +16,8 @@ world_make :: (allocator := context.allocator) -> ^World { world.center_chunk = .{0,0,0}; sl := world.chunk_dist * 2 + 1; - memory.alloc_slice(^world.chunks, sl * sl * sl); - memory.fill_slice(world.chunks, null); + world.chunks = make([] ^Chunk, sl * sl * sl); + array.fill(world.chunks, null); return world; } @@ -135,8 +135,8 @@ world_move_center :: (use world: ^World, new_center: Vector3i) { if center_chunk == new_center do return; sl := world.chunk_dist * 2 + 1; - current_chunks := memory.copy_slice(world.chunks); - memory.fill_slice(world.chunks, null); + current_chunks := array.copy(world.chunks); + array.fill(world.chunks, null); center_chunk = new_center; for current_chunks { @@ -153,7 +153,7 @@ world_move_center :: (use world: ^World, new_center: Vector3i) { if world_get_chunk(world, x, y, z) == null do world.chunks_to_load << .{x, y, z}; } - memory.free_slice(^current_chunks); + delete(^current_chunks); } world_get_aabbs :: (use world: ^World, center: Vector3, radius: f32, buffer: [] AABB = .[]) -> [] AABB { diff --git a/src/config.onyx.template b/src/config.onyx.template index ffac594..841d5fc 100644 --- a/src/config.onyx.template +++ b/src/config.onyx.template @@ -1,3 +1,8 @@ package runtime.vars ONYX_MODULE_DIR :: "" + +Game_Version :: 0x0001 +Game_Port :: cast(u16) 5123 + +#load_path "./../lib" diff --git a/src/server/build.onyx b/src/server/build.onyx index ec6af84..7bb2c07 100644 --- a/src/server/build.onyx +++ b/src/server/build.onyx @@ -11,6 +11,6 @@ package runtime.vars #load "server" #load "./../client/utils/vecmath" -#load "./../shared/packets" +#load_all "./../shared" #load "onyx-net/src/module" diff --git a/src/server/server.onyx b/src/server/server.onyx index fcbccc2..0719a08 100644 --- a/src/server/server.onyx +++ b/src/server/server.onyx @@ -92,7 +92,9 @@ handle_packet :: (peer: ^onet.Peer, packet_data: [] u8) { } } - case .Chat_Message { + case .Chat_Message, .Block_Updates { + @TODO // Validation of block updates + packet := new(onet.Packet); packet.flags |= .Reliable; packet.data = memory.copy_slice(packet_data); @@ -107,7 +109,7 @@ handle_packet :: (peer: ^onet.Peer, packet_data: [] u8) { player.position = moved.position; player.velocity = moved.velocity; - player.facing = moved.facing; + player.facing = moved.facing; packet := new(onet.Packet); packet.data = memory.copy_slice(packet_data); diff --git a/src/shared/packets.onyx b/src/shared/packets.onyx index 569b16c..b50d5df 100644 --- a/src/shared/packets.onyx +++ b/src/shared/packets.onyx @@ -1,6 +1,7 @@ package packets -use package main { Vector3 } +use package main { Vector3, Vector3i } +use package main { Block } Type :: enum (u8) { Connect; @@ -13,6 +14,8 @@ Type :: enum (u8) { Player_Joined; Player_Left; Player_Moved; + + Block_Updates; } Packet_Base :: struct #pack { @@ -73,3 +76,13 @@ Chat_Message :: struct #pack { message_data: void; } +Block_Updates :: struct #pack { + use base := Packet_Base.{ .Block_Updates }; + update_count: u16; + updates: void; + + Update :: struct { + position: Vector3i; + new_block: Block; + } +} \ No newline at end of file diff --git a/src/shared/world.onyx b/src/shared/world.onyx new file mode 100644 index 0000000..6ede368 --- /dev/null +++ b/src/shared/world.onyx @@ -0,0 +1,22 @@ + +Block :: #distinct u32; +#operator == macro (b1, b2: Block) => cast(u32) b1 == cast(u32) b2; +#operator != macro (b1, b2: Block) => cast(u32) b1 != cast(u32) b2; + +Block_Empty :: cast(Block) 0; + +Block_Options :: struct { + texture_enabled := true; +} + +block_make :: (red, green, blue: f32, brightness: f32, options := Block_Options.{}) -> Block { + r := cast(u32) (red * 15.0f); + g := cast(u32) (green * 15.0f); + b := cast(u32) (blue * 15.0f); + i := cast(u32) (brightness * 15.0f); + + tex := 1 if options.texture_enabled else 0; + + return ~~((tex << 16) | (i << 12) | (b << 8) | (g << 4) | r); +} + -- 2.25.1