done
[fbgfx] / src / tunnel.c
index a348431..85ca7b4 100644 (file)
 #include <stdlib.h>
 #include <math.h>
 #include <imago2.h>
+#include "tpool.h"
 #include "tunnel.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
+
+extern unsigned long time_msec;
+
+static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines);
+static int count_bits(unsigned int x);
+static int count_zeros(unsigned int x);
+
+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)
 {
-       int i, j;
+       int i, j, n;
        unsigned int *tmap;
+       unsigned char *fog;
+       float aspect = (float)x / (float)y;
 
        xsz = x;
        ysz = y;
+       vxsz = xsz / USCALE;
+       vysz = ysz / VSCALE;
 
-       printf("precalculating tunnel map...\n");
-
-       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<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 * TEX_USCALE) & 0xffff;
+                       int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
 
-                       int tx = (int)(tu * 65535.0) & 0xffff;
-                       int ty = (int)(tv * 65535.0) & 0xffff;
+                       int f = (int)(d * 95.0);
 
                        *tmap++ = (tx << 16) | ty;
+                       *fog++ = f > 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<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;
+
+       if(!(tpool = tpool_create(0))) {
+               fprintf(stderr, "failed to create thread pool\n");
+               return -1;
+       }
+
        return 0;
 }
 
 void destroy_tunnel(void)
 {
+       tpool_destroy(tpool);
        free(tunnel_map);
+       free(tunnel_fog);
+}
+
+#define NUM_WORK_ITEMS 32
+
+static struct work {
+       unsigned short *pixels;
+       int starty, num_lines;
+} work[NUM_WORK_ITEMS];
+
+static void work_func(void *cls)
+{
+       struct work *w = (struct work*)cls;
+       draw_tunnel_range(w->pixels, w->starty, w->num_lines);
 }
 
 void draw_tunnel(unsigned short *pixels)
 {
-       int i, j, r, g, b;
-       unsigned int *tmap = tunnel_map;
+       int i, num_lines = vysz / NUM_WORK_ITEMS;
+       for(i=0; i<NUM_WORK_ITEMS; i++) {
+               work[i].pixels = pixels;
+               work[i].starty = i * num_lines;
+               work[i].num_lines = num_lines;
+
+               tpool_enqueue(tpool, work + i, work_func, 0);
+       }
+       tpool_wait(tpool);
+}
+
+#define PACK_RGB16(r, g, b) \
+       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | ((b) & 0x1f))
+
+static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines)
+{
+       int i, j, k, r, g, b;
+       unsigned int *tmap = tunnel_map + starty * vxsz;
+       unsigned char *fog = tunnel_fog + starty * vxsz;
+
+       long toffs = time_msec / 4;
+       pixels += starty * xsz * VSCALE;
 
-       for(i=0; i<ysz; i++) {
-               for(j=0; j<xsz; j++) {
-                       unsigned int tx = (*tmap >> 16) & 0xffff;
-                       unsigned int ty = *tmap & 0xffff;
+       for(i=0; i<num_lines; i++) {
+               for(j=0; j<vxsz; j++) {
+                       unsigned short *ptr;
+                       unsigned int col;
+                       unsigned int tx = (((*tmap >> 16) & 0xffff) << tex_xshift) >> 16;
+                       unsigned int ty = ((*tmap & 0xffff) << tex_yshift) >> 16;
                        ++tmap;
 
-                       r = tx >> 8;
-                       g = ty >> 8;
+                       tx += toffs;
+                       ty += toffs << 1;
+
+                       tx &= tex_xmask;
+                       ty &= tex_ymask;
 
-                       *pixels++ = ((((r >> 3) & 0x1f) << 11) |
-                                       (((g >> 2) & 0x3f) << 5));/* |
-                                       ((b >> 3) & 0x1f));*/
+                       col = tex_pixels[(ty << tex_xshift) + tx];
+                       r = col & 0xff;
+                       g = (col >> 8) & 0xff;
+                       b = (col >> 16) & 0xff;
+
+                       r = (r * *fog) >> 8;
+                       g = (g * *fog) >> 8;
+                       b = (b * *fog) >> 8;
+                       ++fog;
+
+                       col = ((((r >> 3) & 0x1f) << 11) | (((g >> 2) & 0x3f) << 5) | ((b >> 3) & 0x1f));
+
+                       ptr = pixels;
+                       for(k=0; k<VSCALE; k++) {
+                               switch(USCALE) {
+                               case 4:
+                                       ptr[3] = col;
+                               case 3:
+                                       ptr[2] = col;
+                               case 2:
+                                       ptr[1] = col;
+                               case 1:
+                                       *ptr = col;
+                               }
+                               ptr += xsz;
+                       }
+                       pixels += USCALE;
                }
+               pixels += xsz * (VSCALE - 1);
+       }
+}
+
+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;
 }