25 static int gamescr_start(void);
26 static void gamescr_stop(void);
27 static void gamescr_frame(void);
28 static void gamescr_vblank(void);
30 static int update(void);
31 static void draw(void);
32 static void victory(void);
34 static struct screen gamescr = {
43 struct vox_object vobj;
48 #define ENEMY_VALID(e) ((e)->anm != 0)
50 static uint16_t *framebuf;
52 static int nframes, backbuf;
53 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
55 static int32_t pos[2], angle, horizon = 80;
56 static long last_shot, hitfrm;
57 static int hit_px, hit_py;
59 #define COLOR_HORIZON 192
60 #define COLOR_ZENITH 255
63 static uint16_t oam[4 * MAX_SPR];
64 static int dynspr_base, dynspr_count;
67 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
68 static struct enemy enemies[MAX_ENEMIES];
69 static int num_kills, total_enemies;
73 #define ENEMY_ENERGY 4
76 static unsigned long total_time, start_time;
78 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
79 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
80 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
83 static short vblcount;
84 static void *prev_iwram_top;
87 static inline void xform_pixel(int *xp, int *yp);
90 struct screen *init_game_screen(void)
95 static void setup_palette(void)
98 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
100 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
102 for(i=0; i<256; i++) {
104 int g = cmap[i * 3 + 1];
105 int b = cmap[i * 3 + 2];
106 gba_bgpal[i] = RGB555(r, g, b);
110 static int gamescr_start(void)
116 prev_iwram_top = iwram_sbrk(0);
118 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
119 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
123 pos[0] = pos[1] = VOX_SZ << 15;
125 last_shot = -P_RATE - 1;
127 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
128 vox_proj(FOV, NEAR, FAR);
129 vox_view(pos[0], pos[1], -40, angle);
131 /* setup color image palette */
134 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
138 for(i=0; i<MAX_SPR; i++) {
139 spr_oam_clear(oam, i);
143 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
144 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
145 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
146 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
147 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
148 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
151 num_kills = total_enemies = 0;
154 memset(enemies, 0, sizeof enemies);
156 for(i=0; i<VOX_SZ; i++) {
157 for(j=0; j<VOX_SZ; j++) {
159 /* player spawn point */
163 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
164 /* enemy spawn point */
165 int idx = *cptr - CMAP_SPAWN0;
166 enemy = enemies + idx;
168 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
169 j, i, enemy->vobj.x, enemy->vobj.y);
175 enemy->hp = ENEMY_ENERGY;
176 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
177 if(++total_enemies >= MAX_ENEMIES) {
185 /* check continuity */
186 for(i=0; i<total_enemies; i++) {
187 if(enemies[i].anm <= 0) {
188 panic(get_pc(), "discontinuous enemy list");
190 enemies[i].anm = rand() & 7;
193 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
202 start_time = timer_msec;
209 static void gamescr_stop(void)
211 iwram_brk(prev_iwram_top);
216 /* reset background rot/scale state */
226 static void gamescr_frame(void)
228 backbuf = ++nframes & 1;
229 framebuf = vram[backbuf];
231 vox_framebuf(240, 160, framebuf, horizon);
242 if(!(nframes & 15)) {
243 emuprint("vbl: %d", vblperf_count);
252 #define NS(x) (SPRID_UINUM + ((x) << 1))
253 static int numspr[][2] = {
254 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
255 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
256 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
257 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
258 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
259 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
260 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
261 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
264 #define WALK_SPEED 0x40000
265 #define TURN_SPEED 0x200
268 static int update(void)
270 int32_t fwd[2], right[2];
276 if(KEYPRESS(BN_START)) {
277 /* TODO pause menu */
278 change_screen(find_screen("menu"));
283 if(keystate & BN_LEFT) {
286 if(keystate & BN_RIGHT) {
290 fwd[0] = -SIN(angle);
295 if(keystate & BN_A) {
300 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
301 last_shot = timer_msec;
302 for(i=0; i<total_enemies; i++) {
303 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
304 int dx = enemies[i].vobj.px - 120;
305 int dy = enemies[i].vobj.py - 80;
306 int rad = enemies[i].vobj.scale >> 5;
308 /*emuprint("rad: %d (%d,%d)", rad, enemies[i].vobj.px, enemies[i].vobj.py);*/
311 if(abs(dx) < rad && abs(dy) < (rad << 1)) {
312 if(--enemies[i].hp <= 0) {
313 if(++num_kills >= total_enemies) {
317 hit_px = enemies[i].vobj.px;
318 hit_py = enemies[i].vobj.py;
325 if(keystate & BN_UP) {
326 if(horizon > 40) horizon -= ELEV_SPEED;
328 if(keystate & BN_DOWN) {
329 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
331 if(keystate & BN_RT) {
335 if(keystate & BN_LT) {
340 vox_view(pos[0], pos[1], -40, angle);
345 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
346 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
347 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
348 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
350 if(energy == MAX_ENERGY) {
351 ledspr = SPRID_LEDBLU;
353 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
356 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
357 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
359 /* blaster sprites */
360 if(timer_msec - last_shot <= SHOT_TIME) {
361 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
362 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
363 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
364 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
366 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
367 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
368 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
369 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
372 if(nframes - hitfrm < 5) {
373 int id = SPRID_SPARK0 + (nframes - hitfrm);
374 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
375 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
378 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
380 for(i=0; i<total_enemies; i++) {
381 int sid, anm, px, py, yoffs;
384 int32_t sa, ca, scale;
386 if(enemy->vobj.px >= 0) {
387 flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
389 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
390 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
391 flags |= SPR_SZ32 | SPR_VRECT;
400 px = enemy->vobj.px - 120;
401 py = enemy->vobj.py - 80;
402 xform_pixel(&px, &py);
404 spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
406 scale = enemy->vobj.scale;
407 if(scale > 0x10000) scale = 0x10000;
408 sa = xform_sa / scale;
409 ca = xform_ca / scale;
410 mat[0] = anm >= 8 ? -ca : ca;
415 spr_transform(oam, 0, mat);
420 for(i=snum; i<dynspr_count; i++) {
421 spr_oam_clear(oam, dynspr_base + i);
431 static void draw(void)
433 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
434 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
437 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
438 //vox_sky_solid(COLOR_ZENITH);
441 static void victory(void)
443 total_time = timer_msec - start_time;
446 /* TODO enter name */
447 save_score("???", score, total_time, 0);
451 static inline void xform_pixel(int *xp, int *yp)
453 int32_t sa = xform_sa >> 8;
454 int32_t ca = xform_ca >> 8;
458 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
459 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
462 #define MAXBANK 0x100
465 static void gamescr_vblank(void)
467 static int bank, bankdir, theta;
472 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
473 * DMA them from cartridge easily
476 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
477 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
479 theta = -(bank << 3);
480 xform_sa = SIN(theta);
481 xform_ca = COS(theta);
483 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
484 sa = (((xform_sa) >> 8) * xform_s) >> 12;
485 ca = (((xform_ca) >> 8) * xform_s) >> 12;
487 xform_s = (MAXBANK + (abs(bank) >> 3));
488 sa = xform_sa / xform_s;
489 ca = xform_ca / xform_s;
492 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
493 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
500 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
502 bank -= bankdir << 4;
504 } else if(keystate & BN_LEFT) {
506 if(bank > -MAXBANK) bank -= 16;
507 } else if(keystate & BN_RIGHT) {
509 if(bank < MAXBANK) bank += 16;