foo
[dosdemo] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "imago2.h"
7 #include "demo.h"
8 #include "screen.h"
9 #include "gfxutil.h"
10
11 #ifndef M_PI
12 #define M_PI    3.1415926535
13 #endif
14
15 #define VSCALE  1.5
16
17 #define TEX_FNAME       "data/grid.png"
18 #define TEX_USCALE      4
19 #define TEX_VSCALE      2
20
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);
26
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);
30
31 static struct screen scr = {
32         "tunnel",
33         init,
34         destroy,
35         start,
36         stop,
37         draw
38 };
39
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;
44
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;
49
50 static long trans_start, trans_dur;
51 static int trans_dir;
52
53
54 struct screen *tunnel_screen(void)
55 {
56         return &scr;
57 }
58
59
60 static int init(void)
61 {
62         int i, j, n;
63         unsigned int *tmap;
64         unsigned char *fog;
65         float aspect = (float)fb_width / (float)fb_height;
66
67         xsz = fb_width;
68         ysz = fb_height;
69         vxsz = xsz * VSCALE;
70         vysz = ysz * VSCALE;
71
72         pan_width = vxsz - xsz;
73         pan_height = vysz - ysz;
74
75         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
76                 fprintf(stderr, "failed to allocate tunnel map\n");
77                 return -1;
78         }
79         if(!(tunnel_fog = malloc(vxsz * vysz))) {
80                 fprintf(stderr, "failed to allocate tunnel fog map\n");
81                 return -1;
82         }
83
84         tmap = tunnel_map;
85         fog = tunnel_fog;
86
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;
94
95                         int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
96                         int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
97
98                         int f = (int)(d * 192.0);
99
100                         *tmap++ = (tx << 16) | ty;
101                         *fog++ = f > 255 ? 255 : f;
102                 }
103         }
104
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");
107                 return -1;
108         }
109         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
110                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
111                 return -1;
112         }
113
114         /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/
115
116         n = count_zeros(tex_xsz);
117         for(i=0; i<n; i++) {
118                 tex_xmask |= 1 << i;
119         }
120         tex_xshift = n;
121
122         n = count_zeros(tex_ysz);
123         for(i=0; i<n; i++) {
124                 tex_ymask |= 1 << i;
125         }
126         tex_yshift = n;
127
128         return 0;
129 }
130
131 static void destroy(void)
132 {
133         free(tunnel_map);
134         free(tunnel_fog);
135         img_free_pixels(tex_pixels);
136 }
137
138 static void start(long trans_time)
139 {
140         if(trans_time) {
141                 trans_start = time_msec;
142                 trans_dur = trans_time;
143                 trans_dir = 1;
144         }
145 }
146
147 static void stop(long trans_time)
148 {
149         if(trans_time) {
150                 trans_start = time_msec;
151                 trans_dur = trans_time;
152                 trans_dir = -1;
153         }
154 }
155
156 #define NUM_WORK_ITEMS  8
157
158 static void draw(void)
159 {
160         int i, num_lines = ysz / NUM_WORK_ITEMS;
161         int draw_lines = num_lines;
162         float t;
163         int xoffs, yoffs;
164
165         if(trans_dir) {
166                 long interval = time_msec - trans_start;
167                 int progr = num_lines * interval / trans_dur;
168                 if(trans_dir < 0) {
169                         draw_lines = num_lines - progr - 1;
170                 } else {
171                         draw_lines = progr;
172                 }
173                 if(progr >= num_lines) {
174                         trans_dir = 0;
175                 }
176         }
177
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;
181
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);
187                 if(rest_lines) {
188                         memset(fb_pixels + resty * fb_width, 0, rest_lines * fb_width * 2);
189                 }
190         }
191
192         swap_buffers(0);
193 }
194
195 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
196 {
197         int r, g, b;
198         unsigned int col;
199         unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
200         unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
201         tx += toffs;
202         ty += toffs << 1;
203
204         tx &= tex_xmask;
205         ty &= tex_ymask;
206
207         col = tex_pixels[(ty << tex_xshift) + tx];
208         r = col & 0xff;
209         g = (col >> 8) & 0xff;
210         b = (col >> 16) & 0xff;
211
212         *rp = (r * fog) >> 8;
213         *gp = (g * fog) >> 8;
214         *bp = (b * fog) >> 8;
215 }
216
217 static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
218 {
219         int i, j;
220         unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
221         unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
222
223         long toffs = tm / 8;
224         unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
225
226         for(i=0; i<num_lines; i++) {
227                 for(j=0; j<(xsz>>1); j++) {
228                         unsigned int col;
229                         int r, g, b, idx = j << 1;
230
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;
235                         *pixels++ = col;
236                 }
237                 tmap += vxsz;
238                 fog += vxsz;
239         }
240 }
241
242 static int count_bits(unsigned int x)
243 {
244         int i, nbits = 0;
245         for(i=0; i<32; i++) {
246                 if(x & 1) ++nbits;
247                 x >>= 1;
248         }
249         return nbits;
250 }
251
252 static int count_zeros(unsigned int x)
253 {
254         int i, num = 0;
255         for(i=0; i<32; i++) {
256                 if(x & 1) break;
257                 ++num;
258                 x >>= 1;
259         }
260         return num;
261 }
262
263 /*
264 static unsigned int *gen_test_image(int *wptr, int *hptr)
265 {
266         int i, j;
267         int xsz = 256, ysz = 256;
268         unsigned int *pixels, *pix;
269
270         if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
271                 return 0;
272         }
273         pix = pixels;
274
275         for(i=0; i<ysz; i++) {
276                 for(j=0; j<xsz; j++) {
277                         int val = i ^ j;
278
279                         *pix++ = PACK_RGB32(val, val / 2, val / 4);
280                 }
281         }
282
283         *wptr = xsz;
284         *hptr = ysz;
285         return pixels;
286 }
287 */