fix
[gbajam21] / src / gamescr.c
index 9e9d6e1..23a8e21 100644 (file)
@@ -13,7 +13,7 @@
 static void draw_tunnel(void);
 static void vblank(void);
 
-static int nframes, backbuf;
+static int nframes, num_vbl, backbuf;
 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
 static unsigned char *tex;
 static uint16_t bnstate;
@@ -90,69 +90,87 @@ void gamescr(void)
        }
 }
 
+#define TUN_U(x)       ((x) & 0x3f)
+#define TUN_V(x)       (((x) >> 6) & 0x3ff)
+
 __attribute__((noinline, target("arm"), section(".iwram")))
 static void draw_tunnel(void)
 {
-       int i, j, tx, ty, angle, depth, zoffs, uoffs;
-       uint16_t pptop;
-       uint16_t *top;
-       uint16_t tun;
-       int32_t startx, starty;
-       uint16_t *tmap = (uint16_t*)tunmap;
-
-       zoffs = nframes;
-
-       /*
-       if(bnstate & KEY_LT) tunrot++;
-       if(bnstate & KEY_RT) tunrot--;
-       */
-       tunrot = nframes;
-
-       tunmat[0] = COS(tunrot);
-       tunmat[1] = -SIN(tunrot);
-       tunmat[2] = SIN(tunrot);
-       tunmat[3] = COS(tunrot);
-       tunx = (128 << 8) - tunmat[0] * 120 + tunmat[1] * -80;
-       tuny = (128 << 8) - tunmat[2] * 120 + tunmat[3] * -80;
-
-       uoffs = tunrot;
+       int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn;
+       static int tunsweep;
+       uint16_t pptop, ppbot;
+       uint16_t *top, *bot;
+       uint32_t tun, *tunptr;
+
+       //tunsweep = SIN(nframes) >> 4;
+
+       if((bnstate & BN_RT) && tunsweep > -31) tunsweep--;
+       if((bnstate & BN_LT) && tunsweep < 31) tunsweep++;
+
+       flip = tunsweep < 0;
+       tunturn = abs(tunsweep) & 0x1f;
+
+       zoffs = num_vbl;
+       uoffs = (flip ? -num_vbl : num_vbl) >> 1;
 
        top = vram[backbuf];
-       for(i=0; i<160; i++) {
-               startx = tunx;
-               starty = tuny;
-               for(j=0; j<240/2; j++) {
-                       tx = (tunx >> 8) & 0xff;
-                       ty = (tuny >> 8) & 0xff;
-                       tun = tmap[(ty << 8) + tx];
-
-                       tunx += tunmat[0];
-                       tuny += tunmat[2];
-
-                       angle = tun & 0xff;
-                       depth = (tun >> 8) & 0xff;
-                       tx = ((angle >> 1) - uoffs) & 0x1f;
-                       ty = ((depth >> 1) + zoffs) & 0x1f;
-                       pptop = tex[(ty << 5) + tx];
-
-                       tx = (tunx >> 8) & 0xff;
-                       ty = (tuny >> 8) & 0xff;
-                       tun = tmap[(ty << 8) + tx];
-
-                       tunx += tunmat[0];
-                       tuny += tunmat[2];
-
-                       angle = tun & 0xff;
-                       depth = (tun >> 8) & 0xff;
-                       tx = ((angle >> 1) - uoffs) & 0x1f;
-                       ty = ((depth >> 1) + zoffs) & 0x1f;
-                       pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
-
-                       *top++ = pptop;
+       bot = vram[backbuf] + 159 * 240 / 2;
+       tunptr = tunmap + tunturn * 9600;
+
+       if(flip) {
+               tunptr += 240/2;
+               for(i=0; i<80; i++) {
+                       for(j=0; j<240/2; j++) {
+                               tun = *--tunptr;
+
+                               angle = TUN_U(tun >> 16);
+                               depth = TUN_V(tun >> 16);
+                               tx = ~(angle - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop = tex[(ty << 5) + tx];
+                               tx = (angle + uoffs) & 0x1f;
+                               ppbot = tex[(ty << 5) + tx];
+
+                               angle = TUN_U(tun);
+                               depth = TUN_V(tun);
+                               tx = ~(angle - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               tx = (angle + uoffs) & 0x1f;
+                               ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
+
+                               *top++ = pptop;
+                               *bot++ = ppbot;
+                       }
+                       bot -= 240;
+                       tunptr += 240;
+               }
+       } else {
+               for(i=0; i<80; i++) {
+                       for(j=0; j<240/2; j++) {
+                               tun = *tunptr++;
+
+                               angle = TUN_U(tun);
+                               depth = TUN_V(tun);
+                               tx = (angle - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop = tex[(ty << 5) + tx];
+                               tx = ~(angle + uoffs) & 0x1f;
+                               ppbot = tex[(ty << 5) + tx];
+
+                               angle = TUN_U(tun >> 16);
+                               depth = TUN_V(tun >> 16);
+                               tx = (angle - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               tx = ~(angle + uoffs) & 0x1f;
+                               ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
+
+                               *top++ = pptop;
+                               *bot++ = ppbot;
+                       }
+                       bot -= 240;
                }
-
-               tunx = startx + tunmat[1];
-               tuny = starty + tunmat[3];
        }
 }
 
@@ -163,6 +181,8 @@ static void vblank(void)
        int16_t mat[4];
        static short gate_speed;
 
+       num_vbl++;
+
        bnstate = ~REG_KEYINPUT;
        if(bnstate & BN_DPAD) {
                if(gate_speed < 5) {