watcom build imago.lib
[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 unsigned int *gen_test_image(int *wptr, int *hptr);
31
32 static struct screen scr = {
33         "tunnel",
34         init,
35         destroy,
36         start,
37         stop,
38         draw
39 };
40
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;
45
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;
50
51 static long trans_start, trans_dur;
52 static int trans_dir;
53
54
55 struct screen *tunnel_screen(void)
56 {
57         return &scr;
58 }
59
60
61 static int init(void)
62 {
63         int i, j, n;
64         unsigned int *tmap;
65         unsigned char *fog;
66         float aspect = (float)fb_width / (float)fb_height;
67
68         xsz = fb_width;
69         ysz = fb_height;
70         vxsz = xsz * VSCALE;
71         vysz = ysz * VSCALE;
72
73         pan_width = vxsz - xsz;
74         pan_height = vysz - ysz;
75
76         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
77                 fprintf(stderr, "failed to allocate tunnel map\n");
78                 return -1;
79         }
80         if(!(tunnel_fog = malloc(vxsz * vysz))) {
81                 fprintf(stderr, "failed to allocate tunnel fog map\n");
82                 return -1;
83         }
84
85         tmap = tunnel_map;
86         fog = tunnel_fog;
87
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;
95
96                         int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
97                         int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
98
99                         int f = (int)(d * 192.0);
100
101                         *tmap++ = (tx << 16) | ty;
102                         *fog++ = f > 255 ? 255 : f;
103                 }
104         }
105
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");
108                 return -1;
109         }
110         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
111                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
112                 return -1;
113         }
114
115         /*tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);*/
116
117         n = count_zeros(tex_xsz);
118         for(i=0; i<n; i++) {
119                 tex_xmask |= 1 << i;
120         }
121         tex_xshift = n;
122
123         n = count_zeros(tex_ysz);
124         for(i=0; i<n; i++) {
125                 tex_ymask |= 1 << i;
126         }
127         tex_yshift = n;
128
129         return 0;
130 }
131
132 static void destroy(void)
133 {
134         free(tunnel_map);
135         free(tunnel_fog);
136         img_free_pixels(tex_pixels);
137 }
138
139 static void start(long trans_time)
140 {
141         if(trans_time) {
142                 trans_start = time_msec;
143                 trans_dur = trans_time;
144                 trans_dir = 1;
145         }
146 }
147
148 static void stop(long trans_time)
149 {
150         if(trans_time) {
151                 trans_start = time_msec;
152                 trans_dur = trans_time;
153                 trans_dir = -1;
154         }
155 }
156
157 #define NUM_WORK_ITEMS  8
158
159 static void draw(void)
160 {
161         int i, num_lines = ysz / NUM_WORK_ITEMS;
162         int draw_lines = num_lines;
163         float t;
164         int xoffs, yoffs;
165
166         if(trans_dir) {
167                 long interval = time_msec - trans_start;
168                 int progr = num_lines * interval / trans_dur;
169                 if(trans_dir < 0) {
170                         draw_lines = num_lines - progr - 1;
171                 } else {
172                         draw_lines = progr;
173                 }
174                 if(progr >= num_lines) {
175                         trans_dir = 0;
176                 }
177         }
178
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;
182
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);
188                 if(rest_lines) {
189                         memset((unsigned short*)fb_pixels + resty * fb_width, 0, rest_lines * fb_width * 2);
190                 }
191         }
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 static unsigned int *gen_test_image(int *wptr, int *hptr)
268 {
269         int i, j;
270         int xsz = 256, ysz = 256;
271         unsigned int *pixels, *pix;
272
273         if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
274                 return 0;
275         }
276         pix = pixels;
277
278         for(i=0; i<ysz; i++) {
279                 for(j=0; j<xsz; j++) {
280                         int val = i ^ j;
281
282                         *pix++ = PACK_RGB32(val, val / 2, val / 4);
283                 }
284         }
285
286         *wptr = xsz;
287         *hptr = ysz;
288         return pixels;
289 }