skippable logo splash
[gbajam22] / src / gamescr.c
index 2703795..b148d22 100644 (file)
@@ -21,7 +21,7 @@ static void gamescr_stop(void);
 static void gamescr_frame(void);
 static void gamescr_vblank(void);
 
-static void update(void);
+static int update(void);
 static void draw(void);
 
 static struct screen gamescr = {
@@ -68,6 +68,7 @@ static int32_t xform_sa, xform_ca;    /* for viewport bank/zoom */
 static int xform_s;
 
 static short vblcount;
+static void *prev_iwram_top;
 
 
 static inline void xform_pixel(int *xp, int *yp);
@@ -84,7 +85,10 @@ static int gamescr_start(void)
        uint8_t *cptr;
        struct enemy *enemy;
 
+       prev_iwram_top = iwram_sbrk(0);
+
        gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
+       fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
 
        vblperf_setcolor(0);
 
@@ -119,13 +123,10 @@ static int gamescr_start(void)
        spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
        dynspr_base = sidx;
 
-       wait_vblank();
-       dma_copy32(3, (void*)OAM_ADDR, oam, sidx * 2, 0);
-
        num_enemies = total_enemies = 0;
        energy = 5;
 
-       srand(0);
+       memset(enemies, 0, sizeof enemies);
        cptr = color_pixels;
        for(i=0; i<VOX_SZ; i++) {
                for(j=0; j<VOX_SZ; j++) {
@@ -166,12 +167,31 @@ endspawn:
 
        vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
 
+       energy = MAX_ENERGY;
+       xform_sa = 0;
+       xform_ca = 0x10000;
+       xform_s = 0x100;
+
+       vblcount = 0;
        nframes = 0;
        return 0;
 }
 
 static void gamescr_stop(void)
 {
+       iwram_brk(prev_iwram_top);
+
+       wait_vblank();
+       /* clear sprites */
+       spr_clear();
+       /* reset background rot/scale state */
+       REG_BG2X = 0;
+       REG_BG2Y = 0;
+       REG_BG2PA = 0x100;
+       REG_BG2PB = 0;
+       REG_BG2PC = 0;
+       REG_BG2PD = 0x100;
+
 }
 
 static void gamescr_frame(void)
@@ -181,7 +201,9 @@ static void gamescr_frame(void)
 
        vox_framebuf(240, 160, framebuf, horizon);
 
-       update();
+       if(update() == -1) {
+               return;
+       }
        draw();
 
        vblperf_end();
@@ -214,7 +236,7 @@ static int numspr[][2] = {
 #define TURN_SPEED     0x200
 #define ELEV_SPEED     8
 
-static void update(void)
+static int update(void)
 {
        int32_t fwd[2], right[2];
        int i, snum, ledspr;
@@ -222,8 +244,10 @@ static void update(void)
 
        update_keyb();
 
-       if(KEYPRESS(BN_SELECT)) {
-               vox_quality ^= 1;
+       if(KEYPRESS(BN_START)) {
+               /* TODO pause menu */
+               change_screen(find_screen("menu"));
+               return -1;
        }
 
        if(keystate) {
@@ -244,8 +268,17 @@ static void update(void)
                        pos[1] += fwd[1];
                }
                if(keystate & BN_B) {
-                       pos[0] -= fwd[0];
-                       pos[1] -= fwd[1];
+                       for(i=0; i<num_enemies; i++) {
+                               if(enemies[i].hp && enemies[i].vobj.px >= 0) {
+                                       int dx = enemies[i].vobj.px - 120;
+                                       int dy = enemies[i].vobj.py - 80;
+                                       if(abs(dx) < 10 && abs(dy) < 10) {
+                                               emuprint("pow");
+                                               enemies[i].hp--;
+                                               break;
+                                       }
+                               }
+                       }
                }
                if(keystate & BN_UP) {
                        if(horizon > 40) horizon -= ELEV_SPEED;
@@ -328,6 +361,8 @@ static void update(void)
        mask(INTR_VBLANK);
        dynspr_count = snum;
        unmask(INTR_VBLANK);
+
+       return 0;
 }
 
 static void draw(void)
@@ -361,8 +396,6 @@ static void gamescr_vblank(void)
 
        vblcount++;
 
-       if(!nframes) return;
-
        /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
         * DMA them from cartridge easily
         */
@@ -391,8 +424,6 @@ static void gamescr_vblank(void)
        REG_BG2PC = -sa;
        REG_BG2PD = ca;
 
-       keystate = ~REG_KEYINPUT;
-
        if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
                if(bank) {
                        bank -= bankdir << 4;