From: Brendan Hansen Date: Mon, 19 Jun 2023 02:53:47 +0000 (-0500) Subject: very crude update to new version of onyx X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b070fdf57d2fb1492dc0394287ca426fded3f1e1;p=voxel-shooter.git very crude update to new version of onyx --- diff --git a/onyx-pkg.ini b/onyx-pkg.ini index 93355ac..a0f136c 100644 --- a/onyx-pkg.ini +++ b/onyx-pkg.ini @@ -8,18 +8,21 @@ version=0.0.1 [config] lib_source_directory=./lib lib_bin_directory=./run_tree/lib +run_cmd= +debug_cmd= +test_cmd= [native_library] build_cmd= library= [dependencies] -git://onyxlang.io/repo/onyx-net=0.0.3 -git://onyxlang.io/repo/glfw3=0.0.2 -git://onyxlang.io/repo/opengles=0.0.2 -git://onyxlang.io/repo/perlin=0.1.0 -git://onyxlang.io/repo/stb_image=0.0.2 -git://onyxlang.io/repo/stb_truetype=0.0.2 +git://onyxlang.io/repo/onyx-net=0.0.7 +git://onyxlang.io/repo/glfw3=0.0.3 +git://onyxlang.io/repo/opengles=0.0.8 +git://onyxlang.io/repo/perlin=0.1.1 +git://onyxlang.io/repo/stb_image=0.0.3 +git://onyxlang.io/repo/stb_truetype=0.0.3 [dependency_folders] git://onyxlang.io/repo/onyx-net=onyx-net diff --git a/run_tree/lib/onyx_glfw3.so b/run_tree/lib/onyx_glfw3.so index 168d722..02169df 100755 Binary files a/run_tree/lib/onyx_glfw3.so and b/run_tree/lib/onyx_glfw3.so differ diff --git a/run_tree/lib/onyx_opengles.so b/run_tree/lib/onyx_opengles.so index 333ecb0..5c82218 100755 Binary files a/run_tree/lib/onyx_opengles.so and b/run_tree/lib/onyx_opengles.so differ diff --git a/run_tree/lib/stb_image.so b/run_tree/lib/stb_image.so index a2305d7..da56c1e 100755 Binary files a/run_tree/lib/stb_image.so and b/run_tree/lib/stb_image.so differ diff --git a/run_tree/lib/stb_truetype.so b/run_tree/lib/stb_truetype.so index 55c198d..00b4dbe 100755 Binary files a/run_tree/lib/stb_truetype.so and b/run_tree/lib/stb_truetype.so differ diff --git a/run_tree/run.sh b/run_tree/run.sh index 7370e69..47e6fbe 100755 --- a/run_tree/run.sh +++ b/run_tree/run.sh @@ -1,6 +1,7 @@ case "$1" in - build) onyx -V build -I ../src/client -o game.wasm $@ ;; - debug) onyx-run --debug game.wasm ;; - *) onyx-run game.wasm ;; + build) onyx build -V build -I ../src/client -o game.wasm $@ ;; + watch) onyx watch build.onyx -I ../src/client ;; + debug) onyx run --debug game.wasm ;; + *) onyx run game.wasm ;; esac diff --git a/server/run.sh b/server/run.sh index b740f25..bb9a16b 100755 --- a/server/run.sh +++ b/server/run.sh @@ -1,7 +1,7 @@ #!/bin/sh case "$1" in - build) onyx -V build -I ../src/server -o server.wasm $@ ;; + build) onyx build -V build -I ../src/server -o server.wasm $@ ;; debug) onyx-run --debug server.wasm ;; *) onyx-run server.wasm ;; esac diff --git a/src/client/build.onyx b/src/client/build.onyx index 1368111..5fc9521 100644 --- a/src/client/build.onyx +++ b/src/client/build.onyx @@ -1,5 +1,3 @@ -#load "core/std" - #load_path "./../src/client" #library_path "./lib" @@ -16,9 +14,4 @@ #load_all "./../shared" // Onyx modules -#load "glfw3/module" -#load "opengles/module" -#load "perlin/module" -#load "stb_image/module" -#load "stb_truetype/module" -#load "onyx-net/module" +#load "./../lib/packages" diff --git a/src/client/game.onyx b/src/client/game.onyx index 48b4420..f205f08 100644 --- a/src/client/game.onyx +++ b/src/client/game.onyx @@ -1,8 +1,10 @@ -use core -use glfw3 -use opengles -use stb_truetype +use core {*} +use glfw3 {*} +use opengles {*} +use stb_truetype {*} +use ui +use game_net #local { world_shader: Shader; @@ -12,7 +14,7 @@ use stb_truetype fog_color :: Vector3.{ 0.6, 0.6, 0.6 }; } -@Relocate // this global variables +// @Relocate // this global variables font: Font; player: Player; world: ^World; @@ -21,7 +23,9 @@ selected_block: Vector3i; player_shader: Shader; -@Relocate +world_chunk_update_thread: thread.Thread; + +// @Relocate world_fog_block: Shader_Block; World_Fog :: struct { u_fog_color: Vector3; @@ -142,7 +146,7 @@ draw_scene :: () { player_manager->draw_players(); } -@Temporary +// @Temporary chat_messages: [..] [] u8; draw_chat :: () { diff --git a/src/client/gfx/canvas.onyx b/src/client/gfx/canvas.onyx index 862c451..99c16bb 100644 --- a/src/client/gfx/canvas.onyx +++ b/src/client/gfx/canvas.onyx @@ -1,6 +1,6 @@ -use core -use opengles +use core {*} +use opengles {*} Canvas :: struct { diff --git a/src/client/gfx/font.onyx b/src/client/gfx/font.onyx index f9dee0d..3e51eab 100644 --- a/src/client/gfx/font.onyx +++ b/src/client/gfx/font.onyx @@ -1,7 +1,7 @@ -use core -use stb_truetype -use opengles +use core {*} +use stb_truetype {*} +use opengles {*} #local { font_registry: Map(FontDescriptor, Font); @@ -126,7 +126,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 = alloc.from_stack(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; @@ -147,7 +147,7 @@ font_draw :: (font: Font, x, y: f32, msg: str) { } font_draw_centered :: (font: Font, x, y, max_width: f32, msg: str) { - quads: ^stbtt_aligned_quad = alloc.from_stack(msg.count * sizeof stbtt_aligned_quad); + quads: [^] stbtt_aligned_quad = alloc.from_stack(msg.count * sizeof stbtt_aligned_quad); quad_num := 0; width := font_get_width(font, msg); @@ -259,9 +259,9 @@ FontDescriptor :: struct { #operator == (f1, f2: FontDescriptor) => f1.path == f2.path && f1.size == f2.size; font_lookup :: (fd := FontDescriptor.{ "assets/calibri.ttf", 12 }) -> Font { - if font_registry->has(fd) { - return font_registry[fd]; - } + font_registry->get_opt(fd)->with([font] { + return font; + }); font := font_make(fd); font_registry[fd] = font; diff --git a/src/client/gfx/immediate.onyx b/src/client/gfx/immediate.onyx index f5fbed7..887531d 100644 --- a/src/client/gfx/immediate.onyx +++ b/src/client/gfx/immediate.onyx @@ -1,7 +1,7 @@ -use core -use opengles -use glfw3 +use core {*} +use opengles {*} +use glfw3 {*} immediate_init :: () { vertex_data = make([] Immediate_Vertex, Maximum_Vertex_Count); diff --git a/src/client/gfx/mesh.onyx b/src/client/gfx/mesh.onyx index 89f1946..fb22edf 100644 --- a/src/client/gfx/mesh.onyx +++ b/src/client/gfx/mesh.onyx @@ -1,5 +1,6 @@ -use core -use opengles +use core {*} +use opengles {*} +use runtime Mesh :: struct (Vertex_Type: type_expr) { handle: GLint; @@ -60,7 +61,7 @@ mesh_make :: (verticies: [] $T, indicies: [] u32, gl_hint := GL_STATIC_DRAW) -> } mesh_update_verticies :: (use mesh: ^Mesh, verticies: [] mesh.Vertex_Type) { - @TODO // Add bounds checking to arrays here. + // @TODO // Add bounds checking to arrays here. glBindBuffer(GL_ARRAY_BUFFER, vertex_handle); glBufferSubData(GL_ARRAY_BUFFER, 0, verticies.count * sizeof mesh.Vertex_Type, verticies.data); diff --git a/src/client/gfx/shader.onyx b/src/client/gfx/shader.onyx index a00b4dc..5119aa2 100644 --- a/src/client/gfx/shader.onyx +++ b/src/client/gfx/shader.onyx @@ -1,6 +1,7 @@ -use core -use opengles +use core {*} +use opengles {*} +use runtime Shader :: struct { vs, fs: GLuint; diff --git a/src/client/gfx/texture.onyx b/src/client/gfx/texture.onyx index 0cf8272..869408d 100644 --- a/src/client/gfx/texture.onyx +++ b/src/client/gfx/texture.onyx @@ -1,7 +1,7 @@ -use core -use opengles -use stb_image +use core {*} +use opengles {*} +use stb_image {*} #local texture_cache: Map(str, Texture); @@ -13,10 +13,10 @@ Texture :: struct { texture_lookup :: #match {} #match texture_lookup (filename: str) -> (Texture, bool) { - if texture_cache->has(filename) { - return texture_cache[filename], true; - } - + texture_cache->get_opt(filename)->with([texture] { + return texture, true; + }); + buffer: [512] u8; memory.copy(~~ buffer, filename.data, math.min(filename.count, 511)); return texture_lookup(cast(cstr) buffer); @@ -24,9 +24,9 @@ texture_lookup :: #match {} #match texture_lookup (path: cstr) -> (Texture, bool) { filename := string.from_cstr(path); - if texture_cache->has(filename) { - return texture_cache[filename], true; - } + texture_cache->get_opt(filename)->with([texture] { + return texture, true; + }); tex: Texture; tex.filename = filename; diff --git a/src/client/gfx/ui.onyx b/src/client/gfx/ui.onyx index b807ef5..daa7f1b 100644 --- a/src/client/gfx/ui.onyx +++ b/src/client/gfx/ui.onyx @@ -3,10 +3,10 @@ // package ui -use core -use opengles -use glfw3 -use main +use core {package, *} +use opengles {*} +use glfw3 {*} +use main {*} UI_Id :: u32 @@ -119,7 +119,7 @@ Slider_Theme :: struct { box_color := Color.{ 0.1, 0.1, 0.1 }; box_border_color := Color.{ 0.2, 0.2, 0.2 }; - box_border_width := 4.0f; @InPixels + box_border_width := 4.0f; // @InPixels bar_color := Color.{ 0.4, 0.4, 0.4 }; bar_hover_color := Color.{ 0, 0, 1 }; @@ -205,10 +205,10 @@ Textbox_Theme :: struct { click_color := Color.{ 0.5, 0.5, 0.7 }; border_color := Color.{ 0.2, 0.2, 0.2 }; - border_width := 6.0f; @InPixels + border_width := 6.0f; // @InPixels cursor_color := Color.{ 0.5, 0.5, 0.5 }; - cursor_width := 4.0f; @InPixels + cursor_width := 4.0f; // @InPixels cursor_blink_speed := 0.04f; // Bigger is faster placeholder_text_color := Color.{ 0.5, 0.5, 0.5 }; @@ -224,7 +224,7 @@ Textbox_Theme :: struct { cursor_animation := 0.0f; cursor_animation_speed := 0.02f; - @HACK // Otherwise the action keys are evaluated every frame. + // @HACK // Otherwise the action keys are evaluated every frame. action_key_timeout := 0.0f; } @@ -467,8 +467,8 @@ Checkbox_Theme :: struct { use animation_theme := Animation_Theme.{}; box_color := Color.{ 0.2, 0.2, 0.2 }; - box_border_width := 4.0f; @InPixels - box_size := 20.0f; @InPixels + box_border_width := 4.0f; // @InPixels + box_size := 20.0f; // @InPixels checked_color := Color.{ 1, 0, 0 }; checked_hover_color := Color.{ 1, 0.6, 0.6 }; @@ -558,8 +558,8 @@ Radio_Theme :: struct { use animation_theme := Animation_Theme.{}; box_color := Color.{ 0.2, 0.2, 0.2 }; - box_border_width := 4.0f; @InPixels - box_size := 20.0f; @InPixels + box_border_width := 4.0f; // @InPixels + box_size := 20.0f; // @InPixels checked_color := Color.{ 0, 0, 1 }; checked_hover_color := Color.{ 0.6, 0.6, 1 }; diff --git a/src/client/globals.onyx b/src/client/globals.onyx index 4776e63..53be2f1 100644 --- a/src/client/globals.onyx +++ b/src/client/globals.onyx @@ -1,5 +1,5 @@ use glfw3 { GLFWwindow_p } -@GlobalVariable window: GLFWwindow_p; +window: GLFWwindow_p; debug_screen := false; diff --git a/src/client/main.onyx b/src/client/main.onyx index 6ae413b..1cc48fc 100644 --- a/src/client/main.onyx +++ b/src/client/main.onyx @@ -1,8 +1,11 @@ -use core -use glfw3 -use opengles +use core {*} +use glfw3 {*} +use opengles {*} use core.intrinsics.onyx { __initialize } +use runtime +use ui +use game_net State :: struct { data: rawptr; @@ -98,12 +101,12 @@ create_window :: () => { glfwMakeContextCurrent(window); glfwSwapInterval(1); - glfwSetWindowSizeCallback(window, "on_resize"); + glfwSetWindowSizeCallback(window, #export_name on_resize); return window; } -#export "on_resize" (window: GLFWwindow_p, width, height: u32) { +on_resize :: (window: GLFWwindow_p, width, height: u32) { glViewport(0, 0, width, height); camera_set_window_size(^camera, ~~width, ~~height); update_view_matrix(); diff --git a/src/client/menu/connect_menu.onyx b/src/client/menu/connect_menu.onyx index 19be689..14069ea 100644 --- a/src/client/menu/connect_menu.onyx +++ b/src/client/menu/connect_menu.onyx @@ -1,7 +1,10 @@ -use core -use glfw3 -use opengles +use core {*} +use glfw3 {*} +use opengles {*} +use game_net +use runtime +use ui #local { ip_addr: [..] u8; @@ -19,7 +22,7 @@ Connect_Menu :: struct { title_font = font_lookup(.{"./assets/fonts/calibri.ttf", 64}); - @HACK @HACK @HACK + // @HACK // @HACK // @HACK player_manager = new(Player_Manager); } diff --git a/src/client/net/handlers.onyx b/src/client/net/handlers.onyx index 19f3156..a56d815 100644 --- a/src/client/net/handlers.onyx +++ b/src/client/net/handlers.onyx @@ -1,20 +1,19 @@ package game_net -#local { - use core - use main - onet :: onyx_net -} +use core {*} +use main {*} +use packets +use onyx_net { onet :: package } -#tag Packet_Handler.{ .Verify_Connect } -#local handle_verify_connect :: (packet: ^packets.Verify_Connect) { +@Packet_Handler.{ .Verify_Connect } +(packet: ^packets.Verify_Connect) { player_manager.player_id = packet.player_id; push_game_state(Game_State, null); } -#tag Packet_Handler.{ .Player_Joined } -#local handle_player_joined :: (packet: ^packets.Player_Joined) { +@Packet_Handler.{ .Player_Joined } +(packet: ^packets.Player_Joined) { name := packet->name(); player_manager->add_player(packet.player_id, name); @@ -23,17 +22,17 @@ package game_net } } -#tag Packet_Handler.{ .Connection_Rejected } -#local handle_connection_rejected :: (packet: ^packets.Connection_Rejected) { - @NetworkErrors // Handle this case. +@Packet_Handler.{ .Connection_Rejected } +(packet: ^packets.Connection_Rejected) { + // @NetworkErrors // Handle this case. printf("Connection refused: {}\n", packet.reason); pop_game_state(); push_game_state(Connect_Menu, null); } -#tag Packet_Handler.{ .Chat_Message } -#local handle_chat_message :: (packet: ^packets.Chat_Message) { +@Packet_Handler.{ .Chat_Message } +(packet: ^packets.Chat_Message) { buf: [1024] u8; msg: str; @@ -47,8 +46,8 @@ package game_net chat_messages << string.alloc_copy(msg); } -#tag Packet_Handler.{ .Player_Moved } -#local handle_player_moved :: (packet: ^packets.Player_Moved) { +@Packet_Handler.{ .Player_Moved } +(packet: ^packets.Player_Moved) { if packet.player_id == player_manager.player_id do return; player := player_manager->get_player(packet.player_id); @@ -58,8 +57,8 @@ package game_net player.on_ground = packet.on_ground; } -#tag Packet_Handler.{ .Block_Updates } -#local handle_block_updates :: (packet: ^packets.Block_Updates) { +@Packet_Handler.{ .Block_Updates } +(packet: ^packets.Block_Updates) { updates := packet->updates(); for updates { pos := it.position; diff --git a/src/client/net/net.onyx b/src/client/net/net.onyx index b6528d2..a984d02 100644 --- a/src/client/net/net.onyx +++ b/src/client/net/net.onyx @@ -1,13 +1,12 @@ package game_net -#local { - use core - use main - onet :: onyx_net -} +use core {*} +use main {*} +use packets +use onyx_net { onet :: package } register_handles :: () { - use runtime.info; + use runtime.info {*}; procs := get_procedures_with_tag(Packet_Handler); defer delete(^procs); @@ -15,14 +14,14 @@ register_handles :: () { packet_handles[it.tag.type] = *cast(^(rawptr) -> void) ^it.func; } - println(packet_handles); + printf("{p}\n", packet_handles); } connect :: (ip_addr: [] u8, port: u16) { addr: net.Socket_Address; addr.addr = net.str_to_ipv4(ip_addr); addr.port = port; - host', _ := onet.host_create(null, 1); + host~, _ := onet.host_create(null, 1); peer = onet.host_connect(host, ^addr, 2); } @@ -73,4 +72,4 @@ Packet_Handler :: struct { on_connect_callback: On_Connect_Callback; any_to_buffer :: macro (x: ^$T) => ([] u8).{ ~~x, sizeof T }; -} \ No newline at end of file +} diff --git a/src/client/net/outbound.onyx b/src/client/net/outbound.onyx index 16b1896..ed96a38 100644 --- a/src/client/net/outbound.onyx +++ b/src/client/net/outbound.onyx @@ -1,10 +1,10 @@ package game_net -#local { - use core - use main - onet :: onyx_net -} +use core {*} +use main {*} +use packets +use onyx_net { onet :: package } +use runtime send_connect :: (name: str) { msg := cast(^packets.Connect) calloc(sizeof packets.Connect + name.count); @@ -64,7 +64,7 @@ send_block_updates :: (updates: [] packets.Block_Updates.Update) { block_updates.type = .Block_Updates; block_updates.update_count = ~~updates.count; - block_update := cast(^packets.Block_Updates.Update) ^block_updates.update_data; + block_update := cast([^]packets.Block_Updates.Update) ^block_updates.update_data; for updates.count { block_update[it].position = updates[it].position; block_update[it].new_block = updates[it].new_block; diff --git a/src/client/utils/camera.onyx b/src/client/utils/camera.onyx index 1e701b1..388f096 100644 --- a/src/client/utils/camera.onyx +++ b/src/client/utils/camera.onyx @@ -1,6 +1,6 @@ -use core +use core {*} -@GlobalVariable +// @GlobalVariable camera: Camera; Camera :: struct { diff --git a/src/client/utils/colors.onyx b/src/client/utils/colors.onyx index b8f18f2..36fbf39 100644 --- a/src/client/utils/colors.onyx +++ b/src/client/utils/colors.onyx @@ -1,5 +1,5 @@ -use core +use core {*} Color :: struct { r, g, b : f32; diff --git a/src/client/utils/input.onyx b/src/client/utils/input.onyx index 6ae5a57..53e8468 100644 --- a/src/client/utils/input.onyx +++ b/src/client/utils/input.onyx @@ -1,5 +1,5 @@ -use core -use glfw3 +use core {package, *} +use glfw3 {*} // If you are offseting the mouse coordinate for world space // or UI scrolling etc., set this function to be the function @@ -32,8 +32,10 @@ Key_Descriptor :: struct { key: u32; scancode: u32 = 0; mod: u32 = 0; + + hash :: (use this: Key_Descriptor) => core.hash.to_u32(key); } -#match hash.to_u32 (use x: Key_Descriptor) => hash.to_u32(key); + #operator == macro (x, y: Key_Descriptor) => x.key == y.key; input_update :: () { diff --git a/src/client/utils/logger.onyx b/src/client/utils/logger.onyx index 8d76c71..4e1ff1a 100644 --- a/src/client/utils/logger.onyx +++ b/src/client/utils/logger.onyx @@ -2,7 +2,7 @@ // This may become an in-game or external file logger in the future, // but for now this is just for logging to the command line. -use core +use core {*} Enable_Log_Colors :: false @@ -54,4 +54,4 @@ debug_log_va :: (level: Log_Level, format: str, args: [] any) { return ""; } -#local log_level := Log_Level.Debug; \ No newline at end of file +#local log_level := Log_Level.Debug; diff --git a/src/client/utils/utils.onyx b/src/client/utils/utils.onyx index 5b3c592..7976469 100644 --- a/src/client/utils/utils.onyx +++ b/src/client/utils/utils.onyx @@ -1,4 +1,4 @@ -use core +use core {*} Ray :: struct { origin, direction: Vector3; diff --git a/src/client/utils/vecmath.onyx b/src/client/utils/vecmath.onyx index dc91d55..f7e512c 100644 --- a/src/client/utils/vecmath.onyx +++ b/src/client/utils/vecmath.onyx @@ -1,25 +1,27 @@ +use core {package, math} + #tag conv.Custom_Format.{format_vector2i} #tag conv.Custom_Parse.{parse_vector2i} Vector2i :: struct { x, y: i32; - } #tag conv.Custom_Format.{format_vector2} #tag conv.Custom_Parse.{parse_vector2} Vector2 :: struct { x, y: f32; +} - mag :: macro (v: Vector2) => math.sqrt(v.x * v.x + v.y * v.y); +#inject Vector2 { + mag :: (v: Vector2) => math.sqrt(v.x * v.x + v.y * v.y); - square_mag :: macro (v: Vector2) => v.x * v.x + v.y * v.y; + square_mag :: (v: Vector2) => v.x * v.x + v.y * v.y; - norm :: macro (v: Vector2) -> Vector2 { + norm :: (v: Vector2) -> Vector2 { l := math.sqrt(v.x * v.x + v.y * v.y); return .{ v.x / l, v.y / l }; } - Zero :: Vector2.{0, 0} } @@ -28,26 +30,36 @@ Vector3i :: struct { x, y, z: i32; } +#inject Vector3i { + manhattan_dist :: (v1, v2: Vector3i) => { + return math.abs(v1.x - v2.x) + + math.abs(v1.y - v2.y) + + math.abs(v1.z - v2.z); + } +} + #tag conv.Custom_Format.{format_vector3} Vector3 :: struct { x, y, z: f32; +} - mag :: macro (v: Vector3) => math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z); +#inject Vector3 { + mag :: (v: Vector3) => math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z); - square_mag :: macro (v: Vector3) => v.x * v.x + v.y * v.y + v.z * v.z; + square_mag :: (v: Vector3) => v.x * v.x + v.y * v.y + v.z * v.z; - neg :: macro (v: Vector3) => Vector3.{ -v.x, -v.y, -v.z }; + neg :: (v: Vector3) => Vector3.{ -v.x, -v.y, -v.z }; - dot :: macro (v1, v2: Vector3) -> f32 { + dot :: (v1, v2: Vector3) -> f32 { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } - norm :: macro (v: Vector3) -> Vector3 { + norm :: (v: Vector3) -> Vector3 { l := math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z); return .{ v.x / l, v.y / l, v.z / l }; } - cross :: macro (v1, v2: Vector3) -> Vector3 { + cross :: (v1, v2: Vector3) -> Vector3 { return .{ v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, @@ -55,7 +67,7 @@ Vector3 :: struct { }; } - clamp :: macro (v: Vector3, min: Vector3, max: Vector3) -> Vector3 { + clamp :: (v: Vector3, min: Vector3, max: Vector3) -> Vector3 { return .{ math.clamp(v.x, min.x, max.x), math.clamp(v.y, min.y, max.y), @@ -64,30 +76,30 @@ Vector3 :: struct { } } -#operator + macro (v1, v2: Vector2i) => (typeof v1).{ v1.x + v2.x, v1.y + v2.y }; -#operator - macro (v1, v2: Vector2i) => (typeof v1).{ v1.x - v2.x, v1.y - v2.y }; -#operator * macro (v: Vector2i, s: i32) => (typeof v ).{ v.x * s, v.y * s }; -#operator * macro (v1, v2: Vector2i) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y }; -#operator == macro (v1, v2: Vector2i) => v1.x == v2.x && v1.y == v2.y; -#match core.hash.to_u32 macro (v: Vector2i) => 13 * v.x + 17 * v.y; - -#operator + macro (v1, v2: Vector2) => (typeof v1).{ v1.x + v2.x, v1.y + v2.y }; -#operator - macro (v1, v2: Vector2) => (typeof v1).{ v1.x - v2.x, v1.y - v2.y }; -#operator * macro (v: Vector2, s: f32) => (typeof v ).{ v.x * s, v.y * s }; -#operator * macro (v1, v2: Vector2) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y }; -#operator == macro (v1, v2: Vector2) => v1.x == v2.x && v1.y == v2.y; - -#operator + macro (v1, v2: Vector3) => Vector3.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; -#operator - macro (v1, v2: Vector3) => Vector3.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; -#operator * macro (v: Vector3, s: f32) => Vector3.{ v.x * s, v.y * s, v.z * s }; -#operator * macro (v1, v2: Vector3) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; -#operator == macro (v1, v2: Vector3) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; - -#operator + macro (v1, v2: Vector3i) => Vector3i.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; -#operator - macro (v1, v2: Vector3i) => Vector3i.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; -#operator * macro (v: Vector3i, s: i32) => Vector3i.{ v.x * s, v.y * s, v.z * s }; -#operator * macro (v1, v2: Vector3i) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; -#operator == macro (v1, v2: Vector3i) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; +#operator + (v1, v2: Vector2i) => Vector2i.{ v1.x + v2.x, v1.y + v2.y }; +#operator - (v1, v2: Vector2i) => Vector2i.{ v1.x - v2.x, v1.y - v2.y }; +#operator * (v: Vector2i, s: i32) => Vector2i.{ v.x * s, v.y * s }; +#operator * (v1, v2: Vector2i) => Vector2i.{ v1.x * v2.x, v1.y * v2.y }; +#operator == (v1, v2: Vector2i) => v1.x == v2.x && v1.y == v2.y; +#match core.hash.to_u32 (v: Vector2i) => 13 * v.x + 17 * v.y; + +#operator + (v1, v2: Vector2) => Vector2.{ v1.x + v2.x, v1.y + v2.y }; +#operator - (v1, v2: Vector2) => Vector2.{ v1.x - v2.x, v1.y - v2.y }; +#operator * (v: Vector2, s: f32) => Vector2.{ v.x * s, v.y * s }; +#operator * (v1, v2: Vector2) => Vector2.{ v1.x * v2.x, v1.y * v2.y }; +#operator == (v1, v2: Vector2) => v1.x == v2.x && v1.y == v2.y; + +#operator + (v1, v2: Vector3) => Vector3.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; +#operator - (v1, v2: Vector3) => Vector3.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; +#operator * (v: Vector3, s: f32) => Vector3.{ v.x * s, v.y * s, v.z * s }; +#operator * (v1, v2: Vector3) => Vector3.{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; +#operator == (v1, v2: Vector3) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; + +#operator + (v1, v2: Vector3i) => Vector3i.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; +#operator - (v1, v2: Vector3i) => Vector3i.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; +#operator * (v: Vector3i, s: i32) => Vector3i.{ v.x * s, v.y * s, v.z * s }; +#operator * (v1, v2: Vector3i) => Vector3i.{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; +#operator == (v1, v2: Vector3i) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; #local { conv :: core.conv @@ -139,7 +151,9 @@ Vector3 :: struct { Rect :: struct { x, y, w, h: f32; +} +#inject Rect { intersects :: (r1, r2: Rect) -> bool { return r1.x < r2.x + r2.w && r1.x + r1.w > r2.x @@ -152,10 +166,10 @@ Rect :: struct { && r.y <= p.y && r.y + r.h >= p.y; } - center :: macro (use r: Rect) => Vector2.{ r.x+r.w/2, r.y+r.h/2 }; + center :: (use r: Rect) => Vector2.{ r.x+r.w/2, r.y+r.h/2 }; - top_left :: macro (use r: Rect) => Vector2.{ r.x, r.y }; - top_right :: macro (use r: Rect) => Vector2.{ r.x+r.w, r.y }; - bottom_left :: macro (use r: Rect) => Vector2.{ r.x, r.y+r.h }; - bottom_right :: macro (use r: Rect) => Vector2.{ r.x+r.w, r.y+r.h }; + top_left :: (use r: Rect) => Vector2.{ r.x, r.y }; + top_right :: (use r: Rect) => Vector2.{ r.x+r.w, r.y }; + bottom_left :: (use r: Rect) => Vector2.{ r.x, r.y+r.h }; + bottom_right :: (use r: Rect) => Vector2.{ r.x+r.w, r.y+r.h }; } diff --git a/src/client/world/chunk.onyx b/src/client/world/chunk.onyx index 4a280ca..9b7ebc3 100644 --- a/src/client/world/chunk.onyx +++ b/src/client/world/chunk.onyx @@ -1,5 +1,5 @@ -use core -use opengles +use core {*} +use opengles {*} Chunk_Vertex :: struct { position : Vector3; @@ -42,7 +42,7 @@ chunk_make :: (x, y, z: i32, allocator := context.allocator) -> ^Chunk { chunk_free :: (chunk: ^Chunk) { chunk_destroy_mesh(chunk); delete(^chunk.blocks); - delete(chunk); + cfree(chunk); } #local in_chunk_bounds :: macro (x, y, z: i32) -> bool { @@ -167,7 +167,7 @@ chunk_destroy_mesh :: (use chunk: ^Chunk) { #local block_texture: Texture; chunk_draw :: (chunk: ^Chunk) { if block_texture.filename.data == null { - block_texture', _ := texture_lookup("assets/textures/block.png"); + block_texture~, _ := texture_lookup("assets/textures/block.png"); } texture_use(^block_texture); @@ -188,7 +188,7 @@ chunk_foreach :: macro (chunk: ^Chunk, body: Code) { chunk_highlight_block :: (x, y, z: f32) { data := 0xf000; - vertex_data := cast(^Chunk_Vertex) alloc.from_stack(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},.{0,0},data}; vertex_data[1] = .{.{x-0.001,y+1.001,z-0.001},.{0,0},data}; vertex_data[2] = .{.{x+1.001,y+1.001,z-0.001},.{0,0},data}; diff --git a/src/client/world/physics.onyx b/src/client/world/physics.onyx index 5406d15..1342907 100644 --- a/src/client/world/physics.onyx +++ b/src/client/world/physics.onyx @@ -1,4 +1,4 @@ -use core +use core {*} AABB :: struct { x0, y0, z0, x1, y1, z1: f32; diff --git a/src/client/world/player.onyx b/src/client/world/player.onyx index 5d6d18f..3565e9a 100644 --- a/src/client/world/player.onyx +++ b/src/client/world/player.onyx @@ -1,5 +1,6 @@ -use core -use glfw3 +use core {*} +use glfw3 {*} +use game_net Player :: struct { camera: ^Camera; // Should the camera exist on the player? Or should the player just control the camera? diff --git a/src/client/world/player_manager.onyx b/src/client/world/player_manager.onyx index 05bf4e6..b15b4f2 100644 --- a/src/client/world/player_manager.onyx +++ b/src/client/world/player_manager.onyx @@ -1,4 +1,4 @@ -use core +use core {*} Remote_Player :: struct { name: str; @@ -44,4 +44,4 @@ Player_Manager :: struct { mesh_draw(Meshes.white_box); } } -} \ No newline at end of file +} diff --git a/src/client/world/world.onyx b/src/client/world/world.onyx index 66d5d5d..2dde6a0 100644 --- a/src/client/world/world.onyx +++ b/src/client/world/world.onyx @@ -1,5 +1,5 @@ -use core +use core {*} World :: struct { chunk_dist: i32; @@ -8,17 +8,21 @@ World :: struct { center_chunk: Vector3i; chunks_to_load: [..] Vector3i; + chunk_mutex: sync.Mutex; + chunk_load_timeout: u32; } world_make :: (allocator := context.allocator) -> ^World { world := new(World, allocator); - world.chunk_dist = 1; + world.chunk_dist = 2; world.center_chunk = .{0,0,0}; sl := world.chunk_dist * 2 + 1; world.chunks = make([] ^Chunk, sl * sl * sl); array.fill(world.chunks, null); + sync.mutex_init(^world.chunk_mutex); + return world; } @@ -46,7 +50,7 @@ world_unload_chunk :: #match { } world_get_chunk :: #match { - @CompilerBug // If I write the following function in quick form: + // @CompilerBug // If I write the following function in quick form: // // (world: ^World, v: Vector3i) => world_get_chunk(world, v.x, v.y, v.z), // @@ -136,6 +140,7 @@ world_move_center :: (use world: ^World, new_center: Vector3i) { sl := world.chunk_dist * 2 + 1; current_chunks := array.copy(world.chunks); + sync.scoped_mutex(^chunk_mutex); array.fill(world.chunks, null); center_chunk = new_center; @@ -153,6 +158,13 @@ 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}; } + nc := new_center; + context->set_user_data(^nc); + array.sort(chunks_to_load, (x, y) => { + c := *(context->get_user_data(Vector3i)); + return y->manhattan_dist(c) - x->manhattan_dist(c); + }); + delete(^current_chunks); } @@ -191,10 +203,14 @@ world_get_aabbs :: (use world: ^World, center: Vector3, radius: f32, buffer: [] world_update :: (use world: ^World, dt: f32) { if chunks_to_load.count > 0 { - chunk_to_load := chunks_to_load[0]; - array.delete(^chunks_to_load, 0); + if chunk_load_timeout == 0 { + chunk_to_load := array.pop(^chunks_to_load); + world_load_chunk(world, chunk_to_load); - world_load_chunk(world, chunk_to_load); + chunk_load_timeout = 5; + } + + chunk_load_timeout -= 1; } } diff --git a/src/client/world/worldgen.onyx b/src/client/world/worldgen.onyx index ae69f8c..fcf0c3b 100644 --- a/src/client/world/worldgen.onyx +++ b/src/client/world/worldgen.onyx @@ -1,5 +1,6 @@ -use core +use core {*} +use perlin #local seed :: 8675309 @@ -11,7 +12,7 @@ generate_chunk :: (world: ^World, chunk: ^Chunk) { n := perlin.noise(px, ~~(10 * seed), pz); l := perlin.noise(px, ~~(10 * seed), pz); - l = (math.clamp(math.max(0, l), 0.2, 0.5) - 0.2) / 0.6 + 0.5; + l = (math.clamp(math.max(l, 0), 0.2, 0.5) - 0.2) / 0.6 + 0.5; h := cast(i32) (n * 32 + 18); h = math.max(h, 14); if n < -0.9 do h += ~~((n + 0.9) * 4); diff --git a/src/config.onyx b/src/config.onyx index 9d0ad7c..ca7f867 100644 --- a/src/config.onyx +++ b/src/config.onyx @@ -4,5 +4,3 @@ package runtime.vars Game_Version :: 0x0001 Game_Port :: cast(u16) 5123 - -#load_path "./../lib" diff --git a/src/server/build.onyx b/src/server/build.onyx index 1a6c22e..3b74d54 100644 --- a/src/server/build.onyx +++ b/src/server/build.onyx @@ -1,6 +1,6 @@ package runtime.vars -#load "core/std" +// #load "core/std" #load_path "./../src/server" #library_path "./lib" @@ -11,4 +11,5 @@ package runtime.vars #load "./../client/utils/vecmath" #load_all "./../shared" +#load_path "./../lib" #load "onyx-net/module" diff --git a/src/server/server.onyx b/src/server/server.onyx index abf004e..f1f22be 100644 --- a/src/server/server.onyx +++ b/src/server/server.onyx @@ -1,8 +1,10 @@ -use core +use core {*} use core.intrinsics.onyx {__initialize} -#local onet :: onyx_net +use packets +use onyx_net {onet :: package} +use runtime Player_Data :: struct { id: u16; @@ -25,7 +27,7 @@ setup_server :: () { addr: net.Socket_Address; addr.port = runtime.vars.Game_Port; - host', err := onet.host_create(^addr, 16); + host~, err := onet.host_create(^addr, 16); if err != .None { println(err); os.exit(1); @@ -88,7 +90,7 @@ handle_packet :: (peer: ^onet.Peer, packet_data: [] u8) { } case .Chat_Message, .Block_Updates { - @TODO // Validation of block updates + // @TODO // Validation of block updates packet := new(onet.Packet); packet.flags |= .Reliable; diff --git a/src/shared/packets.onyx b/src/shared/packets.onyx index 7a9acbd..f514e16 100644 --- a/src/shared/packets.onyx +++ b/src/shared/packets.onyx @@ -2,6 +2,7 @@ package packets use main { Vector3, Vector3i } use main { Block } +use core Type :: enum (u8) { Connect; @@ -97,4 +98,4 @@ Block_Updates :: struct #pack { updates :: (use bu: ^Block_Updates) => ([] Update).{ ~~^update_data, ~~update_count }; -} \ No newline at end of file +}