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 unsigned 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;
71 #define XFORM_PIXEL_X(x, y) (xform_ca * (x) - xform_sa * (y) + (120 << 8))
72 #define XFORM_PIXEL_Y(x, y) (xform_sa * (x) + xform_ca * (y) + (80 << 8))
73 static int32_t xform_sa, xform_ca; /* for viewport bank/zoom */
76 static short vblcount;
77 static void *prev_iwram_top;
80 static inline void xform_pixel(int *xp, int *yp);
83 struct screen *init_game_screen(void)
88 static void setup_palette(void)
91 unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
93 emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
95 for(i=0; i<256; i++) {
97 int g = cmap[i * 3 + 1];
98 int b = cmap[i * 3 + 2];
99 gba_bgpal[i] = RGB555(r, g, b);
103 static int gamescr_start(void)
109 prev_iwram_top = iwram_sbrk(0);
111 gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
112 fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
116 pos[0] = pos[1] = VOX_SZ << 15;
118 last_shot = timer_msec > P_RATE ? timer_msec - P_RATE : 0;
120 vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
121 vox_proj(FOV, NEAR, FAR);
122 vox_view(pos[0], pos[1], -40, angle);
124 /* setup color image palette */
127 spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
131 for(i=0; i<MAX_SPR; i++) {
132 spr_oam_clear(oam, i);
136 spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
137 spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
138 spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
139 spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
140 spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
141 spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
144 num_kills = total_enemies = 0;
147 memset(enemies, 0, sizeof enemies);
149 for(i=0; i<VOX_SZ; i++) {
150 for(j=0; j<VOX_SZ; j++) {
152 /* player spawn point */
156 } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
157 /* enemy spawn point */
158 int idx = *cptr - CMAP_SPAWN0;
159 enemy = enemies + idx;
161 panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
162 j, i, enemy->vobj.x, enemy->vobj.y);
169 enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
170 if(++total_enemies >= MAX_ENEMIES) {
178 /* check continuity */
179 for(i=0; i<total_enemies; i++) {
180 if(enemies[i].anm <= 0) {
181 panic(get_pc(), "discontinuous enemy list");
183 enemies[i].anm = rand() & 7;
186 vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
198 static void gamescr_stop(void)
200 iwram_brk(prev_iwram_top);
205 /* reset background rot/scale state */
215 static void gamescr_frame(void)
217 backbuf = ++nframes & 1;
218 framebuf = vram[backbuf];
220 vox_framebuf(240, 160, framebuf, horizon);
231 if(!(nframes & 15)) {
232 emuprint("vbl: %d", vblperf_count);
241 #define NS(x) (SPRID_UINUM + ((x) << 1))
242 static int numspr[][2] = {
243 {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
244 {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
245 {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
246 {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
247 {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
248 {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
249 {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
250 {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
253 #define WALK_SPEED 0x40000
254 #define TURN_SPEED 0x200
257 static int update(void)
259 int32_t fwd[2], right[2];
265 if(KEYPRESS(BN_START)) {
266 /* TODO pause menu */
267 change_screen(find_screen("menu"));
272 if(keystate & BN_LEFT) {
275 if(keystate & BN_RIGHT) {
279 fwd[0] = -SIN(angle);
284 if(keystate & BN_A) {
289 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
290 last_shot = timer_msec;
291 for(i=0; i<total_enemies; i++) {
292 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
293 int dx = enemies[i].vobj.px - 120;
294 int dy = enemies[i].vobj.py - 80;
295 if(abs(dx) < 10 && abs(dy) < 10) {
302 if(keystate & BN_UP) {
303 if(horizon > 40) horizon -= ELEV_SPEED;
305 if(keystate & BN_DOWN) {
306 if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
308 if(keystate & BN_RT) {
312 if(keystate & BN_LT) {
317 vox_view(pos[0], pos[1], -40, angle);
322 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
323 spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
324 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
325 spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
327 if(energy == MAX_ENERGY) {
328 ledspr = SPRID_LEDBLU;
330 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
333 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
334 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
337 /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
339 for(i=0; i<total_enemies; i++) {
340 int sid, anm, px, py;
343 int32_t sa, ca, scale;
345 if(enemy->vobj.px >= 0) {
346 flags = SPR_SZ32 | SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
347 if(enemies->hp > 0) {
348 anm = (enemies->anm + (vblcount >> 3)) & 0xf;
349 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
356 px = enemy->vobj.px - 120;
357 py = enemy->vobj.py - 80;
358 xform_pixel(&px, &py);
360 spr_oam(oam, dynspr_base + snum++, sid, px - 20, py - 32, flags);
362 scale = enemy->vobj.scale;
363 if(scale > 0x10000) scale = 0x10000;
364 sa = xform_sa / scale;
365 ca = xform_ca / scale;
366 mat[0] = anm >= 8 ? -ca : ca;
371 spr_transform(oam, 0, mat);
376 if(timer_msec - last_shot <= SHOT_TIME) {
377 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
378 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
379 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
380 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
382 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
383 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
384 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
385 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
387 for(i=snum; i<dynspr_count; i++) {
388 spr_oam_clear(oam, dynspr_base + i);
398 static void draw(void)
400 //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
401 fillblock_16byte(framebuf, 0, 240 * 160 / 16);
404 //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
405 //vox_sky_solid(COLOR_ZENITH);
408 static inline void xform_pixel(int *xp, int *yp)
410 int32_t sa = xform_sa >> 8;
411 int32_t ca = xform_ca >> 8;
415 *xp = (ca * x - sa * y + (120 << 8)) >> 8;
416 *yp = (sa * x + ca * y + (80 << 8)) >> 8;
419 #define MAXBANK 0x100
422 static void gamescr_vblank(void)
424 static int bank, bankdir, theta;
429 /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
430 * DMA them from cartridge easily
433 /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
434 dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
436 theta = -(bank << 3);
437 xform_sa = SIN(theta);
438 xform_ca = COS(theta);
440 xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
441 sa = (((xform_sa) >> 8) * xform_s) >> 12;
442 ca = (((xform_ca) >> 8) * xform_s) >> 12;
444 xform_s = (MAXBANK + (abs(bank) >> 3));
445 sa = xform_sa / xform_s;
446 ca = xform_ca / xform_s;
449 REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
450 REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
457 if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
459 bank -= bankdir << 4;
461 } else if(keystate & BN_LEFT) {
463 if(bank > -MAXBANK) bank -= 16;
464 } else if(keystate & BN_RIGHT) {
466 if(bank < MAXBANK) bank += 16;