turns
[gbajam22] / src / voxscape.c
index ef11974..b57cad4 100644 (file)
@@ -7,8 +7,20 @@
 #include "voxscape.h"
 #include "debug.h"
 
+/* hardcoded dimensions for the GBA */
 #define FBWIDTH                240
 #define FBHEIGHT       160
+#define FBPITCH                240
+/* map size */
+#define XSZ                    512
+#define YSZ                    512
+#define XSHIFT         9
+#define XMASK          0x1ff
+#define YMASK          0x1ff
+#define HSCALE         40
+
+
+#define NO_LERP
 
 #define XLERP(a, b, t, fp) \
        ((((a) << (fp)) + ((b) - (a)) * (t)) >> fp)
@@ -51,6 +63,8 @@ struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
 {
        struct voxscape *vox;
 
+       assert(xsz == XSZ && ysz == YSZ);
+
        if(!(vox = calloc(1, sizeof *vox))) {
                return 0;
        }
@@ -108,10 +122,15 @@ void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
 }
 
 #define H(x, y)        \
-       vox->height[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 #define C(x, y) \
-       vox->color[((((y) >> 16) & vox->ymask) << vox->xshift) + (((x) >> 16) & vox->xmask)]
+       vox->color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
+
+#ifdef NO_LERP
+#define vox_height(vox, x, y)  H(x, y)
+#define vox_color(vox, x, y)   C(x, y)
 
+#else
 
 int vox_height(struct voxscape *vox, int32_t x, int32_t y)
 {
@@ -156,6 +175,7 @@ int vox_color(struct voxscape *vox, int32_t x, int32_t y)
        c1 = XLERP(c10, c11, v, 16);
        return XLERP(c0, c1, u, 16);
 }
+#endif /* !NO_LERP */
 
 
 void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
@@ -167,9 +187,8 @@ void vox_filter(struct voxscape *vox, int hfilt, int cfilt)
 void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
 {
        if(xres != vox->fbwidth) {
-               free(vox->coltop);
-               if(!(vox->coltop = malloc(xres * sizeof *vox->coltop))) {
-                       fprintf(stderr, "vox_framebuf: failed to allocate column table (%d)\n", xres);
+               if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
+                       panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
                        return;
                }
        }
@@ -201,8 +220,8 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
 
        vox->nslices = vox->zfar - vox->znear;
        free(vox->slicelen);
-       if(!(vox->slicelen = malloc(vox->nslices * sizeof *vox->slicelen))) {
-               fprintf(stderr, "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
+       if(!(vox->slicelen = iwram_sbrk(vox->nslices * sizeof *vox->slicelen))) {
+               panic(get_pc(), "vox_proj: failed to allocate slice length table (%d)\n", vox->nslices);
                return;
        }
 
@@ -214,22 +233,25 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
  * for each column step along this line and compute height for each pixel
  * fill the visible (top) part of each column
  */
-
+ARM_IWRAM
 void vox_render(struct voxscape *vox)
 {
        int i;
 
        vox_begin(vox);
        for(i=0; i<vox->nslices; i++) {
+               /*if(i >= 10 && (i & 1) == 0) {
+                       continue;
+               }*/
                vox_render_slice(vox, i);
        }
 }
 
+ARM_IWRAM
 void vox_begin(struct voxscape *vox)
 {
        int i;
 
-       memset(vox->fb, 0, FBWIDTH * FBHEIGHT);
        memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
 
        if(!(vox->valid & SLICELEN)) {
@@ -241,55 +263,46 @@ void vox_begin(struct voxscape *vox)
        }
 }
 
+ARM_IWRAM
 void vox_render_slice(struct voxscape *vox, int n)
 {
-       int i, j, hval, colstart, colheight, col, z;
+       int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
        int32_t x, y, len, xstep, ystep;
-       uint8_t color;
+       uint8_t color, last_col;
        uint16_t *fbptr;
 
        z = vox->znear + n;
 
        len = vox->slicelen[n] >> 8;
-       xstep = (((COS(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
-       ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / FBWIDTH;
-
-       x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
-       y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
-
-       for(i=0; i<FBWIDTH / 2; i++) {
-               col = i * 2;
-               hval = vox_height(vox, x, y) - vox->vheight;
-               hval = hval * 40 / (vox->znear + n) + vox->horizon;
-               if(hval > FBHEIGHT) hval = FBHEIGHT;
-               if(hval > vox->coltop[col]) {
-                       color = vox_color(vox, x, y);
-                       colstart = FBHEIGHT - hval;
-                       colheight = hval - vox->coltop[col];
-                       fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
-
-                       for(j=0; j<colheight; j++) {
-                               *fbptr |= color;
-                               fbptr += FBWIDTH / 2;
-                       }
-                       vox->coltop[col] = hval;
+       xstep = (((COS(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
+       ystep = (((SIN(vox->angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
+
+       x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
+       y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
+
+       for(i=0; i<FBWIDTH/2; i++) {
+               col = i << 1;
+               offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
+               if(offs == last_offs) {
+                       hval = last_hval;
+                       color = last_col;
+               } else {
+                       hval = vox->height[offs] - vox->vheight;
+                       hval = hval * HSCALE / (vox->znear + n) + vox->horizon;
+                       if(hval > FBHEIGHT) hval = FBHEIGHT;
+                       color = vox->color[offs];
+                       last_offs = offs;
+                       last_hval = hval;
+                       last_col = color;
                }
-               x += xstep;
-               y += ystep;
-
-               col++;
-               hval = vox_height(vox, x, y) - vox->vheight;
-               hval = hval * 40 / (vox->znear + n) + vox->horizon;
-               if(hval > FBHEIGHT) hval = FBHEIGHT;
                if(hval > vox->coltop[col]) {
-                       color = vox_color(vox, x, y);
                        colstart = FBHEIGHT - hval;
                        colheight = hval - vox->coltop[col];
-                       fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
+                       fbptr = vox->fb + colstart * (FBPITCH / 2) + i;
 
                        for(j=0; j<colheight; j++) {
-                               *fbptr |= ((uint16_t)color << 8);
-                               fbptr += FBWIDTH / 2;
+                               *fbptr = color | ((uint16_t)color << 8);
+                               fbptr += FBPITCH / 2;
                        }
                        vox->coltop[col] = hval;
                }
@@ -298,39 +311,27 @@ void vox_render_slice(struct voxscape *vox, int n)
        }
 }
 
+ARM_IWRAM
 void vox_sky_solid(struct voxscape *vox, uint8_t color)
 {
-       int i, j, colh0, colh1, colhboth;
+       int i, j, colheight;
        uint16_t *fbptr;
 
        for(i=0; i<FBWIDTH / 2; i++) {
                fbptr = vox->fb + i;
-               colh0 = FBHEIGHT - vox->coltop[i << 1];
-               colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
-               colhboth = colh0 < colh1 ? colh0 : colh1;
+               colheight = FBHEIGHT - vox->coltop[i << 1];
 
-               for(j=0; j<colhboth; j++) {
+               for(j=0; j<colheight; j++) {
                        *fbptr = color | ((uint16_t)color << 8);
-                       fbptr += FBWIDTH / 2;
-               }
-
-               if(colh0 > colh1) {
-                       for(j=colhboth; j<colh0; j++) {
-                               *fbptr |= color;
-                               fbptr += FBWIDTH / 2;
-                       }
-               } else {
-                       for(j=colhboth; j<colh1; j++) {
-                               *fbptr |= (uint16_t)color << 8;
-                               fbptr += FBWIDTH / 2;
-                       }
+                       fbptr += FBPITCH / 2;
                }
        }
 }
 
+ARM_IWRAM
 void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
 {
-       int i, j, colh0, colh1, colhboth, t;
+       int i, j, colheight, t;
        int d = FBHEIGHT - vox->horizon;
        uint8_t grad[FBHEIGHT];
        uint16_t *fbptr;
@@ -345,25 +346,11 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
 
        for(i=0; i<FBWIDTH / 2; i++) {
                fbptr = vox->fb + i;
-               colh0 = FBHEIGHT - vox->coltop[i << 1];
-               colh1 = FBHEIGHT - vox->coltop[(i << 1) + 1];
-               colhboth = colh0 < colh1 ? colh0 : colh1;
+               colheight = FBHEIGHT - vox->coltop[i << 1];
 
-               for(j=0; j<colhboth; j++) {
+               for(j=0; j<colheight; j++) {
                        *fbptr = grad[j] | ((uint16_t)grad[j] << 8);
-                       fbptr += FBWIDTH / 2;
-               }
-
-               if(colh0 > colh1) {
-                       for(j=colhboth; j<colh0; j++) {
-                               *fbptr |= grad[j];
-                               fbptr += FBWIDTH / 2;
-                       }
-               } else {
-                       for(j=colhboth; j<colh1; j++) {
-                               *fbptr |= (uint16_t)grad[j] << 8;
-                               fbptr += FBWIDTH / 2;
-                       }
+                       fbptr += FBPITCH / 2;
                }
        }
 }