16bit LUT
[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 bgmat[4];
28 static int32_t bgx, bgy;
29 static int32_t tunrot;
30
31 void gamescr(void)
32 {
33         int i;
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         /*select_input(BN_DPAD);*/
65
66         mask(INTR_VBLANK);
67         screen_vblank = vblank;
68         unmask(INTR_VBLANK);
69
70         nframes = 0;
71         for(;;) {
72                 backbuf = ++nframes & 1;
73
74                 bnstate = ~REG_KEYINPUT;
75
76                 draw_tunnel();
77
78                 vblperf_end();
79                 wait_vblank();
80                 present(backbuf);
81
82                 tunrot = nframes;
83                 bgmat[0] = (COS(tunrot) * 140) >> 8;
84                 bgmat[1] = (-SIN(tunrot) * 140) >> 8;
85                 bgmat[2] = (SIN(tunrot) * 140) >> 8;
86                 bgmat[3] = (COS(tunrot) * 140) >> 8;
87                 bgx = (120 << 8) - bgmat[0] * 120 + bgmat[1] * -80;
88                 bgy = (80 << 8) - bgmat[2] * 120 + bgmat[3] * -80;
89
90                 vblperf_begin();
91         }
92 }
93
94 __attribute__((noinline, target("arm"), section(".iwram")))
95 static void draw_tunnel(void)
96 {
97         int i, j, tx, ty, angle, depth, zoffs, uoffs;
98         uint16_t pptop, ppbot;
99         uint16_t *top, *bot;
100         uint32_t tun, *tunptr;
101
102         zoffs = nframes;
103
104         uoffs = tunrot >> 1;
105
106         top = vram[backbuf] + 20;
107         bot = vram[backbuf] + 159 * 240 / 2 + 20;
108         tunptr = tunmap;
109         for(i=0; i<80; i++) {
110                 for(j=0; j<160/2; j++) {
111 #ifdef VBLBAR
112                         if(j == 160/4) {
113                                 tunptr++;
114                                 top++;
115                                 bot++;
116                                 continue;
117                         }
118 #endif
119                         tun = *tunptr++;
120
121                         angle = tun & 0xff;
122                         depth = (tun >> 8) & 0xff;
123                         tx = ((angle >> 1) - uoffs) & 0x1f;
124                         ty = ((depth >> 1) + zoffs) & 0x1f;
125                         pptop = tex[(ty << 5) + tx];
126                         tx = ~((angle >> 1) + uoffs) & 0x1f;
127                         ppbot = tex[(ty << 5) + tx];
128
129                         angle = (tun >> 16) & 0xff;
130                         depth = (tun >> 24) & 0xff;
131                         tx = ((angle >> 1) - uoffs) & 0x1f;
132                         ty = ((depth >> 1) + zoffs) & 0x1f;
133                         pptop |= (uint16_t)tex[(ty << 5) + tx] << 8;
134                         tx = ~((angle >> 1) + uoffs) & 0x1f;
135                         ppbot |= (uint16_t)tex[(ty << 5) + tx] << 8;
136
137                         *top++ = pptop;
138                         *bot++ = ppbot;
139                 }
140                 top += 40;
141                 bot -= 240 - 40;
142         }
143 }
144
145 __attribute__((noinline, target("arm"), section(".iwram")))
146 static void vblank(void)
147 {
148         uint16_t bnstate;
149         int16_t mat[4];
150         static short gate_speed;
151
152         REG_BG2PA = bgmat[0];
153         REG_BG2PB = bgmat[1];
154         REG_BG2PC = bgmat[2];
155         REG_BG2PD = bgmat[3];
156         REG_BG2X = bgx;
157         REG_BG2Y = bgy;
158
159         bnstate = ~REG_KEYINPUT;
160         if(bnstate & BN_DPAD) {
161                 if(gate_speed < 5) {
162                         gate_speed++;
163                 }
164
165                 if(bnstate & BN_LEFT) x -= gate_speed;
166                 if(bnstate & BN_RIGHT) x += gate_speed;
167                 if(bnstate & BN_UP) y -= gate_speed;
168                 if(bnstate & BN_DOWN) y += gate_speed;
169
170                 if(x < 0) x = 0;
171                 if(x > 239) x = 239;
172                 if(y < 0) y = 0;
173                 if(y > 159) y = 159;
174         } else {
175                 gate_speed = 0;
176         }
177
178         if(bnstate & BN_A) rot -= 2;
179         if(bnstate & BN_B) rot += 2;
180
181
182         spr_oam(oam, 0, 512 + 256, x - 64, y - 64, SPR_256COL | SPR_SZ64 | SPR_DBLSZ |
183                         SPR_ROTSCL | SPR_ROTSCL_SEL(0));
184
185         mat[0] = COS(rot);
186         mat[1] = -SIN(rot);
187         mat[2] = SIN(rot);
188         mat[3] = COS(rot);
189         spr_transform(oam, 0, mat);
190
191
192         dma_copy16(3, (void*)OAM_ADDR, oam, sizeof oam / 2, 0);
193 }