#ifndef UTILS_H
#define UTILS_H
+#include <malloc.h>
#include <stdint.h> // NOTE(Brendan): Only for intptr_t
// NOTE(Brendan): Hacky way to get the offset of a struct member. offsetof() is the standard way in C to get it, but it is not guarenteed to be defined in all C implementations.
// NOTE(Brendan): This is useful in many situations and I believe it cleans up the code by making simple, counter based for loops easier to recognize at a glance.
#define foreach(var, lo, hi) for (i32 var = lo; var < hi; var++)
+#define For(iterable) for (auto& it : iterable)
[[noreturn]] void panic_and_die(const char* msg, ...);
+// 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* res = (T *) malloc(sizeof(T) * count);
+ return res;
+}
+
+float randf();
+float randf(float max);
+float randf(float min, float max);
+
#ifndef defer
struct defer_dummy {};
#include <cstdio>
#include <cstdlib>
#include <cstdbool>
+#include <cassert>
#include <malloc.h>
#include <alloca.h>
#include <unistd.h>
foreach (i, 0, CIRCLE_POINT_COUNT)
{
f32 t = (f32) i / (f32) CIRCLE_POINT_COUNT;
- circle_points[i].x = cos(t * 2 * PI) * 40.0f + 400.0f;
- circle_points[i].y = sin(t * 2 * PI) * 40.0f + 200.0f;
+ circle_points[i].x = cos(t * 2 * PI) / 2.0f;
+ circle_points[i].y = sin(t * 2 * PI) / 2.0f;
}
GLuint vertex_buffer;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(circle_mesh);
- glDrawElements(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0);
+ glDrawElementsInstanced(GL_TRIANGLE_FAN, CIRCLE_POINT_COUNT, GL_UNSIGNED_BYTE, 0, 128);
glBindVertexArray(-1);
glfwSwapBuffers(window);
GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj");
glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat);
+ auto bodies = alloc<Body>(128);
+ foreach(i, 0, 128)
+ {
+ bodies[i].pos = V2f{ randf(0, 800), randf(0, 800) };
+ bodies[i].vel = V2f{ 0.0f, 0.0f };
+ bodies[i].mass = randf(10.0f, 50.0f);
+ }
+
+ {
+ glBindVertexArray(circle_mesh);
+ // defer { glBindVertexArray(-1); };
+
+ GLuint body_buffer;
+ glGenBuffers(1, &body_buffer);
+ glBindBuffer(GL_ARRAY_BUFFER, body_buffer);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(Body) * 128, bodies, GL_STREAM_DRAW);
+ glEnableVertexAttribArray(1);
+ glEnableVertexAttribArray(2);
+ glVertexAttribDivisor(1, 1);
+ glVertexAttribDivisor(2, 1);
+ glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(Body), (void *) offset_of(Body, pos.x));
+ glVertexAttribPointer(2, 1, GL_FLOAT, false, sizeof(Body), (void *) offset_of(Body, mass));
+ glBindBuffer(GL_ARRAY_BUFFER, -1);
+
+ // glBindVertexArray(-1);
+ }
+
loop();
return 0;
#include <cstdarg>
#include <cstdlib>
+#include "utils.h"
#include "log.h"
void
panic_and_die(const char* msg, ...)
{
puts("************ PANIC ************");
-
+
va_list va;
va_start(va, msg);
logvprint(LOG_LEVEL_ERROR, msg, va);
va_end(va);
-
+
#ifdef DEBUG
// NOTE: This allows for a debugger to stop here.
__asm("int $3");
#endif
-
+
exit(1);
}
+
+float randf() { return randf(0.0f, 1.0f); }
+float randf(float max) { return randf(0.0f, max); }
+float
+randf(float min, float max)
+{
+ return (float) rand() * (max - min) / (float) RAND_MAX + min;
+}
\ No newline at end of file