From d13db0592573332fe32d9668d822a0cc0c2e920e Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 31 Oct 2024 20:06:15 -0400 Subject: flatten() --- balls.c | 22 ++++++++++++++++++++-- 1 file 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; +} -- cgit v1.2.3