#include "input.h"
#include "gba.h"
#include "sprite.h"
+#include "timer.h"
#include "debug.h"
#include "voxscape.h"
#include "data.h"
#define NEAR 2
#define FAR 85
+#define P_RATE 500
+#define E_RATE 500
+
static int gamescr_start(void);
static void gamescr_stop(void);
static void gamescr_frame(void);
static void gamescr_vblank(void);
-static void update(void);
+static int update(void);
static void draw(void);
static struct screen gamescr = {
struct vox_object vobj;
short hp;
short anm;
- short last_fire;
+ int last_shot;
};
+#define ENEMY_VALID(e) ((e)->anm != 0)
static uint16_t *framebuf;
static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
static int32_t pos[2], angle, horizon = 80;
-static struct voxscape *vox;
+static unsigned long last_shot;
#define COLOR_HORIZON 192
#define COLOR_ZENITH 255
static int dynspr_base, dynspr_count;
-#define MAX_ENEMIES 64
+#define MAX_ENEMIES (255 - CMAP_SPAWN0)
struct enemy enemies[MAX_ENEMIES];
-int num_enemies, total_enemies;
+int num_kills, total_enemies;
static int energy;
#define MAX_ENERGY 5
static int xform_s;
static short vblcount;
+static void *prev_iwram_top;
static inline void xform_pixel(int *xp, int *yp);
return &gamescr;
}
+static void setup_palette(void)
+{
+ int i;
+ unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
+
+ emuprint("setting up %s palette", gba_colors ? "GBA" : "NDS/Emu");
+
+ for(i=0; i<256; i++) {
+ int r = cmap[i * 3];
+ int g = cmap[i * 3 + 1];
+ int b = cmap[i * 3 + 2];
+ gba_bgpal[i] = RGB555(r, g, b);
+ }
+}
+
static int gamescr_start(void)
{
- int i, sidx;
+ int i, j, sidx;
+ uint8_t *cptr;
+ struct enemy *enemy;
+
+ prev_iwram_top = iwram_sbrk(0);
gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
+ fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
vblperf_setcolor(0);
- pos[0] = VOX_SZ << 15;
- pos[1] = (VOX_SZ << 15) + 0x100000;
+ pos[0] = pos[1] = VOX_SZ << 15;
angle = 0x8000;
+ last_shot = timer_msec > P_RATE ? timer_msec - P_RATE : 0;
- if(!(vox = vox_create(VOX_SZ, VOX_SZ, height_pixels, color_pixels))) {
- panic(get_pc(), "vox_create");
- }
- vox_proj(vox, FOV, NEAR, FAR);
- vox_view(vox, pos[0], pos[1], -40, angle);
+ vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
+ vox_proj(FOV, NEAR, FAR);
+ vox_view(pos[0], pos[1], -40, angle);
/* setup color image palette */
- for(i=0; i<256; i++) {
- int r = color_cmap[i * 3];
- int g = color_cmap[i * 3 + 1];
- int b = color_cmap[i * 3 + 2];
- gba_bgpal[i] = (((uint16_t)b << 7) & 0x7c00) | (((uint16_t)g << 2) & 0x3e0) | (((uint16_t)r >> 3) & 0x1f);
- }
- /*
- intr_disable();
- interrupt(INTR_HBLANK, hblank);
- REG_DISPSTAT |= DISPSTAT_IEN_HBLANK;
- unmask(INTR_HBLANK);
- intr_enable();
- */
+ setup_palette();
spr_setup(16, 16, spr_game_pixels, spr_game_cmap);
wait_vblank();
spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
dynspr_base = sidx;
- wait_vblank();
- dma_copy32(3, (void*)OAM_ADDR, oam, sidx * 2, 0);
-
- num_enemies = 0;
- total_enemies = 8;
+ num_kills = total_enemies = 0;
energy = 5;
- srand(0);
+ memset(enemies, 0, sizeof enemies);
+ cptr = color_pixels;
+ for(i=0; i<VOX_SZ; i++) {
+ for(j=0; j<VOX_SZ; j++) {
+ if(*cptr == 255) {
+ /* player spawn point */
+ pos[0] = j << 16;
+ pos[1] = i << 16;
+
+ } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
+ /* enemy spawn point */
+ int idx = *cptr - CMAP_SPAWN0;
+ enemy = enemies + idx;
+ if(enemy->anm) {
+ panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
+ j, i, enemy->vobj.x, enemy->vobj.y);
+ }
+ enemy->vobj.x = j;
+ enemy->vobj.y = i;
+ enemy->vobj.px = -1;
+ enemy->anm = 0xff;
+ enemy->hp = 2;
+ enemy->last_shot = timer_msec > E_RATE ? timer_msec - E_RATE : 0;
+ if(++total_enemies >= MAX_ENEMIES) {
+ goto endspawn;
+ }
+ }
+ cptr++;
+ }
+ }
+endspawn:
+ /* check continuity */
for(i=0; i<total_enemies; i++) {
- enemies[i].vobj.x = rand() % VOX_SZ;
- enemies[i].vobj.y = rand() % VOX_SZ;
- enemies[i].vobj.px = -1;
+ if(enemies[i].anm <= 0) {
+ panic(get_pc(), "discontinuous enemy list");
+ }
enemies[i].anm = rand() & 7;
- enemies[i].hp = 2;
- enemies[i].last_fire = 0;
}
- vox_objects(vox, (struct vox_object*)enemies, total_enemies, sizeof *enemies);
+ vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
+
+ energy = MAX_ENERGY;
+ xform_sa = 0;
+ xform_ca = 0x10000;
+ xform_s = 0x100;
+
+ vblcount = 0;
nframes = 0;
return 0;
}
static void gamescr_stop(void)
{
+ iwram_brk(prev_iwram_top);
+
+ wait_vblank();
+ /* clear sprites */
+ spr_clear();
+ /* reset background rot/scale state */
+ REG_BG2X = 0;
+ REG_BG2Y = 0;
+ REG_BG2PA = 0x100;
+ REG_BG2PB = 0;
+ REG_BG2PC = 0;
+ REG_BG2PD = 0x100;
+
}
static void gamescr_frame(void)
backbuf = ++nframes & 1;
framebuf = vram[backbuf];
- vox_framebuf(vox, 240, 160, framebuf, horizon);
+ vox_framebuf(240, 160, framebuf, horizon);
- update();
+ if(update() == -1) {
+ return;
+ }
draw();
vblperf_end();
#define TURN_SPEED 0x200
#define ELEV_SPEED 8
-static void update(void)
+static int update(void)
{
int32_t fwd[2], right[2];
int i, snum, ledspr;
update_keyb();
- if(KEYPRESS(BN_SELECT)) {
- vox_quality ^= 1;
+ if(KEYPRESS(BN_START)) {
+ /* TODO pause menu */
+ change_screen(find_screen("menu"));
+ return -1;
}
if(keystate) {
pos[0] += fwd[0];
pos[1] += fwd[1];
}
- if(keystate & BN_B) {
- pos[0] -= fwd[0];
- pos[1] -= fwd[1];
+ if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
+ emuprint("pew");
+ last_shot = timer_msec;
+ for(i=0; i<total_enemies; i++) {
+ if(enemies[i].hp && enemies[i].vobj.px >= 0) {
+ int dx = enemies[i].vobj.px - 120;
+ int dy = enemies[i].vobj.py - 80;
+ if(abs(dx) < 10 && abs(dy) < 10) {
+ enemies[i].hp--;
+ break;
+ }
+ }
+ }
}
if(keystate & BN_UP) {
if(horizon > 40) horizon -= ELEV_SPEED;
pos[1] -= right[1];
}
- vox_view(vox, pos[0], pos[1], -40, angle);
+ vox_view(pos[0], pos[1], -40, angle);
}
snum = 0;
/* turrets number */
- spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][0], 200, 144, SPR_VRECT | SPR_256COL);
- spr_oam(oam, dynspr_base + snum++, numspr[num_enemies][1], 208, 144, SPR_VRECT | SPR_256COL);
+ spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
+ spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
/* energy bar */
sid = SPRID_ENEMY0 + ((anm & 7) << 2);
flags |= SPR_VRECT;
} else {
+ anm = 0;
sid = SPRID_HUSK;
}
mask(INTR_VBLANK);
dynspr_count = snum;
unmask(INTR_VBLANK);
+
+ return 0;
}
static void draw(void)
//dma_fill16(3, framebuf, 0, 240 * 160 / 2);
fillblock_16byte(framebuf, 0, 240 * 160 / 16);
- vox_render(vox);
- //vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
- //vox_sky_solid(vox, COLOR_ZENITH);
+ vox_render();
+ //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
+ //vox_sky_solid(COLOR_ZENITH);
}
static inline void xform_pixel(int *xp, int *yp)
vblcount++;
- if(!nframes) return;
-
/* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
* DMA them from cartridge easily
*/
REG_BG2PC = -sa;
REG_BG2PD = ca;
- keystate = ~REG_KEYINPUT;
-
if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
if(bank) {
bank -= bankdir << 4;