8 #define TEX_FNAME "data/grid.png"
15 extern unsigned long time_msec;
17 static void (*draw_tunnel_range)(void*, int, int);
19 static void draw_tunnel_range16(void *pixels, int starty, int num_lines);
20 static void draw_tunnel_range32(void *pixels, int starty, int num_lines);
21 static int count_bits(unsigned int x);
22 static int count_zeros(unsigned int x);
24 static int xsz, ysz, vxsz, vysz;
25 static unsigned int *tunnel_map;
26 static unsigned char *tunnel_fog;
28 static int tex_xsz, tex_ysz;
29 static unsigned int *tex_pixels;
30 static int tex_xshift, tex_yshift;
31 static unsigned int tex_xmask, tex_ymask;
33 static struct thread_pool *tpool;
36 int init_tunnel(int x, int y, int bpp)
41 float aspect = (float)x / (float)y;
45 draw_tunnel_range = draw_tunnel_range16;
48 draw_tunnel_range = draw_tunnel_range32;
51 fprintf(stderr, "unsupported color depth: %d\n", bpp);
60 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
61 fprintf(stderr, "failed to allocate tunnel map\n");
64 if(!(tunnel_fog = malloc(vxsz * vysz))) {
65 fprintf(stderr, "failed to allocate tunnel fog map\n");
72 for(i=0; i<vysz; i++) {
73 float y = 2.0 * (float)i / (float)vysz - 1.0;
74 for(j=0; j<vxsz; j++) {
75 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
76 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
77 float d = sqrt(x * x + y * y);
78 float tv = d == 0.0 ? 0.0 : 1.0 / d;
80 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
81 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
83 int f = (int)(d * 95.0);
85 *tmap++ = (tx << 16) | ty;
86 *fog++ = f > 255 ? 255 : f;
90 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
91 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
94 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
95 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
99 n = count_zeros(tex_xsz);
105 n = count_zeros(tex_ysz);
111 if(!(tpool = tpool_create(0))) {
112 fprintf(stderr, "failed to create thread pool\n");
119 void destroy_tunnel(void)
121 tpool_destroy(tpool);
126 #define NUM_WORK_ITEMS 32
130 int starty, num_lines;
131 } work[NUM_WORK_ITEMS];
133 static void work_func(void *cls)
135 struct work *w = (struct work*)cls;
136 draw_tunnel_range(w->pixels, w->starty, w->num_lines);
139 void draw_tunnel(void *pixels)
141 int i, num_lines = vysz / NUM_WORK_ITEMS;
142 for(i=0; i<NUM_WORK_ITEMS; i++) {
143 work[i].pixels = pixels;
144 work[i].starty = i * num_lines;
145 work[i].num_lines = num_lines;
147 tpool_enqueue(tpool, work + i, work_func, 0);
152 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
156 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
157 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
164 col = tex_pixels[(ty << tex_xshift) + tx];
166 g = (col >> 8) & 0xff;
167 b = (col >> 16) & 0xff;
169 *rp = (r * fog) >> 8;
170 *gp = (g * fog) >> 8;
171 *bp = (b * fog) >> 8;
174 #define PACK_RGB16(r, g, b) \
175 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | ((b) & 0x1f))
176 #define PACK_RGB32(r, g, b) \
177 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
179 #define PUTPIXEL(pixtype, col) \
182 pixtype *ptr = pixels; \
183 for(k=0; k<VSCALE; k++) { \
198 static void draw_tunnel_range16(void *pix, int starty, int num_lines)
201 unsigned int *tmap = tunnel_map + starty * vxsz;
202 unsigned char *fog = tunnel_fog + starty * vxsz;
204 long toffs = time_msec / 4;
205 unsigned short *pixels = (unsigned short*)pix + starty * xsz * VSCALE;
207 for(i=0; i<num_lines; i++) {
208 for(j=0; j<vxsz; j++) {
212 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
213 col = PACK_RGB16(r, g, b);
215 PUTPIXEL(unsigned short, col);
218 pixels += xsz * (VSCALE - 1);
222 static void draw_tunnel_range32(void *pix, int starty, int num_lines)
225 unsigned int *tmap = tunnel_map + starty * vxsz;
226 unsigned char *fog = tunnel_fog + starty * vxsz;
228 long toffs = time_msec / 4;
229 unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
231 for(i=0; i<num_lines; i++) {
232 for(j=0; j<vxsz; j++) {
236 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
237 col = PACK_RGB32(r, g, b);
239 PUTPIXEL(unsigned int, col);
242 pixels += xsz * (VSCALE - 1);
246 static int count_bits(unsigned int x)
249 for(i=0; i<32; i++) {
256 static int count_zeros(unsigned int x)
259 for(i=0; i<32; i++) {