9 #define TEX_FNAME "data/grid.png"
16 extern unsigned long time_msec;
18 static void (*draw_tunnel_range)(void*, int, int);
20 static void draw_tunnel_range16(void *pixels, int starty, int num_lines);
21 static void draw_tunnel_range32(void *pixels, int starty, int num_lines);
22 static int count_bits(unsigned int x);
23 static int count_zeros(unsigned int x);
25 static int xsz, ysz, vxsz, vysz;
26 static unsigned int *tunnel_map;
27 static unsigned char *tunnel_fog;
29 static int tex_xsz, tex_ysz;
30 static unsigned int *tex_pixels;
31 static int tex_xshift, tex_yshift;
32 static unsigned int tex_xmask, tex_ymask;
34 static struct thread_pool *tpool;
37 int init_tunnel(int x, int y, int bpp)
42 float aspect = (float)x / (float)y;
46 draw_tunnel_range = draw_tunnel_range16;
49 draw_tunnel_range = draw_tunnel_range32;
52 fprintf(stderr, "unsupported color depth: %d\n", bpp);
61 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
62 fprintf(stderr, "failed to allocate tunnel map\n");
65 if(!(tunnel_fog = malloc(vxsz * vysz))) {
66 fprintf(stderr, "failed to allocate tunnel fog map\n");
73 for(i=0; i<vysz; i++) {
74 float y = 2.0 * (float)i / (float)vysz - 1.0;
75 for(j=0; j<vxsz; j++) {
76 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
77 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
78 float d = sqrt(x * x + y * y);
79 float tv = d == 0.0 ? 0.0 : 1.0 / d;
81 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
82 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
84 int f = (int)(d * 95.0);
86 *tmap++ = (tx << 16) | ty;
87 *fog++ = f > 255 ? 255 : f;
91 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
92 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
95 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
96 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
100 n = count_zeros(tex_xsz);
106 n = count_zeros(tex_ysz);
112 if(!(tpool = tpool_create(0))) {
113 fprintf(stderr, "failed to create thread pool\n");
120 void destroy_tunnel(void)
122 tpool_destroy(tpool);
127 #define NUM_WORK_ITEMS 32
131 int starty, num_lines;
132 } work[NUM_WORK_ITEMS];
134 static void work_func(void *cls)
136 struct work *w = (struct work*)cls;
137 draw_tunnel_range(w->pixels, w->starty, w->num_lines);
140 void draw_tunnel(void *pixels)
142 int i, num_lines = vysz / NUM_WORK_ITEMS;
143 for(i=0; i<NUM_WORK_ITEMS; i++) {
144 work[i].pixels = pixels;
145 work[i].starty = i * num_lines;
146 work[i].num_lines = num_lines;
148 tpool_enqueue(tpool, work + i, work_func, 0);
153 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
157 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
158 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
165 col = tex_pixels[(ty << tex_xshift) + tx];
167 g = (col >> 8) & 0xff;
168 b = (col >> 16) & 0xff;
170 *rp = (r * fog) >> 8;
171 *gp = (g * fog) >> 8;
172 *bp = (b * fog) >> 8;
175 #define PACK_RGB16(r, g, b) \
176 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
177 #define PACK_RGB32(r, g, b) \
178 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
180 static void draw_tunnel_range16(void *pix, int starty, int num_lines)
183 unsigned int *tmap = tunnel_map + starty * vxsz;
184 unsigned char *fog = tunnel_fog + starty * vxsz;
186 long toffs = time_msec / 4;
187 unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
189 for(i=0; i<num_lines; i++) {
190 for(j=0; j<vxsz; j++) {
194 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
195 col = PACK_RGB16(r, g, b);
201 static void draw_tunnel_range32(void *pix, int starty, int num_lines)
204 unsigned int *tmap = tunnel_map + starty * vxsz;
205 unsigned char *fog = tunnel_fog + starty * vxsz;
207 long toffs = time_msec / 4;
208 unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
210 for(i=0; i<num_lines; i++) {
211 for(j=0; j<vxsz; j++) {
215 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
216 col = PACK_RGB32(r, g, b);
224 static int count_bits(unsigned int x)
227 for(i=0; i<32; i++) {
234 static int count_zeros(unsigned int x)
237 for(i=0; i<32; i++) {