+
+ for(i=0; i<NUM_WORK_ITEMS; i++) {
+ work[i].pixels = fb_pixels;
+ work[i].starty = i * num_lines;
+ work[i].num_lines = draw_lines;
+ work[i].tm = time_msec;
+
+ tpool_enqueue(tpool, work + i, work_func, 0);
+ }
+ tpool_wait(tpool);
+}
+
+static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
+{
+ int r, g, b;
+ unsigned int col;
+ unsigned int tx = (((tpacked >> 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, long tm)
+{
+ int i, j;
+ unsigned int *tmap = tunnel_map + starty * vxsz;
+ unsigned char *fog = tunnel_fog + starty * vxsz;
+
+ long toffs = tm / 4;
+ unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
+
+ for(i=0; i<num_lines; i++) {
+ for(j=0; j<vxsz; j++) {
+ unsigned int col;
+ int r, g, b;
+
+ tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+ col = PACK_RGB16(r, g, b);
+ *pixels++ = col;
+ }
+ }
+}
+
+static void draw_tunnel_range32(void *pix, int starty, int num_lines, long tm)
+{
+ int i, j;
+ unsigned int *tmap = tunnel_map + starty * vxsz;
+ unsigned char *fog = tunnel_fog + starty * vxsz;
+
+ long toffs = tm / 4;
+ unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
+
+ for(i=0; i<num_lines; i++) {
+ for(j=0; j<vxsz; j++) {
+ unsigned int col;
+ int r, g, b;
+
+ tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+ col = PACK_RGB32(r, g, b);
+
+ *pixels++ = col;
+ *pixels++ = col;
+ }
+ }
+}
+
+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;