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