11 #define M_PI 3.1415926535
16 #define TEX_FNAME "data/grid.png"
20 static int init(void);
21 static void destroy(void);
22 static void start(long trans_time);
23 static void stop(long trans_time);
24 static void draw(void);
26 static void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
27 static int count_bits(unsigned int x);
28 static int count_zeros(unsigned int x);
30 static unsigned int *gen_test_image(int *wptr, int *hptr);
32 static struct screen scr = {
41 static int xsz, ysz, vxsz, vysz;
42 static int pan_width, pan_height;
43 static unsigned int *tunnel_map;
44 static unsigned char *tunnel_fog;
46 static int tex_xsz, tex_ysz;
47 static unsigned int *tex_pixels;
48 static int tex_xshift, tex_yshift;
49 static unsigned int tex_xmask, tex_ymask;
51 static long trans_start, trans_dur;
55 struct screen *tunnel_screen(void)
66 float aspect = (float)fb_width / (float)fb_height;
73 pan_width = vxsz - xsz;
74 pan_height = vysz - ysz;
76 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
77 fprintf(stderr, "failed to allocate tunnel map\n");
80 if(!(tunnel_fog = malloc(vxsz * vysz))) {
81 fprintf(stderr, "failed to allocate tunnel fog map\n");
88 for(i=0; i<vysz; i++) {
89 float y = 2.0 * (float)i / (float)vysz - 1.0;
90 for(j=0; j<vxsz; j++) {
91 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
92 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
93 float d = sqrt(x * x + y * y);
94 float tv = d == 0.0 ? 0.0 : 1.0 / d;
96 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
97 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
99 int f = (int)(d * 192.0);
101 *tmap++ = (tx << 16) | ty;
102 *fog++ = f > 255 ? 255 : f;
106 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
107 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
110 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
111 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
115 /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/
117 n = count_zeros(tex_xsz);
123 n = count_zeros(tex_ysz);
132 static void destroy(void)
136 img_free_pixels(tex_pixels);
139 static void start(long trans_time)
142 trans_start = time_msec;
143 trans_dur = trans_time;
148 static void stop(long trans_time)
151 trans_start = time_msec;
152 trans_dur = trans_time;
157 #define NUM_WORK_ITEMS 8
159 static void draw(void)
161 int i, num_lines = ysz / NUM_WORK_ITEMS;
162 int draw_lines = num_lines;
167 long interval = time_msec - trans_start;
168 int progr = num_lines * interval / trans_dur;
170 draw_lines = num_lines - progr - 1;
174 if(progr >= num_lines) {
179 t = time_msec / 10000.0;
180 xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
181 yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
183 for(i=0; i<NUM_WORK_ITEMS; i++) {
184 int starty = i * num_lines;
185 int resty = starty + draw_lines;
186 int rest_lines = num_lines - draw_lines;
187 draw_tunnel_range((unsigned short*)fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec);
189 memset((unsigned short*)fb_pixels + resty * fb_width, 0, rest_lines * fb_width * 2);
194 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
198 unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
199 unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
206 col = tex_pixels[(ty << tex_xshift) + tx];
208 g = (col >> 8) & 0xff;
209 b = (col >> 16) & 0xff;
211 *rp = (r * fog) >> 8;
212 *gp = (g * fog) >> 8;
213 *bp = (b * fog) >> 8;
216 #define PACK_RGB16(r, g, b) \
217 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
218 #define PACK_RGB32(r, g, b) \
219 ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
221 static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
224 unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
225 unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
228 unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
230 for(i=0; i<num_lines; i++) {
231 for(j=0; j<(xsz>>1); j++) {
233 int r, g, b, idx = j << 1;
235 tunnel_color(&r, &g, &b, toffs, tmap[idx], fog[idx]);
236 col = PACK_RGB16(r, g, b);
237 tunnel_color(&r, &g, &b, toffs, tmap[idx + 1], fog[idx + 1]);
238 col |= PACK_RGB16(r, g, b) << 16;
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++) {
267 static unsigned int *gen_test_image(int *wptr, int *hptr)
270 int xsz = 256, ysz = 256;
271 unsigned int *pixels, *pix;
273 if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
278 for(i=0; i<ysz; i++) {
279 for(j=0; j<xsz; j++) {
282 *pix++ = PACK_RGB32(val, val / 2, val / 4);