85ca7b42fa27f2a6576e07db94657c66e6d6bbac
[fbgfx] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <imago2.h>
5 #include "tpool.h"
6 #include "tunnel.h"
7
8 #define TEX_FNAME       "data/grid.png"
9 #define TEX_USCALE      4
10 #define TEX_VSCALE      2
11
12 #define USCALE  2
13 #define VSCALE  1
14
15 extern unsigned long time_msec;
16
17 static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines);
18 static int count_bits(unsigned int x);
19 static int count_zeros(unsigned int x);
20
21 static int xsz, ysz, vxsz, vysz;
22 static unsigned int *tunnel_map;
23 static unsigned char *tunnel_fog;
24
25 static int tex_xsz, tex_ysz;
26 static unsigned int *tex_pixels;
27 static int tex_xshift, tex_yshift;
28 static unsigned int tex_xmask, tex_ymask;
29
30 static struct thread_pool *tpool;
31
32
33 int init_tunnel(int x, int y)
34 {
35         int i, j, n;
36         unsigned int *tmap;
37         unsigned char *fog;
38         float aspect = (float)x / (float)y;
39
40         xsz = x;
41         ysz = y;
42         vxsz = xsz / USCALE;
43         vysz = ysz / VSCALE;
44
45         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
46                 fprintf(stderr, "failed to allocate tunnel map\n");
47                 return -1;
48         }
49         if(!(tunnel_fog = malloc(vxsz * vysz))) {
50                 fprintf(stderr, "failed to allocate tunnel fog map\n");
51                 return -1;
52         }
53
54         tmap = tunnel_map;
55         fog = tunnel_fog;
56
57         for(i=0; i<vysz; i++) {
58                 float y = 2.0 * (float)i / (float)vysz - 1.0;
59                 for(j=0; j<vxsz; j++) {
60                         float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
61                         float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
62                         float d = sqrt(x * x + y * y);
63                         float tv = d == 0.0 ? 0.0 : 1.0 / d;
64
65                         int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
66                         int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
67
68                         int f = (int)(d * 95.0);
69
70                         *tmap++ = (tx << 16) | ty;
71                         *fog++ = f > 255 ? 255 : f;
72                 }
73         }
74
75         if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
76                 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
77                 return -1;
78         }
79         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
80                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
81                 return -1;
82         }
83
84         n = count_zeros(tex_xsz);
85         for(i=0; i<n; i++) {
86                 tex_xmask |= 1 << i;
87         }
88         tex_xshift = n;
89
90         n = count_zeros(tex_ysz);
91         for(i=0; i<n; i++) {
92                 tex_ymask |= 1 << i;
93         }
94         tex_yshift = n;
95
96         if(!(tpool = tpool_create(0))) {
97                 fprintf(stderr, "failed to create thread pool\n");
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 void destroy_tunnel(void)
105 {
106         tpool_destroy(tpool);
107         free(tunnel_map);
108         free(tunnel_fog);
109 }
110
111 #define NUM_WORK_ITEMS  32
112
113 static struct work {
114         unsigned short *pixels;
115         int starty, num_lines;
116 } work[NUM_WORK_ITEMS];
117
118 static void work_func(void *cls)
119 {
120         struct work *w = (struct work*)cls;
121         draw_tunnel_range(w->pixels, w->starty, w->num_lines);
122 }
123
124 void draw_tunnel(unsigned short *pixels)
125 {
126         int i, num_lines = vysz / NUM_WORK_ITEMS;
127         for(i=0; i<NUM_WORK_ITEMS; i++) {
128                 work[i].pixels = pixels;
129                 work[i].starty = i * num_lines;
130                 work[i].num_lines = num_lines;
131
132                 tpool_enqueue(tpool, work + i, work_func, 0);
133         }
134         tpool_wait(tpool);
135 }
136
137 #define PACK_RGB16(r, g, b) \
138         (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | ((b) & 0x1f))
139
140 static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines)
141 {
142         int i, j, k, r, g, b;
143         unsigned int *tmap = tunnel_map + starty * vxsz;
144         unsigned char *fog = tunnel_fog + starty * vxsz;
145
146         long toffs = time_msec / 4;
147         pixels += starty * xsz * VSCALE;
148
149         for(i=0; i<num_lines; i++) {
150                 for(j=0; j<vxsz; j++) {
151                         unsigned short *ptr;
152                         unsigned int col;
153                         unsigned int tx = (((*tmap >> 16) & 0xffff) << tex_xshift) >> 16;
154                         unsigned int ty = ((*tmap & 0xffff) << tex_yshift) >> 16;
155                         ++tmap;
156
157                         tx += toffs;
158                         ty += toffs << 1;
159
160                         tx &= tex_xmask;
161                         ty &= tex_ymask;
162
163                         col = tex_pixels[(ty << tex_xshift) + tx];
164                         r = col & 0xff;
165                         g = (col >> 8) & 0xff;
166                         b = (col >> 16) & 0xff;
167
168                         r = (r * *fog) >> 8;
169                         g = (g * *fog) >> 8;
170                         b = (b * *fog) >> 8;
171                         ++fog;
172
173                         col = ((((r >> 3) & 0x1f) << 11) | (((g >> 2) & 0x3f) << 5) | ((b >> 3) & 0x1f));
174
175                         ptr = pixels;
176                         for(k=0; k<VSCALE; k++) {
177                                 switch(USCALE) {
178                                 case 4:
179                                         ptr[3] = col;
180                                 case 3:
181                                         ptr[2] = col;
182                                 case 2:
183                                         ptr[1] = col;
184                                 case 1:
185                                         *ptr = col;
186                                 }
187                                 ptr += xsz;
188                         }
189                         pixels += USCALE;
190                 }
191                 pixels += xsz * (VSCALE - 1);
192         }
193 }
194
195 static int count_bits(unsigned int x)
196 {
197         int i, nbits = 0;
198         for(i=0; i<32; i++) {
199                 if(x & 1) ++nbits;
200                 x >>= 1;
201         }
202         return nbits;
203 }
204
205 static int count_zeros(unsigned int x)
206 {
207         int i, num = 0;
208         for(i=0; i<32; i++) {
209                 if(x & 1) break;
210                 ++num;
211                 x >>= 1;
212         }
213         return num;
214 }