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);
32 static struct screen gamescr = {
41 struct vox_object vobj;
46 #define ENEMY_VALID(e) ((e)->anm != 0)
48 static uint16_t *framebuf;
50 static int nframes, backbuf;
51 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
53 static int32_t pos[2], angle, horizon = 80;
54 static long last_shot;
56 #define COLOR_HORIZON 192
57 #define COLOR_ZENITH 255
60 static uint16_t oam[4 * MAX_SPR];
61 static int dynspr_base, dynspr_count;
64 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
65 struct enemy enemies[MAX_ENEMIES];
66 int num_kills, total_enemies;
70 #define ENEMY_ENERGY 4
73 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
74 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
75 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
78 static short vblcount;
79 static void *prev_iwram_top;
82 static inline void xform_pixel(int *xp, int *yp);
85 struct screen *init_game_screen(void)
90 static void setup_palette(void)
93 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
95 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
97 for(i=0; i<256; i++) {
99 int g = cmap[i * 3 + 1];
100 int b = cmap[i * 3 + 2];
101 gba_bgpal[i] = RGB555(r, g, b);
105 static int gamescr_start(void)
111 prev_iwram_top = iwram_sbrk(0);
113 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
114 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
118 pos[0] = pos[1] = VOX_SZ << 15;
120 last_shot = -P_RATE - 1;
122 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
123 vox_proj(FOV, NEAR, FAR);
124 vox_view(pos[0], pos[1], -40, angle);
126 /* setup color image palette */
129 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
133 for(i=0; i<MAX_SPR; i++) {
134 spr_oam_clear(oam, i);
138 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
139 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
140 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
141 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
142 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
143 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
146 num_kills = total_enemies = 0;
149 memset(enemies, 0, sizeof enemies);
151 for(i=0; i<VOX_SZ; i++) {
152 for(j=0; j<VOX_SZ; j++) {
154 /* player spawn point */
158 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
159 /* enemy spawn point */
160 int idx = *cptr - CMAP_SPAWN0;
161 enemy = enemies + idx;
163 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
164 j, i, enemy->vobj.x, enemy->vobj.y);
170 enemy->hp = ENEMY_ENERGY;
171 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
172 if(++total_enemies >= MAX_ENEMIES) {
180 /* check continuity */
181 for(i=0; i<total_enemies; i++) {
182 if(enemies[i].anm <= 0) {
183 panic(get_pc(), "discontinuous enemy list");
185 enemies[i].anm = rand() & 7;
188 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
200 static void gamescr_stop(void)
202 iwram_brk(prev_iwram_top);
207 /* reset background rot/scale state */
217 static void gamescr_frame(void)
219 backbuf = ++nframes & 1;
220 framebuf = vram[backbuf];
222 vox_framebuf(240, 160, framebuf, horizon);
233 if(!(nframes & 15)) {
234 emuprint("vbl: %d", vblperf_count);
243 #define NS(x) (SPRID_UINUM + ((x) << 1))
244 static int numspr[][2] = {
245 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
246 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
247 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
248 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
249 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
250 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
251 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
252 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
255 #define WALK_SPEED 0x40000
256 #define TURN_SPEED 0x200
259 static int update(void)
261 int32_t fwd[2], right[2];
267 if(KEYPRESS(BN_START)) {
268 /* TODO pause menu */
269 change_screen(find_screen("menu"));
274 if(keystate & BN_LEFT) {
277 if(keystate & BN_RIGHT) {
281 fwd[0] = -SIN(angle);
286 if(keystate & BN_A) {
291 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
292 last_shot = timer_msec;
293 for(i=0; i<total_enemies; i++) {
294 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
295 int dx = enemies[i].vobj.px - 120;
296 int dy = enemies[i].vobj.py - 80;
297 int rad = enemies[i].vobj.scale >> 5;
299 /*emuprint("rad: %d (%d,%d)", rad, enemies[i].vobj.px, enemies[i].vobj.py);*/
302 if(abs(dx) < rad && abs(dy) < (rad << 1)) {
309 if(keystate & BN_UP) {
310 if(horizon > 40) horizon -= ELEV_SPEED;
312 if(keystate & BN_DOWN) {
313 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
315 if(keystate & BN_RT) {
319 if(keystate & BN_LT) {
324 vox_view(pos[0], pos[1], -40, angle);
329 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
330 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
331 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
332 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
334 if(energy == MAX_ENERGY) {
335 ledspr = SPRID_LEDBLU;
337 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
340 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
341 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
344 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
346 for(i=0; i<total_enemies; i++) {
347 int sid, anm, px, py, yoffs;
350 int32_t sa, ca, scale;
352 if(enemy->vobj.px >= 0) {
353 flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
354 if(enemies->hp > 0) {
355 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
356 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
357 flags |= SPR_SZ32 | SPR_VRECT;
366 px = enemy->vobj.px - 120;
367 py = enemy->vobj.py - 80;
368 xform_pixel(&px, &py);
370 spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
372 scale = enemy->vobj.scale;
373 if(scale > 0x10000) scale = 0x10000;
374 sa = xform_sa / scale;
375 ca = xform_ca / scale;
376 mat[0] = anm >= 8 ? -ca : ca;
381 spr_transform(oam, 0, mat);
386 if(timer_msec - last_shot <= SHOT_TIME) {
387 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
388 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
389 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
390 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
392 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
393 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
394 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
395 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
397 for(i=snum; i<dynspr_count; i++) {
398 spr_oam_clear(oam, dynspr_base + i);
408 static void draw(void)
410 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
411 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
414 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
415 //vox_sky_solid(COLOR_ZENITH);
418 static inline void xform_pixel(int *xp, int *yp)
420 int32_t sa = xform_sa >> 8;
421 int32_t ca = xform_ca >> 8;
425 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
426 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
429 #define MAXBANK 0x100
432 static void gamescr_vblank(void)
434 static int bank, bankdir, theta;
439 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
440 * DMA them from cartridge easily
443 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
444 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
446 theta = -(bank << 3);
447 xform_sa = SIN(theta);
448 xform_ca = COS(theta);
450 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
451 sa = (((xform_sa) >> 8) * xform_s) >> 12;
452 ca = (((xform_ca) >> 8) * xform_s) >> 12;
454 xform_s = (MAXBANK + (abs(bank) >> 3));
455 sa = xform_sa / xform_s;
456 ca = xform_ca / xform_s;
459 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
460 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
467 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
469 bank -= bankdir << 4;
471 } else if(keystate & BN_LEFT) {
473 if(bank > -MAXBANK) bank -= 16;
474 } else if(keystate & BN_RIGHT) {
476 if(bank < MAXBANK) bank += 16;