almost
[fbgfx] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <imago2.h>
5 #include "tunnel.h"
6
7 static int xsz, ysz;
8 static unsigned int *tunnel_map;
9
10
11 int init_tunnel(int x, int y)
12 {
13         int i, j;
14         unsigned int *tmap;
15
16         xsz = x;
17         ysz = y;
18
19         printf("precalculating tunnel map...\n");
20
21         if(!(tunnel_map = malloc(xsz * ysz * sizeof *tunnel_map))) {
22                 fprintf(stderr, "failed to allocate tunnel map\n");
23                 return -1;
24         }
25         tmap = tunnel_map;
26
27         for(i=0; i<ysz; i++) {
28                 float y = 2.0 * (float)i / (float)ysz - 0.5;
29                 for(j=0; j<xsz; j++) {
30                         float x = 2.0 * (float)j / (float)xsz - 0.5;
31                         float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
32                         float tv = sqrt(x*x + y*y);
33
34                         int tx = (int)(tu * 65535.0) & 0xffff;
35                         int ty = (int)(tv * 65535.0) & 0xffff;
36
37                         *tmap++ = (tx << 16) | ty;
38                 }
39         }
40
41         return 0;
42 }
43
44 void destroy_tunnel(void)
45 {
46         free(tunnel_map);
47 }
48
49 void draw_tunnel(unsigned short *pixels)
50 {
51         int i, j, r, g, b;
52         unsigned int *tmap = tunnel_map;
53
54         for(i=0; i<ysz; i++) {
55                 for(j=0; j<xsz; j++) {
56                         unsigned int tx = (*tmap >> 16) & 0xffff;
57                         unsigned int ty = *tmap & 0xffff;
58                         ++tmap;
59
60                         r = tx >> 8;
61                         g = ty >> 8;
62
63                         *pixels++ = ((((r >> 3) & 0x1f) << 11) |
64                                         (((g >> 2) & 0x3f) << 5));/* |
65                                         ((b >> 3) & 0x1f));*/
66                 }
67         }
68 }