optimization: fixed spawn points on color texture
[gbajam22] / src / voxscape.c
index 940ae06..8ebc692 100644 (file)
@@ -6,6 +6,7 @@
 #include <assert.h>
 #include "voxscape.h"
 #include "debug.h"
+#include "data.h"
 
 /* hardcoded dimensions for the GBA */
 #define FBWIDTH                240
@@ -19,6 +20,8 @@
 #define YMASK          0x1ff
 #define HSCALE         40
 
+/* XXX */
+#define OBJ_STRIDE_SHIFT       5
 
 #define NO_LERP
 
@@ -57,11 +60,13 @@ struct voxscape {
        uint8_t fogcolor;
 
        unsigned int valid;
-};
 
-static int *projtab;
+       struct vox_object *obj;
+       int num_obj, obj_stride;
+};
 
 int vox_quality = 1;
+int *projlut;
 
 struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg)
 {
@@ -193,7 +198,6 @@ void vox_framebuf(struct voxscape *vox, int xres, int yres, void *fb, int horizo
        if(xres != vox->fbwidth) {
                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;
@@ -227,8 +231,8 @@ void vox_proj(struct voxscape *vox, int fov, int znear, int zfar)
                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(!(projtab = iwram_sbrk(vox->nslices * sizeof *projtab))) {
-                       panic(get_pc(), "vox_proj: failed to allocate projection 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);
                }
        }
 
@@ -272,7 +276,7 @@ void vox_begin(struct voxscape *vox)
                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);
-                       projtab[i] = (HSCALE << 8) / (vox->znear + i);
+                       projlut[i] = (HSCALE << 8) / (vox->znear + i);
                }
                vox->valid |= SLICELEN;
        }
@@ -285,6 +289,8 @@ void vox_render_slice(struct voxscape *vox, int n)
        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;
 
@@ -295,6 +301,8 @@ void vox_render_slice(struct voxscape *vox, int n)
        x = vox->x - SIN(vox->angle) * z - xstep * (FBWIDTH / 4);
        y = vox->y + COS(vox->angle) * z - ystep * (FBWIDTH / 4);
 
+       /*proj = (HSCALE << 8) / (vox->znear + n);*/
+
        for(i=0; i<FBWIDTH/2; i++) {
                col = i << 1;
                offs = (((y >> 16) & YMASK) << XSHIFT) + ((x >> 16) & XMASK);
@@ -303,8 +311,7 @@ void vox_render_slice(struct voxscape *vox, int n)
                        color = last_col;
                } else {
                        hval = vox->height[offs] - vox->vheight;
-                       /*hval = hval * HSCALE / (vox->znear + n) + vox->horizon;*/
-                       hval = ((hval * projtab[n]) >> 8) + vox->horizon;
+                       hval = ((hval * projlut[n]) >> 8) + vox->horizon;
                        if(hval > FBHEIGHT) hval = FBHEIGHT;
                        color = vox->color[offs];
                        last_offs = offs;
@@ -321,6 +328,15 @@ void vox_render_slice(struct voxscape *vox, int n)
                                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];
+                       }
                }
                x += xstep;
                y += ystep;
@@ -370,3 +386,24 @@ void vox_sky_grad(struct voxscape *vox, uint8_t chor, uint8_t ctop)
                }
        }
 }
+
+void vox_objects(struct voxscape *vox, 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);
+       }
+}