39ed733439ba3bc41cd0fe3c204f750e4f46ed98
[gbajam21] / src / gamescr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gbaregs.h"
4 #include "game.h"
5 #include "dma.h"
6 #include "data.h"
7 #include "util.h"
8 #include "intr.h"
9 #include "input.h"
10 #include "sprite.h"
11 #include "debug.h"
12
13 static void draw_tunnel(void);
14 static void vblank(void);
15
16 static int nframes, backbuf;
17 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
18 static unsigned char *tex;
19 static uint16_t bnstate;
20
21 #define MAX_SPR         4
22 static uint16_t oam[4 * MAX_SPR];
23
24 static short x = 120, y = 80;
25 static unsigned char rot;
26
27 void gamescr(void)
28 {
29         int i;
30         uint16_t *cdst;
31         unsigned char *csrc;
32
33         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
34
35         vblperf_setcolor(0xff);
36
37         /* sprite setup */
38         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
39
40         wait_vblank();
41         spr_clear();
42         spr_oam_clear(oam, 0);
43         spr_oam_clear(oam, 1);
44         spr_oam_clear(oam, 2);
45         spr_oam_clear(oam, 3);
46
47         cdst = (uint16_t*)CRAM_BG_ADDR;
48         csrc = tuncross_cmap;
49         for(i=0; i<256; i++) {
50                 *cdst++ = CONV_RGB24_RGB15(csrc[0], csrc[1], csrc[2]);
51                 csrc += 3;
52         }
53
54         fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
55         fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
56
57         tex = iwram_sbrk(32 * 32);
58         memcpy(tex, tuncross_pixels, 32 * 32);
59
60         /*select_input(BN_DPAD);*/
61
62         mask(INTR_VBLANK);
63         screen_vblank = vblank;
64         unmask(INTR_VBLANK);
65
66         nframes = 0;
67         for(;;) {
68                 backbuf = ++nframes & 1;
69
70                 bnstate = ~REG_KEYINPUT;
71
72                 draw_tunnel();
73
74                 vblperf_end();
75                 wait_vblank();
76                 present(backbuf);
77                 vblperf_begin();
78         }
79 }
80
81 __attribute__((noinline, target("arm"), section(".iwram")))
82 static void draw_tunnel(void)
83 {
84         static int uoffs;
85         int i, j, tx, ty, angle, depth, zoffs;
86         uint16_t pptop, ppbot;
87         uint16_t *top, *bot;
88         uint32_t tun, *tunptr;
89
90         zoffs = nframes;
91
92         if(bnstate & BN_LT) uoffs++;
93         if(bnstate & BN_RT) uoffs--;
94
95         top = vram[backbuf];
96         bot = vram[backbuf] + 159 * 240 / 2;
97         tunptr = tunmap;
98         for(i=0; i<80; i++) {
99 #ifdef VBLBAR
100                 top++;
101                 bot++;
102                 tunptr++;
103
104                 for(j=1; j<240/2; j++) {
105 #else
106                 for(j=0; j<240/2; j++) {
107 #endif
108                         tun = *tunptr++;
109
110                         angle = tun & 0xff;
111                         depth = (tun >> 8) & 0xff;
112                         tx = ((angle >> 1) + uoffs) & 0x1f;
113                         ty = ((depth >> 1) + zoffs) & 0x1f;
114                         pptop = tex[(ty << 5) + tx];
115                         tx = ~((angle >> 1) - uoffs) & 0x1f;
116                         ppbot = tex[(ty << 5) + tx];
117
118                         angle = (tun >> 16) & 0xff;
119                         depth = (tun >> 24) & 0xff;
120                         tx = ((angle >> 1) + uoffs) & 0x1f;
121                         ty = ((depth >> 1) + zoffs) & 0x1f;
122                         pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
123                         tx = ~((angle >> 1) - uoffs) & 0x1f;
124                         ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
125
126                         *top++ = pptop;
127                         *bot++ = ppbot;
128                 }
129                 bot -= 240;
130         }
131 }
132
133 __attribute__((noinline, target("arm"), section(".iwram")))
134 static void vblank(void)
135 {
136         uint16_t bnstate;
137         int16_t mat[4];
138         static short gate_speed;
139
140         bnstate = ~REG_KEYINPUT;
141         if(bnstate & BN_DPAD) {
142                 if(gate_speed < 5) {
143                         gate_speed++;
144                 }
145
146                 if(bnstate & BN_LEFT) x -= gate_speed;
147                 if(bnstate & BN_RIGHT) x += gate_speed;
148                 if(bnstate & BN_UP) y -= gate_speed;
149                 if(bnstate & BN_DOWN) y += gate_speed;
150
151                 if(x < 0) x = 0;
152                 if(x > 239) x = 239;
153                 if(y < 0) y = 0;
154                 if(y > 159) y = 159;
155         } else {
156                 gate_speed = 0;
157         }
158
159         if(bnstate & BN_A) rot -= 2;
160         if(bnstate & BN_B) rot += 2;
161
162         spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
163                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
164
165         mat[0] = COS(rot) << 1;
166         mat[1] = -SIN(rot) << 1;
167         mat[2] = SIN(rot) << 1;
168         mat[3] = COS(rot) << 1;
169         spr_transform(oam, 0, mat);
170
171
172         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
173 }