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 struct screen scr = {
39 static int xsz, ysz, vxsz, vysz;
40 static int pan_width, pan_height;
41 static unsigned int *tunnel_map;
42 static unsigned char *tunnel_fog;
44 static int tex_xsz, tex_ysz;
45 static unsigned int *tex_pixels;
46 static int tex_xshift, tex_yshift;
47 static unsigned int tex_xmask, tex_ymask;
49 static long trans_start, trans_dur;
53 struct screen *tunnel_screen(void)
64 float aspect = (float)fb_width / (float)fb_height;
71 pan_width = vxsz - xsz;
72 pan_height = vysz - ysz;
74 if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
75 fprintf(stderr, "failed to allocate tunnel map\n");
78 if(!(tunnel_fog = malloc(vxsz * vysz))) {
79 fprintf(stderr, "failed to allocate tunnel fog map\n");
86 for(i=0; i<vysz; i++) {
87 float y = 2.0 * (float)i / (float)vysz - 1.0;
88 for(j=0; j<vxsz; j++) {
89 float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
90 float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
91 float d = sqrt(x * x + y * y);
92 float tv = d == 0.0 ? 0.0 : 1.0 / d;
94 int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
95 int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
97 int f = (int)(d * 192.0);
99 *tmap++ = (tx << 16) | ty;
100 *fog++ = f > 255 ? 255 : f;
104 if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
105 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
108 if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
109 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
113 /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/
115 n = count_zeros(tex_xsz);
121 n = count_zeros(tex_ysz);
130 static void destroy(void)
134 img_free_pixels(tex_pixels);
137 static void start(long trans_time)
140 trans_start = time_msec;
141 trans_dur = trans_time;
146 static void stop(long trans_time)
149 trans_start = time_msec;
150 trans_dur = trans_time;
155 #define NUM_WORK_ITEMS 8
157 static void draw(void)
159 int i, num_lines = ysz / NUM_WORK_ITEMS;
160 int draw_lines = num_lines;
165 long interval = time_msec - trans_start;
166 int progr = num_lines * interval / trans_dur;
168 draw_lines = num_lines - progr - 1;
172 if(progr >= num_lines) {
177 t = time_msec / 10000.0;
178 xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
179 yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
181 for(i=0; i<NUM_WORK_ITEMS; i++) {
182 int starty = i * num_lines;
183 int resty = starty + draw_lines;
184 int rest_lines = num_lines - draw_lines;
185 draw_tunnel_range(vmem_back, xoffs, yoffs, starty, draw_lines, time_msec);
187 memset(vmem_back + 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++) {
268 static unsigned int *gen_test_image(int *wptr, int *hptr)
271 int xsz = 256, ysz = 256;
272 unsigned int *pixels, *pix;
274 if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
279 for(i=0; i<ysz; i++) {
280 for(j=0; j<xsz; j++) {
283 *pix++ = PACK_RGB32(val, val / 2, val / 4);