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);
25 static void draw_tunnel_range16(void *pixels, int starty, int num_lines);
26 static void draw_tunnel_range32(void *pixels, int starty, int num_lines);
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;
51 struct screen *tunnel_screen(void)
62 float aspect = (float)fb_width / (float)fb_height;
66 draw_tunnel_range = draw_tunnel_range16;
69 draw_tunnel_range = draw_tunnel_range32;
72 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
81 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
82 fprintf(stderr, "failed to allocate tunnel map\n");
85 if(!(tunnel_fog = malloc(vxsz * vysz))) {
86 fprintf(stderr, "failed to allocate tunnel fog map\n");
93 for(i=0; i<vysz; i++) {
94 float y = 2.0 * (float)i / (float)vysz - 1.0;
95 for(j=0; j<vxsz; j++) {
96 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
97 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
98 float d = sqrt(x * x + y * y);
99 float tv = d == 0.0 ? 0.0 : 1.0 / d;
101 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
102 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
104 int f = (int)(d * 95.0);
106 *tmap++ = (tx << 16) | ty;
107 *fog++ = f > 255 ? 255 : f;
111 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
112 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
115 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
116 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
120 n = count_zeros(tex_xsz);
126 n = count_zeros(tex_ysz);
132 if(!(tpool = tpool_create(0))) {
133 fprintf(stderr, "failed to create thread pool\n");
140 static void destroy(void)
142 tpool_destroy(tpool);
147 static void start(long trans_time)
151 static void stop(long trans_time)
155 #define NUM_WORK_ITEMS 32
159 int starty, num_lines;
160 } work[NUM_WORK_ITEMS];
162 static void work_func(void *cls)
164 struct work *w = (struct work*)cls;
165 draw_tunnel_range(w->pixels, w->starty, w->num_lines);
168 static void draw(void)
170 int i, num_lines = vysz / NUM_WORK_ITEMS;
171 for(i=0; i<NUM_WORK_ITEMS; i++) {
172 work[i].pixels = fb_pixels;
173 work[i].starty = i * num_lines;
174 work[i].num_lines = num_lines;
176 tpool_enqueue(tpool, work + i, work_func, 0);
181 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
185 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
186 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
193 col = tex_pixels[(ty << tex_xshift) + tx];
195 g = (col >> 8) & 0xff;
196 b = (col >> 16) & 0xff;
198 *rp = (r * fog) >> 8;
199 *gp = (g * fog) >> 8;
200 *bp = (b * fog) >> 8;
203 #define PACK_RGB16(r, g, b) \
204 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
205 #define PACK_RGB32(r, g, b) \
206 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
208 static void draw_tunnel_range16(void *pix, int starty, int num_lines)
211 unsigned int *tmap = tunnel_map + starty * vxsz;
212 unsigned char *fog = tunnel_fog + starty * vxsz;
214 long toffs = time_msec / 4;
215 unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
217 for(i=0; i<num_lines; i++) {
218 for(j=0; j<vxsz; j++) {
222 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
223 col = PACK_RGB16(r, g, b);
229 static void draw_tunnel_range32(void *pix, int starty, int num_lines)
232 unsigned int *tmap = tunnel_map + starty * vxsz;
233 unsigned char *fog = tunnel_fog + starty * vxsz;
235 long toffs = time_msec / 4;
236 unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
238 for(i=0; i<num_lines; i++) {
239 for(j=0; j<vxsz; j++) {
243 tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
244 col = PACK_RGB32(r, g, b);
252 static int count_bits(unsigned int x)
255 for(i=0; i<32; i++) {
262 static int count_zeros(unsigned int x)
265 for(i=0; i<32; i++) {