code cleanup; removing uses of 'memory' package
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:55:50 +0000 (15:55 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:55:50 +0000 (15:55 -0500)
16 files changed:
lib/onyx-net
src/client/connect_menu.onyx
src/client/game.onyx
src/client/gfx/immediate.onyx
src/client/gfx/ui.onyx
src/client/main.onyx
src/client/net.onyx
src/client/world/chunk.onyx
src/client/world/physics.onyx
src/client/world/player.onyx
src/client/world/world.onyx
src/config.onyx.template
src/server/build.onyx
src/server/server.onyx
src/shared/packets.onyx
src/shared/world.onyx [new file with mode: 0644]

index 27c896ce946092882f9665b1ec39f6a3d74c5094..d851e9c7762bba2c40e9ea0e4fdeb4e935009406 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 27c896ce946092882f9665b1ec39f6a3d74c5094
+Subproject commit d851e9c7762bba2c40e9ea0e4fdeb4e935009406
index ab7bf394ccd2345d82b0ff6b09b92f4932cfa7a9..976a629969f9fe5e2c292d6e5e856d1413aaf83f 100644 (file)
@@ -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) {
index 1907f92c6572b3cf473e61c080b1af01f418b3e9..565a56ebf6ac78d96928229fc290b070d6d87e75 100644 (file)
@@ -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);
index d1c764e4097fe44d54e270f38a9f9cf23be5086f..4adb269b5828c348b56118147ee857cdb18462b9 100644 (file)
@@ -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);
index 48e7b9e8600d99a6678265ca24c85d71692f3569..c9d7a4e7bdc638dcb511177f0dd132d4a469e4f9 100644 (file)
@@ -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;
index e013ecc9ac5e1474054463203477fa59525cf12d..0369bd307a4e56f944a0912087c1c73bf58ccbe2 100644 (file)
@@ -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();
 }
 
 
index 1afa509f10e72e956c5bb26645fb72dab1db1413..3ae54818b9d64df1671263ae57f18653a77dfdb9 100644 (file)
@@ -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;
index bba0ce1866e5d15dcf2eeb5491633b0f600f52b9..8e49480b86b1a2ec1aee7ad3aac430d66fbc640d 100644 (file)
@@ -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;
index 746e39da77893e6c9c1c6d54e4ee1027adf89760..2c725c2937793cdf015ea5fe59228e0dab3f5e3e 100644 (file)
@@ -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;
             }
index b2a3bebd0044463821cf7afb9f5ef21e243c8e6a..28141074553a03a73c750eed9e1c690f36fde708 100644 (file)
@@ -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)}]);
     }
 }
 
index 397b3802e7029a2f9ca94436fde1217d984c80b4..2bff142c183067769078860561d9e3a082cee7c0 100644 (file)
@@ -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 {
index ffac594c40eb6c806833f50e6a5da10944952c6c..841d5fc68b1a955d3b11aa994a09a861a7b4fef7 100644 (file)
@@ -1,3 +1,8 @@
 package runtime.vars
 
 ONYX_MODULE_DIR :: ""
+
+Game_Version :: 0x0001
+Game_Port :: cast(u16) 5123
+
+#load_path "./../lib"
index ec6af84c02b7a1348eaf79afe4a5f5810d2f388a..7bb2c076d985692755fd8615f2e2f2534d4bb20c 100644 (file)
@@ -11,6 +11,6 @@ package runtime.vars
 #load "server"
 
 #load "./../client/utils/vecmath"
-#load "./../shared/packets"
+#load_all "./../shared"
 
 #load "onyx-net/src/module"
index fcbccc24e4593742d88e15d464ae23825c0b7ae3..0719a08657d566fca611914855b6e1eb9951856d 100644 (file)
@@ -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);
index 569b16cec5a84206d62911fef2dfb415b0d857da..b50d5dfb645437f81236a921326d03c041a89e2a 100644 (file)
@@ -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 (file)
index 0000000..6ede368
--- /dev/null
@@ -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);
+}
+