Debian Codegraph @

plugins/actor/corona/corona.cpp

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2000, 2001 Richard Ashburn <[email protected]>
4 *
5 * Authors: Richard Ashburn <[email protected]>
6 * Jean-Christophe Hoelt <[email protected]>
7 * Dennis Smit <[email protected]>
8 *
9 * $Id: corona.cpp,v 1.10 2006/02/05 18:47:26 synap Exp $
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /////////////////////////////////////////////////////////////////////////////
27 //
28 // Corona.cpp : Implementation of CCorona
29 //
30 /////////////////////////////////////////////////////////////////////////////
31
32 #include <libvisual/libvisual.h>
33
34 #include "corona.h"
35 #include <stdio.h>
36 #include <cstdlib>
37 #include <cmath>
38 using namespace std;
39
40
41 /////////////////////////////////////////////////////////////////////////////
42 // Corona::Corona
43 // Constructor
44
45 Corona::Corona()
46 {
47 m_clrForeground = 0x0000FF;
48 m_swirltime = 0;
49 m_testing = false;
50 m_silent = false;
51 m_avg = 1;
52 m_oldval = 0;
53 m_pos = 0;
54
55 m_nPreset = PRESET_CORONA;
56
57 m_image = 0;
58 m_real_image = 0;
59 m_deltafield = 0;
60 m_width = -1;
61 m_height = -1;
62 m_real_height = -1;
63 nbParticules = 1000;
64 m_reflArray = 0;
65 m_waveloop = 0.0;
66
67 m_particles = (Particle*)calloc (nbParticules, sizeof(Particle));
68 // Create particles in random positions
69 for (int i = nbParticules - 1; i >= 0; --i)
70 {
71 Particle *it = m_particles + i;
72 it->x = random(0, 1);
73 it->y = random(0, 1);
74 it->xvel = it->yvel = 0;
75 }
76 // Set up the background swirling effect
77 chooseRandomSwirl();
78 }
79
80 /////////////////////////////////////////////////////////////////////////////
81 // Corona::~Corona
82 // Destructor
83
84 Corona::~Corona()
85 {
86 if (m_real_image) free(m_real_image);
87 if (m_deltafield) free(m_deltafield);
88 }
89
90 double Corona::random(double min, double max) const {
91 return rand() * (max - min) / RAND_MAX + min;
92 }
93
94 bool Corona::setUpSurface(int width, int height) {
95 // Delete any image that might have previously been allocated
96 if (m_real_image) free(m_real_image);
97 if (m_deltafield) free(m_deltafield);
98 if (m_reflArray) free(m_reflArray);
99
100 // Fill in the size details in the BitmapInfo structure
101 m_width = width;
102 m_height = (height*4) / 5;
103 m_real_height = height;
104
105 // Allocate the image data
106 m_real_image = (unsigned char *)calloc(1,width*height);
107 if (m_real_image == 0) return false;
108 m_image = m_real_image + m_width * (m_real_height - m_height);
109 m_reflArray = (int*)malloc((m_real_height - m_height) * sizeof(*m_reflArray));
110
111 // Allocate the delta-field memory, and initialise it
112 m_deltafield = (unsigned char**)malloc(m_width * m_height * sizeof(unsigned char*));
113
114 for (int x = 0; x < m_width; ++x) {
115 for (int y = 0; y < m_height; ++y) {
116 setPointDelta(x, y);
117 }
118 }
119
120 // Change the number of particles
121 int newsize = (int) (::sqrt(m_width * m_height) * 3.0);
122 if (newsize < 2000) newsize = 2000;
123 int oldsize = (int) nbParticules;
124 nbParticules = newsize;
125 m_particles = (Particle*)realloc (m_particles, sizeof(Particle) * newsize);
126 for (int i = oldsize; i < newsize; ++i) {
127 m_particles[i].x = random(0, 1);
128 m_particles[i].y = random(0, 1);
129 m_particles[i].xvel = m_particles[i].yvel = 0;
130 }
131
132 return true;
133 }
134
135 void Corona::drawLine(int x0, int y0, int x1, int y1, unsigned char col)
136 {
137 int incx = (x1 > x0) ? 1 : -1;
138 int incy = (y1 > y0) ? m_width : -m_width;
139 int dincx = 2 * abs(y1 - y0);
140 int dincy = 2 * abs(x1 - x0);
141 unsigned char* p = &(m_image[x0 + y0 * m_width]);
142 unsigned char* const end = &(m_image[m_width + (m_height - 1) * m_width]);
143 unsigned char* const start = m_image;
144 int n, d; // n is the "pixel counter"
145
146 // Always draw at least one pixel
147 if (start <= p && p < end) *p = col;
148
149 if (abs(x1 - x0) > abs(y1 - y0)) {
150 d = x0 - x1;
151 for (n = abs(x1 - x0); n > 0; --n, p += incx) {
152 if (start <= p && p < end) *p = col;
153 d += dincx;
154 if (d > 0) {
155 p += incy;
156 d -= dincy;
157 }
158 }
159 }
160 else {
161 d = y0 - y1;
162 for (n = abs(y1 - y0); n > 0; --n, p += incy) {
163 if (start <= p && p < end) *p = col;
164 d += dincy;
165 if (d > 0) {
166 p += incx;
167 d -= dincx;
168 }
169 }
170 }
171 }
172
173
174 void Corona::chooseRandomSwirl()
175 {
176 m_swirl.x = random(0.2, 0.8);
177 m_swirl.y = random(0.2, 0.8);
178 m_swirl.tightness = random(-0.01, 0.01);
179 m_swirl.pull = random(1.0, 1.04);
180 }
181
182 // FIXME: inline, optimize, simd
183 void Corona::setPointDelta(int x, int y)
184 {
185 /* FIXME calculate using fixed point ? */
186 double tx = ((double) x / m_width) - m_swirl.x;
187 double ty = ((double) y / m_height) - m_swirl.y;
188 double d = tx * tx + ty * ty;
189 double ds = ::sqrt(d);
190 double ang = atan2(ty, tx) + m_swirl.tightness / (d + 0.01);
191 int dx = (int) ((ds * m_swirl.pull * cos(ang) - tx) * m_width) + rand() % 5 - 2;
192 int dy = (int) ((ds * m_swirl.pull * sin(ang) - ty) * m_height) + rand() % 5 - 2;
193 if (x + dx < 0) dx = -dx - x;
194 if (x + dx >= m_width) dx = 2 * m_width - 2 * x - dx - 1;
195 if (y + dy < 0) dy = -dy - y;
196 if (y + dy >= m_height) dy = 2 * m_height - 2 * y - dy - 1;
197 m_deltafield[x + y * m_width] = &(m_image[x + dx + (y + dy) * m_width]);
198 }
199
200 void Corona::applyDeltaField(bool heavy)
201 {
202 /* FIXME MMXify */
203 if (heavy) {
204 for (int y = 0; y < m_height; ++y) {
205 unsigned char *s = &(m_image[y * m_width]);
206 unsigned char **p = &(m_deltafield[y * m_width]);
207 for (int x = 0; x < m_width; ++x, ++s, ++p) {
208 *s = (*s + *(*p)) >> 1;
209 if (*s >= 2) *s -= 2;
210 }
211 }
212 }
213 else {
214 for (int y = 0; y < m_height; ++y) {
215 unsigned char *s = &(m_image[y * m_width]);
216 unsigned char **p = &(m_deltafield[y * m_width]);
217 for (int x = 0; x < m_width; ++x, ++s, ++p) {
218 *s = (*s + **p) >> 1;
219 if (*s >= 1) *s -= 1;
220 }
221 }
222 }
223 }
224
225 int Corona::getBeatVal(TimedLevel *tl)
226 {
227 int total = 0;
228 for (int i = 50; i < 250; ++i) {
229 int n = tl->frequency[0][i];
230 total += n;
231 }
232 total /= 3;
233
234 m_avg = 0.9 * m_avg + 0.1 * total;
235 if (m_avg < 1000) m_avg = 1000;
236
237 if (total > m_avg * 1.2 && tl->timeStamp - tl->lastbeat > 750) {
238 m_avg = total;
239 tl->lastbeat = tl->timeStamp;
240 if (total > 2500) return 2500;
241 else return total;
242 }
243 else return 0;
244 }
245
246 void Corona::drawParticules()
247 {
248 int p;
249 for (p = 0; p < nbParticules; ++p) {
250 Particle *it = m_particles + p;
251 int x = (int) (it->x * m_width);
252 int y = (int) (it->y * m_height);
253 int xv = (int) (it->xvel * m_width);
254 int yv = (int) (it->yvel * m_height);
255 drawLine(x, y, x - xv, y - yv, 255);
256 }
257 }
258
259 void Corona::drawParticulesWithShift()
260 {
261 int p;
262 for (p = 0; p < nbParticules; ++p) {
263 Particle *it = m_particles + p;
264 int x = (int) (it->x * m_width);
265 int y = (int) (it->y * m_height);
266 int xv = (int) (it->xvel * m_width);
267 int yv = (int) (it->yvel * m_height);
268 double l = (xv * xv + yv * yv);
269 if (l > 10.0 * 10.0) {
270 l = ::sqrt(l);
271 double dl = 10 / (l + 0.01);
272 xv = (int) (xv * dl);
273 yv = (int) (yv * dl);
274 }
275 drawLine(x, y, x - xv, y - yv, 255);
276 }
277 }
278
279 void Corona::getAvgParticlePos(double& x, double& y) const
280 {
281 x = y = 0;
282 for (int i = 0; i < 10; ++i) {
283 int r = rand() % nbParticules;
284 x += m_particles[r].x;
285 y += m_particles[r].y;
286 }
287 x /= 10;
288 y /= 10;
289 }
290
291 #define REFL_MIN_WIDTH 3.0
292 #define REFL_INC_WIDTH 0.08
293
294 void Corona::genReflectedWaves(double loop)
295 {
296 double fdec = 0.0;
297 double floop = 0.0;
298 double fwidth = (m_real_height - m_height) * REFL_INC_WIDTH + REFL_MIN_WIDTH;
299 double REFL_MAX_WIDTH = fwidth;
300
301 // int *reflArray;
302 // reflArray = new int[(m_real_height - m_height) + m_width];
303
304 for (int i = 0; i < m_real_height - m_height; ++i)
305 {
306 double fincr = (3.1415 / 2.0) * (1.0 - (fwidth - REFL_MIN_WIDTH) / REFL_MAX_WIDTH);
307 floop += fincr;
308
309 fwidth -= REFL_INC_WIDTH;
310 fdec = fwidth * sin(floop + loop);
311 m_reflArray[i] = (int)fdec;
312 }
313 }
314
315 void Corona::drawReflected()
316 {
317 genReflectedWaves(m_waveloop);
318 int offsetDest = (m_real_height - m_height - 1) * m_width;
319 int offsetSrc = (m_real_height - m_height) * m_width;
320
321 for (int i = m_real_height - m_height; i--;)
322 {
323 int idec = m_reflArray[i];
324
325 for (int x = m_width; x--;)
326 {
327 int out = m_real_image[(offsetSrc++) + idec];
328 m_real_image[offsetDest++] = out;
329 }
330
331 offsetDest -= m_width * 2;
332 offsetSrc += m_width;
333 }
334 }
335
336 void Corona::blurImage()
337 {
338 uint8_t *ptr = m_real_image + m_width;
339 int n = (m_real_height - 2) * m_width;
340
341 if (visual_cpu_get_mmx ()) {
342 #if defined(VISUAL_ARCH_X86) || defined(VISUAL_ARCH_X86_64)
343 __asm __volatile
344 ("pxor %%mm6, %%mm6"
345 ::);
346
347 while (n -= 4) {
348 __asm __volatile
349 ("\n\t movd %[dest], %%mm0"
350 "\n\t movd %[pix2], %%mm1"
351 "\n\t punpcklbw %%mm6, %%mm0"
352 "\n\t movd %[pix3], %%mm2"
353 "\n\t punpcklbw %%mm6, %%mm1"
354 "\n\t movd %[pix4], %%mm3"
355 "\n\t punpcklbw %%mm6, %%mm2"
356 "\n\t movd %[pix5], %%mm4"
357 "\n\t punpcklbw %%mm6, %%mm3"
358 "\n\t paddw %%mm1, %%mm2"
359 "\n\t punpcklbw %%mm6, %%mm4"
360 "\n\t paddw %%mm3, %%mm2"
361 "\n\t paddw %%mm2, %%mm4"
362 "\n\t psrlw $2, %%mm4"
363 "\n\t packuswb %%mm6, %%mm4"
364 "\n\t movd %%mm4, %[dest]"
365 :: [dest] "m" (*(ptr))
366 , [pix2] "m" (*(ptr - m_width))
367 , [pix3] "m" (*(ptr - 1))
368 , [pix4] "m" (*(ptr + 1))
369 , [pix5] "m" (*(ptr + m_width)));
370 ptr += 4;
371 }
372
373 __asm __volatile
374 ("\n\t emms");
375 #endif
376 } else {
377 while (n--) {
378 int val = *(ptr + 1);
379 val += *(ptr - 1);
380 val += *(ptr - m_width);
381 val += *(ptr + m_width);
382 val >>= 2;
383
384 *(ptr++) = val;
385 }
386 }
387 }
388
389 void Corona::update(TimedLevel *pLevels)
390 {
391 // Check for a beat
392 int beatval = getBeatVal(pLevels);
393 if (beatval > 1000)
394 {
395 int total = 0;
396 for (int i = 0; i < 512; ++i)
397 total += 2 * pLevels->frequency[0][i];
398 double currval = 1.0 - exp(-total / 40000.0);
399 m_oldval = (m_oldval + currval) / 2.0;
400
401 double tx, ty;
402 getAvgParticlePos(tx, ty);
403 // If most of the particles are low down, use a launch
404 if (ty < 0.2 && rand() % 4 != 0) {
405 int p;
406 double bv = m_oldval * 5.0;
407 for (p = 0; p < nbParticules; ++p)
408 {
409 Particle *it = m_particles + p;
410 if (it->y < 0.1) {
411 double x = (it->x - tx) / bv;
412 it->yvel += 0.01 * bv * exp(-1000.0 * x * x);
413 }
414 }
415 }
416 else
417 { // Otherwise use a swirl
418 tx += random(-0.1, 0.1);
419 ty += random(-0.1, 0.1);
420 double bv = 0.009 * m_oldval;
421 double bv2 = 0.0036 * m_oldval;
422 if (rand() % 2 == 0) bv = -bv;
423 m_movement.x = tx;
424 m_movement.y = ty;
425 m_movement.tightness = random(0.8 * bv, bv);
426 m_movement.pull = random(1 - bv2, 1 - 0.2 * bv2);
427 m_swirltime = 1;
428 }
429
430 pLevels->lastbeat = pLevels->timeStamp;
431 }
432
433 // Deal with the particles
434 int p;
435 for (p = 0; p < nbParticules; ++p)
436 {
437 Particle *it = m_particles + p;
438 // Apply gravity
439 it->yvel -= 0.0006; // the gravity value
440
441 // If there's an active swirl, swirl around it
442 if (m_swirltime > 0) {
443 double dx = it->x - m_movement.x;
444 double dy = it->y - m_movement.y;
445 double d = dx * dx + dy * dy;
446 double ds = ::sqrt(d);
447 double ang = atan2(dy, dx) + m_movement.tightness / (d + 0.01);
448 it->xvel += (ds * m_movement.pull * cos(ang) - dx);
449 it->yvel += (ds * m_movement.pull * sin(ang) - dy);
450 }
451
452 // Gitter
453 it->xvel += random(-0.0002, 0.0002);
454 it->yvel += random(-0.0002, 0.0002);
455
456 // Clamp the velocity
457 if (it->xvel < -0.1 ) it->xvel = -0.1;
458 if (it->xvel > 0.1 ) it->xvel = 0.1;
459 if (it->yvel < -0.1 ) it->yvel = -0.1;
460 if (it->yvel > 0.1 ) it->yvel = 0.1;
461
462 // Randomly move the particle once in a while
463 if (rand() % (nbParticules / 5) == 0)
464 {
465 it->x = random(0, 1);
466 it->y = random(0, 1);
467 it->xvel = it->yvel = 0;
468 }
469
470 // Move and bounce the particle
471 it->x += it->xvel;
472 it->y += it->yvel;
473 if (it->x < 0) { it->x = -it->x; it->xvel *= -0.25; it->yvel *= 0.25; }
474 if (it->y < 0) { it->y = -it->y; it->xvel *= 0.25; it->yvel *= -0.25; }
475 if (it->x > 1) { it->x = 2.0 - it->x; it->xvel *= -0.25; it->yvel *= 0.25; }
476 if (it->y > 1) { it->y = 2.0 - it->y; it->xvel *= 0.25; it->yvel = 0; }
477 }
478
479 if (m_swirltime > 0) --m_swirltime;
480
481 // Randomly change the delta field
482 if (rand() % 200 == 0) chooseRandomSwirl();
483
484 // Animate the waves
485 m_waveloop += 0.6;
486
487 // drawing.
488 if (m_image != 0)
489 {
490 // Draw the particles on the bitmap
491 drawParticules();
492
493 // Apply the deltafield and update a few of its points
494 applyDeltaField(m_nPreset == PRESET_BLAZE && m_width * m_height < 150000);
495
496 int n = (m_width * m_height) / 100;
497 for (int i = 0; i < n; ++i)
498 setPointDelta(rand() % m_width, rand() % m_height);
499
500 // If on the blaze preset, draw the particles again
501 if (m_nPreset == PRESET_BLAZE)
502 drawParticules();
503
504 drawReflected();
505
506 // Blur the bitmap
507 blurImage();
508
509 // If on the blaze preset, draw the particles one last time
510 if (m_nPreset == PRESET_BLAZE)
511 drawParticulesWithShift();
512 }
513 }
514