24 static int gamescr_start(void);
25 static void gamescr_stop(void);
26 static void gamescr_frame(void);
27 static void gamescr_vblank(void);
29 static int update(void);
30 static void draw(void);
31 static void victory(void);
33 static struct screen gamescr = {
42 struct vox_object vobj;
47 #define ENEMY_VALID(e) ((e)->anm != 0)
49 static uint16_t *framebuf;
51 static int nframes, backbuf;
52 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
54 static int32_t pos[2], angle, horizon = 80;
55 static long last_shot, hitfrm;
56 static int hit_px, hit_py;
58 #define COLOR_HORIZON 192
59 #define COLOR_ZENITH 255
62 static uint16_t oam[4 * MAX_SPR];
63 static int dynspr_base, dynspr_count;
66 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
67 static struct enemy enemies[MAX_ENEMIES];
68 static int num_kills, total_enemies;
72 #define ENEMY_ENERGY 4
75 static unsigned long total_time, start_time;
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 */
82 static short vblcount;
83 static void *prev_iwram_top;
86 static inline void xform_pixel(int *xp, int *yp);
89 struct screen *init_game_screen(void)
94 static void setup_palette(void)
97 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
99 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
101 for(i=0; i<256; i++) {
103 int g = cmap[i * 3 + 1];
104 int b = cmap[i * 3 + 2];
105 gba_bgpal[i] = RGB555(r, g, b);
109 static int gamescr_start(void)
115 prev_iwram_top = iwram_sbrk(0);
117 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
118 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
122 pos[0] = pos[1] = VOX_SZ << 15;
124 last_shot = -P_RATE - 1;
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);
130 /* setup color image palette */
133 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
137 for(i=0; i<MAX_SPR; i++) {
138 spr_oam_clear(oam, i);
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);
150 num_kills = total_enemies = 0;
153 memset(enemies, 0, sizeof enemies);
155 for(i=0; i<VOX_SZ; i++) {
156 for(j=0; j<VOX_SZ; j++) {
158 /* player spawn point */
162 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
163 /* enemy spawn point */
164 int idx = *cptr - CMAP_SPAWN0;
165 enemy = enemies + idx;
167 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
168 j, i, enemy->vobj.x, enemy->vobj.y);
174 enemy->hp = ENEMY_ENERGY;
175 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
176 if(++total_enemies >= MAX_ENEMIES) {
184 /* check continuity */
185 for(i=0; i<total_enemies; i++) {
186 if(enemies[i].anm <= 0) {
187 panic(get_pc(), "discontinuous enemy list");
189 enemies[i].anm = rand() & 7;
192 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
201 start_time = timer_msec;
208 static void gamescr_stop(void)
210 iwram_brk(prev_iwram_top);
215 /* reset background rot/scale state */
225 static void gamescr_frame(void)
227 backbuf = ++nframes & 1;
228 framebuf = vram[backbuf];
230 vox_framebuf(240, 160, framebuf, horizon);
241 if(!(nframes & 15)) {
242 emuprint("vbl: %d", vblperf_count);
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)}
263 #define WALK_SPEED 0x40000
264 #define TURN_SPEED 0x200
267 static int update(void)
269 int32_t fwd[2], right[2];
275 if(KEYPRESS(BN_START)) {
276 /* TODO pause menu */
277 change_screen(find_screen("menu"));
282 if(keystate & BN_LEFT) {
285 if(keystate & BN_RIGHT) {
289 fwd[0] = -SIN(angle);
294 if(keystate & BN_A) {
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;
307 /*emuprint("rad: %d (%d,%d)", rad, enemies[i].vobj.px, enemies[i].vobj.py);*/
310 if(abs(dx) < rad && abs(dy) < (rad << 1)) {
311 if(--enemies[i].hp <= 0) {
312 if(++num_kills >= total_enemies) {
316 hit_px = enemies[i].vobj.px;
317 hit_py = enemies[i].vobj.py;
324 if(keystate & BN_UP) {
325 if(horizon > 40) horizon -= ELEV_SPEED;
327 if(keystate & BN_DOWN) {
328 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
330 if(keystate & BN_RT) {
334 if(keystate & BN_LT) {
339 vox_view(pos[0], pos[1], -40, angle);
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);
349 if(energy == MAX_ENERGY) {
350 ledspr = SPRID_LEDBLU;
352 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
355 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
356 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
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);
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);
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));
377 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
379 for(i=0; i<total_enemies; i++) {
380 int sid, anm, px, py, yoffs;
383 int32_t sa, ca, scale;
385 if(enemy->vobj.px >= 0) {
386 flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
388 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
389 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
390 flags |= SPR_SZ32 | SPR_VRECT;
399 px = enemy->vobj.px - 120;
400 py = enemy->vobj.py - 80;
401 xform_pixel(&px, &py);
403 spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
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;
414 spr_transform(oam, 0, mat);
419 for(i=snum; i<dynspr_count; i++) {
420 spr_oam_clear(oam, dynspr_base + i);
430 static void draw(void)
432 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
433 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
436 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
437 //vox_sky_solid(COLOR_ZENITH);
440 static void victory(void)
442 total_time = timer_msec - start_time;
446 static inline void xform_pixel(int *xp, int *yp)
448 int32_t sa = xform_sa >> 8;
449 int32_t ca = xform_ca >> 8;
453 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
454 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
457 #define MAXBANK 0x100
460 static void gamescr_vblank(void)
462 static int bank, bankdir, theta;
467 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
468 * DMA them from cartridge easily
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);
474 theta = -(bank << 3);
475 xform_sa = SIN(theta);
476 xform_ca = COS(theta);
478 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
479 sa = (((xform_sa) >> 8) * xform_s) >> 12;
480 ca = (((xform_ca) >> 8) * xform_s) >> 12;
482 xform_s = (MAXBANK + (abs(bank) >> 3));
483 sa = xform_sa / xform_s;
484 ca = xform_ca / xform_s;
487 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
488 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
495 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
497 bank -= bankdir << 4;
499 } else if(keystate & BN_LEFT) {
501 if(bank > -MAXBANK) bank -= 16;
502 } else if(keystate & BN_RIGHT) {
504 if(bank < MAXBANK) bank += 16;