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 for(i=0; i<256; i++) {
114 int g = cmap[i * 3 + 1];
115 int b = cmap[i * 3 + 2];
116 gba_bgpal[i] = RGB555(r, g, b);
119 color0 = gba_bgpal[0];
122 static int gamescr_start(void)
128 prev_iwram_top = iwram_sbrk(0);
130 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
131 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
135 pos[0] = pos[1] = VOX_SZ << 15;
137 last_shot = -P_RATE - 1;
139 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
140 vox_proj(FOV, NEAR, FAR);
141 pheight = vox_view(pos[0], pos[1], -40, angle);
143 /* setup color image palette */
146 spr_setup(16, 16, spr_game_pixels, gba_colors ? spr_game_gba_cmap : spr_game_cmap);
150 for(i=0; i<MAX_SPR; i++) {
151 spr_oam_clear(oam, i);
155 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
156 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
157 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
158 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
159 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
160 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
163 num_kills = total_enemies = 0;
166 memset(enemies, 0, sizeof enemies);
168 for(i=0; i<VOX_SZ; i++) {
169 for(j=0; j<VOX_SZ; j++) {
171 /* player spawn point */
175 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
176 /* enemy spawn point */
177 int idx = *cptr - CMAP_SPAWN0;
178 enemy = enemies + idx;
180 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
181 j, i, enemy->vobj.x, enemy->vobj.y);
187 enemy->hp = ENEMY_ENERGY;
188 enemy->last_shot = -1;
189 enemy->shot_frame = -1;
190 if(++total_enemies >= MAX_ENEMIES) {
198 /* check continuity */
199 for(i=0; i<total_enemies; i++) {
200 if(enemies[i].anm <= 0) {
201 panic(get_pc(), "discontinuous enemy list");
203 enemies[i].anm = rand() & 7;
206 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
216 start_time = timer_msec;
227 static void gamescr_stop(void)
231 iwram_brk(prev_iwram_top);
236 /* reset background rot/scale state */
246 static void gamescr_frame(void)
248 backbuf = ++nframes & 1;
249 framebuf = vram[backbuf];
251 vox_framebuf(240, 160, framebuf, horizon);
263 if(!(nframes & 15)) {
264 emuprint("vbl: %d", vblperf_count);
273 #define NS(x) (SPRID_UINUM + ((x) << 1))
274 static int numspr[][2] = {
275 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
276 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
277 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
278 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
279 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
280 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
281 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
282 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
285 #define WALK_SPEED 0x40000
286 #define TURN_SPEED 0x200
289 #define MAX(a, b) ((a > (b) ? (a) : (b)))
291 static int update(void)
293 int32_t fwd[2], right[2];
302 if(KEYPRESS(BN_START)) {
303 /* TODO pause menu */
304 change_screen(find_screen("menu"));
309 goto skip_game_logic;
313 if(keystate & BN_LEFT) {
316 if(keystate & BN_RIGHT) {
320 fwd[0] = -SIN(angle);
325 if(keystate & BN_A) {
326 pos[0] = (pos[0] + fwd[0]) & POS_MASK;
327 pos[1] = (pos[1] + fwd[1]) & POS_MASK;
328 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
329 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
332 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
333 last_shot = timer_msec;
334 for(i=0; i<total_enemies; i++) {
335 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
336 int dx = enemies[i].vobj.px - 120;
337 int dy = enemies[i].vobj.py - 80;
338 int rad = enemies[i].vobj.scale >> 5;
342 if(abs(dx) < rad && abs(dy) < (rad << 1)) {
343 enemies[i].shot_frame = -1;
344 if(--enemies[i].hp <= 0) {
345 if(++num_kills >= total_enemies) {
349 hit_px = enemies[i].vobj.px;
350 hit_py = enemies[i].vobj.py;
357 if(keystate & BN_UP) {
358 if(horizon > 40) horizon -= ELEV_SPEED;
360 if(keystate & BN_DOWN) {
361 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
363 if(keystate & BN_RT) {
364 pos[0] = (pos[0] + right[0]) & POS_MASK;
365 pos[1] = (pos[1] + right[1]) & POS_MASK;
366 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
367 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
370 if(keystate & BN_LT) {
371 pos[0] = (pos[0] - right[0]) & POS_MASK;
372 pos[1] = (pos[1] - right[1]) & POS_MASK;
373 if(pos[0] < 0) pos[0] += VOX_SZ << 16;
374 if(pos[1] < 0) pos[1] += VOX_SZ << 16;
378 pheight = vox_view(pos[0], pos[1], -40, angle);
383 for(i=0; i<total_enemies; i++) {
384 /* only consider enemies which are not dead */
390 if(enemy->shot_frame >= 0) {
391 /* in the process of charging a shot */
392 if(++enemy->shot_frame >= NUM_SHOT_FRAMES - 1) {
393 /* only get hit if we can see the enemy and we didn't strafe */
394 if(!did_strafe && enemy->vobj.px >= 0) {
400 enemy->shot_frame = -1;
402 } else if(enemy->vobj.px >= 0) {
403 /* check rate of fire and start a shot if necessary */
404 if(enemy->last_shot == -1) {
405 enemy->last_shot = timer_msec;
406 } else 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 int sec, time_bonus = 0;
558 total_time = timer_msec - start_time;
559 sec = total_time / 1000;
562 time_bonus = 1000 + (60 - sec) * 100;
563 } else if(sec < 120) {
564 time_bonus = 250 + (60 - (sec - 60)) * 10;
565 } else if(sec < 300) {
569 score = energy * 250 + time_bonus;
571 /* TODO enter name */
572 save_score("???", score, total_time, 0);
576 static inline void xform_pixel(int *xp, int *yp)
578 int32_t sa = xform_sa >> 8;
579 int32_t ca = xform_ca >> 8;
583 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
584 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
587 #define MAXBANK 0x100
590 static void gamescr_vblank(void)
592 static int bank, bankdir, theta;
599 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
600 * DMA them from cartridge easily
603 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
604 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
608 theta = -(bank << 3);
609 xform_sa = SIN(theta);
610 xform_ca = COS(theta);
612 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
613 sa = (((xform_sa) >> 8) * xform_s) >> 12;
614 ca = (((xform_ca) >> 8) * xform_s) >> 12;
616 xform_s = (MAXBANK + (abs(bank) >> 3));
617 sa = xform_sa / xform_s;
618 ca = xform_ca / xform_s;
621 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
622 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
629 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
631 bank -= bankdir << 4;
633 } else if(keystate & BN_LEFT) {
635 if(bank > -MAXBANK) bank -= 16;
636 } else if(keystate & BN_RIGHT) {
638 if(bank < MAXBANK) bank += 16;