looks right, extremely slow
[gbajam22] / src / gamescr.c
index 6449914..4f9f754 100644 (file)
 #include "intr.h"
 #include "input.h"
 #include "player.h"
+#include "gba.h"
 #include "sprite.h"
 #include "debug.h"
 #include "level.h"
-#include "xgl.h"
-#include "polyfill.h"
+#include "voxscape.h"
+#include "data.h"
+
+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 void draw(void);
-static void vblank(void);
 
-static int nframes, num_vbl, backbuf;
-static uint16_t *vram[] = { (uint16_t*)VRAM_LFB_FB0_ADDR, (uint16_t*)VRAM_LFB_FB1_ADDR };
-
-static const char *testlvl =
-       "########\n"
-       "###   s#\n"
-       "### ####\n"
-       "###    #\n"
-       "##     #\n"
-       "##     #\n"
-       "##     #\n"
-       "## ### #\n"
-       "## ### #\n"
-       "##     #\n"
-       "#### ###\n"
-       "########\n";
-
-static struct xvertex tm_floor[] __attribute__((section(".rodata"))) = {
-       {0x10000, -0x10000, 0x10000,    0, 0x10000, 0,  210},
-       {-0x10000, -0x10000, 0x10000,   0, 0x10000, 0,  210},
-       {-0x10000, -0x10000, -0x10000,  0, 0x10000, 0,  210},
-       {0x10000, -0x10000, -0x10000,   0, 0x10000, 0,  210}
+static struct screen gamescr = {
+       "game",
+       gamescr_start,
+       gamescr_stop,
+       gamescr_frame,
+       gamescr_vblank
 };
 
+static int nframes, num_vbl, backbuf;
+static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
+
+static int32_t pos[2], angle;
+static struct voxscape *vox;
 
-static struct level *lvl;
+#define COLOR_HORIZON  192
+#define COLOR_ZENITH   255
 
-static struct player player;
 
 
-void gamescr(void)
+struct screen *init_game_screen(void)
+{
+       return &gamescr;
+}
+
+static int gamescr_start(void)
 {
        int i;
-       unsigned char *fb;
-       uint16_t *cmap;
 
-       REG_DISPCNT = 4 | DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1;
+       gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
 
        vblperf_setcolor(1);
 
-       lvl = init_level(testlvl);
-
-       xgl_init();
+       pos[0] = pos[1] = 256 << 16;
 
-       memset(&player, 0, sizeof player);
-       player.phi = 0x100;
+       if(!(vox = vox_create(512, 512, height_pixels, color_pixels))) {
+               panic(get_pc(), "vox_create");
+       }
+       vox_proj(vox, 45, 1, 250);
+
+       /* setup color image palette */
+       for(i=0; i<192; 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);
+       }
 
-       cmap = (uint16_t*)CRAM_BG_ADDR;
-       *cmap++ = 0;
-       for(i=1; i<255; i++) {
-               *cmap++ = rand();
+       /* setup sky gradient palette */
+       for(i=0; i<64; i++) {
+               int t = (i << 8) / 64;
+               int r = (0xcc00 + (0x55 - 0xcc) * t) >> 8;
+               int g = (0x7700 + (0x88 - 0x77) * t) >> 8;
+               int b = (0xff00 + (0xcc - 0xff) * t) >> 8;
+               int cidx = COLOR_HORIZON + i;
+               gba_bgpal[cidx] = ((b << 7) & 0x7c00) | ((g << 2) & 0x3e0) | (r >> 3);
        }
-       *cmap = 0xffff;
 
+       /*select_input(BN_DPAD | BN_LT | BN_RT);*/
+
+       nframes = 0;
+       return 0;
+}
 
-       select_input(BN_DPAD | BN_A | BN_B);
+static void gamescr_stop(void)
+{
+}
 
-       mask(INTR_VBLANK);
-       screen_vblank = vblank;
-       unmask(INTR_VBLANK);
+static void gamescr_frame(void)
+{
+       unsigned char *fb;
 
-       nframes = 0;
-       for(;;) {
-               backbuf = ++nframes & 1;
-               fb = (unsigned char*)vram[backbuf];
+       backbuf = ++nframes & 1;
+       fb = (unsigned char*)vram[backbuf];
 
-               polyfill_framebuffer(fb, 240, 160);
-               fillblock_16byte(fb, 0, 240 * 160 / 16);
+       vox_framebuf(vox, 240, 160, fb, -1);
 
-               update();
-               draw();
+       update();
+       draw();
 
-               vblperf_end();
-               wait_vblank();
-               present(backbuf);
-               vblperf_begin();
-       }
+       //vblperf_end();
+       wait_vblank();
+       present(backbuf);
+       //vblperf_begin();
 }
 
+#define WALK_SPEED     0x40000
+#define TURN_SPEED     0x100
+
 static void update(void)
 {
-       uint16_t bnstate;
+       int32_t fwd[2], right[2];
+       uint16_t input;
 
-       bnstate = get_input();
+       input = read_input();
 
-       player_input(&player, bnstate);
+       if(input & BN_LT) angle += TURN_SPEED;
+       if(input & BN_RT) angle -= TURN_SPEED;
 
-       upd_vis(lvl, &player);
+       fwd[0] = -SIN(angle);
+       fwd[1] = COS(angle);
+       right[0] = fwd[1];
+       right[1] = -fwd[0];
+
+       if(input & BN_UP) {
+               pos[0] += fwd[0];
+               pos[1] += fwd[1];
+       }
+       if(input & BN_DOWN) {
+               pos[0] -= fwd[0];
+               pos[1] -= fwd[1];
+       }
+       if(input & BN_RIGHT) {
+               pos[0] += right[0];
+               pos[1] += right[1];
+       }
+       if(input & BN_LEFT) {
+               pos[0] -= right[0];
+               pos[1] -= right[1];
+       }
+
+       vox_view(vox, pos[0], pos[1], -30, angle);
 }
 
 static void draw(void)
 {
-       int i, x, y;
-       struct cell *cell;
-
-       xgl_load_identity();
-       xgl_translate(0, 0, 0x100000);
-       xgl_rotate_x(player.phi);
-       xgl_rotate_y(player.theta);
-       xgl_translate(player.x, 0, player.y);
-
-       for(i=0; i<lvl->numvis; i++) {
-               cell = lvl->vis[i];
-
-               x = (int32_t)(cell->x - player.cx) << 17;
-               y = -(int32_t)(cell->y - player.cy) << 17;
-
-               xgl_push_matrix();
-               xgl_translate(x, 0, y);
-               xgl_index(i + 1);
-               xgl_draw(XGL_QUADS, tm_floor, sizeof tm_floor / sizeof *tm_floor);
-               xgl_pop_matrix();
-       }
+       vox_render(vox);
+       vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH);
+       //vox_sky_solid(vox, COLOR_ZENITH);
 }
 
+#ifdef BUILD_GBA
 __attribute__((noinline, target("arm"), section(".iwram")))
-static void vblank(void)
+#endif
+static void gamescr_vblank(void)
 {
        num_vbl++;
 }