--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <imago2.h>
+#include "tunnel.h"
+
+static int xsz, ysz;
+static unsigned int *tunnel_map;
+
+
+int init_tunnel(int x, int y)
+{
+ int i, j;
+ unsigned int *tmap;
+
+ xsz = x;
+ ysz = y;
+
+ printf("precalculating tunnel map...\n");
+
+ if(!(tunnel_map = malloc(xsz * ysz * 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;
+ float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
+ float tv = sqrt(x*x + y*y);
+
+ int tx = (int)(tu * 65535.0) & 0xffff;
+ int ty = (int)(tv * 65535.0) & 0xffff;
+
+ *tmap++ = (tx << 16) | ty;
+ }
+ }
+
+ return 0;
+}
+
+void destroy_tunnel(void)
+{
+ free(tunnel_map);
+}
+
+void draw_tunnel(unsigned short *pixels)
+{
+ int i, j, r, g, b;
+ unsigned int *tmap = tunnel_map;
+
+ for(i=0; i<ysz; i++) {
+ for(j=0; j<xsz; j++) {
+ unsigned int tx = (*tmap >> 16) & 0xffff;
+ unsigned int ty = *tmap & 0xffff;
+ ++tmap;
+
+ r = tx >> 8;
+ g = ty >> 8;
+
+ *pixels++ = ((((r >> 3) & 0x1f) << 11) |
+ (((g >> 2) & 0x3f) << 5));/* |
+ ((b >> 3) & 0x1f));*/
+ }
+ }
+}