small changes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Oct 2020 19:20:06 +0000 (14:20 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Oct 2020 19:20:06 +0000 (14:20 -0500)
include/container.h
include/physics.h
include/utils.h
src/physics.cpp
src/sim.cpp

index 0d292c2a351e4ad38620927428c2d35ba8ec21cc..3fdaa6b798d6dea3584a5325c094ec3412885a97 100644 (file)
@@ -11,7 +11,10 @@ struct Array
     u32 capacity;
     T  *data;
     
-    Array()
+    Array() { this->init(); }
+    
+    // NOTE(Brendan): This should only be called when the Array is being initialized for the first time. If it is called a second time, it will leak memory. I would add a check for that, but that defeats the whole point of initialization.
+    void init()
     {
         count = 0;
         capacity = 0;
index 44646d79cdb346efda7b978b18d2443bb0f54de7..881dfa4e91177fa59d428700e8353e6a25e58c30 100644 (file)
@@ -11,6 +11,4 @@ struct Body
     f32 mass;
 };
 
-Body* alloc_bodies(i32 body_count = 1);
-
 #endif //PHYSICS_H
\ No newline at end of file
index 6ad3837ff0bdccb30e9bba0ae033f7fcd448111c..5bb89248377c25b74d9c82231613372909a3d1b2 100644 (file)
@@ -20,7 +20,7 @@
 
 // NOTE(Brendan): The fact that I have to define this hear solidifies the stupidity of C++ in my mind.
 template <typename T>
-T* alloc(int32_t count)
+T* alloc(int32_t count = 1)
 {
     T* res = (T *) malloc(sizeof(T) * count);
     return res;
index 92ad088289f8a17aea23734b4d53ee924e5e0b67..90ca46c1bb119dbb5af50e383d6044b1b9e96554 100644 (file)
@@ -2,6 +2,3 @@
 #include "utils.h"
 #include "types.h"
 
-Body* alloc_bodies(i32 body_count) {
-    return alloc<Body>(body_count);
-}
\ No newline at end of file
index 28cae4f9b05e4bbd95846f5e54bdec58db68ee20..1d81471c5a314312c5f4fb6d662d58abc9d61cd8 100644 (file)
@@ -84,6 +84,7 @@ load_shader(GLenum shader_type, const char* shader_loc)
     GLuint shader = glCreateShader(shader_type);
     
     FILE* shader_file = fopen(shader_loc, "rb");
+    defer { fclose(shader_file); };
     if (shader_file == NULL) panic_and_die("Shader file not found: %s\n", shader_loc);
     
     fseek(shader_file, 0, SEEK_END);
@@ -91,8 +92,8 @@ load_shader(GLenum shader_type, const char* shader_loc)
     fseek(shader_file, 0, SEEK_SET);
     
     char* shader_buffer = (char *) malloc(shader_file_size + 1);
+    defer { free(shader_buffer); };
     fread(shader_buffer, 1, shader_file_size, shader_file);
-    fclose(shader_file);
     
     shader_buffer[shader_file_size] = 0;
     
@@ -113,8 +114,6 @@ load_shader(GLenum shader_type, const char* shader_loc)
                       shader_log);
     }
     
-    free(shader_buffer);
-    
     return shader;
 }
 
@@ -145,6 +144,11 @@ create_program(GLuint vertex_shader, GLuint fragment_shader)
 
 
 
+struct SimState
+{
+    Array<Body> bodies;
+};
+
 
 
 #define CIRCLE_POINT_COUNT 36 // NOTE(Brendan): Treat a circle as a many-sided polygon.
@@ -188,17 +192,12 @@ create_circle_mesh()
     return vao;
 }
 
-
-
-internal Array<Body> bodies;
-
 // NOTE(Brendan): dt is expected to be in units of "per second".
 internal void
-update(f64 dt)
+update(SimState* state, f64 dt)
 {
-    For (bodies)
+    For (state->bodies)
     {
-        // it.vel *= 0.99f;
         it.pos += it.vel * dt;
     }
 }
@@ -207,25 +206,28 @@ internal GLuint body_buffer;
 internal GLuint circle_mesh;
 
 internal void
-draw()
+draw(SimState* state)
 {
-    
+    // NOTE(Brendan): Rebuffer all the body data to the GPU.
     glBindBuffer(GL_ARRAY_BUFFER, body_buffer);
-    glBufferSubData(GL_ARRAY_BUFFER, 0, bodies.count * sizeof(Body), bodies.data);
+    glBufferSubData(GL_ARRAY_BUFFER, 0, state->bodies.count * sizeof(Body), state->bodies.data);
     glBindBuffer(GL_ARRAY_BUFFER, -1);
     
+    // NOTE(Brendan): Clear the screen.
     glClearColor(0.1, 0.1, 0.1, 1.0);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
+    // NOTE(Brendan): Draw the bodies.
     glBindVertexArray(circle_mesh);
-    glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, bodies.count);
+    glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, state->bodies.count);
     glBindVertexArray(-1);
     
+    // NOTE(Brendan): Present the changes to the screen.
     glfwSwapBuffers(window);
 }
 
 internal void
-loop()
+loop(SimState* state)
 {
     f64 last_time = glfwGetTime();
     f64 curr_time = last_time;
@@ -241,8 +243,8 @@ loop()
         
         if (delta > 0.0)
         {
-            update(delta);
-            draw();
+            update(state, delta);
+            draw(state);
         }
     }
 }
@@ -268,13 +270,18 @@ main(i32 argc, char* argv[])
     GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj");
     glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat);
     
+    auto state = alloc<SimState>();
+    // 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(128);
+    
     foreach (i, 0, 128)
     {
         Body tmp_body;
         tmp_body.pos = V2f{ randf(0, 800), randf(0, 800) };
         tmp_body.vel = V2f{ randf(-50.0f, 50.0f), randf(-50.0f, 50.0f) };
         tmp_body.mass = randf(10.0f, 50.0f);
-        bodies.push(tmp_body);
+        state->bodies.push(tmp_body);
     }
     
     {
@@ -283,7 +290,7 @@ main(i32 argc, char* argv[])
         
         glGenBuffers(1, &body_buffer);
         glBindBuffer(GL_ARRAY_BUFFER, body_buffer);
-        glBufferData(GL_ARRAY_BUFFER, sizeof(Body) * bodies.count, bodies.data, GL_STREAM_DRAW);
+        glBufferData(GL_ARRAY_BUFFER, sizeof(Body) * state->bodies.count, state->bodies.data, GL_STREAM_DRAW);
         glEnableVertexAttribArray(1);
         glEnableVertexAttribArray(2);
         glVertexAttribDivisor(1, 1);
@@ -293,7 +300,7 @@ main(i32 argc, char* argv[])
         glBindBuffer(GL_ARRAY_BUFFER, -1);
     }
     
-    loop();
+    loop(state);
     
     return 0;
 }