From: Brendan Hansen Date: Sun, 11 Oct 2020 15:38:05 +0000 (-0500) Subject: small changes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a97557946c3db22939120bebc85f1301c95fc18b;p=onyx-game.git small changes --- diff --git a/src/gfx/font.onyx b/src/gfx/font.onyx index f3e4415..84957c8 100644 --- a/src/gfx/font.onyx +++ b/src/gfx/font.onyx @@ -6,7 +6,7 @@ use package vecmath use package main { RenderContext, draw_textured_rect } #private_file -font_chars :: "0123456789!\"#$% ABCDEFGHIJKLMNOPQRSTUVWXYZ'()*+,abcdefghijklmnopqrstuvwxyz,-./\\,^:;<=>?[]~|_"; +font_chars := "0123456789!\"#$% ABCDEFGHIJKLMNOPQRSTUVWXYZ'()*+,abcdefghijklmnopqrstuvwxyz,-./\\,^:;<=>?[]~|_"; #private_file char_to_tex_offset :: proc (ch: u8) -> u32 { @@ -29,5 +29,5 @@ draw_text :: proc (renderer: ^RenderContext, s: string, pos: V2f, scale := 1.0f) draw_textured_rect(renderer, x, y, font_size, font_size, char_sprite.x, char_sprite.y, char_sprite.w, char_sprite.h); x += font_size * 0.85f; - } + } } \ No newline at end of file diff --git a/src/main.onyx b/src/main.onyx index f8ec46d..b0cfcd6 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -65,7 +65,7 @@ draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) { size = V2f.{ w, h }, color = color, - tex_pos = V2f.{ -1.0f, -1.0f }, + tex_pos = V2f.{ -1.0f, -1.0f } }); curr_quad_idx += 1; @@ -122,7 +122,7 @@ poll_events :: proc () { half_window_width = window_width / 2; half_window_height = window_height / 2; - tilemap.origin = V2f.{ ~~-half_window_width, ~~-half_window_height }; + // tilemap.origin = V2f.{ ~~-half_window_width, ~~-half_window_height }; gl.canvasSize(window_width, window_height); gl.viewport(0, 0, window_width, window_height); @@ -163,7 +163,7 @@ update :: proc (dt: f32) { scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 512 * 1024); scratch_alloc_init(^quad_alloc, ^quad_scratch); - quadtree_init(^dude_tree, AABB.{ ~~-half_window_width, ~~-half_window_height, ~~window_width, ~~window_height }); + quadtree_init(^dude_tree, AABB.{ 0.0f, 0.0f, ~~tilemap.width * TILE_SIZE, ~~tilemap.height * TILE_SIZE}); for ^d: dudes do quadtree_insert(^dude_tree, d, quad_alloc); for ^d: dudes do dude_update(d, dt, ^dude_tree); @@ -192,6 +192,8 @@ draw :: proc () { tilemap_draw(^tilemap, ^renderer); + // draw_quad_tree(^dude_tree); + dude_sprite := atlas_lookup(^atlas, 0); for ^d: dudes { aabb := dude_get_aabb(d); @@ -207,7 +209,7 @@ draw :: proc () { draw_rect(^renderer, 10.0f, 10.0f, 1000.0f, 50.0f); renderer.color = Color4f32.{ 0.0f, 0.0f, 0.0f, 1.0f }; - draw_text(^renderer, "Hello. Test(12486),$! 0AaQq:", V2f.{ 10.0f, 10.0f }, 2.0f); + draw_text(^renderer, "Hello. Test(12486)", V2f.{ 10.0f, 10.0f }, 2.0f); draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 10f, 10f); render_context_ui(^renderer); @@ -218,7 +220,7 @@ debugger :: proc () #foreign "dummy" "breakable" --- // This procedure is called asynchronously from JS every frame. -// @CLEANUP: Add local persistant variables so this can go in the loop() body. +// @CLEANUP: Add local persistent variables so this can go in the loop() body. last_time := 0; loop :: proc () #export { @@ -267,7 +269,7 @@ main :: proc (args: [] cstring) { Dude_Color_Table[2] = Color4f32.{ 0.2f, 0.2f, 1.0f, 1.0f }; array_init(^dudes); - for i: 0 .. 1000 { + for i: 0 .. 2000 { array_push(^dudes, dude_create_random()); } @@ -280,10 +282,15 @@ main :: proc (args: [] cstring) { Dude_Color_Table : [3] Color4f32; -Dude :: struct { +Entity :: struct { pos : V2f; vel : V2f; size : f32; +} + +Dude :: struct { + use entity: Entity; + color : Color4f32; can_move_x : bool = true; @@ -293,10 +300,16 @@ Dude :: struct { dude_create_random :: proc () -> Dude { return Dude.{ - pos = V2f.{ random_float(-600.0f, 600.0f), random_float(-600.0f, 600.0f) }, + pos = V2f.{ random_float(0.0f, 1200.0f), random_float(0.0f, 1200.0f) }, vel = V2f.{ random_float(-3.0f, 3.0f), random_float(-3.0f, 3.0f) }, size = random_float(4.0f, 6.0f), color = Dude_Color_Table[random() % 3], + + // BUG: These should not have to be here but because of the "use entity" in the defintion of Dude, they do. + // I presume this is because entity counts as toward the number of needed elements, even though it shouldn't. + can_move_x = false, + can_move_y = false, + couldnt_move_count = ~~0, }; } @@ -337,7 +350,7 @@ dude_try_move :: proc (use dude: ^Dude, dt: f32, other_dudes: ^QuadTree(Dude)) { collided := false; pos.x += vel.x * dt; - if pos.x - size < ~~-half_window_width || pos.x + size >= ~~half_window_width do collided = true; + if !aabb_contains(other_dudes.region, pos) do collided = true; dude_aabb := dude_get_aabb(dude); for other: potential_dudes { @@ -354,7 +367,8 @@ dude_try_move :: proc (use dude: ^Dude, dt: f32, other_dudes: ^QuadTree(Dude)) { collided = false; pos.y += vel.y * dt; - if pos.y - size < ~~-half_window_height || pos.y + size >= ~~half_window_height do collided = true; +// if pos.y - size < ~~-half_window_height || pos.y + size >= ~~half_window_height do collided = true; + if !aabb_contains(other_dudes.region, pos) do collided = true; dude_aabb = dude_get_aabb(dude); for other: potential_dudes { @@ -373,7 +387,7 @@ dude_get_aabb :: proc (use dude: ^Dude) -> AABB { return AABB.{ x = pos.x - size, y = pos.y - size, w = size * 2.0f, h = size * 2.0f }; } -TILE_SIZE :: 10.0f; +TILE_SIZE :: 20.0f; Tilemap :: struct { width : u32; diff --git a/src/main_backup.onyx b/src/main_backup.onyx deleted file mode 100644 index 591514f..0000000 --- a/src/main_backup.onyx +++ /dev/null @@ -1,297 +0,0 @@ -package main - -#include_file "core/std/js" -#include_file "core/js/webgl" - -#include_folder "src/" -#include_file "utils/gl" -#include_file "gfx/quad_renderer" -#include_file "gfx/texture" -#include_file "gfx/atlas" -#include_file "events" -#include_file "input" -#include_file "font" -#include_file "vecmath" -#include_file "aabb" -#include_file "quad_tree" - -use package core -use package gfx -use package gl as gl -use package event as event -use package input as input { Key } -use package vecmath -use package ttf_font - -use package quad_tree -use package aabb - -NUM_QUADS :: 1 << 10 - -RenderContext :: struct { - quad_renderer: ^QuadRenderer; - - curr_quad_idx: i32 = 0; - max_quad_idx: i32 = 0; - - color: Color4f32 = Color4f32.{ 1.0f, 1.0f, 1.0f, 1.0f }; -} - -render_context_init :: proc (use rc: ^RenderContext, qr: ^QuadRenderer = null) { - aqr := qr; - - if aqr == null { - aqr = calloc(sizeof QuadRenderer); - quad_renderer_init(aqr, NUM_QUADS); - } - - *rc = RenderContext.{ - quad_renderer = aqr, - max_quad_idx = qr.quad_data.count, - }; -} - -draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) { - if curr_quad_idx >= max_quad_idx do return; - - quad_update_at_index(quad_renderer, curr_quad_idx, Quad.{ - pos = V2f.{ x, y }, - size = V2f.{ w, h }, - color = color, - - tex_pos = V2f.{ -1.0f, -1.0f }, - }); - - curr_quad_idx += 1; -} - -draw_textured_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32, tx: f32, ty: f32, tw: f32, th: f32) { - if curr_quad_idx >= max_quad_idx do return; - - quad_update_at_index(quad_renderer, curr_quad_idx, Quad.{ - pos = V2f.{ x, y }, - size = V2f.{ w, h }, - color = color, - - tex_pos = V2f.{ tx, ty }, - tex_size = V2f.{ tw, th }, - }); - - curr_quad_idx += 1; -} - -draw_quad :: proc (use rc: ^RenderContext, quad: ^Quad) { - if curr_quad_idx >= max_quad_idx do return; - quad.color = color; - quad_update_at_index(quad_renderer, curr_quad_idx, *quad); - curr_quad_idx += 1; -} - -render_context_flush :: proc (use rc: ^RenderContext) { - quad_rebuffer_data(quad_renderer); - quad_renderer_draw(quad_renderer, curr_quad_idx); - - // for i: 0 .. curr_quad_idx do quad_update_at_index(quad_renderer, i, Quad.{}); - - curr_quad_idx = 0; -} - - - -// @Cleanup -window_width := 0 -window_height := 0 - -renderer : RenderContext -input_state : input.InputState -atlas : Atlas -dudes : [..] Dude -dude_tree : QuadTree(Dude) - -poll_events :: proc () { - use event.DomEventKind; - - ev : event.Event; - while event.poll(^ev) do switch ev.kind { - case KeyDown, KeyUp, MouseDown, MouseUp, MouseMove do input.process_event(^input_state, ^ev); - - case Resize { - window_width = ev.resize.width; - window_height = ev.resize.height; - - gl.canvasSize(window_width, window_height); - gl.viewport(0, 0, window_width, window_height); - - quad_renderer_update_view(renderer.quad_renderer); - } - } -} - -quad_scratch_buffer : [128 * 1024] u8; - -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); - - 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); -} - -draw :: proc () { - gl.clearColor(0.1f, 0.1f, 0.1f, 1.0f); - gl.clear(gl.COLOR_BUFFER_BIT); - - texture_use(^textures.tilemap); - draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 10f, 10f); - - for ^d: dudes { - aabb := dude_get_aabb(d); - draw_rect(^renderer, aabb.x, aabb.y, aabb.w, aabb.h); - } - - render_context_flush(^renderer); -} - -// This procedure is called asynchronously from JS every frame. -new_frame_callback :: proc () #export { - update(); - draw(); -} - -main :: proc (args: [] cstring) { - println("Setting up WebGL2 canvas..."); - - if !gl.init("gamecanvas") { - print("Failed to initialize GL canvas."); - return; - } - - gl.enable(gl.CULL_FACE); - gl.cullFace(gl.BACK); - - gl.enable(gl.BLEND); - gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); - - render_context_init(^renderer, null); - textures_init(); - - atlas = atlas_create(^textures.tilemap); - atlas_map(^atlas, 0, AtlasSprite.{ x = 16, y = 0, w = 16, h = 16 }); - - event.init(); - input.init(^input_state); - - array_init(^dudes); - for i: 0 .. 50 { - array_push(^dudes, dude_create_random()); - } - // array_push(^dudes, Dude.{ - // pos = V2f.{ 100.0f, 100.0f }, - // vel = V2f.{ 2.0f, 0.0f }, - // size = 10.0f, - // color = Color4f32.{ 1.0f, 1.0f, 1.0f, 1.0f }, - // }); - - // array_push(^dudes, Dude.{ - // pos = V2f.{ 1000.0f, 100.0f }, - // vel = V2f.{ -2.0f, 0.0f }, - // size = 10.0f, - // color = Color4f32.{ 1.0f, 1.0f, 1.0f, 1.0f }, - // }); - - game_launch :: proc () #foreign "game" "launch" ---; - game_launch(); -} - - -Dude_Color_Table : [4] Color4f32; - -Dude :: struct { - pos : V2f; - vel : V2f; - size : f32; - color : Color4f32; -} - -dude_create_random :: proc () -> Dude { - return Dude.{ - pos = V2f.{ random_float(0.0f, 800.0f), random_float(0.0f, 800.0f) }, - vel = V2f.{ random_float(-3.0f, 3.0f), random_float(-3.0f, 3.0f) }, - size = random_float(5.0f, 30.0f), - color = Dude_Color_Table[random_between(0, 3)], - }; -} - -dude_update :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) { - // if random_between(0, 100) < 2 { - // vel.x = random_float(-2.0f, 2.0f); - // vel.y = random_float(-2.0f, 2.0f); - // } - - dude_try_move(dude, other_dudes); -} - -dude_try_move :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) { - old_pos := pos; - - potential_dudes : [..] ^Dude; - array_init(^potential_dudes); - defer array_free(^potential_dudes); - - around := AABB.{ x = pos.x - size * 10.0f, y = pos.y - size * 10.0f, w = size * 20.0f, h = size * 20.0f }; - quadtree_query(other_dudes, around, ^potential_dudes); - - collided := false; - - pos.x += vel.x; - if pos.x - size < 0.0f || pos.x + size >= ~~window_width do collided = true; - - dude_aabb := dude_get_aabb(dude); - for other: potential_dudes { - if other == dude do continue; - - other_aabb := dude_get_aabb(other); - if aabb_intersects(dude_aabb, other_aabb) { - collided = true; - break; - } - } - - if collided { - pos.x -= vel.x; - vel.x = -vel.x; - } - - collided = false; - - pos.y += vel.y; - if pos.y - size < 0.0f || pos.y + size >= ~~window_height do collided = true; - - dude_aabb = dude_get_aabb(dude); - for other: potential_dudes { - if other == dude do continue; - - other_aabb := dude_get_aabb(other); - if aabb_intersects(dude_aabb, other_aabb) { - collided = true; - break; - } - } - - if collided { - pos.y -= vel.y; - vel.y = -vel.y; - } -} - -dude_get_aabb :: proc (use dude: ^Dude) -> AABB { - return AABB.{ x = pos.x - size, y = pos.y - size, w = size * 2.0f, h = size * 2.0f }; -} diff --git a/tags b/tags index 92e0848..14ac687 100644 --- a/tags +++ b/tags @@ -546,7 +546,7 @@ TEXTURE_MIN_LOD /usr/share/onyx/core/js/webgl.onyx /^TEXTURE_MIN_LOD TEXTURE_WRAP_R /usr/share/onyx/core/js/webgl.onyx /^TEXTURE_WRAP_R :: 0x8072$/ TEXTURE_WRAP_S /usr/share/onyx/core/js/webgl.onyx /^TEXTURE_WRAP_S :: 0x2802$/ TEXTURE_WRAP_T /usr/share/onyx/core/js/webgl.onyx /^TEXTURE_WRAP_T :: 0x2803$/ -TILE_SIZE src/main.onyx /^TILE_SIZE :: 10.0f;$/ +TILE_SIZE src/main.onyx /^TILE_SIZE :: 20.0f;$/ TIMEOUT_EXPIRED /usr/share/onyx/core/js/webgl.onyx /^TIMEOUT_EXPIRED :: 0x911B$/ TIMEOUT_IGNORED /usr/share/onyx/core/js/webgl.onyx /^TIMEOUT_IGNORED :: -1$/ TRANSFORM_FEEDBACK /usr/share/onyx/core/js/webgl.onyx /^TRANSFORM_FEEDBACK :: 0x8E22$/