#include <stdio.h>
#include <stdlib.h>
#include <math.h>
+#include <assert.h>
#include <imago2.h>
-#include "tunnel.h"
+#ifndef NO_THREADS
+#include "tpool.h"
+#endif
+#include "demo.h"
+#include "screen.h"
-static int xsz, ysz;
-static unsigned int *tunnel_map;
+#define VSCALE 1.5
+#define TEX_FNAME "data/grid.png"
+#define TEX_USCALE 4
+#define TEX_VSCALE 2
-int init_tunnel(int x, int y)
+#define NUM_WORK_ITEMS 8
+
+static struct work {
+ void *pixels;
+ int starty, num_lines;
+ long tm;
+ int xoffs, yoffs;
+} work[NUM_WORK_ITEMS];
+
+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, int, int, long);
+
+static void draw_tunnel_range16(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
+static void draw_tunnel_range32(void *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
+};
+
+struct tunmap {
+ int u, v;
+ unsigned char fog;
+};
+
+static int xsz, ysz, vxsz, vysz;
+static int pan_width, pan_height;
+static struct tunmap *tunnel_map;
+
+static int tex_xsz, tex_ysz;
+static unsigned int *tex_pixels;
+static int tex_xshift, tex_yshift;
+static unsigned int tex_xmask, tex_ymask;
+
+#ifndef NO_THREADS
+static struct thread_pool *tpool;
+#endif
+
+static long trans_start, trans_dur;
+static int trans_dir;
+
+
+struct screen *tunnel_screen(void)
{
- int i, j;
- unsigned int *tmap;
+ return &scr;
+}
- xsz = x;
- ysz = y;
- printf("precalculating tunnel map...\n");
+static int init(void)
+{
+ int i, j, n;
+ struct tunmap *tmap;
+ float aspect = (float)fb_width / (float)fb_height;
- if(!(tunnel_map = malloc(xsz * ysz * sizeof *tunnel_map))) {
+ 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;
+ }
+
+ xsz = fb_width / 2;
+ 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;
}
+
tmap = tunnel_map;
- for(i=0; i<ysz; i++) {
- float y = 2.0 * (float)i / (float)ysz - 0.5;
- for(j=0; j<xsz; j++) {
- float x = 2.0 * (float)j / (float)xsz - 0.5;
+ for(i=0; i<vysz; i++) {
+ float y = 2.0 * (float)i / (float)vysz - 1.0;
+ for(j=0; j<vxsz; j++) {
+ float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
- float tv = sqrt(x*x + y*y);
+ float d = sqrt(x * x + y * y);
+ float tv = d == 0.0 ? 0.0 : 1.0 / d;
- int tx = (int)(tu * 65535.0) & 0xffff;
- int ty = (int)(tv * 65535.0) & 0xffff;
+ int fog = (int)(d * 128.0f);
- *tmap++ = (tx << 16) | ty;
+ tmap->u = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
+ tmap->v = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
+ tmap->fog = fog > 255 ? 255 : fog;
+ tmap++;
}
}
+ 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<n; i++) {
+ tex_xmask |= 1 << i;
+ }
+ tex_xshift = n;
+
+ n = count_zeros(tex_ysz);
+ for(i=0; i<n; i++) {
+ tex_ymask |= 1 << i;
+ }
+ tex_yshift = n;
+
+#ifndef NO_THREADS
+ if(!(tpool = tpool_create(0))) {
+ fprintf(stderr, "failed to create thread pool\n");
+ return -1;
+ }
+#endif
+
+ /* initialize the constant part of all work items */
+ for(i=0; i<NUM_WORK_ITEMS; i++) {
+ int num_lines = ysz / NUM_WORK_ITEMS;
+ work[i].pixels = fb_pixels;
+ work[i].starty = i * num_lines;
+ }
+
return 0;
}
-void destroy_tunnel(void)
+static void destroy(void)
{
+#ifndef NO_THREADS
+ tpool_destroy(tpool);
+#endif
free(tunnel_map);
}
-void draw_tunnel(unsigned short *pixels)
+static void start(long trans_time)
{
- int i, j, r, g, b;
- unsigned int *tmap = tunnel_map;
+ if(trans_time) {
+ trans_start = time_msec;
+ trans_dur = trans_time;
+ trans_dir = 1;
+ }
+}
- for(i=0; i<ysz; i++) {
+static void stop(long trans_time)
+{
+ if(trans_time) {
+ trans_start = time_msec;
+ trans_dur = trans_time;
+ trans_dir = -1;
+ }
+}
+
+
+static void work_func(void *cls)
+{
+ struct work *w = (struct work*)cls;
+ draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
+}
+
+static void draw(void)
+{
+ int i, num_lines = ysz / NUM_WORK_ITEMS;
+ int draw_lines = num_lines;
+ float t;
+ int xoffs, yoffs;
+
+ if(trans_dir) {
+ long interval = time_msec - trans_start;
+ int progr = num_lines * interval / trans_dur;
+ if(trans_dir < 0) {
+ draw_lines = num_lines - progr - 1;
+ } else {
+ draw_lines = progr;
+ }
+ if(progr >= 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<NUM_WORK_ITEMS; i++) {
+ work[i].num_lines = draw_lines;
+ work[i].tm = time_msec;
+ work[i].xoffs = xoffs;
+ work[i].yoffs = yoffs;
+
+#ifdef NO_THREADS
+ work_func(work + i);
+ }
+#else
+ tpool_enqueue(tpool, work + i, work_func, 0);
+ }
+ tpool_wait(tpool);
+#endif
+}
+
+static void tunnel_color(int *rp, int *gp, int *bp, long toffs, struct tunmap *tmap)
+{
+ int r, g, b;
+ unsigned int col;
+ unsigned int tx = (tmap->u << tex_xshift) >> 16;
+ unsigned int ty = (tmap->v << 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 * tmap->fog) >> 8;
+ *gp = (g * tmap->fog) >> 8;
+ *bp = (b * tmap->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 xoffs, int yoffs, int starty, int num_lines, long tm)
+{
+ int i, j;
+ struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
+
+ long toffs = tm / 4;
+ unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
+
+ for(i=0; i<num_lines; i++) {
for(j=0; j<xsz; j++) {
- unsigned int tx = (*tmap >> 16) & 0xffff;
- unsigned int ty = *tmap & 0xffff;
- ++tmap;
+ unsigned int col;
+ int r, g, b;
- r = tx >> 8;
- g = ty >> 8;
+ tunnel_color(&r, &g, &b, toffs, tmap + j);
+ col = PACK_RGB16(r, g, b);
+ *pixels++ = (col << 16) | col;
+ }
+ tmap += vxsz;
+ }
+}
- *pixels++ = ((((r >> 3) & 0x1f) << 11) |
- (((g >> 2) & 0x3f) << 5));/* |
- ((b >> 3) & 0x1f));*/
+static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
+{
+ int i, j;
+ struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
+
+ long toffs = tm / 4;
+ unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
+
+ for(i=0; i<num_lines; i++) {
+ for(j=0; j<xsz; j++) {
+ unsigned int col;
+ int r, g, b;
+
+ tunnel_color(&r, &g, &b, toffs, tmap + j);
+ col = PACK_RGB32(r, g, b);
+
+ *pixels++ = col;
+ *pixels++ = col;
}
+ tmap += 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;
}