df11abc485988f8c32b316371441fb94aa6414f1
[gbajam22] / 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 "util.h"
7 #include "intr.h"
8 #include "input.h"
9 #include "gba.h"
10 #include "sprite.h"
11 #include "debug.h"
12 #include "voxscape.h"
13 #include "data.h"
14
15 #define FOV             30
16 #define NEAR    2
17 #define FAR             85
18
19 static int gamescr_start(void);
20 static void gamescr_stop(void);
21 static void gamescr_frame(void);
22 static void gamescr_vblank(void);
23
24 static void update(void);
25 static void draw(void);
26
27 static struct screen gamescr = {
28         "game",
29         gamescr_start,
30         gamescr_stop,
31         gamescr_frame,
32         gamescr_vblank
33 };
34
35 struct enemy {
36         struct vox_object vobj;
37         short hp;
38         short anm;
39         int last_fire;
40 };
41 #define ENEMY_VALID(e)  ((e)->anm != 0)
42
43 static uint16_t *framebuf;
44
45 static int nframes, backbuf;
46 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
47
48 static int32_t pos[2], angle, horizon = 80;
49 static struct voxscape *vox;
50
51 #define COLOR_HORIZON   192
52 #define COLOR_ZENITH    255
53
54 #define MAX_SPR         32
55 static uint16_t oam[4 * MAX_SPR];
56 static int dynspr_base, dynspr_count;
57
58
59 #define MAX_ENEMIES     (256 - CMAP_SPAWN0)
60 struct enemy enemies[MAX_ENEMIES];
61 int num_enemies, total_enemies;
62 static int energy;
63 #define MAX_ENERGY      5
64
65
66 #define XFORM_PIXEL_X(x, y)     (xform_ca * (x) - xform_sa * (y) + (120 << 8))
67 #define XFORM_PIXEL_Y(x, y)     (xform_sa * (x) + xform_ca * (y) + (80 << 8))
68 static int32_t xform_sa, xform_ca;      /* for viewport bank/zoom */
69 static int xform_s;
70
71 static short vblcount;
72
73
74 static inline void xform_pixel(int *xp, int *yp);
75
76
77 struct screen *init_game_screen(void)
78 {
79         return &gamescr;
80 }
81
82 static int gamescr_start(void)
83 {
84         int i, j, sidx;
85         uint8_t *cptr;
86         struct enemy *enemy;
87
88         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
89
90         vblperf_setcolor(0);
91
92         pos[0] = pos[1] = VOX_SZ << 15;
93
94         if(!(vox = vox_create(VOX_SZ, VOX_SZ, height_pixels, color_pixels))) {
95                 panic(get_pc(), "vox_create");
96         }
97         vox_proj(vox, FOV, NEAR, FAR);
98         vox_view(vox, pos[0], pos[1], -40, angle);
99
100         /* setup color image palette */
101         for(i=0; i<256; i++) {
102                 int r = color_cmap[i * 3];
103                 int g = color_cmap[i * 3 + 1];
104                 int b = color_cmap[i * 3 + 2];
105                 gba_bgpal[i] = (((uint16_t)b << 7) & 0x7c00) | (((uint16_t)g << 2) & 0x3e0) | (((uint16_t)r >> 3) & 0x1f);
106         }
107         /*
108         intr_disable();
109         interrupt(INTR_HBLANK, hblank);
110         REG_DISPSTAT |= DISPSTAT_IEN_HBLANK;
111         unmask(INTR_HBLANK);
112         intr_enable();
113         */
114
115         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
116         wait_vblank();
117         spr_clear();
118
119         for(i=0; i<MAX_SPR; i++) {
120                 spr_oam_clear(oam, i);
121         }
122
123         sidx = 0;
124         spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
125         spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
126         spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
127         spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
128         spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
129         spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
130         dynspr_base = sidx;
131
132         wait_vblank();
133         dma_copy32(3, (void*)OAM_ADDR, oam, sidx * 2, 0);
134
135         num_enemies = total_enemies = 0;
136         energy = 5;
137
138         srand(0);
139         cptr = color_pixels;
140         for(i=0; i<VOX_SZ; i++) {
141                 for(j=0; j<VOX_SZ; j++) {
142                         if(*cptr == 0) {
143                                 /* player spawn point */
144                                 pos[0] = j << 16;
145                                 pos[1] = i << 16;
146
147                         } else if(*cptr >= CMAP_SPAWN0) {
148                                 /* enemy spawn point */
149                                 enemy = enemies + *cptr - CMAP_SPAWN0;
150                                 if(enemy->anm) {
151                                         panic(get_pc(), "double spawn at %d,%d", j, i);
152                                 }
153                                 enemy->vobj.x = j;
154                                 enemy->vobj.y = i;
155                                 enemy->vobj.px = -1;
156                                 enemy->anm = 0xff;
157                                 enemy->hp = 2;
158                                 enemy->last_fire = 0;
159                                 total_enemies++;
160                         }
161                         cptr++;
162                 }
163         }
164         /* check continuity */
165         for(i=0; i<total_enemies; i++) {
166                 if(enemy->anm <= 0) {
167                         panic(get_pc(), "discontinuous enemy list");
168                 }
169                 enemies[i].anm = rand() & 7;
170         }
171
172         vox_objects(vox, (struct vox_object*)enemies, total_enemies, sizeof *enemies);
173
174         nframes = 0;
175         return 0;
176 }
177
178 static void gamescr_stop(void)
179 {
180 }
181
182 static void gamescr_frame(void)
183 {
184         backbuf = ++nframes & 1;
185         framebuf = vram[backbuf];
186
187         vox_framebuf(vox, 240, 160, framebuf, horizon);
188
189         update();
190         draw();
191
192         vblperf_end();
193         wait_vblank();
194         present(backbuf);
195
196         if(!(nframes & 15)) {
197                 emuprint("vbl: %d", vblperf_count);
198         }
199 #ifdef VBLBAR
200         vblperf_begin();
201 #else
202         vblperf_count = 0;
203 #endif
204 }
205
206 #define NS(x)   (SPRID_UINUM + ((x) << 1))
207 static int numspr[][2] = {
208         {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
209         {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
210         {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
211         {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
212         {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
213         {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
214         {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
215         {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
216 };
217
218 #define WALK_SPEED      0x40000
219 #define TURN_SPEED      0x200
220 #define ELEV_SPEED      8
221
222 static void update(void)
223 {
224         int32_t fwd[2], right[2];
225         int i, snum, ledspr;
226         struct enemy *enemy;
227
228         update_keyb();
229
230         if(KEYPRESS(BN_SELECT)) {
231                 vox_quality ^= 1;
232         }
233
234         if(keystate) {
235                 if(keystate & BN_LEFT) {
236                         angle += TURN_SPEED;
237                 }
238                 if(keystate & BN_RIGHT) {
239                         angle -= TURN_SPEED;
240                 }
241
242                 fwd[0] = -SIN(angle);
243                 fwd[1] = COS(angle);
244                 right[0] = fwd[1];
245                 right[1] = -fwd[0];
246
247                 if(keystate & BN_A) {
248                         pos[0] += fwd[0];
249                         pos[1] += fwd[1];
250                 }
251                 if(keystate & BN_B) {
252                         pos[0] -= fwd[0];
253                         pos[1] -= fwd[1];
254                 }
255                 if(keystate & BN_UP) {
256                         if(horizon > 40) horizon -= ELEV_SPEED;
257                 }
258                 if(keystate & BN_DOWN) {
259                         if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
260                 }
261                 if(keystate & BN_RT) {
262                         pos[0] += right[0];
263                         pos[1] += right[1];
264                 }
265                 if(keystate & BN_LT) {
266                         pos[0] -= right[0];
267                         pos[1] -= right[1];
268                 }
269
270                 vox_view(vox, pos[0], pos[1], -40, angle);
271         }
272
273         snum = 0;
274         /* turrets number */
275         spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][0], 200, 144, SPR_VRECT | SPR_256COL);
276         spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][1], 208, 144, SPR_VRECT | SPR_256COL);
277         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
278         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
279         /* energy bar */
280         if(energy == MAX_ENERGY) {
281                 ledspr = SPRID_LEDBLU;
282         } else {
283                 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
284         }
285         for(i=0; i<5; i++) {
286                 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
287                                 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
288         }
289         /* enemy sprites */
290         /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
291         enemy = enemies;
292         for(i=0; i<total_enemies; i++) {
293                 int sid, anm, px, py;
294                 unsigned int flags;
295                 int16_t mat[4];
296                 int32_t sa, ca, scale;
297
298                 if(enemy->vobj.px >= 0) {
299                         flags = SPR_SZ32 | SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
300                         if(enemies->hp > 0) {
301                                 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
302                                 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
303                                 flags |= SPR_VRECT;
304                         } else {
305                                 sid = SPRID_HUSK;
306                         }
307
308                         px = enemy->vobj.px - 120;
309                         py = enemy->vobj.py - 80;
310                         xform_pixel(&px, &py);
311
312                         spr_oam(oam, dynspr_base + snum++, sid, px - 20, py - 32, flags);
313
314                         scale = enemy->vobj.scale;
315                         if(scale > 0x10000) scale = 0x10000;
316                         sa = xform_sa / scale;
317                         ca = xform_ca / scale;
318                         mat[0] = anm >= 8 ? -ca : ca;
319                         mat[1] = sa;
320                         mat[2] = -sa;
321                         mat[3] = ca;
322
323                         spr_transform(oam, 0, mat);
324                         enemy->vobj.px = -1;
325                 }
326                 enemy++;
327         }
328         for(i=snum; i<dynspr_count; i++) {
329                 spr_oam_clear(oam, dynspr_base + i);
330         }
331
332         mask(INTR_VBLANK);
333         dynspr_count = snum;
334         unmask(INTR_VBLANK);
335 }
336
337 static void draw(void)
338 {
339         //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
340         fillblock_16byte(framebuf, 0, 240 * 160 / 16);
341
342         vox_render(vox);
343         //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
344         //vox_sky_solid(vox, COLOR_ZENITH);
345 }
346
347 static inline void xform_pixel(int *xp, int *yp)
348 {
349         int32_t sa = xform_sa >> 8;
350         int32_t ca = xform_ca >> 8;
351         int x = *xp;
352         int y = *yp;
353
354         *xp = (ca * x - sa * y + (120 << 8)) >> 8;
355         *yp = (sa * x + ca * y + (80 << 8)) >> 8;
356 }
357
358 #define MAXBANK         0x100
359
360 ARM_IWRAM
361 static void gamescr_vblank(void)
362 {
363         static int bank, bankdir, theta;
364         int32_t sa, ca;
365
366         vblcount++;
367
368         if(!nframes) return;
369
370         /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
371          * DMA them from cartridge easily
372          */
373
374         /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
375         dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
376
377         theta = -(bank << 3);
378         xform_sa = SIN(theta);
379         xform_ca = COS(theta);
380 #if 0
381         xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
382         sa = (((xform_sa) >> 8) * xform_s) >> 12;
383         ca = (((xform_ca) >> 8) * xform_s) >> 12;
384 #else
385         xform_s = (MAXBANK + (abs(bank) >> 3));
386         sa = xform_sa / xform_s;
387         ca = xform_ca / xform_s;
388 #endif
389
390         REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
391         REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
392
393         REG_BG2PA = ca;
394         REG_BG2PB = sa;
395         REG_BG2PC = -sa;
396         REG_BG2PD = ca;
397
398         keystate = ~REG_KEYINPUT;
399
400         if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
401                 if(bank) {
402                         bank -= bankdir << 4;
403                 }
404         } else if(keystate & BN_LEFT) {
405                 bankdir = -1;
406                 if(bank > -MAXBANK) bank -= 16;
407         } else if(keystate & BN_RIGHT) {
408                 bankdir = 1;
409                 if(bank < MAXBANK) bank += 16;
410         }
411 }