screen system
[fbgfx] / 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 "tpool.h"
7 #include "demo.h"
8 #include "screen.h"
9
10 #define TEX_FNAME       "data/grid.png"
11 #define TEX_USCALE      4
12 #define TEX_VSCALE      2
13
14 #define USCALE  2
15 #define VSCALE  1
16
17 static int init(void);
18 static void destroy(void);
19 static void start(long trans_time);
20 static void stop(long trans_time);
21 static void draw(void);
22
23 static void (*draw_tunnel_range)(void*, int, int);
24
25 static void draw_tunnel_range16(void *pixels, int starty, int num_lines);
26 static void draw_tunnel_range32(void *pixels, int starty, int num_lines);
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 unsigned int *tunnel_map;
41 static unsigned char *tunnel_fog;
42
43 static int tex_xsz, tex_ysz;
44 static unsigned int *tex_pixels;
45 static int tex_xshift, tex_yshift;
46 static unsigned int tex_xmask, tex_ymask;
47
48 static struct thread_pool *tpool;
49
50
51 struct screen *tunnel_screen(void)
52 {
53         return &scr;
54 }
55
56
57 static int init(void)
58 {
59         int i, j, n;
60         unsigned int *tmap;
61         unsigned char *fog;
62         float aspect = (float)fb_width / (float)fb_height;
63
64         switch(fb_depth) {
65         case 16:
66                 draw_tunnel_range = draw_tunnel_range16;
67                 break;
68         case 32:
69                 draw_tunnel_range = draw_tunnel_range32;
70                 break;
71         default:
72                 fprintf(stderr, "unsupported color depth: %d\n", fb_depth);
73                 return -1;
74         }
75
76         xsz = fb_width;
77         ysz = fb_height;
78         vxsz = xsz / USCALE;
79         vysz = ysz / VSCALE;
80
81         if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
82                 fprintf(stderr, "failed to allocate tunnel map\n");
83                 return -1;
84         }
85         if(!(tunnel_fog = malloc(vxsz * vysz))) {
86                 fprintf(stderr, "failed to allocate tunnel fog map\n");
87                 return -1;
88         }
89
90         tmap = tunnel_map;
91         fog = tunnel_fog;
92
93         for(i=0; i<vysz; i++) {
94                 float y = 2.0 * (float)i / (float)vysz - 1.0;
95                 for(j=0; j<vxsz; j++) {
96                         float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
97                         float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
98                         float d = sqrt(x * x + y * y);
99                         float tv = d == 0.0 ? 0.0 : 1.0 / d;
100
101                         int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
102                         int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
103
104                         int f = (int)(d * 95.0);
105
106                         *tmap++ = (tx << 16) | ty;
107                         *fog++ = f > 255 ? 255 : f;
108                 }
109         }
110
111         if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
112                 fprintf(stderr, "failed to load image " TEX_FNAME "\n");
113                 return -1;
114         }
115         if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
116                 fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
117                 return -1;
118         }
119
120         n = count_zeros(tex_xsz);
121         for(i=0; i<n; i++) {
122                 tex_xmask |= 1 << i;
123         }
124         tex_xshift = n;
125
126         n = count_zeros(tex_ysz);
127         for(i=0; i<n; i++) {
128                 tex_ymask |= 1 << i;
129         }
130         tex_yshift = n;
131
132         if(!(tpool = tpool_create(0))) {
133                 fprintf(stderr, "failed to create thread pool\n");
134                 return -1;
135         }
136
137         return 0;
138 }
139
140 static void destroy(void)
141 {
142         tpool_destroy(tpool);
143         free(tunnel_map);
144         free(tunnel_fog);
145 }
146
147 static void start(long trans_time)
148 {
149 }
150
151 static void stop(long trans_time)
152 {
153 }
154
155 #define NUM_WORK_ITEMS  32
156
157 static struct work {
158         void *pixels;
159         int starty, num_lines;
160 } work[NUM_WORK_ITEMS];
161
162 static void work_func(void *cls)
163 {
164         struct work *w = (struct work*)cls;
165         draw_tunnel_range(w->pixels, w->starty, w->num_lines);
166 }
167
168 static void draw(void)
169 {
170         int i, num_lines = vysz / NUM_WORK_ITEMS;
171         for(i=0; i<NUM_WORK_ITEMS; i++) {
172                 work[i].pixels = fb_pixels;
173                 work[i].starty = i * num_lines;
174                 work[i].num_lines = num_lines;
175
176                 tpool_enqueue(tpool, work + i, work_func, 0);
177         }
178         tpool_wait(tpool);
179 }
180
181 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
182 {
183         int r, g, b;
184         unsigned int col;
185         unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
186         unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
187         tx += toffs;
188         ty += toffs << 1;
189
190         tx &= tex_xmask;
191         ty &= tex_ymask;
192
193         col = tex_pixels[(ty << tex_xshift) + tx];
194         r = col & 0xff;
195         g = (col >> 8) & 0xff;
196         b = (col >> 16) & 0xff;
197
198         *rp = (r * fog) >> 8;
199         *gp = (g * fog) >> 8;
200         *bp = (b * fog) >> 8;
201 }
202
203 #define PACK_RGB16(r, g, b) \
204         (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
205 #define PACK_RGB32(r, g, b) \
206         ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
207
208 static void draw_tunnel_range16(void *pix, int starty, int num_lines)
209 {
210         int i, j;
211         unsigned int *tmap = tunnel_map + starty * vxsz;
212         unsigned char *fog = tunnel_fog + starty * vxsz;
213
214         long toffs = time_msec / 4;
215         unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
216
217         for(i=0; i<num_lines; i++) {
218                 for(j=0; j<vxsz; j++) {
219                         unsigned int col;
220                         int r, g, b;
221
222                         tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
223                         col = PACK_RGB16(r, g, b);
224                         *pixels++ = col;
225                 }
226         }
227 }
228
229 static void draw_tunnel_range32(void *pix, int starty, int num_lines)
230 {
231         int i, j;
232         unsigned int *tmap = tunnel_map + starty * vxsz;
233         unsigned char *fog = tunnel_fog + starty * vxsz;
234
235         long toffs = time_msec / 4;
236         unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
237
238         for(i=0; i<num_lines; i++) {
239                 for(j=0; j<vxsz; j++) {
240                         unsigned int col;
241                         int r, g, b;
242
243                         tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
244                         col = PACK_RGB32(r, g, b);
245
246                         *pixels++ = col;
247                         *pixels++ = col;
248                 }
249         }
250 }
251
252 static int count_bits(unsigned int x)
253 {
254         int i, nbits = 0;
255         for(i=0; i<32; i++) {
256                 if(x & 1) ++nbits;
257                 x >>= 1;
258         }
259         return nbits;
260 }
261
262 static int count_zeros(unsigned int x)
263 {
264         int i, num = 0;
265         for(i=0; i<32; i++) {
266                 if(x & 1) break;
267                 ++num;
268                 x >>= 1;
269         }
270         return num;
271 }