code cleanup
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 31 Oct 2020 13:41:15 +0000 (08:41 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 31 Oct 2020 13:41:15 +0000 (08:41 -0500)
include/utils.h
src/physics.cpp
src/sim.cpp

index 6481b97222cec7caefaaff9591027cd4767cb4bf..0af574f77933a9a425174868d6e36259557247c4 100644 (file)
@@ -80,7 +80,7 @@ struct AABB
     bool intersects(AABB other) const;
 };
 
-#define QUAD_TREE_POINTS_PER_NODE 8
+#define QUAD_TREE_POINTS_PER_NODE 16
 template <typename T>
 struct QuadTree
 {
index a357cb73e13f63587a23d2c53ec91d219e27569f..9c497f162b83ebd553220f677805133a6730c57d 100644 (file)
@@ -76,7 +76,6 @@ void nocheckin_init_other_bodies()
         }
     }
 
-    other_bodies.init();
     other_bodies.ensure_capacity(1024);
 }
 
index f718c3ab85461644111555133334c92b219de32b..b132a52d9c36bef356336bc41b87ff43c9b43269 100644 (file)
@@ -30,7 +30,7 @@
 #define PARTICLE_COUNT 2500
 
 // :ArbitraryConstant
-#define NUM_THREADS 4
+#define NUM_THREADS 2
 
 // TODO(Brendan): Maybe this can be removed because it isn't really necessary?
 internal void
@@ -122,6 +122,13 @@ create_circle_mesh()
     return vao;
 }
 
+struct SimState;
+struct ThreadData
+{
+    i32 id;
+    SimState* state;
+};
+
 struct SimState
 {
     Array<Body> bodies;
@@ -129,6 +136,10 @@ struct SimState
     FixedArenaAllocator<QuadTree<Body>> qt_body_allocator;
 
     Camera camera;
+
+    pthread_barrier_t thread_sync_barrier;
+    pthread_t threads[NUM_THREADS - 1];
+    ThreadData thread_data[NUM_THREADS - 1];
 };
 
 internal void
@@ -139,26 +150,10 @@ update_bodies_partial(SimState *state, i32 index)
 
     persist const f64 step = 0.01;
 
-    foreach (i, low, high)
-    {
-        body_accumulate_move(&state->bodies[i], &state->qt_bodies, step);
-    }
-
-    foreach (i, low, high)
-    {
-        body_apply_move(&state->bodies[i], step);
-    }
+    foreach (i, low, high) body_accumulate_move(&state->bodies[i], &state->qt_bodies, step);
+    foreach (i, low, high) body_apply_move(&state->bodies[i], step);
 }
 
-internal pthread_barrier_t thread_sync_barrier;
-internal pthread_t threads[NUM_THREADS - 1];
-
-struct ThreadData
-{
-    i32 id;
-    SimState* state;
-};
-
 internal void*
 thread_start(void* data)
 {
@@ -167,16 +162,16 @@ thread_start(void* data)
 
     while (true)
     {
-        pthread_barrier_wait(&thread_sync_barrier);
+        pthread_barrier_wait(&state->thread_sync_barrier);
         update_bodies_partial(state, thread_id + 1);
-        pthread_barrier_wait(&thread_sync_barrier);
+        pthread_barrier_wait(&state->thread_sync_barrier);
         if (thread_id == 0)
         {
             state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 });
             state->qt_body_allocator.reset();
             For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator);
         }
-        pthread_barrier_wait(&thread_sync_barrier);
+        pthread_barrier_wait(&state->thread_sync_barrier);
     }
 
     return NULL;
@@ -204,14 +199,12 @@ sim_state_init(SimState* state)
 
     state->camera.scale = 1.0f;
 
-    pthread_barrier_init(&thread_sync_barrier, NULL, NUM_THREADS);
+    pthread_barrier_init(&state->thread_sync_barrier, NULL, NUM_THREADS);
 
     foreach (i, 0, NUM_THREADS - 1)
     {
-        auto td = alloc<ThreadData>(1); // LEAK
-        td->id = i;
-        td->state = state;
-        pthread_create(&threads[i], NULL, thread_start, (void *) td);
+        state->thread_data[i] = ThreadData { i, state };
+        pthread_create(&state->threads[i], NULL, thread_start, (void *) &state->thread_data[i]);
     }
 }
 
@@ -229,10 +222,10 @@ update(SimState* state, f64 dt)
     if (glfwGetKey(window, GLFW_KEY_Q))     state->camera.scale *= 1.02f;
     if (glfwGetKey(window, GLFW_KEY_A))     state->camera.scale /= 1.02f;
 
-    pthread_barrier_wait(&thread_sync_barrier);
+    pthread_barrier_wait(&state->thread_sync_barrier);
 
     update_bodies_partial(state, 0);
-    pthread_barrier_wait(&thread_sync_barrier);
+    pthread_barrier_wait(&state->thread_sync_barrier);
 }
 
 // NOTE CLEANUP(Brendan): Bunch of graphics state that should go elsewhere.
@@ -280,7 +273,7 @@ draw(SimState* state)
 
     // NOTE(Brendan): Present the changes to the screen.
     glfwSwapBuffers(window);
-    pthread_barrier_wait(&thread_sync_barrier);
+    pthread_barrier_wait(&state->thread_sync_barrier);
 }
 
 internal void
@@ -296,8 +289,6 @@ loop(SimState* state)
 
     while (!glfwWindowShouldClose(window))
     {
-        glfwPollEvents();
-
         curr_time = glfwGetTime();
         delta = curr_time - last_time;
         last_time = curr_time;
@@ -318,6 +309,8 @@ loop(SimState* state)
             frame_rate = frame_count;
             frame_count = 0;
         }
+
+        glfwPollEvents();
     }
 }