From 66ea444187eb5c9dca354610ce4a00370142cc08 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 21 Oct 2020 22:50:35 -0500 Subject: [PATCH] moving circles around --- include/container.h | 6 +++--- include/vecmath.h | 5 +++++ src/sim.cpp | 32 ++++++++++++++------------------ src/vecmath.cpp | 5 +++++ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/include/container.h b/include/container.h index fb9801e..0d292c2 100644 --- a/include/container.h +++ b/include/container.h @@ -110,7 +110,7 @@ struct Array count -= 1; } - bool contains(const T& x) + bool contains(const T& x) const { for (u32 i = 0; i < count; i++) { @@ -121,12 +121,12 @@ struct Array } - T* begin() + T* begin() const { return &data[0]; } - T* end() + T* end() const { return &data[count]; } diff --git a/include/vecmath.h b/include/vecmath.h index 4449d45..fd4492c 100644 --- a/include/vecmath.h +++ b/include/vecmath.h @@ -11,6 +11,11 @@ struct V2f V2f operator+(V2f a, V2f b); V2f operator-(V2f a, V2f b); V2f operator*(V2f a, f32 scalar); + +void operator+=(V2f& a, const V2f& b); +void operator-=(V2f& a, const V2f& b); +void operator*=(V2f& a, const f32& s); + f32 v2f_dot (V2f a, V2f b); f32 v2f_smag(V2f a); f32 v2f_mag (V2f a); diff --git a/src/sim.cpp b/src/sim.cpp index e1de352..28cae4f 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -190,26 +190,25 @@ create_circle_mesh() +internal Array bodies; // NOTE(Brendan): dt is expected to be in units of "per second". internal void update(f64 dt) { + For (bodies) + { + // it.vel *= 0.99f; + it.pos += it.vel * dt; + } } internal GLuint body_buffer; -internal Array bodies; - internal GLuint circle_mesh; + internal void draw() { - For(bodies) - { - it.pos = V2f{ randf(0, 800), randf(0, 800) }; - it.vel = V2f{ 0.0f, 0.0f }; - it.mass = randf(10.0f, 50.0f); - } glBindBuffer(GL_ARRAY_BUFFER, body_buffer); glBufferSubData(GL_ARRAY_BUFFER, 0, bodies.count * sizeof(Body), bodies.data); @@ -219,7 +218,7 @@ draw() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(circle_mesh); - glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, 128); + glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, bodies.count); glBindVertexArray(-1); glfwSwapBuffers(window); @@ -269,16 +268,13 @@ main(i32 argc, char* argv[]) GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj"); glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat); - bodies.ensure_capacity(128); - - // NOTE(Brendan): Setting the count like this does not guarantee that the data will be initialized. - bodies.count = 128; - - For(bodies) + foreach (i, 0, 128) { - it.pos = V2f{ randf(0, 800), randf(0, 800) }; - it.vel = V2f{ 0.0f, 0.0f }; - it.mass = randf(10.0f, 50.0f); + 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); } { diff --git a/src/vecmath.cpp b/src/vecmath.cpp index 2315d49..994c98d 100644 --- a/src/vecmath.cpp +++ b/src/vecmath.cpp @@ -21,6 +21,11 @@ operator*(V2f a, f32 scalar) return (V2f) { .x = a.x * scalar, .y = a.y * scalar }; } +// NOTE(Brendan): I wish these could be implicitly defined since their definitions are so easy. +void operator+=(V2f& a, const V2f& b) { a = a + b; } +void operator-=(V2f& a, const V2f& b) { a = a - b; } +void operator*=(V2f& a, const f32& s) { a = a * s; } + f32 v2f_dot(V2f a, V2f b) { -- 2.25.1