23 static int gamescr_start(void);
24 static void gamescr_stop(void);
25 static void gamescr_frame(void);
26 static void gamescr_vblank(void);
28 static int update(void);
29 static void draw(void);
31 static struct screen gamescr = {
40 struct vox_object vobj;
45 #define ENEMY_VALID(e) ((e)->anm != 0)
47 static uint16_t *framebuf;
49 static int nframes, backbuf;
50 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
52 static int32_t pos[2], angle, horizon = 80;
53 static unsigned long last_shot;
55 #define COLOR_HORIZON 192
56 #define COLOR_ZENITH 255
59 static uint16_t oam[4 * MAX_SPR];
60 static int dynspr_base, dynspr_count;
63 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
64 struct enemy enemies[MAX_ENEMIES];
65 int num_kills, total_enemies;
70 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
71 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
72 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
75 static short vblcount;
76 static void *prev_iwram_top;
79 static inline void xform_pixel(int *xp, int *yp);
82 struct screen *init_game_screen(void)
87 static void setup_palette(void)
90 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
92 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
94 for(i=0; i<256; i++) {
96 int g = cmap[i * 3 + 1];
97 int b = cmap[i * 3 + 2];
98 gba_bgpal[i] = RGB555(r, g, b);
102 static int gamescr_start(void)
108 prev_iwram_top = iwram_sbrk(0);
110 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
111 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
115 pos[0] = pos[1] = VOX_SZ << 15;
117 last_shot = timer_msec > P_RATE ? timer_msec - P_RATE : 0;
119 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
120 vox_proj(FOV, NEAR, FAR);
121 vox_view(pos[0], pos[1], -40, angle);
123 /* setup color image palette */
126 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
130 for(i=0; i<MAX_SPR; i++) {
131 spr_oam_clear(oam, i);
135 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
136 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
137 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
138 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
139 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
140 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
143 num_kills = total_enemies = 0;
146 memset(enemies, 0, sizeof enemies);
148 for(i=0; i<VOX_SZ; i++) {
149 for(j=0; j<VOX_SZ; j++) {
151 /* player spawn point */
155 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
156 /* enemy spawn point */
157 int idx = *cptr - CMAP_SPAWN0;
158 enemy = enemies + idx;
160 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
161 j, i, enemy->vobj.x, enemy->vobj.y);
168 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
169 if(++total_enemies >= MAX_ENEMIES) {
177 /* check continuity */
178 for(i=0; i<total_enemies; i++) {
179 if(enemies[i].anm <= 0) {
180 panic(get_pc(), "discontinuous enemy list");
182 enemies[i].anm = rand() & 7;
185 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
197 static void gamescr_stop(void)
199 iwram_brk(prev_iwram_top);
204 /* reset background rot/scale state */
214 static void gamescr_frame(void)
216 backbuf = ++nframes & 1;
217 framebuf = vram[backbuf];
219 vox_framebuf(240, 160, framebuf, horizon);
230 if(!(nframes & 15)) {
231 emuprint("vbl: %d", vblperf_count);
240 #define NS(x) (SPRID_UINUM + ((x) << 1))
241 static int numspr[][2] = {
242 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
243 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
244 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
245 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
246 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
247 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
248 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
249 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
252 #define WALK_SPEED 0x40000
253 #define TURN_SPEED 0x200
256 static int update(void)
258 int32_t fwd[2], right[2];
264 if(KEYPRESS(BN_START)) {
265 /* TODO pause menu */
266 change_screen(find_screen("menu"));
271 if(keystate & BN_LEFT) {
274 if(keystate & BN_RIGHT) {
278 fwd[0] = -SIN(angle);
283 if(keystate & BN_A) {
287 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
289 last_shot = timer_msec;
290 for(i=0; i<total_enemies; i++) {
291 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
292 int dx = enemies[i].vobj.px - 120;
293 int dy = enemies[i].vobj.py - 80;
294 if(abs(dx) < 10 && abs(dy) < 10) {
301 if(keystate & BN_UP) {
302 if(horizon > 40) horizon -= ELEV_SPEED;
304 if(keystate & BN_DOWN) {
305 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
307 if(keystate & BN_RT) {
311 if(keystate & BN_LT) {
316 vox_view(pos[0], pos[1], -40, angle);
321 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
322 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
323 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
324 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
326 if(energy == MAX_ENERGY) {
327 ledspr = SPRID_LEDBLU;
329 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
332 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
333 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
336 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
338 for(i=0; i<total_enemies; i++) {
339 int sid, anm, px, py;
342 int32_t sa, ca, scale;
344 if(enemy->vobj.px >= 0) {
345 flags = SPR_SZ32 | SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
346 if(enemies->hp > 0) {
347 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
348 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
355 px = enemy->vobj.px - 120;
356 py = enemy->vobj.py - 80;
357 xform_pixel(&px, &py);
359 spr_oam(oam, dynspr_base + snum++, sid, px - 20, py - 32, flags);
361 scale = enemy->vobj.scale;
362 if(scale > 0x10000) scale = 0x10000;
363 sa = xform_sa / scale;
364 ca = xform_ca / scale;
365 mat[0] = anm >= 8 ? -ca : ca;
370 spr_transform(oam, 0, mat);
375 for(i=snum; i<dynspr_count; i++) {
376 spr_oam_clear(oam, dynspr_base + i);
386 static void draw(void)
388 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
389 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
392 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
393 //vox_sky_solid(COLOR_ZENITH);
396 static inline void xform_pixel(int *xp, int *yp)
398 int32_t sa = xform_sa >> 8;
399 int32_t ca = xform_ca >> 8;
403 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
404 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
407 #define MAXBANK 0x100
410 static void gamescr_vblank(void)
412 static int bank, bankdir, theta;
417 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
418 * DMA them from cartridge easily
421 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
422 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
424 theta = -(bank << 3);
425 xform_sa = SIN(theta);
426 xform_ca = COS(theta);
428 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
429 sa = (((xform_sa) >> 8) * xform_s) >> 12;
430 ca = (((xform_ca) >> 8) * xform_s) >> 12;
432 xform_s = (MAXBANK + (abs(bank) >> 3));
433 sa = xform_sa / xform_s;
434 ca = xform_ca / xform_s;
437 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
438 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
445 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
447 bank -= bankdir << 4;
449 } else if(keystate & BN_LEFT) {
451 if(bank > -MAXBANK) bank -= 16;
452 } else if(keystate & BN_RIGHT) {
454 if(bank < MAXBANK) bank += 16;