12 #define M_PI 3.1415926535
17 #define TEX_FNAME "data/grid.png"
21 static int init(void);
22 static void destroy(void);
23 static void start(long trans_time);
24 static void stop(long trans_time);
25 static void draw(void);
27 static void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
28 static int count_bits(unsigned int x);
29 static int count_zeros(unsigned int x);
31 static struct screen scr = {
40 static int xsz, ysz, vxsz, vysz;
41 static int pan_width, pan_height;
42 static unsigned int *tunnel_map;
43 static unsigned char *tunnel_fog;
45 static int tex_xsz, tex_ysz;
46 static unsigned int *tex_pixels;
47 static int tex_xshift, tex_yshift;
48 static unsigned int tex_xmask, tex_ymask;
50 static long trans_start, trans_dur;
54 struct screen *tunnel_screen(void)
65 float aspect = (float)FB_WIDTH / (float)FB_HEIGHT;
72 pan_width = vxsz - xsz;
73 pan_height = vysz - ysz;
75 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
76 fprintf(stderr, "failed to allocate tunnel map\n");
79 if(!(tunnel_fog = malloc(vxsz * vysz))) {
80 fprintf(stderr, "failed to allocate tunnel fog map\n");
87 for(i=0; i<vysz; i++) {
88 float y = 2.0 * (float)i / (float)vysz - 1.0;
89 for(j=0; j<vxsz; j++) {
90 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
91 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
92 float d = sqrt(x * x + y * y);
93 float tv = d == 0.0 ? 0.0 : 1.0 / d;
95 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
96 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
98 int f = (int)(d * 192.0);
100 *tmap++ = (tx << 16) | ty;
101 *fog++ = f > 255 ? 255 : f;
105 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
106 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
109 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
110 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
114 /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/
116 n = count_zeros(tex_xsz);
122 n = count_zeros(tex_ysz);
131 static void destroy(void)
135 img_free_pixels(tex_pixels);
138 static void start(long trans_time)
141 trans_start = time_msec;
142 trans_dur = trans_time;
147 static void stop(long trans_time)
150 trans_start = time_msec;
151 trans_dur = trans_time;
156 #define NUM_WORK_ITEMS 8
158 static void draw(void)
160 int i, num_lines = ysz / NUM_WORK_ITEMS;
161 int draw_lines = num_lines;
166 long interval = time_msec - trans_start;
167 int progr = num_lines * interval / trans_dur;
169 draw_lines = num_lines - progr - 1;
173 if(progr >= num_lines) {
178 t = time_msec / 10000.0;
179 xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
180 yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
182 for(i=0; i<NUM_WORK_ITEMS; i++) {
183 int starty = i * num_lines;
184 int resty = starty + draw_lines;
185 int rest_lines = num_lines - draw_lines;
186 draw_tunnel_range(fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec);
188 memset(fb_pixels + resty * FB_WIDTH, 0, rest_lines * FB_WIDTH * 2);
195 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
199 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
200 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
207 col = tex_pixels[(ty << tex_xshift) + tx];
209 g = (col >> 8) & 0xff;
210 b = (col >> 16) & 0xff;
212 *rp = (r * fog) >> 8;
213 *gp = (g * fog) >> 8;
214 *bp = (b * fog) >> 8;
217 static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
220 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
221 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
224 unsigned int *pixels = (unsigned int*)pix + starty * (FB_WIDTH >> 1);
226 for(i=0; i<num_lines; i++) {
227 for(j=0; j<(xsz>>1); j++) {
229 int r, g, b, idx = j << 1;
231 tunnel_color(&r, &g, &b, toffs, tmap[idx], fog[idx]);
232 col = PACK_RGB16(r, g, b);
233 tunnel_color(&r, &g, &b, toffs, tmap[idx + 1], fog[idx + 1]);
234 col |= PACK_RGB16(r, g, b) << 16;
242 static int count_bits(unsigned int x)
245 for(i=0; i<32; i++) {
252 static int count_zeros(unsigned int x)
255 for(i=0; i<32; i++) {
264 static unsigned int *gen_test_image(int *wptr, int *hptr)
267 int xsz = 256, ysz = 256;
268 unsigned int *pixels, *pix;
270 if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
275 for(i=0; i<ysz; i++) {
276 for(j=0; j<xsz; j++) {
279 *pix++ = PACK_RGB32(val, val / 2, val / 4);