12 #define TEX_FNAME "data/grid.png"
16 #define NUM_WORK_ITEMS 8
20 int starty, num_lines;
23 } work[NUM_WORK_ITEMS];
25 static int init(void);
26 static void destroy(void);
27 static void start(long trans_time);
28 static void stop(long trans_time);
29 static void draw(void);
31 static void (*draw_tunnel_range)(void*, int, int, int, int, long);
33 static void draw_tunnel_range16(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
34 static void draw_tunnel_range32(void *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
35 static int count_bits(unsigned int x);
36 static int count_zeros(unsigned int x);
38 static struct screen scr = {
47 static int xsz, ysz, vxsz, vysz;
48 static int pan_width, pan_height;
49 static unsigned int *tunnel_map;
50 static unsigned char *tunnel_fog;
52 static int tex_xsz, tex_ysz;
53 static unsigned int *tex_pixels;
54 static int tex_xshift, tex_yshift;
55 static unsigned int tex_xmask, tex_ymask;
57 static struct thread_pool *tpool;
59 static long trans_start, trans_dur;
63 struct screen *tunnel_screen(void)
74 float aspect = (float)fb_width / (float)fb_height;
78 draw_tunnel_range = draw_tunnel_range16;
81 draw_tunnel_range = draw_tunnel_range32;
84 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
93 pan_width = vxsz - xsz;
94 pan_height = vysz - ysz;
96 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
97 fprintf(stderr, "failed to allocate tunnel map\n");
100 if(!(tunnel_fog = malloc(vxsz * vysz))) {
101 fprintf(stderr, "failed to allocate tunnel fog map\n");
108 for(i=0; i<vysz; i++) {
109 float y = 2.0 * (float)i / (float)vysz - 1.0;
110 for(j=0; j<vxsz; j++) {
111 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
112 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
113 float d = sqrt(x * x + y * y);
114 float tv = d == 0.0 ? 0.0 : 1.0 / d;
116 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
117 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
119 int f = (int)(d * 128.0);
121 *tmap++ = (tx << 16) | ty;
122 *fog++ = f > 255 ? 255 : f;
126 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
127 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
130 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
131 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
135 n = count_zeros(tex_xsz);
141 n = count_zeros(tex_ysz);
147 if(!(tpool = tpool_create(0))) {
148 fprintf(stderr, "failed to create thread pool\n");
152 /* initialize the constant part of all work items */
153 for(i=0; i<NUM_WORK_ITEMS; i++) {
154 int num_lines = ysz / NUM_WORK_ITEMS;
155 work[i].pixels = fb_pixels;
156 work[i].starty = i * num_lines;
162 static void destroy(void)
164 tpool_destroy(tpool);
169 static void start(long trans_time)
172 trans_start = time_msec;
173 trans_dur = trans_time;
178 static void stop(long trans_time)
181 trans_start = time_msec;
182 trans_dur = trans_time;
188 static void work_func(void *cls)
190 struct work *w = (struct work*)cls;
191 draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
194 static void draw(void)
196 int i, num_lines = ysz / NUM_WORK_ITEMS;
197 int draw_lines = num_lines;
202 long interval = time_msec - trans_start;
203 int progr = num_lines * interval / trans_dur;
205 draw_lines = num_lines - progr - 1;
209 if(progr >= num_lines) {
214 t = time_msec / 10000.0;
215 xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
216 yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
218 for(i=0; i<NUM_WORK_ITEMS; i++) {
219 work[i].num_lines = draw_lines;
220 work[i].tm = time_msec;
221 work[i].xoffs = xoffs;
222 work[i].yoffs = yoffs;
224 tpool_enqueue(tpool, work + i, work_func, 0);
229 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
233 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
234 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
241 col = tex_pixels[(ty << tex_xshift) + tx];
243 g = (col >> 8) & 0xff;
244 b = (col >> 16) & 0xff;
246 *rp = (r * fog) >> 8;
247 *gp = (g * fog) >> 8;
248 *bp = (b * fog) >> 8;
251 #define PACK_RGB16(r, g, b) \
252 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
253 #define PACK_RGB32(r, g, b) \
254 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
256 static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
259 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
260 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
263 unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
265 for(i=0; i<num_lines; i++) {
266 for(j=0; j<xsz; j++) {
270 tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
271 col = PACK_RGB16(r, g, b);
272 *pixels++ = (col << 16) | col;
279 static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
282 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
283 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
286 unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
288 for(i=0; i<num_lines; i++) {
289 for(j=0; j<xsz; j++) {
293 tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
294 col = PACK_RGB32(r, g, b);
304 static int count_bits(unsigned int x)
307 for(i=0; i<32; i++) {
314 static int count_zeros(unsigned int x)
317 for(i=0; i<32; i++) {