optimization: fixed spawn points on color texture
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 26 Oct 2022 00:25:32 +0000 (03:25 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 26 Oct 2022 00:25:32 +0000 (03:25 +0300)
src/data.h
src/gamescr.c
src/voxscape.c

index 4cfe6e4..dd8cc98 100644 (file)
@@ -8,6 +8,7 @@
        (((r) >> 3) | (((uint16_t)(g) & 0xf8) << 2) | (((uint16_t)(b) & 0xf8) << 7))
 
 #define VOX_SZ 512
+#define CMAP_SPAWN0    240
 
 #define SPRID(x, y)            (SPRID_BASE + ((y) * 4) + (x) / 4)
 
index a84aa12..df11abc 100644 (file)
@@ -36,8 +36,9 @@ struct enemy {
        struct vox_object vobj;
        short hp;
        short anm;
-       short last_fire;
+       int last_fire;
 };
+#define ENEMY_VALID(e) ((e)->anm != 0)
 
 static uint16_t *framebuf;
 
@@ -55,7 +56,7 @@ static uint16_t oam[4 * MAX_SPR];
 static int dynspr_base, dynspr_count;
 
 
-#define MAX_ENEMIES    64
+#define MAX_ENEMIES    (256 - CMAP_SPAWN0)
 struct enemy enemies[MAX_ENEMIES];
 int num_enemies, total_enemies;
 static int energy;
@@ -80,15 +81,15 @@ struct screen *init_game_screen(void)
 
 static int gamescr_start(void)
 {
-       int i, sidx;
+       int i, j, sidx;
+       uint8_t *cptr;
+       struct enemy *enemy;
 
        gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
 
        vblperf_setcolor(0);
 
-       pos[0] = VOX_SZ << 15;
-       pos[1] = (VOX_SZ << 15) + 0x100000;
-       angle = 0x8000;
+       pos[0] = pos[1] = VOX_SZ << 15;
 
        if(!(vox = vox_create(VOX_SZ, VOX_SZ, height_pixels, color_pixels))) {
                panic(get_pc(), "vox_create");
@@ -131,19 +132,43 @@ static int gamescr_start(void)
        wait_vblank();
        dma_copy32(3, (void*)OAM_ADDR, oam, sidx * 2, 0);
 
-       num_enemies = 0;
-       total_enemies = 8;
+       num_enemies = total_enemies = 0;
        energy = 5;
 
        srand(0);
+       cptr = color_pixels;
+       for(i=0; i<VOX_SZ; i++) {
+               for(j=0; j<VOX_SZ; j++) {
+                       if(*cptr == 0) {
+                               /* player spawn point */
+                               pos[0] = j << 16;
+                               pos[1] = i << 16;
+
+                       } else if(*cptr >= CMAP_SPAWN0) {
+                               /* enemy spawn point */
+                               enemy = enemies + *cptr - CMAP_SPAWN0;
+                               if(enemy->anm) {
+                                       panic(get_pc(), "double spawn at %d,%d", j, i);
+                               }
+                               enemy->vobj.x = j;
+                               enemy->vobj.y = i;
+                               enemy->vobj.px = -1;
+                               enemy->anm = 0xff;
+                               enemy->hp = 2;
+                               enemy->last_fire = 0;
+                               total_enemies++;
+                       }
+                       cptr++;
+               }
+       }
+       /* check continuity */
        for(i=0; i<total_enemies; i++) {
-               enemies[i].vobj.x = rand() % VOX_SZ;
-               enemies[i].vobj.y = rand() % VOX_SZ;
-               enemies[i].vobj.px = -1;
+               if(enemy->anm <= 0) {
+                       panic(get_pc(), "discontinuous enemy list");
+               }
                enemies[i].anm = rand() & 7;
-               enemies[i].hp = 2;
-               enemies[i].last_fire = 0;
        }
+
        vox_objects(vox, (struct vox_object*)enemies, total_enemies, sizeof *enemies);
 
        nframes = 0;
index 391c9ec..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
 
@@ -286,7 +289,7 @@ 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;
+       /*int proj;*/
        struct vox_object *obj;
 
        z = vox->znear + n;
@@ -327,14 +330,12 @@ void vox_render_slice(struct voxscape *vox, int n)
                        vox->coltop[col] = hval;
 
                        /* check to see if there's an object here */
-                       obj = vox->obj;
-                       for(j=0; j<vox->num_obj; j++) {
-                               if(obj->offs == offs) {
-                                       obj->px = col;
-                                       obj->py = colstart;
-                                       obj->scale = projlut[n];
-                               }
-                               obj = (struct vox_object*)((char*)obj + vox->obj_stride);
+                       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;
@@ -391,6 +392,11 @@ void vox_objects(struct voxscape *vox, struct vox_object *ptr, int count, int st
        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;
@@ -398,7 +404,6 @@ void vox_objects(struct voxscape *vox, struct vox_object *ptr, int count, int st
        obj = ptr;
        for(i=0; i<count; i++) {
                obj->offs = obj->y * XSZ + obj->x;
-               emuprint("%d,%d -> %d", obj->x, obj->y, obj->offs);
                obj = (struct vox_object*)((char*)obj + stride);
        }
 }