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);
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;
shader_log);
}
- free(shader_buffer);
-
return shader;
}
+struct SimState
+{
+ Array<Body> bodies;
+};
+
#define CIRCLE_POINT_COUNT 36 // NOTE(Brendan): Treat a circle as a many-sided polygon.
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;
}
}
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;
if (delta > 0.0)
{
- update(delta);
- draw();
+ update(state, delta);
+ draw(state);
}
}
}
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);
}
{
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);
glBindBuffer(GL_ARRAY_BUFFER, -1);
}
- loop();
+ loop(state);
return 0;
}