speedups... kinda
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 26 Oct 2020 22:24:44 +0000 (17:24 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 26 Oct 2020 22:24:44 +0000 (17:24 -0500)
include/utils.h
src/physics.cpp
src/sim.cpp

index e4a2d8bf7a8a81339add40fd87abc444851393c4..c2e9881d08951c7105e89a565426293bc0fe6191 100644 (file)
@@ -149,12 +149,12 @@ struct QuadTree
         return false;
     }
     
-    void query(AABB r, Array<T*>& point_list) const // NOTE(Brendan): By reference may be a terrible idea here.
+    void query(AABB r, Array<T*>* 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;
         
index 65d6bbd8d601c21baeaefa1d0886bc8df7713c26..2a21459f1e51958a74fa3176cbde0b184b950755 100644 (file)
@@ -83,7 +83,7 @@ body_accumulate_move(Body* body, QuadTree<Body>& 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)
     {
index a754afac7232aae33a0949de864db8ec21ff1b16..93eceedf4822d65a6520f566c9e30aa7fe6a86a7 100644 (file)
@@ -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<T> 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;