size = V2f.{ w, h },
color = color,
- tex_pos = V2f.{ -1.0f, -1.0f },
+ tex_pos = V2f.{ -1.0f, -1.0f }
});
curr_quad_idx += 1;
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);
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);
tilemap_draw(^tilemap, ^renderer);
+ // draw_quad_tree(^dude_tree);
+
dude_sprite := atlas_lookup(^atlas, 0);
for ^d: dudes {
aabb := dude_get_aabb(d);
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);
// 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 {
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());
}
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;
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,
};
}
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 {
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 {
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;
+++ /dev/null
-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 };
-}