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