add missing tools/pngdump to the repo
[gbajam22] / src / voxscape.c
index a9008ab..5f337c4 100644 (file)
@@ -6,17 +6,22 @@
 #include <assert.h>
 #include "voxscape.h"
 #include "debug.h"
+#include "data.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
 
+/* XXX */
+#define OBJ_STRIDE_SHIFT       5
 
 #define NO_LERP
 
@@ -27,201 +32,104 @@ enum {
        SLICELEN        = 1
 };
 
-struct voxscape {
-       int xsz, ysz;
-       unsigned char *height;
-       unsigned char *color;
-       int xshift, xmask, ymask;
+static unsigned char *vox_hmap;
+static unsigned char *vox_color;
+/* framebuffer */
+static uint16_t *vox_fb;
+static int *vox_coltop;
+static int vox_horizon;
+/* view */
+static int32_t vox_x, vox_y, vox_angle;
+static int vox_vheight;
+/* projection */
+static int vox_fov, vox_znear, vox_zfar;
+static int vox_nslices;
+static int32_t *vox_slicelen;
 
-       int hfilt, cfilt;
+static unsigned int vox_valid;
 
-       /* framebuffer */
-       uint16_t *fb;
-       int fbwidth, fbheight;
-       int *coltop;
-       int horizon;
+static struct vox_object *vox_obj;
+static int vox_num_obj, vox_obj_stride;
 
-       /* view */
-       int32_t x, y, angle;
-       int vheight;
+int *projlut;
 
-       /* projection */
-       int fov, znear, zfar;
-       int nslices;
-       int32_t *slicelen;
-       int proj_dist;
-
-       int zfog;       /* fog start Z (0: no fog) */
-       uint8_t fogcolor;
-
-       unsigned int valid;
-};
-
-struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
+int vox_init(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;
-       }
-       vox->height = himg;
-       vox->color = cimg;
-       vox->xsz = xsz;
-       vox->ysz = ysz;
-
-       vox->xmask = vox->xsz - 1;
-       vox->ymask = vox->ysz - 1;
-
-       vox->xshift = -1;
-       while(xsz) {
-               xsz >>= 1;
-               vox->xshift++;
-       }
-
-       vox->vheight = 80;
-       vox->proj_dist = 4;     /* TODO */
-
-       return vox;
-}
-
-void vox_free(struct voxscape *vox)
-{
-       if(!vox) return;
+       vox_hmap = himg;
+       vox_color = cimg;
 
-       free(vox->color);
-       free(vox->height);
-       free(vox->coltop);
-       free(vox->slicelen);
-       free(vox);
-}
+       vox_fb = 0;
+       vox_coltop = 0;
+       vox_horizon = 0;
+       vox_x = vox_y = vox_angle = 0;
+       vox_fov = 0;
+       vox_znear = vox_zfar = 0;
+       vox_nslices = 0;
+       vox_slicelen = 0;
+       vox_valid = 0;
+       projlut = 0;
 
-uint8_t *vox_texture(struct voxscape *vox, uint8_t *data)
-{
-       if(data) {
-               memcpy(vox->color, data, vox->xsz * vox->ysz);
-       }
-       return vox->color;
-}
+       vox_vheight = 80;
 
-uint8_t *vox_heightmap(struct voxscape *vox, uint8_t *data)
-{
-       if(data) {
-               memcpy(vox->height, data, vox->xsz * vox->ysz);
-       }
-       return vox->height;
+       return 0;
 }
 
-void vox_fog(struct voxscape *vox, int zstart, uint8_t color)
+void vox_destroy(void)
 {
-       vox->zfog = zstart;
-       vox->fogcolor = color;
+       /* XXX we rely on the screen to clear up any allocated IWRAM */
 }
 
 #define H(x, y)        \
-       vox->height[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
+       vox_hmap[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 #define C(x, y) \
-       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)
-{
-       int32_t u, v;
-       int h00, h01, h10, h11, h0, h1;
-
-       if(!vox->hfilt) {
-               return H(x, y);
-       }
-
-       h00 = H(x, y);
-       h01 = H(x, y + 0x10000);
-       h10 = H(x + 0x10000, y);
-       h11 = H(x + 0x10000, y + 0x10000);
-
-       u = x & 0xffff;
-       v = y & 0xffff;
-
-       h0 = XLERP(h00, h01, v, 16);
-       h1 = XLERP(h10, h11, v, 16);
-       return XLERP(h0, h1, u, 16);
-}
-
-int vox_color(struct voxscape *vox, int32_t x, int32_t y)
-{
-       int32_t u, v;
-       int c00, c01, c10, c11, c0, c1;
-
-       if(!vox->cfilt) {
-               return C(x, y);
-       }
-
-       c00 = C(x, y);
-       c01 = C(x, y + 0x10000);
-       c10 = C(x + 0x10000, y);
-       c11 = C(x + 0x10000, y + 0x10000);
-
-       u = x & 0xffff;
-       v = y & 0xffff;
+       vox_color[((((y) >> 16) & YMASK) << XSHIFT) + (((x) >> 16) & XMASK)]
 
-       c0 = XLERP(c00, c01, v, 16);
-       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)
-{
-       vox->hfilt = hfilt;
-       vox->cfilt = cfilt;
-}
-
-void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizon)
+void vox_framebuf(int xres, int yres, void *fb, int horizon)
 {
-       if(xres != vox->fbwidth) {
-               if(!(vox->coltop = iwram_sbrk(xres * sizeof *vox->coltop))) {
+       if(!vox_coltop) {
+               if(!(vox_coltop = iwram_sbrk(xres * sizeof *vox_coltop))) {
                        panic(get_pc(), "vox_framebuf: failed to allocate column table (%d)\n", xres);
-                       return;
                }
        }
-       vox->fb = fb;
-       vox->fbwidth = xres;
-       vox->fbheight = yres;
-       vox->horizon = horizon >= 0 ? horizon : (vox->fbheight >> 1);
+       vox_fb = fb;
+       vox_horizon = horizon >= 0 ? horizon : (FBHEIGHT >> 1);
 }
 
-void vox_view(struct voxscape *vox, int32_t x, int32_t y, int h, int32_t angle)
+int vox_view(int32_t x, int32_t y, int h, int32_t angle)
 {
        if(h < 0) {
-               h = vox_height(vox, x, y) - h;
+               h = H(x, y) - h;
        }
 
-       vox->x = x;
-       vox->y = y;
-       vox->vheight = h;
-       vox->angle = angle;
+       vox_x = x;
+       vox_y = y;
+       vox_vheight = h;
+       vox_angle = angle;
 
-       vox->valid &= ~SLICELEN;
+       vox_valid &= ~SLICELEN;
+
+       return h;
 }
 
-void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
+void vox_proj(int fov, int znear, int zfar)
 {
-       vox->fov = fov;
-       vox->znear = znear;
-       vox->zfar = zfar;
-
-       vox->nslices = vox->zfar - vox->znear;
-       free(vox->slicelen);
-       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;
+       vox_fov = fov;
+       vox_znear = znear;
+       vox_zfar = zfar;
+
+       vox_nslices = vox_zfar - vox_znear;
+       if(!vox_slicelen) {
+               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);
+               }
+               if(!(projlut = iwram_sbrk(vox_nslices * sizeof *projlut))) {
+                       panic(get_pc(), "vox_framebuf: failed to allocate projection table (%d)\n", vox_nslices);
+               }
        }
 
-       vox->valid &= ~SLICELEN;
+       vox_valid &= ~SLICELEN;
 }
 
 /* algorithm:
@@ -229,103 +137,90 @@ 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
  */
-
-void vox_render(struct voxscape *vox)
+ARM_IWRAM
+void vox_render(void)
 {
        int i;
 
-       vox_begin(vox);
-       for(i=0; i<vox->nslices; i++) {
-               vox_render_slice(vox, i);
+       vox_begin();
+
+       for(i=0; i<vox_nslices; i++) {
+               vox_render_slice(i);
        }
 }
 
-void vox_begin(struct voxscape *vox)
+ARM_IWRAM
+void vox_begin(void)
 {
        int i;
 
-       memset(vox->fb, 0, FBWIDTH * FBHEIGHT);
-       memset(vox->coltop, 0, FBWIDTH * sizeof *vox->coltop);
+       memset(vox_coltop, 0, FBWIDTH * sizeof *vox_coltop);
 
-       if(!(vox->valid & SLICELEN)) {
-               float theta = (float)vox->fov * M_PI / 360.0f;  /* half angle */
-               for(i=0; i<vox->nslices; i++) {
-                       vox->slicelen[i] = (int32_t)((vox->znear + i) * tan(theta) * 4.0f * 65536.0f);
+       if(!(vox_valid & SLICELEN)) {
+               float theta = (float)vox_fov * M_PI / 360.0f;   /* half angle */
+               for(i=0; i<vox_nslices; i++) {
+                       vox_slicelen[i] = (int32_t)((vox_znear + i) * tan(theta) * 4.0f * 65536.0f);
+                       projlut[i] = (HSCALE << 8) / (vox_znear + i);
                }
-               vox->valid |= SLICELEN;
+               vox_valid |= SLICELEN;
        }
 }
 
 ARM_IWRAM
-void vox_render_slice(struct voxscape *vox, int n)
+void vox_render_slice(int n)
 {
        int i, j, hval, last_hval, colstart, colheight, col, z, offs, last_offs = -1;
        int32_t x, y, len, xstep, ystep;
        uint8_t color, last_col;
        uint16_t *fbptr;
+       /*int proj;*/
+       struct vox_object *obj;
 
-       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;
+       z = vox_znear + n;
 
-       x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 2);
-       y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 2);
+       len = vox_slicelen[n] >> 8;
+       xstep = (((COS(vox_angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
+       ystep = (((SIN(vox_angle) >> 4) * len) >> 4) / (FBWIDTH / 2);
 
-       for(i=1; i<FBWIDTH / 2; i++) {
-               col = i * 2;
-               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 * 40 / (vox->znear + n) + vox->horizon;
-                       if(hval > FBHEIGHT) hval = FBHEIGHT;
-                       color = vox->color[offs];
-                       last_offs = offs;
-                       last_hval = hval;
-                       last_col = color;
-               }
-               if(hval > vox->coltop[col]) {
-                       colstart = FBHEIGHT - hval;
-                       colheight = hval - vox->coltop[col];
-                       fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
+       x = vox_x - SIN(vox_angle) * z - xstep * (FBWIDTH / 4);
+       y = vox_y + COS(vox_angle) * z - ystep * (FBWIDTH / 4);
 
-                       for(j=0; j<colheight; j++) {
-                               *fbptr |= color;
-                               fbptr += FBWIDTH / 2;
-                       }
-                       vox->coltop[col] = hval;
-               }
-               x += xstep;
-               y += ystep;
+       /*proj = (HSCALE << 8) / (vox_znear + n);*/
 
-               col++;
+       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 * 40 / (vox->znear + n) + vox->horizon;
+                       hval = vox_hmap[offs] - vox_vheight;
+                       hval = ((hval * projlut[n]) >> 8) + vox_horizon;
                        if(hval > FBHEIGHT) hval = FBHEIGHT;
-                       color = vox->color[offs];
+                       color = vox_color[offs];
                        last_offs = offs;
                        last_hval = hval;
                        last_col = color;
                }
-               if(hval > vox->coltop[col]) {
+               if(hval >= vox_coltop[col]) {
                        colstart = FBHEIGHT - hval;
-                       colheight = hval - vox->coltop[col];
-                       fbptr = vox->fb + colstart * (FBWIDTH / 2) + i;
+                       colheight = hval - vox_coltop[col];
+                       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;
+
+                       /* check to see if there's an object here */
+                       if(color >= CMAP_SPAWN0) {
+                               int idx = color - CMAP_SPAWN0;
+                               obj = (struct vox_object*)((char*)vox_obj + (idx << OBJ_STRIDE_SHIFT));
+                               obj->px = col;
+                               obj->py = colstart;
+                               obj->scale = projlut[n];
                        }
-                       vox->coltop[col] = hval;
                }
                x += xstep;
                y += ystep;
@@ -333,40 +228,27 @@ void vox_render_slice(struct voxscape *vox, int n)
 }
 
 ARM_IWRAM
-void vox_sky_solid(struct voxscape *vox, uint8_t color)
+void vox_sky_solid(uint8_t color)
 {
-       int i, j, colh0, colh1, colhboth;
+       int i, j, colheight;
        uint16_t *fbptr;
 
-       for(i=1; 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;
+       for(i=0; i<FBWIDTH / 2; i++) {
+               fbptr = vox_fb + i;
+               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;
                }
        }
 }
 
-void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
+ARM_IWRAM
+void vox_sky_grad(uint8_t chor, uint8_t ctop)
 {
-       int i, j, colh0, colh1, colhboth, t;
-       int d = FBHEIGHT - vox->horizon;
+       int i, j, colheight, t;
+       int d = FBHEIGHT - vox_horizon;
        uint8_t grad[FBHEIGHT];
        uint16_t *fbptr;
 
@@ -378,27 +260,45 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
                grad[i] = chor;
        }
 
-       for(i=1; 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;
+       for(i=0; i<FBWIDTH / 2; i++) {
+               fbptr = vox_fb + i;
+               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;
+                       fbptr += FBPITCH / 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;
-                       }
-               }
+void vox_objects(struct vox_object *ptr, int count, int stride)
+{
+       int i;
+       struct vox_object *obj;
+
+       if(stride != 1 << OBJ_STRIDE_SHIFT) {
+               panic(get_pc(), "optimization requires %d byte vox obj (got %d)",
+                               1 << OBJ_STRIDE_SHIFT, stride);
+       }
+
+       vox_obj = ptr;
+       vox_num_obj = count;
+       vox_obj_stride = stride;
+
+       obj = ptr;
+       for(i=0; i<count; i++) {
+               obj->offs = obj->y * XSZ + obj->x;
+               obj = (struct vox_object*)((char*)obj + stride);
        }
 }
+
+int vox_height(int x, int y)
+{
+       return H(x, y);
+}
+
+int vox_check_vis(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
+{
+       /* TODO */
+       return 0;
+}