+ free(tunnel_fog);
+}
+
+static void start(long trans_time)
+{
+ if(trans_time) {
+ trans_start = time_msec;
+ trans_dur = trans_time;
+ trans_dir = 1;
+ }
+}
+
+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;
+
+ 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 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 / 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 col;
+ int r, g, b;
+
+ tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
+ col = PACK_RGB16(r, g, b);
+ *pixels++ = (col << 16) | col;
+ }
+ tmap += vxsz;
+ fog += vxsz;
+ }