Use a more natural guard for the callback definition
[kugel-rb.git] / apps / plugins / fireworks.c
blob263c05792be5201edbd1d560f41ddcdc621f83fd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Zakk Roberts
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/helper.h"
23 #include "lib/playback_control.h"
25 PLUGIN_HEADER
27 /***
28 * FIREWORKS.C by ZAKK ROBERTS
29 * Rockbox plugin simulating a fireworks display.
30 * Supports all bitmap LCDs, fully scalable.
31 * Currently disabled for Archos Recorder - runs too slow.
32 ***/
34 /* All sorts of keymappings.. */
35 #if (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
36 #define BTN_MENU BUTTON_OFF
37 #define BTN_FIRE BUTTON_SELECT
39 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
40 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
41 #define BTN_MENU BUTTON_MENU
42 #define BTN_FIRE BUTTON_SELECT
44 #elif (CONFIG_KEYPAD == RECORDER_PAD)
45 #define BTN_MENU BUTTON_OFF
46 #define BTN_FIRE BUTTON_PLAY
48 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
49 #define BTN_MENU BUTTON_OFF
50 #define BTN_FIRE BUTTON_SELECT
52 #elif (CONFIG_KEYPAD == ONDIO_PAD)
53 #define BTN_MENU BUTTON_MENU
54 #define BTN_FIRE BUTTON_UP
56 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
57 #define BTN_MENU BUTTON_POWER
58 #define BTN_FIRE BUTTON_SELECT
60 #elif (CONFIG_KEYPAD == IRIVER_IFP7XX_PAD)
61 #define BTN_MENU BUTTON_MODE
62 #define BTN_FIRE BUTTON_SELECT
64 #elif (CONFIG_KEYPAD == GIGABEAT_PAD) || \
65 (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \
66 (CONFIG_KEYPAD == MROBE100_PAD)
67 #define BTN_MENU BUTTON_MENU
68 #define BTN_FIRE BUTTON_SELECT
70 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
71 (CONFIG_KEYPAD == SANSA_C200_PAD)
72 #define BTN_MENU BUTTON_POWER
73 #define BTN_FIRE BUTTON_SELECT
75 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
76 #define BTN_MENU (BUTTON_HOME|BUTTON_REPEAT)
77 #define BTN_FIRE BUTTON_SELECT
79 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
80 #define BTN_MENU BUTTON_POWER
81 #define BTN_FIRE BUTTON_PLAY
83 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
84 #define BTN_MENU BUTTON_RC_REC
85 #define BTN_FIRE BUTTON_RC_PLAY
87 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
88 #define BTN_MENU (BUTTON_MENU|BUTTON_REL)
90 #elif CONFIG_KEYPAD == IAUDIO67_PAD
91 #define BTN_MENU BUTTON_MENU
92 #define BTN_FIRE BUTTON_PLAY
94 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
95 #define BTN_MENU BUTTON_MENU
96 #define BTN_FIRE BUTTON_SELECT
98 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
99 #define BTN_MENU BUTTON_MENU
100 #define BTN_FIRE BUTTON_SELECT
102 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
103 #define BTN_MENU BUTTON_MENU
104 #define BTN_FIRE BUTTON_PLAY
106 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
107 #define BTN_MENU (BUTTON_MENU|BUTTON_REL)
109 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
110 #define BTN_MENU BUTTON_LEFT
111 #define BTN_FIRE BUTTON_PLAY
113 #elif (CONFIG_KEYPAD == PBELL_VIBE500_PAD)
114 #define BTN_MENU BUTTON_MENU
115 #define BTN_FIRE BUTTON_OK
117 #elif defined(HAVE_TOUCHSCREEN)
118 /* This is a touchscreen target */
119 #else
120 #error No keymap defined!
121 #endif
123 #ifdef HAVE_TOUCHSCREEN
124 #ifndef BTN_MENU
125 #define BTN_MENU (BUTTON_TOPLEFT|BUTTON_REL)
126 #endif
127 #ifndef BTN_FIRE
128 #define BTN_FIRE BUTTON_CENTER
129 #endif
130 #endif
132 /* The lowdown on source terminology:
133 * a ROCKET is launched from the LCD bottom.
134 * FIREWORKs are ejected from the rocket when it explodes. */
136 #define MAX_ROCKETS 40
137 #define ROCKET_LIFE (LCD_HEIGHT/2)
138 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
139 #define ROCKET_SIZE 2
140 #define ROCKET_MOVEMENT_RANGE 4
141 #define ROCKET_TRAIL_PARTICLES 50
143 #define MAX_FIREWORKS 40
144 #define FIREWORK_MOVEMENT_RANGE 6
145 #define FIREWORK_SIZE 2
147 /* position, speed, "phase" (age), color of all fireworks */
148 int firework_xpoints[MAX_ROCKETS][MAX_FIREWORKS];
149 int firework_ypoints[MAX_ROCKETS][MAX_FIREWORKS];
150 int firework_xspeed[MAX_ROCKETS][MAX_FIREWORKS];
151 int firework_yspeed[MAX_ROCKETS][MAX_FIREWORKS];
152 int firework_phase[MAX_ROCKETS];
153 #ifdef HAVE_LCD_COLOR
154 int firework_color[MAX_ROCKETS][MAX_FIREWORKS];
155 #endif
157 /* position, speed, "phase" (age) of all rockets */
158 int rocket_xpos[MAX_ROCKETS];
159 int rocket_ypos[MAX_ROCKETS];
160 int rocket_xspeed[MAX_ROCKETS];
161 int rocket_yspeed[MAX_ROCKETS];
162 int rocket_phase[MAX_ROCKETS];
163 int rocket_targetphase[MAX_ROCKETS];
165 /* settings values. these should eventually be saved to
166 * disk. maybe a preset loading/saving system? */
167 int autofire_delay = 0;
168 int particles_per_firework = 2;
169 int particle_life = 1;
170 int gravity = 1;
171 int show_rockets = 1;
172 int frames_per_second = 4;
173 bool quit_plugin = false;
175 /* firework colors:
176 * firework_colors = brightest firework color, used most of the time.
177 * DARK colors = fireworks are nearly burnt out.
178 * DARKER colors = fireworks are several frames away from burning out.
179 * DARKEST colors = fireworks are a couple frames from burning out. */
180 #ifdef HAVE_LCD_COLOR
181 static const unsigned firework_colors[] = {
182 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
183 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
184 LCD_RGBPACK(151,84,213) };
186 static const unsigned firework_dark_colors[] = {
187 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
188 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
189 LCD_RGBPACK(75,42,105) };
191 static const unsigned firework_darker_colors[] = {
192 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
193 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
194 LCD_RGBPACK(38,21,52) };
196 static const unsigned firework_darkest_colors[] = {
197 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
198 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
199 LCD_RGBPACK(19,10,26) };
201 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
203 #endif
205 static const struct opt_items autofire_delay_settings[15] = {
206 { "Off", -1 },
207 { "50ms", -1 },
208 { "100ms", -1 },
209 { "200ms", -1 },
210 { "300ms", -1 },
211 { "400ms", -1 },
212 { "500ms", -1 },
213 { "600ms", -1 },
214 { "700ms", -1 },
215 { "800ms", -1 },
216 { "900ms", -1 },
217 { "1s", -1 },
218 { "2s", -1 },
219 { "3s", -1 },
220 { "4s", -1 }
223 int autofire_delay_values[15] = {
224 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
226 static const struct opt_items particle_settings[8] = {
227 { "5", -1 },
228 { "10", -1 },
229 { "15", -1 },
230 { "20", -1 },
231 { "25", -1 },
232 { "30", -1 },
233 { "35", -1 },
234 { "40", -1 },
237 int particle_values[8] = {
238 5, 10, 15, 20, 25, 30, 35, 40 };
240 static const struct opt_items particle_life_settings[9] = {
241 { "20 cycles", -1 },
242 { "30 cycles", -1 },
243 { "40 cycles", -1 },
244 { "50 cycles", -1 },
245 { "60 cycles", -1 },
246 { "70 cycles", -1 },
247 { "80 cycles", -1 },
248 { "90 cycles", -1 },
249 { "100 cycles", -1 }
252 int particle_life_values[9] = {
253 20, 30, 40, 50, 60, 70, 80, 90, 100 };
255 static const struct opt_items gravity_settings[4] = {
256 { "Off", -1 },
257 { "Weak", -1 },
258 { "Moderate", -1 },
259 { "Strong", -1 },
262 int gravity_values[4] = {
263 0, 30, 20, 10 };
265 #ifdef HAVE_LCD_COLOR
267 static const struct opt_items rocket_settings[3] = {
268 { "No", -1 },
269 { "Yes (no trails)", -1 },
270 { "Yes (with trails)", -1 },
272 int rocket_values[4] = {
273 2, 1, 0 };
275 #else
277 static const struct opt_items rocket_settings[2] = {
278 { "No", -1 },
279 { "Yes", -1 },
281 int rocket_values[4] = {
282 1, 0 };
284 #endif
286 static const struct opt_items fps_settings[9] = {
287 { "20 FPS", -1 },
288 { "25 FPS", -1 },
289 { "30 FPS", -1 },
290 { "35 FPS", -1 },
291 { "40 FPS", -1 },
292 { "45 FPS", -1 },
293 { "50 FPS", -1 },
294 { "55 FPS", -1 },
295 { "60 FPS", -1 }
298 int fps_values[9] = {
299 20, 25, 30, 35, 40, 45, 50, 55, 60 };
301 MENUITEM_STRINGLIST(menu, "Fireworks Menu", NULL,
302 "Start Demo", "Auto-Fire", "Particles Per Firework",
303 "Particle Life", "Gravity", "Show Rockets",
304 "FPS (Speed)", "Playback Control", "Quit");
306 /* called on startup. initializes all variables, etc */
307 void init_all(void)
309 int j;
311 for(j=0; j<MAX_ROCKETS; j++)
313 rocket_phase[j] = -1;
314 firework_phase[j] = -1;
318 /* called when a rocket hits its destination height.
319 * prepares all associated fireworks to be expelled. */
320 void init_explode(int x, int y, int firework, int points)
322 int i;
324 for(i=0; i<points; i++)
326 rb->srand(*rb->current_tick * i);
328 firework_xpoints[firework][i] = x;
329 firework_ypoints[firework][i] = y;
331 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE)
332 - FIREWORK_MOVEMENT_RANGE/2;
333 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE)
334 - FIREWORK_MOVEMENT_RANGE/2;
336 #ifdef HAVE_LCD_COLOR
337 firework_color[firework][i] = rb->rand() % 7;
338 #endif
342 /* called when a rocket is launched.
343 * prepares said rocket to start moving towards its destination. */
344 void init_rocket(int rocket)
346 rb->srand(*rb->current_tick);
348 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
349 rocket_ypos[rocket] = LCD_HEIGHT;
351 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE)
352 - ROCKET_MOVEMENT_RANGE/2;
353 rocket_yspeed[rocket] = 3;
355 rocket_phase[rocket] = 0;
356 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR))
357 / rocket_yspeed[rocket];
360 /* startup/configuration menu. */
361 void fireworks_menu(void)
363 int selected = 0, result;
364 bool menu_quit = false;
366 rb->lcd_setfont(FONT_UI);
367 #ifdef HAVE_LCD_COLOR
368 rb->lcd_set_background(LCD_BLACK);
369 rb->lcd_set_foreground(LCD_WHITE);
370 #endif
371 rb->lcd_clear_display();
372 rb->lcd_update();
374 rb->button_clear_queue();
376 while(!menu_quit)
378 result = rb->do_menu(&menu, &selected, NULL, false);
380 switch(result)
382 case 0:
383 rb->lcd_setfont(FONT_SYSFIXED);
385 #ifdef HAVE_LCD_COLOR
386 rb->lcd_set_background(LCD_BLACK);
387 rb->lcd_set_foreground(LCD_WHITE);
388 #endif
390 rb->lcd_clear_display();
391 rb->lcd_update();
393 init_all();
394 menu_quit = true;
395 break;
397 case 1:
398 rb->set_option("Auto-Fire", &autofire_delay, INT,
399 autofire_delay_settings, 15, NULL);
400 break;
402 case 2:
403 rb->set_option("Particles Per Firework", &particles_per_firework,
404 INT, particle_settings, 8, NULL);
405 break;
407 case 3:
408 rb->set_option("Particle Life", &particle_life, INT,
409 particle_life_settings, 9, NULL);
410 break;
412 case 4:
413 rb->set_option("Gravity", &gravity, INT,
414 gravity_settings, 4, NULL);
415 break;
417 case 5:
418 rb->set_option("Show Rockets", &show_rockets, INT,
419 rocket_settings, 3, NULL);
420 break;
422 case 6:
423 rb->set_option("FPS (Speed)", &frames_per_second, INT,
424 fps_settings, 9, NULL);
425 break;
427 case 7:
428 playback_control(NULL);
429 break;
431 case 8:
432 quit_plugin = true;
433 menu_quit = true;
434 break;
439 /* this is the plugin entry point */
440 enum plugin_status plugin_start(const void* parameter)
442 (void)parameter;
444 int j, i;
445 int thisrocket=0;
446 int start_tick, elapsed_tick;
447 int button;
449 /* set everything up.. no BL timeout, no backdrop,
450 white-text-on-black-background. */
451 backlight_force_on(); /* backlight control in lib/helper.c */
452 #if LCD_DEPTH > 1
453 rb->lcd_set_backdrop(NULL);
454 rb->lcd_set_background(LCD_BLACK);
455 rb->lcd_set_foreground(LCD_WHITE);
456 #endif
458 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
459 rb->cpu_boost(true);
460 #endif
462 fireworks_menu();
464 start_tick = *rb->current_tick;
466 while(!quit_plugin)
468 rb->lcd_clear_display();
470 /* loop through every possible rocket */
471 for(j=0; j<MAX_ROCKETS; j++)
473 /* if the current rocket is actually moving/"alive" then go on and
474 * move/update/explode it */
475 if(rocket_phase[j] > -1)
477 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
478 if(show_rockets==2)
480 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
481 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
482 ROCKET_SIZE, ROCKET_SIZE);
483 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
484 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j],
485 rocket_ypos[j]+rocket_yspeed[j],
486 ROCKET_SIZE, ROCKET_SIZE);
488 #endif
490 /* move rocket */
491 rocket_xpos[j] += rocket_xspeed[j];
492 rocket_ypos[j] -= rocket_yspeed[j];
494 #ifdef HAVE_LCD_COLOR
495 rb->lcd_set_foreground(LCD_WHITE);
496 #endif
497 if(show_rockets==2 || show_rockets==1)
498 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
499 ROCKET_SIZE, ROCKET_SIZE);
501 /* if(rocket isn't "there" yet) keep moving
502 * if(rocket IS there) explode it. */
503 if(rocket_phase[j] < rocket_targetphase[j])
504 rocket_phase[j]++;
505 else
507 rocket_phase[j] = -1;
509 firework_phase[j] = 0;
510 init_explode(rocket_xpos[j], rocket_ypos[j], j,
511 particle_values[particles_per_firework]);
515 /* and now onto the fireworks for this particular rocket... */
516 if(firework_phase[j] > -1)
518 for(i=0; i<particle_values[particles_per_firework]; i++)
520 firework_xpoints[j][i] += firework_xspeed[j][i];
521 firework_ypoints[j][i] += firework_yspeed[j][i];
523 if(gravity != 0)
524 firework_ypoints[j][i] += firework_phase[j]
525 /gravity_values[gravity];
527 #ifdef HAVE_LCD_COLOR
528 rb->lcd_set_foreground(
529 firework_darkest_colors[firework_color[j][i]]);
530 rb->lcd_fillrect(firework_xpoints[j][i]-1,
531 firework_ypoints[j][i]-1,
532 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
534 int phase_left = particle_life_values[particle_life]
535 - firework_phase[j];
536 if(phase_left > 10)
537 rb->lcd_set_foreground(
538 firework_colors[firework_color[j][i]]);
539 else if(phase_left > 7)
540 rb->lcd_set_foreground(
541 firework_dark_colors[firework_color[j][i]]);
542 else if(phase_left > 3)
543 rb->lcd_set_foreground(
544 firework_darker_colors[firework_color[j][i]]);
545 else
546 rb->lcd_set_foreground(
547 firework_darkest_colors[firework_color[j][i]]);
548 #endif
549 rb->lcd_fillrect(firework_xpoints[j][i],
550 firework_ypoints[j][i],
551 FIREWORK_SIZE, FIREWORK_SIZE);
552 /* WIP - currently ugly explosion effect
553 #ifdef HAVE_LCD_COLOR
554 if(firework_phase[j] < 10)
556 rb->lcd_set_foreground(EXPLOSION_COLOR);
557 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
558 rocket_ypos[j]-firework_phase[j],
559 firework_phase[j]*2, firework_phase[j]*2);
561 #endif */
564 #ifdef HAVE_LCD_COLOR
565 rb->lcd_set_foreground(LCD_WHITE);
566 #endif
568 /* firework at its destination age?
569 * no = keep aging; yes = delete it. */
570 if(firework_phase[j] < particle_life_values[particle_life])
571 firework_phase[j]++;
572 else
573 firework_phase[j] = -1;
577 /* is autofire on? */
578 if(autofire_delay != 0)
580 elapsed_tick = *rb->current_tick - start_tick;
582 if(elapsed_tick > autofire_delay_values[autofire_delay])
584 init_rocket(thisrocket);
585 if(++thisrocket == MAX_ROCKETS)
586 thisrocket = 0;
588 start_tick = *rb->current_tick;
592 rb->lcd_update();
594 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
595 switch(button)
597 case BTN_MENU: /* back to config menu */
598 fireworks_menu();
599 break;
601 case BTN_FIRE: /* fire off rockets manually */
602 case BTN_FIRE|BUTTON_REPEAT:
603 init_rocket(thisrocket);
604 if(++thisrocket == MAX_ROCKETS)
605 thisrocket=0;
606 break;
609 /* Turn on backlight timeout (revert to settings) */
610 backlight_use_settings(); /* backlight control in lib/helper.c */
612 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
613 rb->cpu_boost(false);
614 #endif
616 return PLUGIN_OK;