Debian Codegraph @

src/io/gfx/paletteeffects.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file paletteeffects.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created main.c
10 * - 22nd July 2008: Created util.c from parts of main.c
11 * - 3rd February 2009: Renamed main.c to main.cpp and util.c to util.cpp
12 * - 4th February 2009: Created palette.cpp from parts of main.cpp and util.cpp
13 * - 1st August 2009: Renamed palette.cpp to paletteeffects.cpp
14 *
15 * @par Licence:
16 * Copyright (c) 2005-2013 AJ Thomson
17 *
18 * OpenJazz is distributed under the terms of
19 * the GNU General Public License, version 2.0
20 *
21 */
22
23
24 #include "paletteeffects.h"
25 #include "video.h"
26
27 #include "jj1/level/jj1level.h"
28 #ifdef ENABLE_JJ2
29 #include "jj2/level/jj2level.h"
30 #endif
31 #include "level/levelplayer.h"
32 #include "player/player.h"
33
34 #include <string.h>
35
36 #ifdef OJ_SDL3
37 #include <SDL3/SDL.h>
38 #else
39 #include <SDL.h>
40 #endif
41
42 /**
43 * Create a new palette effect.
44 *
45 * @param nextPE The next palette effect
46 */
47 PaletteEffect::PaletteEffect (PaletteEffect* nextPE) {
48
49 next = nextPE;
50
51 }
52
53
54 /**
55 * Delete the palette effect.
56 */
57 PaletteEffect::~PaletteEffect () {
58
59 if (next) delete next;
60
61 }
62
63
64 /**
65 * Apply the palette effect.
66 *
67 * @param shownPalette The palette the effect will be applied to
68 * @param direct Whether or not to apply the effect directly
69 * @param mspf Ticks per frame
70 * @param isStatic Whether the effect should advance after applying
71 */
72 void PaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
73
74 // Apply the next palette effect
75 if (next) next->apply(shownPalette, direct, mspf, isStatic);
76
77 }
78
79
80 /**
81 * Create a new white-in palette effect.
82 *
83 * @param newDuration The length of time the effect will last
84 * @param nextPE The next palette effect
85 */
86 WhiteInPaletteEffect::WhiteInPaletteEffect
87 (int newDuration, PaletteEffect* nextPE) : PaletteEffect (nextPE) {
88
89 duration = newDuration;
90 whiteness = F1 + FH;
91
92 }
93
94
95 /**
96 * Apply the palette effect.
97 *
98 * @param shownPalette The palette the effect will be applied to
99 * @param direct Whether or not to apply the effect directly
100 * @param mspf Ticks per frame
101 * @param isStatic Whether the effect should advance after applying
102 */
103 void WhiteInPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
104
105 int count;
106
107 // Apply the next palette effect
108 if (next) next->apply(shownPalette, direct, mspf, isStatic);
109
110 if (whiteness > F1) {
111
112 memset(shownPalette, 255, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
113
114 if (!isStatic) whiteness -= ITOF(mspf) / duration;
115
116 } else if (whiteness > 0) {
117
118 for (count = 0; count < MAX_PALETTE_COLORS; count++) {
119
120 shownPalette[count].r = 255 -
121 FTOI((255 - shownPalette[count].r) * (F1 - whiteness));
122 shownPalette[count].g = 255 -
123 FTOI((255 - shownPalette[count].g) * (F1 - whiteness));
124 shownPalette[count].b = 255 -
125 FTOI((255 - shownPalette[count].b) * (F1 - whiteness));
126
127 }
128
129 if (!isStatic) whiteness -= ITOF(mspf) / duration;
130
131 }
132
133 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
134
135 }
136
137
138 /**
139 * Create a new fade-in palette effect.
140 *
141 * @param newDuration The length of time the effect will last
142 * @param nextPE The next palette effect
143 */
144 FadeInPaletteEffect::FadeInPaletteEffect
145 (int newDuration, PaletteEffect* nextPE) : PaletteEffect (nextPE) {
146
147 duration = newDuration;
148 blackness = F1 + FH;
149
150 }
151
152
153 /**
154 * Apply the palette effect.
155 *
156 * @param shownPalette The palette the effect will be applied to
157 * @param direct Whether or not to apply the effect directly
158 * @param mspf Ticks per frame
159 * @param isStatic Whether the effect should advance after applying
160 */
161 void FadeInPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
162
163 int count;
164
165 // Apply the next palette effect
166 if (next) next->apply(shownPalette, direct, mspf, isStatic);
167
168
169 if (blackness > F1) {
170
171 memset(shownPalette, 0, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
172
173 if (!isStatic) blackness -= ITOF(mspf) / duration;
174
175 } else if (blackness > 0) {
176
177 for (count = 0; count < MAX_PALETTE_COLORS; count++) {
178
179 shownPalette[count].r =
180 FTOI(shownPalette[count].r * (F1 - blackness));
181 shownPalette[count].g =
182 FTOI(shownPalette[count].g * (F1 - blackness));
183 shownPalette[count].b =
184 FTOI(shownPalette[count].b * (F1 - blackness));
185
186 }
187
188 if (!isStatic) blackness -= ITOF(mspf) / duration;
189
190 }
191
192 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
193
194 }
195
196
197 /**
198 * Create a new white-out palette effect.
199 *
200 * @param newDuration The length of time the effect will last
201 * @param nextPE The next palette effect
202 */
203 WhiteOutPaletteEffect::WhiteOutPaletteEffect
204 (int newDuration, PaletteEffect* nextPE) : PaletteEffect (nextPE) {
205
206 duration = newDuration;
207 whiteness = 0;
208
209 }
210
211
212 /**
213 * Apply the palette effect.
214 *
215 * @param shownPalette The palette the effect will be applied to
216 * @param direct Whether or not to apply the effect directly
217 * @param mspf Ticks per frame
218 * @param isStatic Whether the effect should advance after applying
219 */
220 void WhiteOutPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
221
222 // Apply the next palette effect
223 if (next) next->apply(shownPalette, direct, mspf, isStatic);
224
225
226 if (whiteness > F1) {
227
228 memset(shownPalette, 255, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
229
230 } else {
231
232 if (whiteness > 0) {
233
234 for (int i = 0; i < MAX_PALETTE_COLORS; i++) {
235
236 shownPalette[i].r = 255 -
237 FTOI((255 - shownPalette[i].r) * (F1 - whiteness));
238 shownPalette[i].g = 255 -
239 FTOI((255 - shownPalette[i].g) * (F1 - whiteness));
240 shownPalette[i].b = 255 -
241 FTOI((255 - shownPalette[i].b) * (F1 - whiteness));
242
243 }
244 }
245
246 if (!isStatic) whiteness += ITOF(mspf) / duration;
247
248 }
249
250 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
251
252 }
253
254
255 /**
256 * Create a new fade-out palette effect.
257 *
258 * @param newDuration The length of time the effect will last
259 * @param nextPE The next palette effect
260 */
261 FadeOutPaletteEffect::FadeOutPaletteEffect
262 (int newDuration, PaletteEffect* nextPE) : PaletteEffect (nextPE) {
263
264 duration = newDuration;
265 blackness = -(F2 + F1);
266
267 }
268
269
270 /**
271 * Apply the palette effect.
272 *
273 * @param shownPalette The palette the effect will be applied to
274 * @param direct Whether or not to apply the effect directly
275 * @param mspf Ticks per frame
276 * @param isStatic Whether the effect should advance after applying
277 */
278 void FadeOutPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
279
280 // Apply the next palette effect
281 if (next) next->apply(shownPalette, direct, mspf, isStatic);
282
283 if (blackness > F1) {
284
285 memset(shownPalette, 0, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
286
287 } else {
288
289 if (blackness > 0) {
290
291 for (int i = 0; i < MAX_PALETTE_COLORS; i++) {
292
293 shownPalette[i].r = FTOI(shownPalette[i].r * (F1 - blackness));
294 shownPalette[i].g = FTOI(shownPalette[i].g * (F1 - blackness));
295 shownPalette[i].b = FTOI(shownPalette[i].b * (F1 - blackness));
296
297 }
298
299 }
300
301 if (!isStatic) blackness += ITOF(mspf) / duration;
302
303 }
304
305 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
306
307 }
308
309
310 /**
311 * Create a new flash-to-colour palette effect.
312 *
313 * @param newRed The red component of the colour
314 * @param newGreen The green component of the colour
315 * @param newBlue The blue component of the colour
316 * @param newDuration The length of time the effect will last
317 * @param nextPE The next palette effect
318 */
319 FlashPaletteEffect::FlashPaletteEffect
320 (unsigned char newRed, unsigned char newGreen, unsigned char newBlue,
321 int newDuration, PaletteEffect* nextPE) :
322 PaletteEffect (nextPE) {
323
324 duration = newDuration;
325 progress = -F1;
326 red = newRed;
327 green = newGreen;
328 blue = newBlue;
329
330 }
331
332
333 /**
334 * Apply the palette effect.
335 *
336 * @param shownPalette The palette the effect will be applied to
337 * @param direct Whether or not to apply the effect directly
338 * @param mspf Ticks per frame
339 * @param isStatic Whether the effect should advance after applying
340 */
341 void FlashPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
342
343 int count;
344
345 // Apply the next palette effect
346 if (next) next->apply(shownPalette, direct, mspf, isStatic);
347
348 if (progress < 0) {
349
350 for (count = 0; count < MAX_PALETTE_COLORS; count++) {
351
352 shownPalette[count].r = FTOI((shownPalette[count].r * -progress) +
353 (red * (progress + F1)));
354 shownPalette[count].g = FTOI((shownPalette[count].g * -progress) +
355 (green * (progress + F1)));
356 shownPalette[count].b = FTOI((shownPalette[count].b * -progress) +
357 (blue * (progress + F1)));
358
359 }
360
361 if (!isStatic) progress += ITOF(mspf) / duration;
362
363 } else if (progress < F1) {
364
365 for (count = 0; count < MAX_PALETTE_COLORS; count++) {
366
367 shownPalette[count].r = FTOI((shownPalette[count].r * progress) +
368 (red * (F1 - progress)));
369 shownPalette[count].g = FTOI((shownPalette[count].g * progress) +
370 (green * (F1 - progress)));
371 shownPalette[count].b = FTOI((shownPalette[count].b * progress) +
372 (blue * (F1 - progress)));
373
374 }
375
376 if (!isStatic) progress += ITOF(mspf) / duration;
377
378 }
379
380 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
381
382 }
383
384
385 /**
386 * Create a new colour rotation palette effect.
387 *
388 * @param newFirst The first palette index to be affected
389 * @param newAmount The number of palette indices to be affected
390 * @param newSpeed The speed of he rotation
391 * @param nextPE The next palette effect
392 */
393 RotatePaletteEffect::RotatePaletteEffect
394 (unsigned char newFirst, int newAmount, fixed newSpeed, PaletteEffect * nextPE) :
395 PaletteEffect (nextPE) {
396
397 first = newFirst;
398 amount = newAmount;
399 speed = newSpeed;
400 position = 0;
401
402 }
403
404
405 /**
406 * Apply the palette effect.
407 *
408 * @param shownPalette The palette the effect will be applied to
409 * @param direct Whether or not to apply the effect directly
410 * @param mspf Ticks per frame
411 * @param isStatic Whether the effect should advance after applying
412 */
413 void RotatePaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
414
415 SDL_Color* currentPalette;
416 int count;
417
418 // Apply the next palette effect
419 if (next) next->apply(shownPalette, direct, mspf, isStatic);
420
421 currentPalette = video.getPalette();
422
423 for (count = 0; count < amount; count++) {
424
425 memcpy(shownPalette + first + count,
426 currentPalette + first +
427 ((count + FTOI(position)) % amount), sizeof(SDL_Color));
428
429 }
430
431 if (!isStatic) {
432
433 position -= (mspf * speed) >> 10;
434 while (position < 0) position += ITOF(amount);
435
436 }
437
438 if (direct) video.changePalette(shownPalette + first, first, amount);
439
440 }
441
442
443 /**
444 * Create a new parallaxing sky background palette effect.
445 *
446 * @param newFirst The first palette index to be affected
447 * @param newAmount The number of palette indices to be affected
448 * @param newSpeed The relative speed of the background
449 * @param newSkyPalette Palette containing the sky's colours
450 * @param nextPE The next palette effect
451 */
452 SkyPaletteEffect::SkyPaletteEffect
453 (unsigned char newFirst, int newAmount, fixed newSpeed,
454 SDL_Color* newSkyPalette, PaletteEffect* nextPE) :
455 PaletteEffect (nextPE) {
456
457 skyPalette = newSkyPalette;
458 first = newFirst;
459 amount = newAmount;
460 speed = newSpeed;
461
462 }
463
464
465 /**
466 * Apply the palette effect.
467 *
468 * @param shownPalette The palette the effect will be applied to
469 * @param direct Whether or not to apply the effect directly
470 * @param mspf Ticks per frame
471 * @param isStatic Whether the effect should advance after applying
472 */
473 void SkyPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
474
475 int position, count, y;
476
477 // Apply the next palette effect
478 if (next) next->apply(shownPalette, direct, mspf, isStatic);
479
480 position = viewY + ((canvasH - 33) << 9) - F4;
481 y = ((canvasH - 34) / 100) + 1;
482 count = (((position * speed) / y) >> 20) % 255;
483
484 if (direct) {
485
486 if (count > 255 - amount) {
487
488 video.changePalette(skyPalette + count, first, 255 - count);
489 video.changePalette(skyPalette, first + (255 - count), count + amount - 255);
490
491 } else {
492
493 video.changePalette(skyPalette + count, first, amount);
494
495 }
496
497 } else {
498
499 if (count > 255 - amount) {
500
501 memcpy(shownPalette + first, skyPalette + count,
502 sizeof(SDL_Color) * (255 - count));
503 memcpy(shownPalette + first + (255 - count), skyPalette,
504 sizeof(SDL_Color) * (count + amount - 255));
505
506 } else {
507
508 memcpy(shownPalette + first, skyPalette + count,
509 sizeof(SDL_Color) * amount);
510
511 }
512
513 }
514
515 }
516
517
518 /**
519 * Create a new 2D parallaxing background palette effect.
520 *
521 * @param newFirst The first palette index to be affected
522 * @param newAmount The number of palette indices to be affected
523 * @param newSpeed The relative speed of the background
524 * @param nextPE The next palette effect
525 */
526 P2DPaletteEffect::P2DPaletteEffect
527 (unsigned char newFirst, int newAmount, fixed newSpeed, PaletteEffect* nextPE) :
528 PaletteEffect (nextPE) {
529
530 first = newFirst;
531 amount = newAmount;
532 speed = newSpeed;
533
534 }
535
536
537 /**
538 * Apply the palette effect.
539 *
540 * @param shownPalette The palette the effect will be applied to
541 * @param direct Whether or not to apply the effect directly
542 * @param mspf Ticks per frame
543 * @param isStatic Whether the effect should advance after applying
544 */
545 void P2DPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
546
547 SDL_Color* currentPalette;
548 int count, x, y, j;
549
550 // Apply the next palette effect
551 if (next) next->apply(shownPalette, direct, mspf, isStatic);
552
553 currentPalette = video.getPalette();
554 x = FTOI(((256 * 32) - FTOI(viewX)) * speed);
555 y = FTOI(((64 * 32) - FTOI(viewY)) * speed);
556
557 for (count = 0; count < amount >> 3; count++) {
558
559 for (j = 0; j < 8; j++) {
560
561 memcpy(shownPalette + first + (count << 3) + j,
562 currentPalette + first + (((count + y) % 8) << 3) +
563 ((j + x) % 8), sizeof(SDL_Color));
564
565 }
566
567 }
568
569 if (direct) video.changePalette(shownPalette + first, first, amount);
570
571 }
572
573
574 /**
575 * Create a new 1D parallaxing background palette effect.
576 *
577 * @param newFirst The first palette index to be affected
578 * @param newAmount The number of palette indices to be affected
579 * @param newSpeed The relative speed of the background
580 * @param nextPE The next palette effect
581 */
582 P1DPaletteEffect::P1DPaletteEffect
583 (unsigned char newFirst, int newAmount, fixed newSpeed, PaletteEffect* nextPE) :
584 PaletteEffect (nextPE) {
585
586 first = newFirst;
587 amount = newAmount;
588 speed = newSpeed;
589
590 }
591
592
593 /**
594 * Apply the palette effect.
595 *
596 * @param shownPalette The palette the effect will be applied to
597 * @param direct Whether or not to apply the effect directly
598 * @param mspf Ticks per frame
599 * @param isStatic Whether the effect should advance after applying
600 */
601 void P1DPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
602
603 SDL_Color* currentPalette;
604 fixed position;
605
606 // Apply the next palette effect
607 if (next) next->apply(shownPalette, direct, mspf, isStatic);
608
609 currentPalette = video.getPalette();
610 position = viewX + viewY;
611
612 for (int i = 0; i < amount; i++) {
613
614 memcpy(shownPalette + first + i,
615 currentPalette + first + ((i +
616 (amount - 1 - (FTOI(MUL(position, speed)) % amount))) % amount),
617 sizeof(SDL_Color));
618
619 }
620
621 if (direct) video.changePalette(shownPalette + first, first, amount);
622
623 }
624
625
626 /**
627 * Create a new water palette effect.
628 *
629 * @param newDepth Water depth
630 * @param nextPE The next palette effect
631 */
632 WaterPaletteEffect::WaterPaletteEffect (fixed newDepth, PaletteEffect* nextPE)
633 : PaletteEffect (nextPE) {
634
635 depth = newDepth;
636
637 }
638
639
640 /**
641 * Apply the palette effect.
642 *
643 * @param shownPalette The palette the effect will be applied to
644 * @param direct Whether or not to apply the effect directly
645 * @param mspf Ticks per frame
646 * @param isStatic Whether the effect should advance after applying
647 */
648 void WaterPaletteEffect::apply (SDL_Color* shownPalette, bool direct, int mspf, bool isStatic) {
649
650 SDL_Color* currentPalette;
651 int position;
652
653 // Apply the next palette effect
654 if (next) next->apply(shownPalette, direct, mspf, isStatic);
655
656 currentPalette = video.getPalette();
657
658 if (level) position = localPlayer->getLevelPlayer()->getY() - level->getWaterLevel();
659 #ifdef ENABLE_JJ2
660 else if (jj2Level) position = localPlayer->getLevelPlayer()->getY() - jj2Level->getWaterLevel();
661 #endif
662 else return;
663
664 if (position <= 0) return;
665
666 if (position < depth) {
667
668 for (int i = 0; i < MAX_PALETTE_COLORS; i++) {
669
670 shownPalette[i].r = FTOI(currentPalette[i].r * (1023 - DIV(position, depth)));
671 shownPalette[i].g = FTOI(currentPalette[i].g * (1023 - DIV(position, depth)));
672 shownPalette[i].b = FTOI(currentPalette[i].b * (1023 - DIV(position, depth)));
673
674 }
675
676 } else memset(shownPalette, 0, sizeof(SDL_Color) * MAX_PALETTE_COLORS);
677
678 if (direct) video.changePalette(shownPalette, 0, MAX_PALETTE_COLORS);
679
680 }