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