correct tunnel curving
[gbajam21] / src / gamescr.c
index 3941883..7da593a 100644 (file)
@@ -1,25 +1,52 @@
 #include <stdlib.h>
 #include <string.h>
 #include "gbaregs.h"
+#include "game.h"
 #include "dma.h"
 #include "data.h"
 #include "util.h"
 #include "intr.h"
+#include "input.h"
+#include "sprite.h"
 #include "debug.h"
 
-static unsigned char *tex;//[32 * 32] __attribute__((section(".iwram")));
+static void draw_tunnel(void);
+static void vblank(void);
+
+static int nframes, 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;
+
+#define MAX_SPR                4
+static uint16_t oam[4 * MAX_SPR];
+
+static short x = 120, y = 80;
+static unsigned char rot;
+
+static int32_t bgmat[4];
+static int32_t bgx, bgy;
+static int32_t tunrot;
 
 void gamescr(void)
 {
-       int i, j, tx, ty, angle, depth, nframes, backbuf, zoffs;
-       static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
-       uint16_t *cdst, *top, *bot;
+       int i;
+       uint16_t *cdst;
        unsigned char *csrc;
-       uint32_t tun, *tunptr;
 
-       REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_FB1;
+       REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
+
+       vblperf_setcolor(0xff);
 
-       vblperf_start(0xff);
+       /* sprite setup */
+       spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
+
+       wait_vblank();
+       spr_clear();
+       spr_oam_clear(oam, 0);
+       spr_oam_clear(oam, 1);
+       spr_oam_clear(oam, 2);
+       spr_oam_clear(oam, 3);
 
        cdst = (uint16_t*)CRAM_BG_ADDR;
        csrc = tuncross_cmap;
@@ -34,35 +61,100 @@ void gamescr(void)
        tex = iwram_sbrk(32 * 32);
        memcpy(tex, tuncross_pixels, 32 * 32);
 
+       /*select_input(BN_DPAD);*/
+
+       mask(INTR_VBLANK);
+       screen_vblank = vblank;
+       unmask(INTR_VBLANK);
+
        nframes = 0;
        for(;;) {
                backbuf = ++nframes & 1;
 
-               zoffs = nframes << 1;
+               bnstate = ~REG_KEYINPUT;
+
+               draw_tunnel();
 
-               top = vram[backbuf];
-               bot = vram[backbuf] + 159 * 240 / 2;
-               tunptr = tunmap;
+               vblperf_end();
+               wait_vblank();
+               present(backbuf);
+               vblperf_begin();
+       }
+}
+
+__attribute__((noinline, target("arm"), section(".iwram")))
+static void draw_tunnel(void)
+{
+       int i, j, tx, ty, 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 = nframes;
+
+       uoffs = flip ? -nframes : nframes;
+
+       top = vram[backbuf];
+       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++) {
-                               uint16_t pptop, ppbot;
+                               tun = *--tunptr;
+                               tun = (tun >> 16) | (tun << 16);
+
+                               angle = tun & 0xff;
+                               depth = (tun >> 8) & 0xff;
+                               tx = ~((angle >> 1) - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop = tex[(ty << 5) + tx];
+                               tx = ((angle >> 1) + uoffs) & 0x1f;
+                               ppbot = tex[(ty << 5) + tx];
+
+                               angle = (tun >> 16) & 0xff;
+                               depth = (tun >> 24) & 0xff;
+                               tx = ~((angle >> 1) - uoffs) & 0x1f;
+                               ty = ((depth >> 1) + zoffs) & 0x1f;
+                               pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
+                               tx = ((angle >> 1) + 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 & 0xff;
                                depth = (tun >> 8) & 0xff;
-                               tx = ((angle >> 1) + zoffs) & 0x1f;
+                               tx = ((angle >> 1) - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
                                pptop = tex[(ty << 5) + tx];
-                               tx = ((angle >> 1) - zoffs) & 0x1f;
+                               tx = ~((angle >> 1) + uoffs) & 0x1f;
                                ppbot = tex[(ty << 5) + tx];
 
                                angle = (tun >> 16) & 0xff;
                                depth = (tun >> 24) & 0xff;
-                               tx = ((angle >> 1) + zoffs) & 0x1f;
+                               tx = ((angle >> 1) - uoffs) & 0x1f;
                                ty = ((depth >> 1) + zoffs) & 0x1f;
                                pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
-                               tx = ((angle >> 1) - zoffs) & 0x1f;
+                               tx = ~((angle >> 1) + uoffs) & 0x1f;
                                ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
 
                                *top++ = pptop;
@@ -70,10 +162,48 @@ void gamescr(void)
                        }
                        bot -= 240;
                }
+       }
+}
 
-               vblperf_end();
-               wait_vblank();
-               present(backbuf);
-               vblperf_begin();
+__attribute__((noinline, target("arm"), section(".iwram")))
+static void vblank(void)
+{
+       uint16_t bnstate;
+       int16_t mat[4];
+       static short gate_speed;
+
+       bnstate = ~REG_KEYINPUT;
+       if(bnstate & BN_DPAD) {
+               if(gate_speed < 5) {
+                       gate_speed++;
+               }
+
+               if(bnstate & BN_LEFT) x -= gate_speed;
+               if(bnstate & BN_RIGHT) x += gate_speed;
+               if(bnstate & BN_UP) y -= gate_speed;
+               if(bnstate & BN_DOWN) y += gate_speed;
+
+               if(x < 0) x = 0;
+               if(x > 239) x = 239;
+               if(y < 0) y = 0;
+               if(y > 159) y = 159;
+       } else {
+               gate_speed = 0;
        }
+
+       if(bnstate & BN_A) rot -= 2;
+       if(bnstate & BN_B) rot += 2;
+
+
+       spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
+                       SPR_ROTSCL | SPR_ROTSCL_SEL(0));
+
+       mat[0] = COS(rot);
+       mat[1] = -SIN(rot);
+       mat[2] = SIN(rot);
+       mat[3] = COS(rot);
+       spr_transform(oam, 0, mat);
+
+
+       dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
 }