19 static int gamescr_start(void);
20 static void gamescr_stop(void);
21 static void gamescr_frame(void);
22 static void gamescr_vblank(void);
24 static int update(void);
25 static void draw(void);
27 static struct screen gamescr = {
36 struct vox_object vobj;
41 #define ENEMY_VALID(e) ((e)->anm != 0)
43 static uint16_t *framebuf;
45 static int nframes, backbuf;
46 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
48 static int32_t pos[2], angle, horizon = 80;
50 #define COLOR_HORIZON 192
51 #define COLOR_ZENITH 255
54 static uint16_t oam[4 * MAX_SPR];
55 static int dynspr_base, dynspr_count;
58 #define MAX_ENEMIES (255 - CMAP_SPAWN0)
59 struct enemy enemies[MAX_ENEMIES];
60 int num_enemies, total_enemies;
65 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
66 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
67 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
70 static short vblcount;
71 static void *prev_iwram_top;
74 static inline void xform_pixel(int *xp, int *yp);
77 struct screen *init_game_screen(void)
82 static int gamescr_start(void)
88 prev_iwram_top = iwram_sbrk(0);
90 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
91 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
95 pos[0] = pos[1] = VOX_SZ << 15;
97 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
98 vox_proj(FOV, NEAR, FAR);
99 vox_view(pos[0], pos[1], -40, angle);
101 /* setup color image palette */
102 for(i=0; i<256; i++) {
103 int r = color_cmap[i * 3];
104 int g = color_cmap[i * 3 + 1];
105 int b = color_cmap[i * 3 + 2];
106 gba_bgpal[i] = (((uint16_t)b << 7) & 0x7c00) | (((uint16_t)g << 2) & 0x3e0) | (((uint16_t)r >> 3) & 0x1f);
109 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
113 for(i=0; i<MAX_SPR; i++) {
114 spr_oam_clear(oam, i);
118 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
119 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
120 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
121 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
122 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
123 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
126 num_enemies = total_enemies = 0;
129 memset(enemies, 0, sizeof enemies);
131 for(i=0; i<VOX_SZ; i++) {
132 for(j=0; j<VOX_SZ; j++) {
134 /* player spawn point */
138 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
139 /* enemy spawn point */
140 int idx = *cptr - CMAP_SPAWN0;
141 enemy = enemies + idx;
143 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
144 j, i, enemy->vobj.x, enemy->vobj.y);
151 enemy->last_fire = 0;
152 if(++total_enemies >= MAX_ENEMIES) {
160 /* check continuity */
161 for(i=0; i<total_enemies; i++) {
162 if(enemies[i].anm <= 0) {
163 panic(get_pc(), "discontinuous enemy list");
165 enemies[i].anm = rand() & 7;
168 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
180 static void gamescr_stop(void)
182 iwram_brk(prev_iwram_top);
187 /* reset background rot/scale state */
197 static void gamescr_frame(void)
199 backbuf = ++nframes & 1;
200 framebuf = vram[backbuf];
202 vox_framebuf(240, 160, framebuf, horizon);
213 if(!(nframes & 15)) {
214 emuprint("vbl: %d", vblperf_count);
223 #define NS(x) (SPRID_UINUM + ((x) << 1))
224 static int numspr[][2] = {
225 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
226 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
227 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
228 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
229 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
230 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
231 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
232 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
235 #define WALK_SPEED 0x40000
236 #define TURN_SPEED 0x200
239 static int update(void)
241 int32_t fwd[2], right[2];
247 if(KEYPRESS(BN_START)) {
248 /* TODO pause menu */
249 change_screen(find_screen("menu"));
254 if(keystate & BN_LEFT) {
257 if(keystate & BN_RIGHT) {
261 fwd[0] = -SIN(angle);
266 if(keystate & BN_A) {
270 if(keystate & BN_B) {
274 if(keystate & BN_UP) {
275 if(horizon > 40) horizon -= ELEV_SPEED;
277 if(keystate & BN_DOWN) {
278 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
280 if(keystate & BN_RT) {
284 if(keystate & BN_LT) {
289 vox_view(pos[0], pos[1], -40, angle);
294 spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][0], 200, 144, SPR_VRECT | SPR_256COL);
295 spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][1], 208, 144, SPR_VRECT | SPR_256COL);
296 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
297 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
299 if(energy == MAX_ENERGY) {
300 ledspr = SPRID_LEDBLU;
302 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
305 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
306 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
309 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
311 for(i=0; i<total_enemies; i++) {
312 int sid, anm, px, py;
315 int32_t sa, ca, scale;
317 if(enemy->vobj.px >= 0) {
318 flags = SPR_SZ32 | SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
319 if(enemies->hp > 0) {
320 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
321 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
328 px = enemy->vobj.px - 120;
329 py = enemy->vobj.py - 80;
330 xform_pixel(&px, &py);
332 spr_oam(oam, dynspr_base + snum++, sid, px - 20, py - 32, flags);
334 scale = enemy->vobj.scale;
335 if(scale > 0x10000) scale = 0x10000;
336 sa = xform_sa / scale;
337 ca = xform_ca / scale;
338 mat[0] = anm >= 8 ? -ca : ca;
343 spr_transform(oam, 0, mat);
348 for(i=snum; i<dynspr_count; i++) {
349 spr_oam_clear(oam, dynspr_base + i);
359 static void draw(void)
361 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
362 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
365 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
366 //vox_sky_solid(COLOR_ZENITH);
369 static inline void xform_pixel(int *xp, int *yp)
371 int32_t sa = xform_sa >> 8;
372 int32_t ca = xform_ca >> 8;
376 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
377 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
380 #define MAXBANK 0x100
383 static void gamescr_vblank(void)
385 static int bank, bankdir, theta;
390 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
391 * DMA them from cartridge easily
394 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
395 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
397 theta = -(bank << 3);
398 xform_sa = SIN(theta);
399 xform_ca = COS(theta);
401 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
402 sa = (((xform_sa) >> 8) * xform_s) >> 12;
403 ca = (((xform_ca) >> 8) * xform_s) >> 12;
405 xform_s = (MAXBANK + (abs(bank) >> 3));
406 sa = xform_sa / xform_s;
407 ca = xform_ca / xform_s;
410 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
411 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
418 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
420 bank -= bankdir << 4;
422 } else if(keystate & BN_LEFT) {
424 if(bank > -MAXBANK) bank -= 16;
425 } else if(keystate & BN_RIGHT) {
427 if(bank < MAXBANK) bank += 16;