software tunnel rotation / lower framerate
[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 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 __attribute__((noinline, target("arm"), section(".iwram")))
94 static void draw_tunnel(void)
95 {
96         int i, j, tx, ty, angle, depth, zoffs, uoffs;
97         uint16_t pptop;
98         uint16_t *top;
99         uint16_t tun;
100         int32_t startx, starty;
101         uint16_t *tmap = (uint16_t*)tunmap;
102
103         zoffs = nframes;
104
105         /*
106         if(bnstate & KEY_LT) tunrot++;
107         if(bnstate & KEY_RT) tunrot--;
108         */
109         tunrot = nframes;
110
111         tunmat[0] = COS(tunrot);
112         tunmat[1] = -SIN(tunrot);
113         tunmat[2] = SIN(tunrot);
114         tunmat[3] = COS(tunrot);
115         tunx = (128 << 8) - tunmat[0] * 120 + tunmat[1] * -80;
116         tuny = (128 << 8) - tunmat[2] * 120 + tunmat[3] * -80;
117
118         uoffs = tunrot;
119
120         top = vram[backbuf];
121         for(i=0; i<160; i++) {
122                 startx = tunx;
123                 starty = tuny;
124                 for(j=0; j<240/2; j++) {
125                         tx = (tunx >> 8) & 0xff;
126                         ty = (tuny >> 8) & 0xff;
127                         tun = tmap[(ty << 8) + tx];
128
129                         tunx += tunmat[0];
130                         tuny += tunmat[2];
131
132                         angle = tun & 0xff;
133                         depth = (tun >> 8) & 0xff;
134                         tx = ((angle >> 1) - uoffs) & 0x1f;
135                         ty = ((depth >> 1) + zoffs) & 0x1f;
136                         pptop = tex[(ty << 5) + tx];
137
138                         tx = (tunx >> 8) & 0xff;
139                         ty = (tuny >> 8) & 0xff;
140                         tun = tmap[(ty << 8) + tx];
141
142                         tunx += tunmat[0];
143                         tuny += tunmat[2];
144
145                         angle = tun & 0xff;
146                         depth = (tun >> 8) & 0xff;
147                         tx = ((angle >> 1) - uoffs) & 0x1f;
148                         ty = ((depth >> 1) + zoffs) & 0x1f;
149                         pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
150
151                         *top++ = pptop;
152                 }
153
154                 tunx = startx + tunmat[1];
155                 tuny = starty + tunmat[3];
156         }
157 }
158
159 __attribute__((noinline, target("arm"), section(".iwram")))
160 static void vblank(void)
161 {
162         uint16_t bnstate;
163         int16_t mat[4];
164         static short gate_speed;
165
166         bnstate = ~REG_KEYINPUT;
167         if(bnstate & BN_DPAD) {
168                 if(gate_speed < 5) {
169                         gate_speed++;
170                 }
171
172                 if(bnstate & BN_LEFT) x -= gate_speed;
173                 if(bnstate & BN_RIGHT) x += gate_speed;
174                 if(bnstate & BN_UP) y -= gate_speed;
175                 if(bnstate & BN_DOWN) y += gate_speed;
176
177                 if(x < 0) x = 0;
178                 if(x > 239) x = 239;
179                 if(y < 0) y = 0;
180                 if(y > 159) y = 159;
181         } else {
182                 gate_speed = 0;
183         }
184
185         if(bnstate & BN_A) rot -= 2;
186         if(bnstate & BN_B) rot += 2;
187
188
189         spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
190                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
191
192         mat[0] = COS(rot);
193         mat[1] = -SIN(rot);
194         mat[2] = SIN(rot);
195         mat[3] = COS(rot);
196         spr_transform(oam, 0, mat);
197
198
199         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
200 }