From: Brendan Hansen Date: Thu, 22 Oct 2020 19:20:06 +0000 (-0500) Subject: small changes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=1998d5116be4f07f71b4dcd3ff6c0e241da42850;p=csc718.git small changes --- diff --git a/include/container.h b/include/container.h index 0d292c2..3fdaa6b 100644 --- a/include/container.h +++ b/include/container.h @@ -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; diff --git a/include/physics.h b/include/physics.h index 44646d7..881dfa4 100644 --- a/include/physics.h +++ b/include/physics.h @@ -11,6 +11,4 @@ struct Body f32 mass; }; -Body* alloc_bodies(i32 body_count = 1); - #endif //PHYSICS_H \ No newline at end of file diff --git a/include/utils.h b/include/utils.h index 6ad3837..5bb8924 100644 --- a/include/utils.h +++ b/include/utils.h @@ -20,7 +20,7 @@ // NOTE(Brendan): The fact that I have to define this hear solidifies the stupidity of C++ in my mind. template -T* alloc(int32_t count) +T* alloc(int32_t count = 1) { T* res = (T *) malloc(sizeof(T) * count); return res; diff --git a/src/physics.cpp b/src/physics.cpp index 92ad088..90ca46c 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -2,6 +2,3 @@ #include "utils.h" #include "types.h" -Body* alloc_bodies(i32 body_count) { - return alloc(body_count); -} \ No newline at end of file diff --git a/src/sim.cpp b/src/sim.cpp index 28cae4f..1d81471 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -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 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 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(); + // 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(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; }