14 #define TEX_FNAME "data/grid.png"
18 #define NUM_WORK_ITEMS 8
22 int starty, num_lines;
25 } work[NUM_WORK_ITEMS];
27 static int init(void);
28 static void destroy(void);
29 static void start(long trans_time);
30 static void stop(long trans_time);
31 static void draw(void);
33 static void (*draw_tunnel_range)(void*, int, int, int, int, long);
35 static void draw_tunnel_range16(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
36 static void draw_tunnel_range32(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
37 static int count_bits(unsigned int x);
38 static int count_zeros(unsigned int x);
40 static struct screen scr = {
54 static int xsz, ysz, vxsz, vysz;
55 static int pan_width, pan_height;
56 static struct tunmap *tunnel_map;
58 static int tex_xsz, tex_ysz;
59 static unsigned int *tex_pixels;
60 static int tex_xshift, tex_yshift;
61 static unsigned int tex_xmask, tex_ymask;
64 static struct thread_pool *tpool;
67 static long trans_start, trans_dur;
71 struct screen *tunnel_screen(void)
81 float aspect = (float)fb_width / (float)fb_height;
85 draw_tunnel_range = draw_tunnel_range16;
88 draw_tunnel_range = draw_tunnel_range32;
91 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
100 pan_width = vxsz - xsz;
101 pan_height = vysz - ysz;
103 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
104 fprintf(stderr, "failed to allocate tunnel map\n");
110 for(i=0; i<vysz; i++) {
111 float y = 2.0 * (float)i / (float)vysz - 1.0;
112 for(j=0; j<vxsz; j++) {
113 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
114 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
115 float d = sqrt(x * x + y * y);
116 float tv = d == 0.0 ? 0.0 : 1.0 / d;
118 int fog = (int)(d * 128.0f);
120 tmap->u = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
121 tmap->v = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
122 tmap->fog = fog > 255 ? 255 : fog;
127 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
128 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
131 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
132 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
136 n = count_zeros(tex_xsz);
142 n = count_zeros(tex_ysz);
149 if(!(tpool = tpool_create(0))) {
150 fprintf(stderr, "failed to create thread pool\n");
155 /* initialize the constant part of all work items */
156 for(i=0; i<NUM_WORK_ITEMS; i++) {
157 int num_lines = ysz / NUM_WORK_ITEMS;
158 work[i].pixels = fb_pixels;
159 work[i].starty = i * num_lines;
165 static void destroy(void)
168 tpool_destroy(tpool);
173 static void start(long trans_time)
176 trans_start = time_msec;
177 trans_dur = trans_time;
182 static void stop(long trans_time)
185 trans_start = time_msec;
186 trans_dur = trans_time;
192 static void work_func(void *cls)
194 struct work *w = (struct work*)cls;
195 draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
198 static void draw(void)
200 int i, num_lines = ysz / NUM_WORK_ITEMS;
201 int draw_lines = num_lines;
206 long interval = time_msec - trans_start;
207 int progr = num_lines * interval / trans_dur;
209 draw_lines = num_lines - progr - 1;
213 if(progr >= num_lines) {
218 t = time_msec / 10000.0;
219 xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
220 yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
222 for(i=0; i<NUM_WORK_ITEMS; i++) {
223 work[i].num_lines = draw_lines;
224 work[i].tm = time_msec;
225 work[i].xoffs = xoffs;
226 work[i].yoffs = yoffs;
232 tpool_enqueue(tpool, work + i, work_func, 0);
238 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, struct tunmap *tmap)
242 unsigned int tx = (tmap->u << tex_xshift) >> 16;
243 unsigned int ty = (tmap->v << tex_yshift) >> 16;
250 col = tex_pixels[(ty << tex_xshift) + tx];
252 g = (col >> 8) & 0xff;
253 b = (col >> 16) & 0xff;
255 *rp = (r * tmap->fog) >> 8;
256 *gp = (g * tmap->fog) >> 8;
257 *bp = (b * tmap->fog) >> 8;
260 #define PACK_RGB16(r, g, b) \
261 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
262 #define PACK_RGB32(r, g, b) \
263 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
265 static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
268 struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
271 unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
273 for(i=0; i<num_lines; i++) {
274 for(j=0; j<xsz; j++) {
278 tunnel_color(&r, &g, &b, toffs, tmap + j);
279 col = PACK_RGB16(r, g, b);
280 *pixels++ = (col << 16) | col;
286 static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
289 struct tunmap *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
292 unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
294 for(i=0; i<num_lines; i++) {
295 for(j=0; j<xsz; j++) {
299 tunnel_color(&r, &g, &b, toffs, tmap + j);
300 col = PACK_RGB32(r, g, b);
309 static int count_bits(unsigned int x)
312 for(i=0; i<32; i++) {
319 static int count_zeros(unsigned int x)
322 for(i=0; i<32; i++) {