random additions
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 29 Dec 2021 19:04:02 +0000 (13:04 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 29 Dec 2021 19:04:02 +0000 (13:04 -0600)
run_tree/run.sh
src/build.onyx
src/camera.onyx
src/chunk.onyx
src/font.onyx
src/input.onyx [new file with mode: 0644]
src/main.onyx
src/physics.onyx [new file with mode: 0644]
src/world.onyx

index b4ccbc534fd00a6d403bd8f1b178d031d25f8a8c..a9fbfeaa360bb24ef9769cc11a9dd0b00462ad15 100755 (executable)
@@ -1,2 +1,2 @@
 
-onyx build -I ../src -o game.wasm && onyxrun game.wasm
+onyx build -I ../src -o game.wasm $@ && onyxrun game.wasm
index 310b3d434be6172bd6a8e3760fa4674274a53b55..3cacaa6312ba80f019ad263c841aae42d820f1d6 100644 (file)
@@ -16,6 +16,8 @@
 #load "world"
 #load "font"
 #load "shader"
+#load "input"
+#load "physics"
 
 // Onyx library code
 #load "stb_truetype"
index 070d41be8bf6a7ffcf0efd86bf4c45d4a93decac..c8bc72d9384f2b5ed9a2f982eab18c12b18828cb 100644 (file)
@@ -1,5 +1,8 @@
 use package core
 
+@GlobalVariable
+camera: Camera;
+
 Camera :: struct {
     fov: f32; // Radians
     window_width, window_height: f32;
index 12395d5c689ce1c7072c3f6f71188e3c5e36f6a5..6afef32dbee0e11b609b09eed1cf12e3ab3f0192 100644 (file)
@@ -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};
index fca42bb1e98453360b225f1dab6f3e8631152add..9ba493d29cc25feeb149884126b65eeca649f77f 100644 (file)
@@ -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 (file)
index 0000000..c33f6cb
--- /dev/null
@@ -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) {
+       
+}
index 825eed6b0561e51b1d26d75ee4649079db5d805a..5467f5edba915dfee5b655406af352f54f132c92 100644 (file)
@@ -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 (file)
index 0000000..cf7f9ca
--- /dev/null
@@ -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 {
+
+       }
+}
+
+
index 43f2c53c606a8bbc0233e9b018845ba9e8b1296a..81796ff79314173fe267ef04546facfcc364c643 100644 (file)
@@ -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 };
             }
         }
     }