reorganized the source code
[dosdemo] / src / scr / bump.c
1 // Bump effect (not moving yet of course, I have many ideas on this to commit before it's ready)
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <math.h>
7
8 #include "demo.h"
9 #include "screen.h"
10
11 static int init(void);
12 static void destroy(void);
13 static void start(long trans_time);
14 static void draw(void);
15
16 static struct screen scr = {
17         "bump",
18         init,
19         destroy,
20         start,
21         0,
22         draw
23 };
24
25 struct point {
26         int x, y;
27 };
28
29 #define NUM_BIG_LIGHTS 3
30 #define BIG_LIGHT_WIDTH 256
31 #define BIG_LIGHT_HEIGHT BIG_LIGHT_WIDTH
32
33 #define NUM_PARTICLES 64
34 #define PARTICLE_LIGHT_WIDTH 32
35 #define PARTICLE_LIGHT_HEIGHT 32
36
37
38 static unsigned long startingTime;
39
40 static unsigned char *heightmap;
41 static unsigned short *lightmap;
42 static int *bumpOffset;
43
44 static unsigned short *bigLight[NUM_BIG_LIGHTS];
45 static struct point bigLightPoint[NUM_BIG_LIGHTS];
46
47 static unsigned short *particleLight;
48 static struct point particlePoint[NUM_PARTICLES];
49
50 struct screen *bump_screen(void)
51 {
52         return &scr;
53 }
54
55 static int init(void)
56 {
57         int i, j, x, y, c, r, g, b;
58
59         const int numBlurs = 3;
60         const int lightRadius = BIG_LIGHT_WIDTH / 2;
61         const int particleRadius = PARTICLE_LIGHT_WIDTH / 2;
62
63         const int bigLightSize = BIG_LIGHT_WIDTH * BIG_LIGHT_HEIGHT;
64         const int particleLightSize = PARTICLE_LIGHT_WIDTH * PARTICLE_LIGHT_HEIGHT;
65         const int fb_size = fb_width * fb_height;
66
67         // Just some parameters to temporary test the colors of 3 lights
68         // if every light uses it's own channel bits, it's better
69         const float rgbMul[9] = { 1.0f, 0.0f, 0.0f, 
70                                                                   0.0f, 1.0f, 0.0f,
71                                                                   0.0f, 0.0f, 1.0f};
72
73         heightmap = malloc(sizeof(*heightmap) * fb_size);
74         lightmap = malloc(sizeof(*lightmap) * fb_size);
75         bumpOffset = malloc(sizeof(*bumpOffset) * fb_size);
76
77         for (i = 0; i < NUM_BIG_LIGHTS; i++)
78                 bigLight[i] = malloc(sizeof(*bigLight[i]) * bigLightSize);
79
80         particleLight = malloc(sizeof(*particleLight) * particleLightSize);
81
82         memset(lightmap, 0, sizeof(*lightmap) * fb_size);
83         memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size);
84         memset(particlePoint, 0, sizeof(*particlePoint) * NUM_PARTICLES);
85
86         // Create random junk
87         for (i = 0; i < fb_size; i++)
88                 heightmap[i] = rand() & 255;
89
90         // Blur to smooth
91         for (j = 0; j < numBlurs; j++)
92                 for (i = 0; i < fb_size; i++)
93                         heightmap[i] = (heightmap[abs((i - 1) % fb_size)] + heightmap[abs((i + 1) % fb_size)] + heightmap[abs((i - fb_width) % fb_size)] + heightmap[abs((i + fb_width) % fb_size)]) >> 2;
94
95         // Inclination precalculation
96         i = 0;
97         for (y = 0; y < fb_height; y++)
98         {
99                 for (x = 0; x < fb_width; x++)
100                 {
101                         const float offsetPower = 0.75f;
102                         int dx, dy, xp, yp;
103
104                         dx = i < fb_size - 1 ? (int)((heightmap[i] - heightmap[i + 1]) * offsetPower) : 0;
105                         dy = i < fb_size - fb_width ? (int)((heightmap[i] - heightmap[i + fb_width]) * offsetPower) : 0;
106
107                         xp = x + dx;
108                         if (xp < 0) xp = 0;
109                         if (xp > fb_width - 1) xp = fb_width - 1;
110
111                         yp = y + dy;
112                         if (yp < 0) yp = 0;
113                         if (yp > fb_height - 1) yp = fb_height - 1;
114
115                         bumpOffset[i++] = yp * fb_width + xp;
116                 }
117         }
118
119         // Generate three lights
120         i = 0;
121         for (y = 0; y < BIG_LIGHT_HEIGHT; y++)
122         {
123                 int yc = y - (BIG_LIGHT_HEIGHT / 2);
124                 for (x = 0; x < BIG_LIGHT_WIDTH; x++)
125                 {
126                         int xc = x - (BIG_LIGHT_WIDTH / 2);
127                         float d = (float)sqrt(xc * xc + yc * yc);
128                         float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius;
129                         if (invDist < 0.0f) invDist = 0.0f;
130
131                         c = (int)(invDist * 63);
132                         r = c >> 1;
133                         g = c;
134                         b = c >> 1;
135
136                         for (j = 0; j < NUM_BIG_LIGHTS; j++)
137                         {
138                                 bigLight[j][i] = ((int)(r * rgbMul[j * 3]) << 11) | ((int)(g * rgbMul[j * 3 + 1]) << 5) | (int)(b * rgbMul[j * 3 + 2]);
139                         }
140                         i++;
141                 }
142         }
143
144         i = 0;
145         for (y = 0; y < PARTICLE_LIGHT_HEIGHT; y++)
146         {
147                 int yc = y - (PARTICLE_LIGHT_HEIGHT / 2);
148                 for (x = 0; x < PARTICLE_LIGHT_WIDTH; x++)
149                 {
150                         int xc = x - (PARTICLE_LIGHT_WIDTH / 2);
151
152                         float invDist = ((float)particleRadius - (float)sqrt(xc * xc + yc * yc)) / (float)particleRadius;
153                         if (invDist < 0.0f) invDist = 0.0f;
154
155                         c = (int)(pow(invDist, 0.75f) * 63);
156                         particleLight[i++] = ((c >> 1) << 11) | (c << 5) | (c >> 1);
157                 }
158         }
159
160         return 0;
161 }
162
163 static void destroy(void)
164 {
165 }
166
167 static void start(long trans_time)
168 {
169         startingTime = time_msec;
170 }
171
172 static void eraseArea(struct point *p, int width, int height)
173 {
174         int x, y, dx;
175         unsigned short *dst;
176
177         int x0 = p->x;
178         int y0 = p->y;
179         int x1 = p->x + width;
180         int y1 = p->y + height;
181
182         if (x0 < 0) x0 = 0;
183         if (y0 < 0) y0 = 0;
184         if (x1 > fb_width) x1 = fb_width;
185         if (y1 > fb_height) y1 = fb_height;
186
187         dx = x1 - x0;
188
189         dst = lightmap + y0 * fb_width + x0;
190
191         for (y = y0; y < y1; y++)
192         {
193                 for (x = x0; x < x1; x++)
194                 {
195                         *dst++ = 0;
196                 }
197                 dst += fb_width - dx;
198         }
199 }
200
201
202 static void renderLight(struct point *p, int width, int height, unsigned short *light)
203 {
204         int x, y, dx;
205         unsigned short *src, *dst;
206
207         int x0 = p->x;
208         int y0 = p->y;
209         int x1 = p->x + width;
210         int y1 = p->y + height;
211
212         int xl = 0;
213         int yl = 0;
214
215         if (x0 < 0)
216         {
217                 xl = -x0;
218                 x0 = 0;
219         }
220
221         if (y0 < 0)
222         {
223                 yl = -y0;
224                 y0 = 0;
225         }
226
227         if (x1 > fb_width) x1 = fb_width;
228         if (y1 > fb_height) y1 = fb_height;
229
230         dx = x1 - x0;
231
232         dst = lightmap + y0 * fb_width + x0;
233         src = light + yl * width + xl;
234
235         for (y = y0; y < y1; y++)
236         {
237                 for (x = x0; x < x1; x++)
238                 {
239                         *dst++ |= *src++;
240                 }
241                 dst += fb_width - dx;
242                 src += width - dx;
243         }
244 }
245
246 static void eraseLights()
247 {
248         int i;
249         for (i = 0; i < NUM_BIG_LIGHTS; i++)
250                 eraseArea(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT);
251 }
252
253 static void renderLights()
254 {
255         int i;
256         for (i = 0; i < NUM_BIG_LIGHTS; i++)
257                 renderLight(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT, bigLight[i]);
258 }
259
260 static void renderParticles()
261 {
262         int i;
263         for (i = 0; i < NUM_PARTICLES; i++)
264                 renderLight(&particlePoint[i], PARTICLE_LIGHT_WIDTH, PARTICLE_LIGHT_HEIGHT, particleLight);
265 }
266
267 static void animateLights()
268 {
269         struct point center;
270         float dt = (float)(time_msec - startingTime) / 1000.0f;
271         float tt = 1.0f - sin(dt);
272         float disperse = tt * 64.0f;
273
274         center.x = (fb_width >> 1) - (BIG_LIGHT_WIDTH / 2);
275         center.y = (fb_height >> 1) - (BIG_LIGHT_HEIGHT / 2);
276
277         bigLightPoint[0].x = center.x + sin(1.2f * dt) * (144.0f + disperse);
278         bigLightPoint[0].y = center.y + sin(0.8f * dt) * (148.0f + disperse);
279
280         bigLightPoint[1].x = center.x + sin(1.5f * dt) * (156.0f + disperse);
281         bigLightPoint[1].y = center.y + sin(1.1f * dt) * (92.0f + disperse);
282
283         bigLightPoint[2].x = center.x + sin(2.0f * dt) * (112.0f + disperse);
284         bigLightPoint[2].y = center.y + sin(1.3f * dt) * (136.0f + disperse);
285 }
286
287 static void renderBump(unsigned short *vram)
288 {
289         int i;
290         for (i = 0; i < fb_width * fb_height; i++)
291         {
292                 unsigned short c = lightmap[bumpOffset[i]];
293                 *vram++ = c;
294         }
295 }
296
297 static void animateParticles()
298 {
299         int i;
300         struct point center;
301         float dt = (float)(time_msec - startingTime) / 2000.0f;
302         float tt = sin(dt);
303
304         center.x = (fb_width >> 1) - (PARTICLE_LIGHT_WIDTH / 2);
305         center.y = (fb_height >> 1) - (PARTICLE_LIGHT_HEIGHT / 2);
306
307         for (i = 0; i < NUM_PARTICLES; i++)
308         {
309                 particlePoint[i].x = center.x + (sin(1.2f * (i*i*i + dt)) * 74.0f + sin(0.6f * (i + dt)) * 144.0f) * tt;
310                 particlePoint[i].y = center.y + (sin(1.8f * (i + dt)) * 68.0f + sin(0.5f * (i*i + dt)) * 132.0f) * tt;
311         }
312 }
313
314 static void draw(void)
315 {
316         memset(lightmap, 0, fb_width * fb_height * sizeof(*lightmap));
317
318         //eraseLights();
319         animateLights();
320         renderLights();
321
322         animateParticles();
323         renderParticles();
324
325         renderBump((unsigned short*)fb_pixels);
326
327         swap_buffers(0);
328 }