023114b38c345c83c6301b5fbcc3145e0d642921
[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, num_vbl, backbuf;
17 static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
18 static unsigned char *tex, *shadelut;
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 static int32_t tunrot;
28 static int32_t tunmat[4], tunx, tuny;
29
30 void gamescr(void)
31 {
32         int i;
33         int32_t scale;
34         uint16_t *cdst;
35         unsigned char *csrc;
36
37         REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
38
39         vblperf_setcolor(0xff);//192);
40
41         /* sprite setup */
42         spr_setup(32, 16, spr_game_pixels, spr_game_cmap);
43
44         wait_vblank();
45         spr_clear();
46         spr_oam_clear(oam, 0);
47         spr_oam_clear(oam, 1);
48         spr_oam_clear(oam, 2);
49         spr_oam_clear(oam, 3);
50
51         cdst = (uint16_t*)CRAM_BG_ADDR;
52         csrc = tuncross_cmap;
53         for(i=0; i<256; i++) {
54                 *cdst++ = CONV_RGB24_RGB15(csrc[0], csrc[1], csrc[2]);
55                 csrc += 3;
56         }
57
58         fillblock_16byte(vram[0], 0xffffffff, 240 * 160 / 16);
59         fillblock_16byte(vram[1], 0xffffffff, 240 * 160 / 16);
60
61         tex = iwram_sbrk(32 * 32);
62         memcpy(tex, tuncross_pixels, 32 * 32);
63         shadelut = iwram_sbrk(256 * 8);
64         memcpy(shadelut, tuncross_shade, 256 * 8);
65
66         scale = 0x100;//230;
67         REG_BG2PA = scale;
68         REG_BG2PB = 0;
69         REG_BG2PC = 0;
70         REG_BG2PD = scale;
71         REG_BG2X = (120 << 8) - scale * 120;
72         REG_BG2Y = (80 << 8) - scale * 80;
73
74         /*select_input(BN_DPAD);*/
75
76         mask(INTR_VBLANK);
77         screen_vblank = vblank;
78         unmask(INTR_VBLANK);
79
80         nframes = 0;
81         for(;;) {
82                 backbuf = ++nframes & 1;
83
84                 bnstate = ~REG_KEYINPUT;
85
86                 draw_tunnel();
87
88                 vblperf_end();
89                 wait_vblank();
90                 present(backbuf);
91                 vblperf_begin();
92         }
93 }
94
95 #define TUN_U(x)        ((x) & 0x3f)
96 #define TUN_V(x)        (((x) >> 6) & 0x3ff)
97 #define TEXEL(x, y, lvl) \
98         shadelut[((int)tex[((y) << 5) + (x)] << 3) + (lvl)]
99
100 __attribute__((noinline, target("arm"), section(".iwram")))
101 static void draw_tunnel(void)
102 {
103         int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn, shade;
104         static int tunsweep;
105         uint16_t pptop, ppbot;
106         uint16_t *top, *bot;
107         uint32_t tun, *tunptr;
108
109         //tunsweep = SIN(nframes) >> 4;
110
111         if((bnstate & BN_RT) && tunsweep > -31) tunsweep--;
112         if((bnstate & BN_LT) && tunsweep < 31) tunsweep++;
113
114         flip = tunsweep < 0;
115         tunturn = abs(tunsweep) & 0x1f;
116
117         zoffs = num_vbl;
118         uoffs = (flip ? -num_vbl : num_vbl) >> 1;
119
120         top = vram[backbuf];
121         bot = vram[backbuf] + 159 * 240 / 2;
122         tunptr = tunmap + tunturn * 9600;
123
124         if(flip) {
125                 tunptr += 240/2;
126                 for(i=0; i<80; i++) {
127                         for(j=0; j<240/2; j++) {
128                                 tun = *--tunptr;
129
130                                 angle = TUN_U(tun >> 16);
131                                 depth = TUN_V(tun >> 16);
132                                 shade = depth >> 7;
133                                 tx = ~(angle - uoffs) & 0x1f;
134                                 ty = ((depth >> 1) + zoffs) & 0x1f;
135                                 pptop = TEXEL(tx, ty, shade);
136                                 tx = (angle + uoffs) & 0x1f;
137                                 ppbot = TEXEL(tx, ty, shade);
138
139                                 angle = TUN_U(tun);
140                                 depth = TUN_V(tun);
141                                 shade = depth >> 7;
142                                 tx = ~(angle - uoffs) & 0x1f;
143                                 ty = ((depth >> 1) + zoffs) & 0x1f;
144                                 pptop |= (uint16_t)TEXEL(tx, ty, shade) << 8;
145                                 tx = (angle + uoffs) & 0x1f;
146                                 ppbot |= (uint16_t)TEXEL(tx, ty, shade) << 8;
147
148                                 *top++ = pptop;
149                                 *bot++ = ppbot;
150                         }
151                         bot -= 240;
152                         tunptr += 240;
153                 }
154         } else {
155                 for(i=0; i<80; i++) {
156                         for(j=0; j<240/2; j++) {
157                                 tun = *tunptr++;
158
159                                 angle = TUN_U(tun);
160                                 depth = TUN_V(tun);
161                                 shade = depth >> 7;
162                                 tx = (angle - uoffs) & 0x1f;
163                                 ty = ((depth >> 1) + zoffs) & 0x1f;
164                                 pptop = TEXEL(tx, ty, shade);
165                                 tx = ~(angle + uoffs) & 0x1f;
166                                 ppbot = TEXEL(tx, ty, shade);
167
168                                 angle = TUN_U(tun >> 16);
169                                 depth = TUN_V(tun >> 16);
170                                 shade = depth >> 7;
171                                 tx = (angle - uoffs) & 0x1f;
172                                 ty = ((depth >> 1) + zoffs) & 0x1f;
173                                 pptop |= (uint16_t)TEXEL(tx, ty, shade) << 8;
174                                 tx = ~(angle + uoffs) & 0x1f;
175                                 ppbot |= (uint16_t)TEXEL(tx, ty, shade) << 8;
176
177                                 *top++ = pptop;
178                                 *bot++ = ppbot;
179                         }
180                         bot -= 240;
181                 }
182         }
183 }
184
185 __attribute__((noinline, target("arm"), section(".iwram")))
186 static void vblank(void)
187 {
188         uint16_t bnstate;
189         int16_t mat[4];
190         static short gate_speed;
191
192         num_vbl++;
193
194         bnstate = ~REG_KEYINPUT;
195         if(bnstate & BN_DPAD) {
196                 if(gate_speed < 5) {
197                         gate_speed++;
198                 }
199
200                 if(bnstate & BN_LEFT) x -= gate_speed;
201                 if(bnstate & BN_RIGHT) x += gate_speed;
202                 if(bnstate & BN_UP) y -= gate_speed;
203                 if(bnstate & BN_DOWN) y += gate_speed;
204
205                 if(x < 0) x = 0;
206                 if(x > 239) x = 239;
207                 if(y < 0) y = 0;
208                 if(y > 159) y = 159;
209         } else {
210                 gate_speed = 0;
211         }
212
213         if(bnstate & BN_A) rot -= 2;
214         if(bnstate & BN_B) rot += 2;
215
216
217         spr_oam(oam, 0, 512, x - 64, y - 64, SPR_SZ64 | SPR_DBLSZ |
218                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
219
220         mat[0] = COS(rot);
221         mat[1] = -SIN(rot);
222         mat[2] = SIN(rot);
223         mat[3] = COS(rot);
224         spr_transform(oam, 0, mat);
225
226
227         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
228 }