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