From a4b392b15da0d7da367b1121ec5949f26e10628a Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Mon, 17 Oct 2022 22:19:13 +0300 Subject: [PATCH] porting voxscape to gba --- Makefile.pc | 2 +- src/gamescr.c | 34 ++++++++++++++++++++++++++++++---- src/input.h | 6 ++++++ src/util.h | 10 ++++++++-- src/voxscape.c | 15 +++++---------- src/voxscape.h | 2 +- tools/lutgen.c | 10 +++++++--- 7 files changed, 58 insertions(+), 21 deletions(-) diff --git a/Makefile.pc b/Makefile.pc index 9770eda..208175a 100644 --- a/Makefile.pc +++ b/Makefile.pc @@ -1,5 +1,5 @@ src = $(wildcard src/*.c) $(wildcard src/pc/*.c) -ssrc = data/lut.s +ssrc = $(wildcard src/*.s) data/lut.s obj = $(src:.c=.o) $(ssrc:.s=.o) dep = $(src:.c=.d) bin = gbajam22 diff --git a/src/gamescr.c b/src/gamescr.c index 7981cb4..3adb5c8 100644 --- a/src/gamescr.c +++ b/src/gamescr.c @@ -11,6 +11,8 @@ #include "sprite.h" #include "debug.h" #include "level.h" +#include "voxscape.h" +#include "data.h" static int gamescr_start(void); static void gamescr_stop(void); @@ -34,8 +36,8 @@ static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 }; static int32_t pos[2], angle; static struct voxscape *vox; -#define COLOR_HORIZON 0xcc77ff -#define COLOR_ZENITH 0x5588cc +#define COLOR_HORIZON 192 +#define COLOR_ZENITH 255 @@ -46,15 +48,37 @@ struct screen *init_game_screen(void) static int gamescr_start(void) { + int i; + gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1); vblperf_setcolor(1); - if(!(vox = vox_create(512, 512))) { + pos[0] = pos[1] = 256 << 16; + + 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] = ((r << 8) & 0x7c00) | ((g << 2) & 0x3e0) | (b >> 3); + } + + /* setup sky gradient palette */ + for(i=0; i<64; i++) { + int t = i << 8; + 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] = ((r << 8) & 0x7c00) | ((g << 2) & 0x3e0) | (b >> 3); + } + /*select_input(BN_DPAD | BN_LT | BN_RT);*/ nframes = 0; @@ -91,7 +115,7 @@ static void update(void) int32_t fwd[2], right[2]; uint16_t input; - input = ~REG_KEYINPUT; + input = read_input(); if(input & BN_LT) angle += TURN_SPEED; if(input & BN_RT) angle -= TURN_SPEED; @@ -123,8 +147,10 @@ static void update(void) static void draw(void) { +// vox_begin(vox); vox_render(vox); vox_sky_grad(vox, COLOR_HORIZON, COLOR_ZENITH); + //vox_sky_solid(vox, COLOR_ZENITH); } #ifdef BUILD_GBA diff --git a/src/input.h b/src/input.h index 3ed22a0..b7694c8 100644 --- a/src/input.h +++ b/src/input.h @@ -21,4 +21,10 @@ enum { void select_input(uint16_t bmask); uint16_t get_input(void); +#ifdef BUILD_GBA +#define read_input() (~REG_KEYINPUT) +#else +#define read_input() get_input() +#endif + #endif /* INPUT_H_ */ diff --git a/src/util.h b/src/util.h index 3bbafbd..f719b17 100644 --- a/src/util.h +++ b/src/util.h @@ -29,8 +29,14 @@ void present(int buf); /* defined in src/pc/main.c */ extern int16_t sinlut[]; -#define SIN(x) sinlut[(x) & 0xff] -#define COS(x) sinlut[((x) + 64) & 0xff] +#define SINLUT_BITS 8 +#define SINLUT_SIZE (1 << SINLUT_BITS) + +#define SIN(angle) \ + ((int32_t)sinlut[((angle) >> (16 - SINLUT_BITS)) & (SINLUT_SIZE - 1)] << 1) + +#define COS(angle) \ + ((int32_t)sinlut[(((angle) >> (16 - SINLUT_BITS)) + (SINLUT_SIZE / 4)) & (SINLUT_SIZE - 1)] << 1) int iwram_brk(void *addr); void *iwram_sbrk(intptr_t delta); diff --git a/src/voxscape.c b/src/voxscape.c index 2f2296e..462ed75 100644 --- a/src/voxscape.c +++ b/src/voxscape.c @@ -5,7 +5,6 @@ #include #include #include "voxscape.h" -#include "util.h" #include "debug.h" #define XLERP(a, b, t, fp) \ @@ -45,19 +44,15 @@ struct voxscape { unsigned int valid; }; -struct voxscape *vox_create(int xsz, int ysz) +struct voxscape *vox_create(int xsz, int ysz, uint8_t *himg, uint8_t *cimg) { struct voxscape *vox; if(!(vox = calloc(1, sizeof *vox))) { return 0; } - if(!(vox->height = calloc(xsz * ysz, 1))) { - panic(get_pc(), "vox_create: failed to allocate %dx%d heightmap\n", xsz, ysz); - } - if(!(vox->color = calloc(xsz * ysz, 1))) { - panic(get_pc(), "vox_create: failed to allocate %dx%d color map\n", xsz, ysz); - } + vox->height = himg; + vox->color = cimg; vox->xsz = xsz; vox->ysz = ysz; @@ -266,7 +261,7 @@ void vox_render_slice(struct voxscape *vox, int n) color = vox_color(vox, x, y); colstart = vox->fbheight - hval; colheight = hval - vox->coltop[i]; - fbptr = vox->fb + colstart * vox->fbwidth + i; + fbptr = vox->fb + colstart * vox->fbwidth / 2 + i / 2; for(j=0; jfbwidth/2; i++) { - fbptr = vox->fb + i; + fbptr = vox->fb + i / 2; colheight = vox->fbheight - vox->coltop[i]; for(j=0; j #include +#define SINLUT_SIZE 256 +#define SINLUT_SCALE 32767.0 + int main(void) { int i; @@ -8,9 +11,10 @@ int main(void) puts("\t.data"); puts("\t.globl sinlut"); puts("sinlut:"); - for(i=0; i<256; i++) { - float x = sin((float)i / 128.0f * M_PI); - printf("\t.short %d\n", (int)(x * 256.0f)); + for(i=0; i