fixed the tunnel precalc to produce a continuous 10 bit depth value
[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) >> 7) & 0x1ff)
95
96 __attribute__((noinline, target("arm"), section(".iwram")))
97 static void draw_tunnel(void)
98 {
99         int i, j, tx, ty, u, v, angle, depth, zoffs, uoffs, flip, tunturn;
100         static int tunsweep;
101         uint16_t pptop, ppbot;
102         uint16_t *top, *bot;
103         uint32_t tun, *tunptr;
104
105         //tunsweep = SIN(nframes) >> 4;
106
107         if((bnstate & BN_RT) && tunsweep > -31) tunsweep--;
108         if((bnstate & BN_LT) && tunsweep < 31) tunsweep++;
109
110         flip = tunsweep < 0;
111         tunturn = abs(tunsweep) & 0x1f;
112
113         zoffs = num_vbl;
114         uoffs = 0;//(flip ? -num_vbl : num_vbl) >> 2;
115
116         top = vram[backbuf];
117         bot = vram[backbuf] + 159 * 240 / 2;
118         tunptr = tunmap + tunturn * 9600;
119
120         if(flip) {
121                 tunptr += 240/2;
122                 for(i=0; i<80; i++) {
123                         for(j=0; j<240/2; j++) {
124                                 tun = *--tunptr;
125
126                                 angle = TUN_U(tun >> 16);
127                                 depth = TUN_V(tun >> 16);
128                                 tx = ~(angle - uoffs) & 0x1f;
129                                 ty = (depth + zoffs) & 0x1f;
130                                 pptop = tex[(ty << 5) + tx];
131                                 tx = (angle + uoffs) & 0x1f;
132                                 ppbot = tex[(ty << 5) + tx];
133
134                                 angle = TUN_U(tun);
135                                 depth = TUN_V(tun);
136                                 tx = ~(angle - uoffs) & 0x1f;
137                                 ty = (depth + zoffs) & 0x1f;
138                                 pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
139                                 tx = (angle + uoffs) & 0x1f;
140                                 ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
141
142                                 *top++ = pptop;
143                                 *bot++ = ppbot;
144                         }
145                         bot -= 240;
146                         tunptr += 240;
147                 }
148         } else {
149                 for(i=0; i<80; i++) {
150                         for(j=0; j<240/2; j++) {
151                                 tun = *tunptr++;
152
153                                 angle = TUN_U(tun);
154                                 depth = TUN_V(tun);
155                                 tx = (angle - uoffs) & 0x1f;
156                                 ty = (depth + zoffs) & 0x1f;
157                                 pptop = tex[(ty << 5) + tx];
158                                 tx = ~(angle + uoffs) & 0x1f;
159                                 ppbot = tex[(ty << 5) + tx];
160
161                                 angle = TUN_U(tun >> 16);
162                                 depth = TUN_V(tun >> 16);
163                                 tx = (angle - uoffs) & 0x1f;
164                                 ty = (depth + zoffs) & 0x1f;
165                                 pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
166                                 tx = ~(angle + uoffs) & 0x1f;
167                                 ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
168
169                                 *top++ = pptop;
170                                 *bot++ = ppbot;
171                         }
172                         bot -= 240;
173                 }
174         }
175 }
176
177 __attribute__((noinline, target("arm"), section(".iwram")))
178 static void vblank(void)
179 {
180         uint16_t bnstate;
181         int16_t mat[4];
182         static short gate_speed;
183
184         num_vbl++;
185
186         bnstate = ~REG_KEYINPUT;
187         if(bnstate & BN_DPAD) {
188                 if(gate_speed < 5) {
189                         gate_speed++;
190                 }
191
192                 if(bnstate & BN_LEFT) x -= gate_speed;
193                 if(bnstate & BN_RIGHT) x += gate_speed;
194                 if(bnstate & BN_UP) y -= gate_speed;
195                 if(bnstate & BN_DOWN) y += gate_speed;
196
197                 if(x < 0) x = 0;
198                 if(x > 239) x = 239;
199                 if(y < 0) y = 0;
200                 if(y > 159) y = 159;
201         } else {
202                 gate_speed = 0;
203         }
204
205         if(bnstate & BN_A) rot -= 2;
206         if(bnstate & BN_B) rot += 2;
207
208
209         spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
210                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
211
212         mat[0] = COS(rot);
213         mat[1] = -SIN(rot);
214         mat[2] = SIN(rot);
215         mat[3] = COS(rot);
216         spr_transform(oam, 0, mat);
217
218
219         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
220 }