8 #define TEX_FNAME "data/grid.png"
15 extern unsigned long time_msec;
17 static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines);
18 static int count_bits(unsigned int x);
19 static int count_zeros(unsigned int x);
21 static int xsz, ysz, vxsz, vysz;
22 static unsigned int *tunnel_map;
23 static unsigned char *tunnel_fog;
25 static int tex_xsz, tex_ysz;
26 static unsigned int *tex_pixels;
27 static int tex_xshift, tex_yshift;
28 static unsigned int tex_xmask, tex_ymask;
30 static struct thread_pool *tpool;
33 int init_tunnel(int x, int y)
38 float aspect = (float)x / (float)y;
45 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
46 fprintf(stderr, "failed to allocate tunnel map\n");
49 if(!(tunnel_fog = malloc(vxsz * vysz))) {
50 fprintf(stderr, "failed to allocate tunnel fog map\n");
57 for(i=0; i<vysz; i++) {
58 float y = 2.0 * (float)i / (float)vysz - 1.0;
59 for(j=0; j<vxsz; j++) {
60 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
61 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
62 float d = sqrt(x * x + y * y);
63 float tv = d == 0.0 ? 0.0 : 1.0 / d;
65 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
66 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
68 int f = (int)(d * 95.0);
70 *tmap++ = (tx << 16) | ty;
71 *fog++ = f > 255 ? 255 : f;
75 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
76 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
79 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
80 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
84 n = count_zeros(tex_xsz);
90 n = count_zeros(tex_ysz);
96 if(!(tpool = tpool_create(0))) {
97 fprintf(stderr, "failed to create thread pool\n");
104 void destroy_tunnel(void)
106 tpool_destroy(tpool);
111 #define NUM_WORK_ITEMS 32
114 unsigned short *pixels;
115 int starty, num_lines;
116 } work[NUM_WORK_ITEMS];
118 static void work_func(void *cls)
120 struct work *w = (struct work*)cls;
121 draw_tunnel_range(w->pixels, w->starty, w->num_lines);
124 void draw_tunnel(unsigned short *pixels)
126 int i, num_lines = vysz / NUM_WORK_ITEMS;
127 for(i=0; i<NUM_WORK_ITEMS; i++) {
128 work[i].pixels = pixels;
129 work[i].starty = i * num_lines;
130 work[i].num_lines = num_lines;
132 tpool_enqueue(tpool, work + i, work_func, 0);
137 #define PACK_RGB16(r, g, b) \
138 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | ((b) & 0x1f))
140 static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines)
142 int i, j, k, r, g, b;
143 unsigned int *tmap = tunnel_map + starty * vxsz;
144 unsigned char *fog = tunnel_fog + starty * vxsz;
146 long toffs = time_msec / 4;
147 pixels += starty * xsz * VSCALE;
149 for(i=0; i<num_lines; i++) {
150 for(j=0; j<vxsz; j++) {
153 unsigned int tx = (((*tmap >> 16) & 0xffff) << tex_xshift) >> 16;
154 unsigned int ty = ((*tmap & 0xffff) << tex_yshift) >> 16;
163 col = tex_pixels[(ty << tex_xshift) + tx];
165 g = (col >> 8) & 0xff;
166 b = (col >> 16) & 0xff;
173 col = ((((r >> 3) & 0x1f) << 11) | (((g >> 2) & 0x3f) << 5) | ((b >> 3) & 0x1f));
176 for(k=0; k<VSCALE; k++) {
191 pixels += xsz * (VSCALE - 1);
195 static int count_bits(unsigned int x)
198 for(i=0; i<32; i++) {
205 static int count_zeros(unsigned int x)
208 for(i=0; i<32; i++) {