896af07ce052551ce3a6dfc5d5bd50e9aa6c10c1
[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 "timer.h"
12 #include "debug.h"
13 #include "voxscape.h"
14 #include "data.h"
15 #include "scoredb.h"
16
17 #define FOV             30
18 #define NEAR    2
19 #define FAR             85
20
21 #define P_RATE  250
22 #define E_RATE  250
23 #define SHOT_TIME       50
24
25 static int gamescr_start(void);
26 static void gamescr_stop(void);
27 static void gamescr_frame(void);
28 static void gamescr_vblank(void);
29
30 static int update(void);
31 static void draw(void);
32 static void victory(void);
33
34 static struct screen gamescr = {
35         "game",
36         gamescr_start,
37         gamescr_stop,
38         gamescr_frame,
39         gamescr_vblank
40 };
41
42 struct enemy {
43         struct vox_object vobj;
44         short hp;
45         short anm;
46         int last_shot;
47 };
48 #define ENEMY_VALID(e)  ((e)->anm != 0)
49
50 static uint16_t *framebuf;
51
52 static int nframes, backbuf;
53 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
54
55 static int32_t pos[2], angle, horizon = 80;
56 static long last_shot, hitfrm;
57 static int hit_px, hit_py;
58
59 #define COLOR_HORIZON   192
60 #define COLOR_ZENITH    255
61
62 #define MAX_SPR         32
63 static uint16_t oam[4 * MAX_SPR];
64 static int dynspr_base, dynspr_count;
65
66
67 #define MAX_ENEMIES             (255 - CMAP_SPAWN0)
68 static struct enemy enemies[MAX_ENEMIES];
69 static int num_kills, total_enemies;
70 static int energy;
71 #define MAX_ENERGY      5
72
73 #define ENEMY_ENERGY    4
74
75 static int score;
76 static unsigned long total_time, start_time;
77
78 #define XFORM_PIXEL_X(x, y)     (xform_ca * (x) - xform_sa * (y) + (120 << 8))
79 #define XFORM_PIXEL_Y(x, y)     (xform_sa * (x) + xform_ca * (y) + (80 << 8))
80 static int32_t xform_sa, xform_ca;      /* for viewport bank/zoom */
81 static int xform_s;
82
83 static short vblcount;
84 static void *prev_iwram_top;
85
86
87 static inline void xform_pixel(int *xp, int *yp);
88
89
90 struct screen *init_game_screen(void)
91 {
92         return &gamescr;
93 }
94
95 static void setup_palette(void)
96 {
97         int i;
98         unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
99
100         emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
101
102         for(i=0; i<256; i++) {
103                 int r = cmap[i * 3];
104                 int g = cmap[i * 3 + 1];
105                 int b = cmap[i * 3 + 2];
106                 gba_bgpal[i] = RGB555(r, g, b);
107         }
108 }
109
110 static int gamescr_start(void)
111 {
112         int i, j, sidx;
113         uint8_t *cptr;
114         struct enemy *enemy;
115
116         prev_iwram_top = iwram_sbrk(0);
117
118         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
119         fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
120
121         vblperf_setcolor(0);
122
123         pos[0] = pos[1] = VOX_SZ << 15;
124         angle = 0x8000;
125         last_shot = -P_RATE - 1;
126
127         vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
128         vox_proj(FOV, NEAR, FAR);
129         vox_view(pos[0], pos[1], -40, angle);
130
131         /* setup color image palette */
132         setup_palette();
133
134         spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
135         wait_vblank();
136         spr_clear();
137
138         for(i=0; i<MAX_SPR; i++) {
139                 spr_oam_clear(oam, i);
140         }
141
142         sidx = 0;
143         spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
144         spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
145         spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
146         spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
147         spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
148         spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
149         dynspr_base = sidx;
150
151         num_kills = total_enemies = 0;
152         energy = 5;
153
154         memset(enemies, 0, sizeof enemies);
155         cptr = color_pixels;
156         for(i=0; i<VOX_SZ; i++) {
157                 for(j=0; j<VOX_SZ; j++) {
158                         if(*cptr == 255) {
159                                 /* player spawn point */
160                                 pos[0] = j << 16;
161                                 pos[1] = i << 16;
162
163                         } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
164                                 /* enemy spawn point */
165                                 int idx = *cptr - CMAP_SPAWN0;
166                                 enemy = enemies + idx;
167                                 if(enemy->anm) {
168                                         panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
169                                                         j, i, enemy->vobj.x, enemy->vobj.y);
170                                 }
171                                 enemy->vobj.x = j;
172                                 enemy->vobj.y = i;
173                                 enemy->vobj.px = -1;
174                                 enemy->anm = 0xff;
175                                 enemy->hp = ENEMY_ENERGY;
176                                 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
177                                 if(++total_enemies >= MAX_ENEMIES) {
178                                         goto endspawn;
179                                 }
180                         }
181                         cptr++;
182                 }
183         }
184 endspawn:
185         total_enemies = 1;      /* XXX DBG */
186         /* check continuity */
187         for(i=0; i<total_enemies; i++) {
188                 if(enemies[i].anm <= 0) {
189                         panic(get_pc(), "discontinuous enemy list");
190                 }
191                 enemies[i].anm = rand() & 7;
192         }
193
194         vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
195
196         energy = MAX_ENERGY;
197         xform_sa = 0;
198         xform_ca = 0x10000;
199         xform_s = 0x100;
200
201         score = -1;
202         total_time = 0;
203         start_time = timer_msec;
204
205         vblcount = 0;
206         nframes = 0;
207         return 0;
208 }
209
210 static void gamescr_stop(void)
211 {
212         iwram_brk(prev_iwram_top);
213
214         wait_vblank();
215         /* clear sprites */
216         spr_clear();
217         /* reset background rot/scale state */
218         REG_BG2X = 0;
219         REG_BG2Y = 0;
220         REG_BG2PA = 0x100;
221         REG_BG2PB = 0;
222         REG_BG2PC = 0;
223         REG_BG2PD = 0x100;
224
225 }
226
227 static void gamescr_frame(void)
228 {
229         backbuf = ++nframes & 1;
230         framebuf = vram[backbuf];
231
232         vox_framebuf(240, 160, framebuf, horizon);
233
234         if(update() == -1) {
235                 return;
236         }
237         draw();
238
239         vblperf_end();
240         wait_vblank();
241         present(backbuf);
242
243         if(!(nframes & 15)) {
244                 emuprint("vbl: %d", vblperf_count);
245         }
246 #ifdef VBLBAR
247         vblperf_begin();
248 #else
249         vblperf_count = 0;
250 #endif
251 }
252
253 #define NS(x)   (SPRID_UINUM + ((x) << 1))
254 static int numspr[][2] = {
255         {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
256         {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
257         {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
258         {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
259         {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
260         {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
261         {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
262         {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
263 };
264
265 #define WALK_SPEED      0x40000
266 #define TURN_SPEED      0x200
267 #define ELEV_SPEED      8
268
269 static int update(void)
270 {
271         int32_t fwd[2], right[2];
272         int i, snum, ledspr;
273         struct enemy *enemy;
274
275         update_keyb();
276
277         if(KEYPRESS(BN_START)) {
278                 /* TODO pause menu */
279                 change_screen(find_screen("menu"));
280                 return -1;
281         }
282
283         if(keystate) {
284                 if(keystate & BN_LEFT) {
285                         angle += TURN_SPEED;
286                 }
287                 if(keystate & BN_RIGHT) {
288                         angle -= TURN_SPEED;
289                 }
290
291                 fwd[0] = -SIN(angle);
292                 fwd[1] = COS(angle);
293                 right[0] = fwd[1];
294                 right[1] = -fwd[0];
295
296                 if(keystate & BN_A) {
297                         pos[0] += fwd[0];
298                         pos[1] += fwd[1];
299                 }
300
301                 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
302                         last_shot = timer_msec;
303                         for(i=0; i<total_enemies; i++) {
304                                 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
305                                         int dx = enemies[i].vobj.px - 120;
306                                         int dy = enemies[i].vobj.py - 80;
307                                         int rad = enemies[i].vobj.scale >> 5;
308
309                                         /*emuprint("rad: %d (%d,%d)", rad, enemies[i].vobj.px, enemies[i].vobj.py);*/
310                                         if(rad < 1) rad = 1;
311
312                                         if(abs(dx) < rad && abs(dy) < (rad << 1)) {
313                                                 if(--enemies[i].hp <= 0) {
314                                                         if(++num_kills >= total_enemies) {
315                                                                 victory();
316                                                         }
317                                                 }
318                                                 hit_px = enemies[i].vobj.px;
319                                                 hit_py = enemies[i].vobj.py;
320                                                 hitfrm = nframes;
321                                                 break;
322                                         }
323                                 }
324                         }
325                 }
326                 if(keystate & BN_UP) {
327                         if(horizon > 40) horizon -= ELEV_SPEED;
328                 }
329                 if(keystate & BN_DOWN) {
330                         if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
331                 }
332                 if(keystate & BN_RT) {
333                         pos[0] += right[0];
334                         pos[1] += right[1];
335                 }
336                 if(keystate & BN_LT) {
337                         pos[0] -= right[0];
338                         pos[1] -= right[1];
339                 }
340
341                 vox_view(pos[0], pos[1], -40, angle);
342         }
343
344         snum = 0;
345         /* turrets number */
346         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
347         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
348         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
349         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
350         /* energy bar */
351         if(energy == MAX_ENERGY) {
352                 ledspr = SPRID_LEDBLU;
353         } else {
354                 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
355         }
356         for(i=0; i<5; i++) {
357                 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
358                                 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
359         }
360         /* blaster sprites */
361         if(timer_msec - last_shot <= SHOT_TIME) {
362                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
363                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
364                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
365                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
366
367                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
368                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
369                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
370                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
371         }
372         /* hit sparks */
373         if(nframes - hitfrm < 5) {
374                 int id = SPRID_SPARK0 + (nframes - hitfrm);
375                 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
376                                 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
377         }
378         /* enemy sprites */
379         /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
380         enemy = enemies;
381         for(i=0; i<total_enemies; i++) {
382                 int sid, anm, px, py, yoffs;
383                 unsigned int flags;
384                 int16_t mat[4];
385                 int32_t sa, ca, scale;
386
387                 if(enemy->vobj.px >= 0) {
388                         flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
389                         if(enemy->hp > 0) {
390                                 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
391                                 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
392                                 flags |= SPR_SZ32 | SPR_VRECT;
393                                 yoffs = 32;
394                         } else {
395                                 anm = 0;
396                                 sid = SPRID_HUSK;
397                                 flags |= SPR_SZ16;
398                                 yoffs = 16;
399                         }
400
401                         px = enemy->vobj.px - 120;
402                         py = enemy->vobj.py - 80;
403                         xform_pixel(&px, &py);
404
405                         spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
406
407                         scale = enemy->vobj.scale;
408                         if(scale > 0x10000) scale = 0x10000;
409                         sa = xform_sa / scale;
410                         ca = xform_ca / scale;
411                         mat[0] = anm >= 8 ? -ca : ca;
412                         mat[1] = sa;
413                         mat[2] = -sa;
414                         mat[3] = ca;
415
416                         spr_transform(oam, 0, mat);
417                         enemy->vobj.px = -1;
418                 }
419                 enemy++;
420         }
421         for(i=snum; i<dynspr_count; i++) {
422                 spr_oam_clear(oam, dynspr_base + i);
423         }
424
425         mask(INTR_VBLANK);
426         dynspr_count = snum;
427         unmask(INTR_VBLANK);
428
429         return 0;
430 }
431
432 static void draw(void)
433 {
434         //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
435         fillblock_16byte(framebuf, 0, 240 * 160 / 16);
436
437         vox_render();
438         //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
439         //vox_sky_solid(COLOR_ZENITH);
440
441         if(score >= 0) {
442                 glyphcolor = 200;
443                 glyphfb = framebuf;
444                 dbg_drawstr(80, 10, "Victory!");
445                 dbg_drawstr(60, 20, "Score: %d", score);
446                 dbg_drawstr(59, 30, "Completed in: %lus", total_time);
447         }
448 }
449
450 static void victory(void)
451 {
452         total_time = timer_msec - start_time;
453         score = 42;
454
455         /* TODO enter name */
456         save_score("???", score, total_time, 0);
457         save_scores();
458 }
459
460 static inline void xform_pixel(int *xp, int *yp)
461 {
462         int32_t sa = xform_sa >> 8;
463         int32_t ca = xform_ca >> 8;
464         int x = *xp;
465         int y = *yp;
466
467         *xp = (ca * x - sa * y + (120 << 8)) >> 8;
468         *yp = (sa * x + ca * y + (80 << 8)) >> 8;
469 }
470
471 #define MAXBANK         0x100
472
473 ARM_IWRAM
474 static void gamescr_vblank(void)
475 {
476         static int bank, bankdir, theta;
477         int32_t sa, ca;
478
479         vblcount++;
480
481         /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
482          * DMA them from cartridge easily
483          */
484
485         /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
486         dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
487
488         theta = -(bank << 3);
489         xform_sa = SIN(theta);
490         xform_ca = COS(theta);
491 #if 0
492         xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
493         sa = (((xform_sa) >> 8) * xform_s) >> 12;
494         ca = (((xform_ca) >> 8) * xform_s) >> 12;
495 #else
496         xform_s = (MAXBANK + (abs(bank) >> 3));
497         sa = xform_sa / xform_s;
498         ca = xform_ca / xform_s;
499 #endif
500
501         REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
502         REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
503
504         REG_BG2PA = ca;
505         REG_BG2PB = sa;
506         REG_BG2PC = -sa;
507         REG_BG2PD = ca;
508
509         if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
510                 if(bank) {
511                         bank -= bankdir << 4;
512                 }
513         } else if(keystate & BN_LEFT) {
514                 bankdir = -1;
515                 if(bank > -MAXBANK) bank -= 16;
516         } else if(keystate & BN_RIGHT) {
517                 bankdir = 1;
518                 if(bank < MAXBANK) bank += 16;
519         }
520 }