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