55eaefadaa56a83ca3948909a46c42380aee0540
[dosdemo] / src / tunnel.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "imago2.h"
6 #include "demo.h"
7 #include "screen.h"
8
9 #ifndef M_PI
10 #define M_PI    3.1415926535
11 #endif
12
13 #define VSCALE  1.5
14
15 #define TEX_FNAME       "data/grid.png"
16 #define TEX_USCALE      4
17 #define TEX_VSCALE      2
18
19 static int init(void);
20 static void destroy(void);
21 static void start(long trans_time);
22 static void stop(long trans_time);
23 static void draw(void);
24
25 static void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
26 static int count_bits(unsigned int x);
27 static int count_zeros(unsigned int x);
28
29 static unsigned int *gen_test_image(int *wptr, int *hptr);
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                 draw_tunnel_range((unsigned short*)fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec);
185         }
186 }
187
188 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
189 {
190         int r, g, b;
191         unsigned int col;
192         unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
193         unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
194         tx += toffs;
195         ty += toffs << 1;
196
197         tx &= tex_xmask;
198         ty &= tex_ymask;
199
200         col = tex_pixels[(ty << tex_xshift) + tx];
201         r = col & 0xff;
202         g = (col >> 8) & 0xff;
203         b = (col >> 16) & 0xff;
204
205         *rp = (r * fog) >> 8;
206         *gp = (g * fog) >> 8;
207         *bp = (b * fog) >> 8;
208 }
209
210 #define PACK_RGB16(r, g, b) \
211         (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
212 #define PACK_RGB32(r, g, b) \
213         ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
214
215 static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
216 {
217         int i, j;
218         unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
219         unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
220
221         long toffs = tm / 4;
222         unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
223
224         for(i=0; i<num_lines; i++) {
225                 for(j=0; j<(xsz>>1); j++) {
226                         unsigned int col;
227                         int r, g, b, idx = j << 1;
228
229                         tunnel_color(&r, &g, &b, toffs, tmap[idx], fog[idx]);
230                         col = PACK_RGB16(r, g, b);
231                         tunnel_color(&r, &g, &b, toffs, tmap[idx + 1], fog[idx + 1]);
232                         col |= PACK_RGB16(r, g, b) << 16;
233                         *pixels++ = col;
234                 }
235                 tmap += vxsz;
236                 fog += vxsz;
237         }
238 }
239
240 static int count_bits(unsigned int x)
241 {
242         int i, nbits = 0;
243         for(i=0; i<32; i++) {
244                 if(x & 1) ++nbits;
245                 x >>= 1;
246         }
247         return nbits;
248 }
249
250 static int count_zeros(unsigned int x)
251 {
252         int i, num = 0;
253         for(i=0; i<32; i++) {
254                 if(x & 1) break;
255                 ++num;
256                 x >>= 1;
257         }
258         return num;
259 }
260
261 static unsigned int *gen_test_image(int *wptr, int *hptr)
262 {
263         int i, j;
264         int xsz = 256, ysz = 256;
265         unsigned int *pixels, *pix;
266
267         if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
268                 return 0;
269         }
270         pix = pixels;
271
272         for(i=0; i<ysz; i++) {
273                 for(j=0; j<xsz; j++) {
274                         int val = i ^ j;
275
276                         *pix++ = PACK_RGB32(val, val / 2, val / 4);
277                 }
278         }
279
280         *wptr = xsz;
281         *hptr = ysz;
282         return pixels;
283 }