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 = -1;
191 enemy->shot_frame = -1;
192 if(++total_enemies >= MAX_ENEMIES) {
200 /* check continuity */
201 for(i=0; i<total_enemies; i++) {
202 if(enemies[i].anm <= 0) {
203 panic(get_pc(), "discontinuous enemy list");
205 enemies[i].anm = rand() & 7;
208 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
218 start_time = timer_msec;
229 static void gamescr_stop(void)
233 iwram_brk(prev_iwram_top);
238 /* reset background rot/scale state */
248 static void gamescr_frame(void)
250 backbuf = ++nframes & 1;
251 framebuf = vram[backbuf];
253 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(enemy->last_shot == -1) {
407 enemy->last_shot = timer_msec;
408 } else if(timer_msec - enemy->last_shot >= E_RATE) {
409 enemy->last_shot = timer_msec;
410 enemy->shot_frame = 0;
420 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
421 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
422 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
423 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
425 if(energy == MAX_ENERGY) {
426 ledspr = SPRID_LEDBLU;
428 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
431 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
432 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
434 /* blaster sprites */
435 if(timer_msec - last_shot <= SHOT_TIME) {
436 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
437 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
438 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
439 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
441 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
442 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
443 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
444 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
447 if(nframes - hitfrm < 5) {
448 int id = SPRID_SPARK0 + (nframes - hitfrm);
449 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
450 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
453 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
455 for(i=0; i<total_enemies; i++) {
456 int sid, anm, px, py, yoffs;
459 int32_t sa, ca, scale;
461 if(enemy->vobj.px >= 0) {
462 flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
464 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
465 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
466 flags |= SPR_SZ32 | SPR_VRECT;
475 px = enemy->vobj.px - 120;
476 py = enemy->vobj.py - 80;
477 xform_pixel(&px, &py);
480 if(enemy->shot_frame >= 0) {
481 if(enemy->shot_frame < SFRM_LVL1) {
482 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT0, px - 16, py - 16,
483 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
484 } else if(enemy->shot_frame < SFRM_LVL2) {
485 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT1, px - 16, py - 16,
486 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
488 spr_oam(oam, dynspr_base + snum++, SPRID_SHOT2, px - 16, py - 16,
489 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
494 spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
496 scale = enemy->vobj.scale;
497 if(scale > 0x10000) scale = 0x10000;
498 sa = xform_sa / scale;
499 ca = xform_ca / scale;
500 mat[0] = anm >= 8 ? -ca : ca;
505 spr_transform(oam, 0, mat);
510 for(i=snum; i<dynspr_count; i++) {
511 spr_oam_clear(oam, dynspr_base + i);
521 static void draw(void)
523 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
524 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
527 gba_bgpal[0] = 0x7fff;
529 gba_bgpal[0] = color0;
532 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
533 //vox_sky_solid(COLOR_ZENITH);
535 if(score >= 0 || energy <= 0) {
536 int sec = total_time / 1000;
538 fillblock_16byte(framebuf + 8 * 240 / 2, 199 | (199 << 8) | (199 << 16) | (199 << 24), 40 * 240 / 16);
544 dbg_drawstr(80, 10, "Victory!");
546 dbg_drawstr(30, 20, " Score: %d", score);
547 dbg_drawstr(30, 28, "Completed in: %lum.%lus", sec / 60, sec % 60);
549 dbg_drawstr(80, 10, "Game Over!");
552 dbg_drawstr(85, 40, "Press start to exit");
556 static void victory(void)
558 total_time = timer_msec - start_time;
561 /* TODO enter name */
562 save_score("???", score, total_time, 0);
566 static inline void xform_pixel(int *xp, int *yp)
568 int32_t sa = xform_sa >> 8;
569 int32_t ca = xform_ca >> 8;
573 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
574 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
577 #define MAXBANK 0x100
580 static void gamescr_vblank(void)
582 static int bank, bankdir, theta;
589 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
590 * DMA them from cartridge easily
593 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
594 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
598 theta = -(bank << 3);
599 xform_sa = SIN(theta);
600 xform_ca = COS(theta);
602 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
603 sa = (((xform_sa) >> 8) * xform_s) >> 12;
604 ca = (((xform_ca) >> 8) * xform_s) >> 12;
606 xform_s = (MAXBANK + (abs(bank) >> 3));
607 sa = xform_sa / xform_s;
608 ca = xform_ca / xform_s;
611 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
612 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
619 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
621 bank -= bankdir << 4;
623 } else if(keystate & BN_LEFT) {
625 if(bank > -MAXBANK) bank -= 16;
626 } else if(keystate & BN_RIGHT) {
628 if(bank < MAXBANK) bank += 16;