X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fscr%2Ftunnel.c;fp=src%2Fscr%2Ftunnel.c;h=9071b74ea4a1cba12010c6bcd4c86c88bfff6da3;hb=ba648ddfc62fc6d3f47294aa8bfc10ea6ca3f479;hp=0000000000000000000000000000000000000000;hpb=3398fc6c4188104048f99b650a6cb90beda9b6ed;p=dosdemo diff --git a/src/scr/tunnel.c b/src/scr/tunnel.c new file mode 100644 index 0000000..9071b74 --- /dev/null +++ b/src/scr/tunnel.c @@ -0,0 +1,287 @@ +#include +#include +#include +#include +#include +#include "imago2.h" +#include "demo.h" +#include "screen.h" +#include "gfxutil.h" + +#ifndef M_PI +#define M_PI 3.1415926535 +#endif + +#define VSCALE 1.5 + +#define TEX_FNAME "data/grid.png" +#define TEX_USCALE 4 +#define TEX_VSCALE 2 + +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(unsigned short *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm); +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 int pan_width, pan_height; +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 long trans_start, trans_dur; +static int trans_dir; + + +struct screen *tunnel_screen(void) +{ + 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 = fb_width; + ysz = fb_height; + vxsz = xsz * VSCALE; + vysz = ysz * VSCALE; + + pan_width = vxsz - xsz; + pan_height = vysz - ysz; + + 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; + } + + /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/ + + n = count_zeros(tex_xsz); + for(i=0; i= num_lines) { + trans_dir = 0; + } + } + + t = time_msec / 10000.0; + xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2; + yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2; + + 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; +} + +static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm) +{ + int i, j; + unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs; + unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs; + + long toffs = tm / 8; + unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1); + + for(i=0; i>1); j++) { + unsigned int col; + int r, g, b, idx = j << 1; + + tunnel_color(&r, &g, &b, toffs, tmap[idx], fog[idx]); + col = PACK_RGB16(r, g, b); + tunnel_color(&r, &g, &b, toffs, tmap[idx + 1], fog[idx + 1]); + col |= PACK_RGB16(r, g, b) << 16; + *pixels++ = col; + } + tmap += vxsz; + fog += vxsz; + } +} + +static int count_bits(unsigned int x) +{ + int i, nbits = 0; + for(i=0; i<32; i++) { + if(x & 1) ++nbits; + x >>= 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; +} + +/* +static unsigned int *gen_test_image(int *wptr, int *hptr) +{ + int i, j; + int xsz = 256, ysz = 256; + unsigned int *pixels, *pix; + + if(!(pixels = malloc(xsz * ysz * sizeof *pix))) { + return 0; + } + pix = pixels; + + for(i=0; i