12 #define TEX_FNAME "data/grid.png"
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);
22 static void (*draw_tunnel_range)(void*, int, int, int, int, long);
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);
29 static struct screen scr = {
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;
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 pan_width = vxsz - xsz;
85 pan_height = vysz - ysz;
87 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
88 fprintf(stderr, "failed to allocate tunnel map\n");
91 if(!(tunnel_fog = malloc(vxsz * vysz))) {
92 fprintf(stderr, "failed to allocate tunnel fog map\n");
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;
107 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
108 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
110 int f = (int)(d * 128.0);
112 *tmap++ = (tx << 16) | ty;
113 *fog++ = f > 255 ? 255 : f;
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");
121 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
122 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
126 n = count_zeros(tex_xsz);
132 n = count_zeros(tex_ysz);
138 if(!(tpool = tpool_create(0))) {
139 fprintf(stderr, "failed to create thread pool\n");
146 static void destroy(void)
148 tpool_destroy(tpool);
153 static void start(long trans_time)
155 trans_start = time_msec;
156 trans_dur = trans_time;
160 static void stop(long trans_time)
162 trans_start = time_msec;
163 trans_dur = trans_time;
167 #define NUM_WORK_ITEMS 32
171 int starty, num_lines;
174 } work[NUM_WORK_ITEMS];
176 static void work_func(void *cls)
178 struct work *w = (struct work*)cls;
179 draw_tunnel_range(w->pixels, w->xoffs, w->yoffs, w->starty, w->num_lines, w->tm);
182 static void draw(void)
184 int i, num_lines = ysz / NUM_WORK_ITEMS;
185 int draw_lines = num_lines;
190 long interval = time_msec - trans_start;
191 int progr = num_lines * interval / trans_dur;
193 draw_lines = num_lines - progr - 1;
197 if(progr >= num_lines) {
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;
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;
214 tpool_enqueue(tpool, work + i, work_func, 0);
219 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
223 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
224 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
231 col = tex_pixels[(ty << tex_xshift) + tx];
233 g = (col >> 8) & 0xff;
234 b = (col >> 16) & 0xff;
236 *rp = (r * fog) >> 8;
237 *gp = (g * fog) >> 8;
238 *bp = (b * fog) >> 8;
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))
246 static void draw_tunnel_range16(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
249 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
250 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
253 unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
255 for(i=0; i<num_lines; i++) {
256 for(j=0; j<xsz; j++) {
260 tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
261 col = PACK_RGB16(r, g, b);
269 static void draw_tunnel_range32(void *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
272 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
273 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
276 unsigned int *pixels = (unsigned int*)pix + starty * fb_width;
278 for(i=0; i<num_lines; i++) {
279 for(j=0; j<xsz; j++) {
283 tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
284 col = PACK_RGB32(r, g, b);
294 static int count_bits(unsigned int x)
297 for(i=0; i<32; i++) {
304 static int count_zeros(unsigned int x)
307 for(i=0; i<32; i++) {