17 #define POS_MASK ((VOX_SZ << 16) - 1)
27 #define ENEMY_VIS_RANGE (2 * FAR / 3)
29 static int gamescr_start(void);
30 static void gamescr_stop(void);
31 static void gamescr_frame(void);
32 static void gamescr_vblank(void);
34 static int update(void);
35 static void draw(void);
36 static void victory(void);
38 static struct screen gamescr = {
46 #define ENEMY_ENERGY 4
48 struct vox_object vobj;
51 signed char shot_frame;
54 #define ENEMY_VALID(e) ((e)->anm != 0)
55 #define NUM_SHOT_FRAMES 16
60 static uint16_t *framebuf;
62 static int nframes, backbuf;
63 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
65 static int32_t pos[2], angle, horizon = 80;
66 static long last_shot, hitfrm;
67 static int hit_px, hit_py;
70 #define COLOR_HORIZON 192
71 #define COLOR_ZENITH 255
74 static uint16_t oam[4 * MAX_SPR];
75 static int dynspr_base, dynspr_count;
78 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
79 static struct enemy enemies[MAX_ENEMIES];
80 static int num_kills, total_enemies;
84 static int score, gameover;
85 static unsigned long total_time, start_time;
88 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
89 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
90 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
93 static short vblcount;
94 static void *prev_iwram_top;
97 static uint16_t color0;
99 static inline void xform_pixel(int *xp, int *yp);
102 struct screen *init_game_screen(void)
107 static void setup_palette(void)
110 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
112 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
114 for(i=0; i<256; i++) {
116 int g = cmap[i * 3 + 1];
117 int b = cmap[i * 3 + 2];
118 gba_bgpal[i] = RGB555(r, g, b);
121 color0 = gba_bgpal[0];
124 static int gamescr_start(void)
130 prev_iwram_top = iwram_sbrk(0);
132 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
133 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
137 pos[0] = pos[1] = VOX_SZ << 15;
139 last_shot = -P_RATE - 1;
141 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
142 vox_proj(FOV, NEAR, FAR);
143 pheight = vox_view(pos[0], pos[1], -40, angle);
145 /* setup color image palette */
148 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
152 for(i=0; i<MAX_SPR; i++) {
153 spr_oam_clear(oam, i);
157 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
158 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
159 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
160 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
161 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
162 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
165 num_kills = total_enemies = 0;
168 memset(enemies, 0, sizeof enemies);
170 for(i=0; i<VOX_SZ; i++) {
171 for(j=0; j<VOX_SZ; j++) {
173 /* player spawn point */
177 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
178 /* enemy spawn point */
179 int idx = *cptr - CMAP_SPAWN0;
180 enemy = enemies + idx;
182 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
183 j, i, enemy->vobj.x, enemy->vobj.y);
189 enemy->hp = ENEMY_ENERGY;
190 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
191 enemy->shot_frame = -1;
192 if(++total_enemies >= MAX_ENEMIES) {
200 total_enemies = 1; /* XXX DBG */
201 /* check continuity */
202 for(i=0; i<total_enemies; i++) {
203 if(enemies[i].anm <= 0) {
204 panic(get_pc(), "discontinuous enemy list");
206 enemies[i].anm = rand() & 7;
209 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
219 start_time = timer_msec;
230 static void gamescr_stop(void)
234 iwram_brk(prev_iwram_top);
239 /* reset background rot/scale state */
249 static void gamescr_frame(void)
251 backbuf = ++nframes & 1;
252 framebuf = vram[backbuf];
254 vox_framebuf(240, 160, framebuf, horizon);
265 if(!(nframes & 15)) {
266 emuprint("vbl: %d", vblperf_count);
275 #define NS(x) (SPRID_UINUM + ((x) << 1))
276 static int numspr[][2] = {
277 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
278 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
279 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
280 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
281 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
282 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
283 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
284 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
287 #define WALK_SPEED 0x40000
288 #define TURN_SPEED 0x200
291 #define MAX(a, b) ((a > (b) ? (a) : (b)))
293 static int update(void)
295 int32_t fwd[2], right[2];
304 if(KEYPRESS(BN_START)) {
305 /* TODO pause menu */
306 change_screen(find_screen("menu"));
311 goto skip_game_logic;
315 if(keystate & BN_LEFT) {
318 if(keystate & BN_RIGHT) {
322 fwd[0] = -SIN(angle);
327 if(keystate & BN_A) {
328 pos[0] = (pos[0] + fwd[0]) & POS_MASK;
329 pos[1] = (pos[1] + fwd[1]) & POS_MASK;
330 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
331 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
334 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
335 last_shot = timer_msec;
336 for(i=0; i<total_enemies; i++) {
337 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
338 int dx = enemies[i].vobj.px - 120;
339 int dy = enemies[i].vobj.py - 80;
340 int rad = enemies[i].vobj.scale >> 5;
344 if(abs(dx) < rad && abs(dy) < (rad << 1)) {
345 enemies[i].shot_frame = -1;
346 if(--enemies[i].hp <= 0) {
347 if(++num_kills >= total_enemies) {
351 hit_px = enemies[i].vobj.px;
352 hit_py = enemies[i].vobj.py;
359 if(keystate & BN_UP) {
360 if(horizon > 40) horizon -= ELEV_SPEED;
362 if(keystate & BN_DOWN) {
363 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
365 if(keystate & BN_RT) {
366 pos[0] = (pos[0] + right[0]) & POS_MASK;
367 pos[1] = (pos[1] + right[1]) & POS_MASK;
368 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
369 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
372 if(keystate & BN_LT) {
373 pos[0] = (pos[0] - right[0]) & POS_MASK;
374 pos[1] = (pos[1] - right[1]) & POS_MASK;
375 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
376 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
380 pheight = vox_view(pos[0], pos[1], -40, angle);
385 for(i=0; i<total_enemies; i++) {
386 /* only consider enemies which are not dead */
392 if(enemy->shot_frame >= 0) {
393 /* in the process of charging a shot */
394 if(++enemy->shot_frame >= NUM_SHOT_FRAMES - 1) {
395 /* only get hit if we can see the enemy and we didn't strafe */
396 if(!did_strafe && enemy->vobj.px >= 0) {
402 enemy->shot_frame = -1;
404 } else if(enemy->vobj.px >= 0) {
405 /* check rate of fire and start a shot if necessary */
406 if(timer_msec - enemy->last_shot >= E_RATE) {
407 enemy->last_shot = timer_msec;
408 enemy->shot_frame = 0;
418 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
419 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
420 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
421 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
423 if(energy == MAX_ENERGY) {
424 ledspr = SPRID_LEDBLU;
426 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
429 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
430 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
432 /* blaster sprites */
433 if(timer_msec - last_shot <= SHOT_TIME) {
434 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
435 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
436 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
437 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
439 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
440 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
441 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
442 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
445 if(nframes - hitfrm < 5) {
446 int id = SPRID_SPARK0 + (nframes - hitfrm);
447 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
448 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
451 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
453 for(i=0; i<total_enemies; i++) {
454 int sid, anm, px, py, yoffs;
457 int32_t sa, ca, scale;
459 if(enemy->vobj.px >= 0) {
460 flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
462 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
463 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
464 flags |= SPR_SZ32 | SPR_VRECT;
473 px = enemy->vobj.px - 120;
474 py = enemy->vobj.py - 80;
475 xform_pixel(&px, &py);
478 if(enemy->shot_frame >= 0) {
479 if(enemy->shot_frame < SFRM_LVL1) {
480 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT0, px - 16, py - 16,
481 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
482 } else if(enemy->shot_frame < SFRM_LVL2) {
483 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT1, px - 16, py - 16,
484 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
486 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT2, px - 16, py - 16,
487 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
492 spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
494 scale = enemy->vobj.scale;
495 if(scale > 0x10000) scale = 0x10000;
496 sa = xform_sa / scale;
497 ca = xform_ca / scale;
498 mat[0] = anm >= 8 ? -ca : ca;
503 spr_transform(oam, 0, mat);
508 for(i=snum; i<dynspr_count; i++) {
509 spr_oam_clear(oam, dynspr_base + i);
519 static void draw(void)
521 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
522 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
525 gba_bgpal[0] = 0x7fff;
527 gba_bgpal[0] = color0;
530 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
531 //vox_sky_solid(COLOR_ZENITH);
533 if(score >= 0 || energy <= 0) {
534 int sec = total_time / 1000;
536 fillblock_16byte(framebuf + 8 * 240 / 2, 199 | (199 << 8) | (199 << 16) | (199 << 24), 40 * 240 / 16);
542 dbg_drawstr(80, 10, "Victory!");
544 dbg_drawstr(30, 20, " Score: %d", score);
545 dbg_drawstr(30, 28, "Completed in: %lum.%lus", sec / 60, sec % 60);
547 dbg_drawstr(80, 10, "Game Over!");
550 dbg_drawstr(85, 40, "Press start to exit");
554 static void victory(void)
556 total_time = timer_msec - start_time;
559 /* TODO enter name */
560 save_score("???", score, total_time, 0);
564 static inline void xform_pixel(int *xp, int *yp)
566 int32_t sa = xform_sa >> 8;
567 int32_t ca = xform_ca >> 8;
571 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
572 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
575 #define MAXBANK 0x100
578 static void gamescr_vblank(void)
580 static int bank, bankdir, theta;
587 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
588 * DMA them from cartridge easily
591 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
592 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
596 theta = -(bank << 3);
597 xform_sa = SIN(theta);
598 xform_ca = COS(theta);
600 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
601 sa = (((xform_sa) >> 8) * xform_s) >> 12;
602 ca = (((xform_ca) >> 8) * xform_s) >> 12;
604 xform_s = (MAXBANK + (abs(bank) >> 3));
605 sa = xform_sa / xform_s;
606 ca = xform_ca / xform_s;
609 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
610 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
617 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
619 bank -= bankdir << 4;
621 } else if(keystate & BN_LEFT) {
623 if(bank > -MAXBANK) bank -= 16;
624 } else if(keystate & BN_RIGHT) {
626 if(bank < MAXBANK) bank += 16;