From 25798fa7426dc9347a4e3b21c3886157b0825983 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 26 Oct 2020 17:24:44 -0500 Subject: [PATCH] speedups... kinda --- include/utils.h | 4 ++-- src/physics.cpp | 2 +- src/sim.cpp | 30 ++++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/include/utils.h b/include/utils.h index e4a2d8b..c2e9881 100644 --- a/include/utils.h +++ b/include/utils.h @@ -149,12 +149,12 @@ struct QuadTree return false; } - void query(AABB r, Array& point_list) const // NOTE(Brendan): By reference may be a terrible idea here. + void query(AABB r, Array* point_list) const { if (!region.intersects(r)) return; foreach (i, 0, point_count) - if (r.contains(points[i]->pos.x, points[i]->pos.y)) point_list.push(points[i]); + if (r.contains(points[i]->pos.x, points[i]->pos.y)) point_list->push(points[i]); if (nw == nullptr) return; diff --git a/src/physics.cpp b/src/physics.cpp index 65d6bbd..2a21459 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -83,7 +83,7 @@ body_accumulate_move(Body* body, QuadTree& qt_bodies, f64 dt) V2f force = { 0.0f, 0.0f }; other_bodies.clear(); - qt_bodies.query(AABB { body->pos.x - 300, body->pos.y - 300, 600, 600 }, other_bodies); + qt_bodies.query(AABB { body->pos.x - 200, body->pos.y - 200, 400, 400 }, &other_bodies); For (other_bodies) { diff --git a/src/sim.cpp b/src/sim.cpp index a754afa..93eceed 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -199,9 +199,9 @@ sim_state_init(SimState* state) { // NOTE(Brendan): Need to initialize the array since it does not get constructed because I refuse to use the 'new' keyword. alloc uses malloc under the hood and cannot initialize the result. state->bodies.init(); - state->bodies.ensure_capacity(1024); + state->bodies.ensure_capacity(2048); - foreach (i, 0, 1024) + foreach (i, 0, 2048) { Body tmp_body; tmp_body.pos = V2f{ randf(0, 1200), randf(0, 1200) }; @@ -211,7 +211,7 @@ sim_state_init(SimState* state) state->bodies.push(tmp_body); } - state->qt_body_allocator.init(1024); + state->qt_body_allocator.init(2048); } internal f64 left_over_dt = 0.0; @@ -229,7 +229,22 @@ update(SimState* state, f64 dt) For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator); For (state->bodies) body_accumulate_move(&it, state->qt_bodies, step); - For (state->bodies) body_apply_move(&it); + For (state->bodies) { + body_apply_move(&it); + /* + if (it.pos.x < -window_width) it.pos.x = -window_width; + else if (it.pos.x >= window_width * 2) it.pos.x = window_width * 2; + + if (it.pos.y < -window_height) it.pos.y = -window_height; + else if (it.pos.y >= window_height * 2) it.pos.y = window_height * 2; +*/ + + if (it.pos.x < 0) it.pos.x = 0; + else if (it.pos.x >= window_width) it.pos.x = window_width; + + if (it.pos.y < 0) it.pos.y = 0; + else if (it.pos.y >= window_height) it.pos.y = window_height; + } left_over_dt -= step; } @@ -278,6 +293,7 @@ loop(SimState* state) f64 frame_delta = 0.0; i32 frame_count = 0; + clock_t total_clock_delta = 0; while (!glfwWindowShouldClose(window)) { @@ -287,7 +303,10 @@ loop(SimState* state) delta = curr_time - last_time; last_time = curr_time; + clock_t before_update = clock(); update(state, delta); + clock_t after_update = clock(); + total_clock_delta += (after_update - before_update); frame_count += 1; draw(state); @@ -295,6 +314,9 @@ loop(SimState* state) frame_delta += delta; if (frame_delta >= 1.0) { + logprint(LOG_LEVEL_INFO, "AVG UPD CLK: %10.6f", (f64) (total_clock_delta / frame_count) / CLOCKS_PER_SEC); + + total_clock_delta = 0; frame_delta -= 1.0; logprint(LOG_LEVEL_INFO, "FPS: %d", frame_count); frame_count = 0; -- 2.25.1