From 953e839371b96567e99567f9c5ac9952dce345fe Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 29 Sep 2020 11:07:52 -0500 Subject: [PATCH] bugfixes with input and quads --- src/input.onyx | 2 +- src/main.onyx | 39 ++++++++++++++++++++++----------------- src/quad_tree.onyx | 32 ++++++++++++++++---------------- tags | 9 ++++++--- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/input.onyx b/src/input.onyx index bf9a110..4096d1d 100644 --- a/src/input.onyx +++ b/src/input.onyx @@ -12,7 +12,7 @@ Key :: enum { ArrowRight :: 0x27; ArrowDown :: 0x28; - Space :: 0x32; + Space :: 0x20; A :: 0x41; B; C; D; E; F; G; H; I; J; K; L; M; N; O; P; Q; R; S; T; U; V; W; X; Y; Z; } diff --git a/src/main.onyx b/src/main.onyx index 407de6b..110a4ba 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -26,7 +26,7 @@ use package ttf_font use package quad_tree use package aabb -NUM_QUADS :: 1 << 17 +NUM_QUADS :: 1 << 14 RenderContext :: struct { quad_renderer: ^QuadRenderer; @@ -128,25 +128,31 @@ poll_events :: proc () { quad_scratch_buffer : [512 * 1024] u8; +simulating := true; update :: proc () { input.preupdate(^input_state); defer input.postupdate(^input_state); poll_events(); - quad_scratch : ScratchState; - quad_alloc : Allocator; - scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 128 * 1024); - scratch_alloc_init(^quad_alloc, ^quad_scratch); + if input.key_just_down(^input_state, Key.Space) do simulating = !simulating; - quadtree_init(^dude_tree, AABB.{ 0.0f, 0.0f, ~~window_width, ~~window_height }); - for ^d: dudes do quadtree_insert(^dude_tree, d, quad_alloc); + if simulating { + quad_scratch : ScratchState; + quad_alloc : Allocator; + scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 512 * 1024); + scratch_alloc_init(^quad_alloc, ^quad_scratch); - for ^d: dudes do dude_update(d, ^dude_tree); - for ^d: dudes { - if d.can_move_x do d.pos.x += d.vel.x; - else do d.vel.x = -d.vel.x; - if d.can_move_y do d.pos.y += d.vel.y; - else do d.vel.y = -d.vel.y; + quadtree_init(^dude_tree, AABB.{ 0.0f, 0.0f, ~~window_width, ~~window_height }); + for ^d: dudes do quadtree_insert(^dude_tree, d, quad_alloc); + + for ^d: dudes do dude_update(d, ^dude_tree); + for ^d: dudes { + if !d.can_move_x do d.vel.x = -d.vel.x; + if !d.can_move_y do d.vel.y = -d.vel.y; + + d.pos.x += d.vel.x; + d.pos.y += d.vel.y; + } } } @@ -169,7 +175,7 @@ draw :: proc () { } draw_quad_tree :: proc (qt: ^QuadTree($T), level := 0, corner := 0) { - col := cast(f32) (0.2f + 0.15f * ~~level); + col := cast(f32) (0.0f + ~~ qt.points_count * 0.25f); if col > 1.0f do col = 1.0f; color := Color4f32.{ 0.0f, 0.0f, 0.0f, 1.0f }; @@ -227,7 +233,7 @@ main :: proc (args: [] cstring) { input.init(^input_state); array_init(^dudes); - for i: 0 .. 500 { + for i: 0 .. 2500 { array_push(^dudes, dude_create_random()); } @@ -250,7 +256,7 @@ Dude :: struct { dude_create_random :: proc () -> Dude { return Dude.{ - pos = V2f.{ random_float(0.0f, 300.0f), random_float(0.0f, 300.0f) }, + pos = V2f.{ random_float(0.0f, 1000.0f), random_float(0.0f, 1000.0f) }, vel = V2f.{ random_float(-3.0f, 3.0f), random_float(-3.0f, 3.0f) }, size = random_float(2.0f, 3.0f), color = Dude_Color_Table[random_between(0, 3)], @@ -287,7 +293,6 @@ dude_try_move :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) { other_aabb := dude_get_aabb(other); if aabb_intersects(dude_aabb, other_aabb) { - // printf("COLLISION\n"); collided = true; break; } diff --git a/src/quad_tree.onyx b/src/quad_tree.onyx index 4ec3e21..f57a62c 100644 --- a/src/quad_tree.onyx +++ b/src/quad_tree.onyx @@ -4,12 +4,12 @@ use package core use package aabb #private_file -QUAD_TREE_ENTITY_STORAGE :: 2; +QUAD_TREE_MAX_POINTS :: 4; QuadTree :: struct ($T) { region : AABB; - points : [QUAD_TREE_ENTITY_STORAGE] ^T; + points : [QUAD_TREE_MAX_POINTS] ^T; points_count : u32 = 0; nw: ^QuadTree(T) = null; @@ -29,18 +29,18 @@ quadtree_init :: proc (qt: ^QuadTree($T), initial_region: AABB) { for ^p: qt.points do *p = null; } -// quadtree_free :: proc (qt: ^QuadTree($T), initial := true) { -// if qt.nw != null { -// quadtree_free(qt.nw, false); -// quadtree_free(qt.ne, false); -// quadtree_free(qt.sw, false); -// quadtree_free(qt.se, false); -// } -// -// if !initial { -// cfree(qt); -// } -// } +quadtree_free :: proc (qt: ^QuadTree($T), initial := true) { + if qt.nw != null { + quadtree_free(qt.nw, false); + quadtree_free(qt.ne, false); + quadtree_free(qt.sw, false); + quadtree_free(qt.se, false); + } + + if !initial { + cfree(qt); + } +} quadtree_subdivide :: proc (use qt: ^QuadTree($T), a: Allocator) { if nw != null do return; @@ -54,7 +54,7 @@ quadtree_subdivide :: proc (use qt: ^QuadTree($T), a: Allocator) { qt.se = alloc(a, sizeof QuadTree(T)); quadtree_init(qt.nw, AABB.{ region.x, region.y, hw, hh }); - quadtree_init(qt.ne, AABB.{ region.x + hw, region.y, hw, hh }); + quadtree_init(qt.ne, AABB.{ region.x + hw, region.y, hw, hh }); quadtree_init(qt.sw, AABB.{ region.x, region.y + hh, hw, hh }); quadtree_init(qt.se, AABB.{ region.x + hw, region.y + hh, hw, hh }); } @@ -64,7 +64,7 @@ quadtree_insert :: proc (use qt: ^QuadTree($T), t: ^T, alloc := context.allocato if !aabb_contains(region, pos) do return false; - if points_count < QUAD_TREE_ENTITY_STORAGE && nw == null { + if points_count < QUAD_TREE_MAX_POINTS && nw == null { points[points_count] = t; points_count += 1; return true; diff --git a/tags b/tags index ed3f28b..35910a3 100644 --- a/tags +++ b/tags @@ -313,7 +313,7 @@ NICEST /usr/share/onyx/core/js/webgl.onyx /^NICEST :: 0x NONE /usr/share/onyx/core/js/webgl.onyx /^NONE :: 0$/ NOTEQUAL /usr/share/onyx/core/js/webgl.onyx /^NOTEQUAL :: 0x0205$/ NO_ERROR /usr/share/onyx/core/js/webgl.onyx /^NO_ERROR :: 0$/ -NUM_QUADS src/main.onyx /^NUM_QUADS :: 1 << 10$/ +NUM_QUADS src/main.onyx /^NUM_QUADS :: 1 << 14$/ OBJECT_TYPE /usr/share/onyx/core/js/webgl.onyx /^OBJECT_TYPE :: 0x9112$/ ONE /usr/share/onyx/core/js/webgl.onyx /^ONE :: 1$/ ONE_MINUS_CONSTANT_ALPHA /usr/share/onyx/core/js/webgl.onyx /^ONE_MINUS_CONSTANT_ALPHA :: 0x8004$/ @@ -730,6 +730,7 @@ cstring /usr/share/onyx/core/builtin.onyx /^cstring :: #type ^u8;$/ ctz_i32 /usr/share/onyx/core/intrinsics.onyx /^ctz_i32 :: proc (val: i32) -> i32 #intrinsic ---$/ ctz_i64 /usr/share/onyx/core/intrinsics.onyx /^ctz_i64 :: proc (val: i64) -> i64 #intrinsic ---$/ cullFace /usr/share/onyx/core/js/webgl.onyx /^cullFace :: proc (mode: GLenum) #foreign "gl" "cullFace" ---$/ +debugger src/main.onyx /^debugger :: proc () #foreign "dummy" "breakable" ---$/ deleteBuffer /usr/share/onyx/core/js/webgl.onyx /^deleteBuffer :: proc (buffer: GLBuffer) #foreign "gl" "deleteBuffer" ---$/ deleteFramebuffer /usr/share/onyx/core/js/webgl.onyx /^deleteFramebuffer :: proc (framebuffer: GLFramebuffer) #foreign "gl" "deleteFramebuffer" ---$/ deleteProgram /usr/share/onyx/core/js/webgl.onyx /^deleteProgram :: proc (program: GLProgram) #foreign "gl" "deleteProgram" ---$/ @@ -749,7 +750,7 @@ drawArraysInstanced /usr/share/onyx/core/js/webgl.onyx /^drawArraysInstanced drawElements /usr/share/onyx/core/js/webgl.onyx /^drawElements :: proc (mode: GLenum, count: GLsizei, type: GLenum, offset: GLint) #foreign "gl" "drawElements" ---$/ drawElementsInstanced /usr/share/onyx/core/js/webgl.onyx /^drawElementsInstanced :: proc (mode: GLenum, count: GLsizei, type: GLenum, offset: GLint, instanceCount: GLsizei) #foreign "gl" "drawElementsInstanced" ---$/ draw_quad src/main.onyx /^draw_quad :: proc (use rc: ^RenderContext, quad: ^Quad) {$/ -draw_quad_tree src/main.onyx /^draw_quad_tree :: proc (qt: ^QuadTree($T), level := 0, corner := 4) {$/ +draw_quad_tree src/main.onyx /^draw_quad_tree :: proc (qt: ^QuadTree($T), level := 0, corner := 0) {$/ draw_rect src/main.onyx /^draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) {$/ draw_textured_rect src/main.onyx /^draw_textured_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32, tx: f32, ty: f32, tw: f32, th: f32) {$/ dude_create_random src/main.onyx /^dude_create_random :: proc () -> Dude {$/ @@ -869,8 +870,9 @@ quad_rebuffer_data src/gfx/quad_renderer.onyx /^quad_rebuffer_data :: proc (use quad_renderer_draw src/gfx/quad_renderer.onyx /^quad_renderer_draw :: proc (use qr: ^QuadRenderer, count := -1) {$/ quad_renderer_init src/gfx/quad_renderer.onyx /^quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {$/ quad_renderer_update_view src/gfx/quad_renderer.onyx /^quad_renderer_update_view :: proc (use qr: ^QuadRenderer) {$/ -quad_scratch_buffer src/main.onyx /^quad_scratch_buffer : [128 * 1024] u8;$/ +quad_scratch_buffer src/main.onyx /^quad_scratch_buffer : [512 * 1024] u8;$/ quad_update_at_index src/gfx/quad_renderer.onyx /^quad_update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) {$/ +quadtree_free src/quad_tree.onyx /^quadtree_free :: proc (qt: ^QuadTree($T), initial := true) {$/ quadtree_init src/quad_tree.onyx /^quadtree_init :: proc (qt: ^QuadTree($T), initial_region: AABB) {$/ quadtree_insert src/quad_tree.onyx /^quadtree_insert :: proc (use qt: ^QuadTree($T), t: ^T, alloc := context.allocator) -> bool {$/ quadtree_query src/quad_tree.onyx /^quadtree_query :: proc (use qt: ^QuadTree($T), r: AABB, p: ^[..] ^T) {$/ @@ -902,6 +904,7 @@ seed /usr/share/onyx/core/random.onyx /^seed := 8675309$/ shaderSource /usr/share/onyx/core/js/webgl.onyx /^shaderSource :: proc (shader: GLShader, source: string) #foreign "gl" "shaderSource" ---$/ shl_i32 /usr/share/onyx/core/intrinsics.onyx /^shl_i32 :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/ shl_i64 /usr/share/onyx/core/intrinsics.onyx /^shl_i64 :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/ +simulating src/main.onyx /^simulating := true;$/ sin /usr/share/onyx/core/math.onyx /^sin :: proc (t_: f32) -> f32 {$/ slr_i32 /usr/share/onyx/core/intrinsics.onyx /^slr_i32 :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/ slr_i64 /usr/share/onyx/core/intrinsics.onyx /^slr_i64 :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/ -- 2.25.1