X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ftunnel.c;h=4707d81a9adb65f712a22265bf463934a6d94412;hb=70c067c2da05fb71f3d2de115cefbc225eff3667;hp=a348431ab3c0d6d3e9550257dac91c7ab3f04269;hpb=4b522caf5387f8075b7bbb2a2b0475c012157456;p=fbgfx diff --git a/src/tunnel.c b/src/tunnel.c index a348431..4707d81 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -1,68 +1,271 @@ #include #include #include +#include #include -#include "tunnel.h" +#include "tpool.h" +#include "demo.h" +#include "screen.h" -static int xsz, ysz; +#define TEX_FNAME "data/grid.png" +#define TEX_USCALE 4 +#define TEX_VSCALE 2 + +#define USCALE 2 +#define VSCALE 1 + +static int init(void); +static void destroy(void); +static void start(long trans_time); +static void stop(long trans_time); +static void draw(void); + +static void (*draw_tunnel_range)(void*, int, int); + +static void draw_tunnel_range16(void *pixels, int starty, int num_lines); +static void draw_tunnel_range32(void *pixels, int starty, int num_lines); +static int count_bits(unsigned int x); +static int count_zeros(unsigned int x); + +static struct screen scr = { + "tunnel", + init, + destroy, + start, + stop, + draw +}; + +static int xsz, ysz, vxsz, vysz; static unsigned int *tunnel_map; +static unsigned char *tunnel_fog; + +static int tex_xsz, tex_ysz; +static unsigned int *tex_pixels; +static int tex_xshift, tex_yshift; +static unsigned int tex_xmask, tex_ymask; + +static struct thread_pool *tpool; -int init_tunnel(int x, int y) +struct screen *tunnel_screen(void) { - int i, j; + return &scr; +} + + +static int init(void) +{ + int i, j, n; unsigned int *tmap; + unsigned char *fog; + float aspect = (float)fb_width / (float)fb_height; - xsz = x; - ysz = y; + switch(fb_depth) { + case 16: + draw_tunnel_range = draw_tunnel_range16; + break; + case 32: + draw_tunnel_range = draw_tunnel_range32; + break; + default: + fprintf(stderr, "unsupported color depth: %d\n", fb_depth); + return -1; + } - printf("precalculating tunnel map...\n"); + xsz = fb_width; + ysz = fb_height; + vxsz = xsz / USCALE; + vysz = ysz / VSCALE; - if(!(tunnel_map = malloc(xsz * ysz * sizeof *tunnel_map))) { + if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) { fprintf(stderr, "failed to allocate tunnel map\n"); return -1; } + if(!(tunnel_fog = malloc(vxsz * vysz))) { + fprintf(stderr, "failed to allocate tunnel fog map\n"); + return -1; + } + tmap = tunnel_map; + fog = tunnel_fog; - for(i=0; i 255 ? 255 : f; } } + if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) { + fprintf(stderr, "failed to load image " TEX_FNAME "\n"); + return -1; + } + if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) { + fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz); + return -1; + } + + n = count_zeros(tex_xsz); + for(i=0; i> 16) & 0xffff; - unsigned int ty = *tmap & 0xffff; - ++tmap; +static void stop(long trans_time) +{ +} + +#define NUM_WORK_ITEMS 32 - r = tx >> 8; - g = ty >> 8; +static struct work { + void *pixels; + int starty, num_lines; +} work[NUM_WORK_ITEMS]; - *pixels++ = ((((r >> 3) & 0x1f) << 11) | - (((g >> 2) & 0x3f) << 5));/* | - ((b >> 3) & 0x1f));*/ +static void work_func(void *cls) +{ + struct work *w = (struct work*)cls; + draw_tunnel_range(w->pixels, w->starty, w->num_lines); +} + +static void draw(void) +{ + int i, num_lines = vysz / NUM_WORK_ITEMS; + for(i=0; i> 16) & 0xffff) << tex_xshift) >> 16; + unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16; + tx += toffs; + ty += toffs << 1; + + tx &= tex_xmask; + ty &= tex_ymask; + + col = tex_pixels[(ty << tex_xshift) + tx]; + r = col & 0xff; + g = (col >> 8) & 0xff; + b = (col >> 16) & 0xff; + + *rp = (r * fog) >> 8; + *gp = (g * fog) >> 8; + *bp = (b * fog) >> 8; +} + +#define PACK_RGB16(r, g, b) \ + (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f)) +#define PACK_RGB32(r, g, b) \ + ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff)) + +static void draw_tunnel_range16(void *pix, int starty, int num_lines) +{ + int i, j; + unsigned int *tmap = tunnel_map + starty * vxsz; + unsigned char *fog = tunnel_fog + starty * vxsz; + + long toffs = time_msec / 4; + unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1); + + for(i=0; i>= 1; + } + return nbits; +} + +static int count_zeros(unsigned int x) +{ + int i, num = 0; + for(i=0; i<32; i++) { + if(x & 1) break; + ++num; + x >>= 1; + } + return num; +}