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