10 #define TEX_FNAME "data/grid.png"
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);
23 static void (*draw_tunnel_range)(void*, int, int, long);
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);
30 static struct screen scr = {
39 static int xsz, ysz, vxsz, vysz;
40 static unsigned int *tunnel_map;
41 static unsigned char *tunnel_fog;
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;
48 static struct thread_pool *tpool;
50 static long trans_start, trans_dur;
54 struct screen *tunnel_screen(void)
65 float aspect = (float)fb_width / (float)fb_height;
69 draw_tunnel_range = draw_tunnel_range16;
72 draw_tunnel_range = draw_tunnel_range32;
75 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
84 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
85 fprintf(stderr, "failed to allocate tunnel map\n");
88 if(!(tunnel_fog = malloc(vxsz * vysz))) {
89 fprintf(stderr, "failed to allocate tunnel fog map\n");
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;
104 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
105 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
107 int f = (int)(d * 95.0);
109 *tmap++ = (tx << 16) | ty;
110 *fog++ = f > 255 ? 255 : f;
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");
118 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
119 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
123 n = count_zeros(tex_xsz);
129 n = count_zeros(tex_ysz);
135 if(!(tpool = tpool_create(0))) {
136 fprintf(stderr, "failed to create thread pool\n");
143 static void destroy(void)
145 tpool_destroy(tpool);
150 static void start(long trans_time)
152 trans_start = time_msec;
153 trans_dur = trans_time;
157 static void stop(long trans_time)
159 trans_start = time_msec;
160 trans_dur = trans_time;
164 #define NUM_WORK_ITEMS 32
168 int starty, num_lines;
170 } work[NUM_WORK_ITEMS];
172 static void work_func(void *cls)
174 struct work *w = (struct work*)cls;
175 draw_tunnel_range(w->pixels, w->starty, w->num_lines, w->tm);
178 static void draw(void)
180 int i, num_lines = vysz / NUM_WORK_ITEMS;
181 int draw_lines = num_lines;
184 long interval = time_msec - trans_start;
185 int progr = num_lines * interval / trans_dur;
187 draw_lines = num_lines - progr - 1;
191 if(progr >= num_lines) {
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;
202 tpool_enqueue(tpool, work + i, work_func, 0);
207 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
211 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
212 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
219 col = tex_pixels[(ty << tex_xshift) + tx];
221 g = (col >> 8) & 0xff;
222 b = (col >> 16) & 0xff;
224 *rp = (r * fog) >> 8;
225 *gp = (g * fog) >> 8;
226 *bp = (b * fog) >> 8;
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))
234 static void draw_tunnel_range16(void *pix, int starty, int num_lines, long tm)
237 unsigned int *tmap = tunnel_map + starty * vxsz;
238 unsigned char *fog = tunnel_fog + starty * vxsz;
241 unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
243 for(i=0; i<num_lines; i++) {
244 for(j=0; j<vxsz; j++) {
248 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
249 col = PACK_RGB16(r, g, b);
255 static void draw_tunnel_range32(void *pix, int starty, int num_lines, long tm)
258 unsigned int *tmap = tunnel_map + starty * vxsz;
259 unsigned char *fog = tunnel_fog + starty * vxsz;
262 unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
264 for(i=0; i<num_lines; i++) {
265 for(j=0; j<vxsz; j++) {
269 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
270 col = PACK_RGB32(r, g, b);
278 static int count_bits(unsigned int x)
281 for(i=0; i<32; i++) {
288 static int count_zeros(unsigned int x)
291 for(i=0; i<32; i++) {