summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-31 20:06:15 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-31 20:06:15 -0400
commitd13db0592573332fe32d9668d822a0cc0c2e920e (patch)
tree78b2cbbb6b08bef82aed198980d98f37a697790a
parentaedd626997abe949126b92db7d4536933319c461 (diff)
downloadballs-d13db0592573332fe32d9668d822a0cc0c2e920e.zip
flatten()
-rw-r--r--balls.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/balls.c b/balls.c
index 20d4ff2..492fc27 100644
--- a/balls.c
+++ b/balls.c
@@ -61,6 +61,7 @@ char *readFile(const char *filename, size_t *size);
void compileShader(GLint shader);
void frameCount(void);
void drawString(const char *str);
+float *flatten(Vector *vs, int n);
int nBalls;
cl_context context;
@@ -214,12 +215,12 @@ createKernel(cl_program prog, const char *kernelFunc) {
void
setPositions(void) {
Vector *hostPositions;
- int *hostPositionBuf;
+ float *hostPositionBuf;
int err;
/* Generate initial ball positions. */
hostPositions = noOverlapPositions(nBalls, bounds, RMAX);
- hostPositionBuf = flatten(hostPositions);
+ hostPositionBuf = flatten(hostPositions, nBalls);
free(hostPositions);
/* Create device-side buffer. */
@@ -621,3 +622,20 @@ drawString(const char *str) {
for (i = 0; i < n; i++)
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, str[i]);
}
+
+/*
+ * Flatten an array of n vectors into an array of 2n floats. vs[i].x is at
+ * position 2i+0, and vs[i].y is at position 2i+1 in the returned array.
+ */
+float *
+flatten(Vector *vs, int n) {
+ float *arr;
+
+ if ((arr = malloc(2*n*sizeof(float))) == NULL)
+ sysfatal("Failed to allocate vector array.\n");
+ while (n-- > 0) {
+ arr[2*n+0] = vs[n].x;
+ arr[2*n+1] = vs[n].y;
+ }
+ return arr;
+}