better tunnel
[fbgfx] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <imago2.h>
6 #include "tpool.h"
7 #include "demo.h"
8 #include "screen.h"
9
10 #define VSCALE  1.5
11
12 #define TEX_FNAME       "data/grid.png"
13 #define TEX_USCALE      4
14 #define TEX_VSCALE      2
15
16 static int init(void);
17 static void destroy(void);
18 static void start(long trans_time);
19 static void stop(long trans_time);
20 static void draw(void);
21
22 static void (*draw_tunnel_range)(void*, int, int, int, int, long);
23
24 static void draw_tunnel_range16(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
25 static void draw_tunnel_range32(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
26 static int count_bits(unsigned int x);
27 static int count_zeros(unsigned int x);
28
29 static struct screen scr = {
30         "tunnel",
31         init,
32         destroy,
33         start,
34         stop,
35         draw
36 };
37
38 static int xsz, ysz, vxsz, vysz;
39 static int pan_width, pan_height;
40 static unsigned int *tunnel_map;
41 static unsigned char *tunnel_fog;
42
43 static int tex_xsz, tex_ysz;
44 static unsigned int *tex_pixels;
45 static int tex_xshift, tex_yshift;
46 static unsigned int tex_xmask, tex_ymask;
47
48 static struct thread_pool *tpool;
49
50 static long trans_start, trans_dur;
51 static int trans_dir;
52
53
54 struct screen *tunnel_screen(void)
55 {
56         return &scr;
57 }
58
59
60 static int init(void)
61 {
62         int i, j, n;
63         unsigned int *tmap;
64         unsigned char *fog;
65         float aspect = (float)fb_width / (float)fb_height;
66
67         switch(fb_depth) {
68         case 16:
69                 draw_tunnel_range = draw_tunnel_range16;
70                 break;
71         case 32:
72                 draw_tunnel_range = draw_tunnel_range32;
73                 break;
74         default:
75                 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
76                 return -1;
77         }
78
79         xsz = fb_width / 2;
80         ysz = fb_height;
81         vxsz = xsz * VSCALE;
82         vysz = ysz * VSCALE;
83
84         pan_width = vxsz - xsz;
85         pan_height = vysz - ysz;
86
87         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
88                 fprintf(stderr, "failed to allocate tunnel map\n");
89                 return -1;
90         }
91         if(!(tunnel_fog = malloc(vxsz * vysz))) {
92                 fprintf(stderr, "failed to allocate tunnel fog map\n");
93                 return -1;
94         }
95
96         tmap = tunnel_map;
97         fog = tunnel_fog;
98
99         for(i=0; i<vysz; i++) {
100                 float y = 2.0 * (float)i / (float)vysz - 1.0;
101                 for(j=0; j<vxsz; j++) {
102                         float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
103                         float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
104                         float d = sqrt(x * x + y * y);
105                         float tv = d == 0.0 ? 0.0 : 1.0 / d;
106
107                         int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
108                         int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
109
110                         int f = (int)(d * 128.0);
111
112                         *tmap++ = (tx << 16) | ty;
113                         *fog++ = f > 255 ? 255 : f;
114                 }
115         }
116
117         if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
118                 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
119                 return -1;
120         }
121         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
122                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
123                 return -1;
124         }
125
126         n = count_zeros(tex_xsz);
127         for(i=0; i<n; i++) {
128                 tex_xmask |= 1 << i;
129         }
130         tex_xshift = n;
131
132         n = count_zeros(tex_ysz);
133         for(i=0; i<n; i++) {
134                 tex_ymask |= 1 << i;
135         }
136         tex_yshift = n;
137
138         if(!(tpool = tpool_create(0))) {
139                 fprintf(stderr, "failed to create thread pool\n");
140                 return -1;
141         }
142
143         return 0;
144 }
145
146 static void destroy(void)
147 {
148         tpool_destroy(tpool);
149         free(tunnel_map);
150         free(tunnel_fog);
151 }
152
153 static void start(long trans_time)
154 {
155         trans_start = time_msec;
156         trans_dur = trans_time;
157         trans_dir = 1;
158 }
159
160 static void stop(long trans_time)
161 {
162         trans_start = time_msec;
163         trans_dur = trans_time;
164         trans_dir = -1;
165 }
166
167 #define NUM_WORK_ITEMS  32
168
169 static struct work {
170         void *pixels;
171         int starty, num_lines;
172         long tm;
173         int xoffs, yoffs;
174 } work[NUM_WORK_ITEMS];
175
176 static void work_func(void *cls)
177 {
178         struct work *w = (struct work*)cls;
179         draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
180 }
181
182 static void draw(void)
183 {
184         int i, num_lines = ysz / NUM_WORK_ITEMS;
185         int draw_lines = num_lines;
186         float t;
187         int xoffs, yoffs;
188
189         if(trans_dir) {
190                 long interval = time_msec - trans_start;
191                 int progr = num_lines * interval / trans_dur;
192                 if(trans_dir < 0) {
193                         draw_lines = num_lines - progr - 1;
194                 } else {
195                         draw_lines = progr;
196                 }
197                 if(progr >= num_lines) {
198                         trans_dir = 0;
199                 }
200         }
201
202         t = time_msec / 10000.0;
203         xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
204         yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
205
206         for(i=0; i<NUM_WORK_ITEMS; i++) {
207                 work[i].pixels = fb_pixels;
208                 work[i].starty = i * num_lines;
209                 work[i].num_lines = draw_lines;
210                 work[i].tm = time_msec;
211                 work[i].xoffs = xoffs;
212                 work[i].yoffs = yoffs;
213
214                 tpool_enqueue(tpool, work + i, work_func, 0);
215         }
216         tpool_wait(tpool);
217 }
218
219 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
220 {
221         int r, g, b;
222         unsigned int col;
223         unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
224         unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
225         tx += toffs;
226         ty += toffs << 1;
227
228         tx &= tex_xmask;
229         ty &= tex_ymask;
230
231         col = tex_pixels[(ty << tex_xshift) + tx];
232         r = col & 0xff;
233         g = (col >> 8) & 0xff;
234         b = (col >> 16) & 0xff;
235
236         *rp = (r * fog) >> 8;
237         *gp = (g * fog) >> 8;
238         *bp = (b * fog) >> 8;
239 }
240
241 #define PACK_RGB16(r, g, b) \
242         (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
243 #define PACK_RGB32(r, g, b) \
244         ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
245
246 static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
247 {
248         int i, j;
249         unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
250         unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
251
252         long toffs = tm / 4;
253         unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
254
255         for(i=0; i<num_lines; i++) {
256                 for(j=0; j<xsz; j++) {
257                         unsigned int col;
258                         int r, g, b;
259
260                         tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
261                         col = PACK_RGB16(r, g, b);
262                         *pixels++ = col;
263                 }
264                 tmap += vxsz;
265                 fog += vxsz;
266         }
267 }
268
269 static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
270 {
271         int i, j;
272         unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
273         unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
274
275         long toffs = tm / 4;
276         unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
277
278         for(i=0; i<num_lines; i++) {
279                 for(j=0; j<xsz; j++) {
280                         unsigned int col;
281                         int r, g, b;
282
283                         tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
284                         col = PACK_RGB32(r, g, b);
285
286                         *pixels++ = col;
287                         *pixels++ = col;
288                 }
289                 tmap += vxsz;
290                 fog += vxsz;
291         }
292 }
293
294 static int count_bits(unsigned int x)
295 {
296         int i, nbits = 0;
297         for(i=0; i<32; i++) {
298                 if(x & 1) ++nbits;
299                 x >>= 1;
300         }
301         return nbits;
302 }
303
304 static int count_zeros(unsigned int x)
305 {
306         int i, num = 0;
307         for(i=0; i<32; i++) {
308                 if(x & 1) break;
309                 ++num;
310                 x >>= 1;
311         }
312         return num;
313 }