Accept FS#9111 - only exit te stats plugin if the exit button is pressed
[Rockbox.git] / apps / plugins / fireworks.c
blobf3beb6532a43a9384e967bb48e67f7c33b0dce5f
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 "oldmenuapi.h"
23 #include "helper.h"
25 PLUGIN_HEADER
27 static const struct plugin_api* rb;
29 /***
30 * FIREWORKS.C by ZAKK ROBERTS
31 * Rockbox plugin simulating a fireworks display.
32 * Supports all bitmap LCDs, fully scalable.
33 * Currently disabled for Archos Recorder - runs too slow.
34 ***/
36 /* All sorts of keymappings.. */
37 #if (CONFIG_KEYPAD == IRIVER_H300_PAD) || (CONFIG_KEYPAD == IRIVER_H100_PAD)
38 #define BTN_MENU BUTTON_OFF
39 #define BTN_FIRE BUTTON_SELECT
40 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
41 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
42 #define BTN_MENU BUTTON_MENU
43 #define BTN_FIRE BUTTON_SELECT
44 #elif (CONFIG_KEYPAD == RECORDER_PAD)
45 #define BTN_MENU BUTTON_OFF
46 #define BTN_FIRE BUTTON_PLAY
47 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
48 #define BTN_MENU BUTTON_OFF
49 #define BTN_FIRE BUTTON_SELECT
50 #elif (CONFIG_KEYPAD == ONDIO_PAD)
51 #define BTN_MENU BUTTON_MENU
52 #define BTN_FIRE BUTTON_UP
53 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
54 #define BTN_MENU BUTTON_POWER
55 #define BTN_FIRE BUTTON_SELECT
56 #elif (CONFIG_KEYPAD == IRIVER_IFP7XX_PAD)
57 #define BTN_MENU BUTTON_MODE
58 #define BTN_FIRE BUTTON_SELECT
59 #elif (CONFIG_KEYPAD == GIGABEAT_PAD) || \
60 (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \
61 (CONFIG_KEYPAD == MROBE100_PAD)
62 #define BTN_MENU BUTTON_MENU
63 #define BTN_FIRE BUTTON_SELECT
64 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
65 (CONFIG_KEYPAD == SANSA_C200_PAD)
66 #define BTN_MENU BUTTON_POWER
67 #define BTN_FIRE BUTTON_SELECT
68 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
69 #define BTN_MENU BUTTON_POWER
70 #define BTN_FIRE BUTTON_PLAY
71 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
72 #define BTN_MENU BUTTON_RC_REC
73 #define BTN_FIRE BUTTON_RC_PLAY
74 #elif (CONFIG_KEYPAD == COWOND2_PAD)
75 #define BTN_MENU (BUTTON_MENU|BUTTON_REL)
76 #else
77 #error No keymap defined!
78 #endif
80 #ifdef HAVE_TOUCHPAD
81 #ifndef BTN_MENU
82 #define BTN_MENU (BUTTON_TOPLEFT|BUTTON_REL)
83 #endif
84 #ifndef BTN_FIRE
85 #define BTN_FIRE BUTTON_CENTER
86 #endif
87 #endif
89 /* The lowdown on source terminology:
90 * a ROCKET is launched from the LCD bottom.
91 * FIREWORKs are ejected from the rocket when it explodes. */
93 #define MAX_ROCKETS 40
94 #define ROCKET_LIFE (LCD_HEIGHT/2)
95 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
96 #define ROCKET_SIZE 2
97 #define ROCKET_MOVEMENT_RANGE 4
98 #define ROCKET_TRAIL_PARTICLES 50
100 #define MAX_FIREWORKS 40
101 #define FIREWORK_MOVEMENT_RANGE 6
102 #define FIREWORK_SIZE 2
104 /* position, speed, "phase" (age), color of all fireworks */
105 int firework_xpoints[MAX_ROCKETS+1][MAX_FIREWORKS];
106 int firework_ypoints[MAX_ROCKETS+1][MAX_FIREWORKS];
107 int firework_xspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
108 int firework_yspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
109 int firework_phase[MAX_ROCKETS+1];
110 #ifdef HAVE_LCD_COLOR
111 int firework_color[MAX_ROCKETS+1][MAX_FIREWORKS];
112 #endif
114 /* position, speed, "phase" (age) of all rockets */
115 int rocket_xpos[MAX_ROCKETS+1];
116 int rocket_ypos[MAX_ROCKETS+1];
117 int rocket_xspeed[MAX_ROCKETS+1];
118 int rocket_yspeed[MAX_ROCKETS+1];
119 int rocket_phase[MAX_ROCKETS+1];
120 int rocket_targetphase[MAX_ROCKETS+1];
122 /* settings values. these should eventually be saved to
123 * disk. maybe a preset loading/saving system? */
124 int autofire_delay = 0;
125 int particles_per_firework = 2;
126 int particle_life = 1;
127 int gravity = 1;
128 int show_rockets = 1;
129 int frames_per_second = 4;
130 bool quit_plugin = false;
132 /* firework colors:
133 * firework_colors = brightest firework color, used most of the time.
134 * DARK colors = fireworks are nearly burnt out.
135 * DARKER colors = fireworks are several frames away from burning out.
136 * DARKEST colors = fireworks are a couple frames from burning out. */
137 #ifdef HAVE_LCD_COLOR
138 static const unsigned firework_colors[] = {
139 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
140 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
141 LCD_RGBPACK(151,84,213) };
143 static const unsigned firework_dark_colors[] = {
144 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
145 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
146 LCD_RGBPACK(75,42,105) };
148 static const unsigned firework_darker_colors[] = {
149 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
150 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
151 LCD_RGBPACK(38,21,52) };
153 static const unsigned firework_darkest_colors[] = {
154 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
155 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
156 LCD_RGBPACK(19,10,26) };
158 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
160 #endif
162 static const struct opt_items autofire_delay_settings[15] = {
163 { "Off", -1 },
164 { "50ms", -1 },
165 { "100ms", -1 },
166 { "200ms", -1 },
167 { "300ms", -1 },
168 { "400ms", -1 },
169 { "500ms", -1 },
170 { "600ms", -1 },
171 { "700ms", -1 },
172 { "800ms", -1 },
173 { "900ms", -1 },
174 { "1s", -1 },
175 { "2s", -1 },
176 { "3s", -1 },
177 { "4s", -1 }
180 int autofire_delay_values[15] = {
181 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
183 static const struct opt_items particle_settings[8] = {
184 { "5", -1 },
185 { "10", -1 },
186 { "15", -1 },
187 { "20", -1 },
188 { "25", -1 },
189 { "30", -1 },
190 { "35", -1 },
191 { "40", -1 },
194 int particle_values[8] = {
195 5, 10, 15, 20, 25, 30, 35, 40 };
197 static const struct opt_items particle_life_settings[9] = {
198 { "20 cycles", -1 },
199 { "30 cycles", -1 },
200 { "40 cycles", -1 },
201 { "50 cycles", -1 },
202 { "60 cycles", -1 },
203 { "70 cycles", -1 },
204 { "80 cycles", -1 },
205 { "90 cycles", -1 },
206 { "100 cycles", -1 }
209 int particle_life_values[9] = {
210 20, 30, 40, 50, 60, 70, 80, 90, 100 };
212 static const struct opt_items gravity_settings[4] = {
213 { "Off", -1 },
214 { "Weak", -1 },
215 { "Moderate", -1 },
216 { "Strong", -1 },
219 int gravity_values[4] = {
220 0, 30, 20, 10 };
222 #ifdef HAVE_LCD_COLOR
224 static const struct opt_items rocket_settings[3] = {
225 { "No", -1 },
226 { "Yes (no trails)", -1 },
227 { "Yes (with trails)", -1 },
229 int rocket_values[4] = {
230 2, 1, 0 };
232 #else
234 static const struct opt_items rocket_settings[2] = {
235 { "No", -1 },
236 { "Yes", -1 },
238 int rocket_values[4] = {
239 1, 0 };
241 #endif
243 static const struct opt_items fps_settings[9] = {
244 { "20 FPS", -1 },
245 { "25 FPS", -1 },
246 { "30 FPS", -1 },
247 { "35 FPS", -1 },
248 { "40 FPS", -1 },
249 { "45 FPS", -1 },
250 { "50 FPS", -1 },
251 { "55 FPS", -1 },
252 { "60 FPS", -1 }
255 int fps_values[9] = {
256 20, 25, 30, 35, 40, 45, 50, 55, 60 };
258 static const struct menu_item items[] = {
259 { "Start Demo", NULL },
260 { "Auto-Fire", NULL },
261 { "Particles Per Firework", NULL },
262 { "Particle Life", NULL },
263 { "Gravity", NULL },
264 { "Show Rockets", NULL },
265 { "FPS (Speed)", NULL },
266 { "Quit", NULL }
269 /* called on startup. initializes all variables, etc */
270 void init_all(void)
272 int j;
274 for(j=0; j<MAX_ROCKETS; j++)
275 firework_phase[j] = -1;
278 /* called when a rocket hits its destination height.
279 * prepares all associated fireworks to be expelled. */
280 void init_explode(int x, int y, int firework, int points)
282 int i;
284 for(i=0; i<points; i++)
286 rb->srand(*rb->current_tick * i);
288 firework_xpoints[firework][i] = x;
289 firework_ypoints[firework][i] = y;
291 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
292 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
294 #ifdef HAVE_LCD_COLOR
295 firework_color[firework][i] = rb->rand() % 7;
296 #endif
300 /* called when a rocket is launched.
301 * prepares said rocket to start moving towards its destination. */
302 void init_rocket(int rocket)
304 rb->srand(*rb->current_tick);
306 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
307 rocket_ypos[rocket] = LCD_HEIGHT;
309 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE) - ROCKET_MOVEMENT_RANGE/2;
310 rocket_yspeed[rocket] = 3;
312 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR)) / rocket_yspeed[rocket];
315 /* startup/configuration menu. */
316 void fireworks_menu(void)
318 int m, result;
319 bool menu_quit = false;
321 rb->lcd_setfont(FONT_UI);
322 #ifdef HAVE_LCD_COLOR
323 rb->lcd_set_background(LCD_BLACK);
324 rb->lcd_set_foreground(LCD_WHITE);
325 #endif
326 rb->lcd_clear_display();
327 rb->lcd_update();
329 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
330 NULL, NULL, NULL, NULL);
332 rb->button_clear_queue();
334 while(!menu_quit)
336 result = menu_show(m);
338 switch(result)
340 case 0:
341 rb->lcd_setfont(FONT_SYSFIXED);
343 #ifdef HAVE_LCD_COLOR
344 rb->lcd_set_background(LCD_BLACK);
345 rb->lcd_set_foreground(LCD_WHITE);
346 #endif
348 rb->lcd_clear_display();
349 rb->lcd_update();
351 init_all();
352 menu_quit = true;
353 break;
355 case 1:
356 rb->set_option("Auto-Fire", &autofire_delay, INT, autofire_delay_settings, 15, NULL);
357 break;
359 case 2:
360 rb->set_option("Particles Per Firework", &particles_per_firework, INT, particle_settings, 8, NULL);
361 break;
363 case 3:
364 rb->set_option("Particle Life", &particle_life, INT, particle_life_settings, 9, NULL);
365 break;
367 case 4:
368 rb->set_option("Gravity", &gravity, INT, gravity_settings, 4, NULL);
369 break;
371 case 5:
372 rb->set_option("Show Rockets", &show_rockets, INT, rocket_settings, 3, NULL);
373 break;
375 case 6:
376 rb->set_option("FPS (Speed)", &frames_per_second, INT, fps_settings, 9, NULL);
377 break;
379 case 7:
380 quit_plugin = true;
381 menu_quit = true;
382 break;
386 menu_exit(m);
389 /* this is the plugin entry point */
390 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
392 (void)parameter;
394 rb = api;
396 int j, i, autofire=0;
397 int thisrocket=0;
398 int start_tick, elapsed_tick;
399 int button;
401 /* set everything up.. no BL timeout, no backdrop,
402 white-text-on-black-background. */
403 backlight_force_on(rb); /* backlight control in lib/helper.c */
404 #if LCD_DEPTH > 1
405 rb->lcd_set_backdrop(NULL);
406 rb->lcd_set_background(LCD_BLACK);
407 rb->lcd_set_foreground(LCD_WHITE);
408 #endif
410 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
411 rb->cpu_boost(true);
412 #endif
414 fireworks_menu();
416 start_tick = *rb->current_tick;
418 while(!quit_plugin)
420 rb->lcd_clear_display();
422 /* loop through every possible rocket */
423 for(j=0; j<MAX_ROCKETS; j++)
425 /* if the current rocket is actually moving/"alive" then go on and
426 * move/update/explode it */
427 if(rocket_phase[j] > -1)
429 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
430 if(show_rockets==2)
432 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
433 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
434 ROCKET_SIZE, ROCKET_SIZE);
435 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
436 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j], rocket_ypos[j]+rocket_yspeed[j],
437 ROCKET_SIZE, ROCKET_SIZE);
439 #endif
441 /* move rocket */
442 rocket_xpos[j] += rocket_xspeed[j];
443 rocket_ypos[j] -= rocket_yspeed[j];
445 #ifdef HAVE_LCD_COLOR
446 rb->lcd_set_foreground(LCD_WHITE);
447 #endif
448 if(show_rockets==2 || show_rockets==1)
449 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
450 ROCKET_SIZE, ROCKET_SIZE);
452 /* if(rocket isn't "there" yet) keep moving
453 * if(rocket IS there) explode it. */
454 if(rocket_phase[j] < rocket_targetphase[j])
455 rocket_phase[j]++;
456 else
458 rocket_phase[j] = -1;
460 firework_phase[j] = 0;
461 init_explode(rocket_xpos[j], rocket_ypos[j], j, particle_values[particles_per_firework]);
465 /* and now onto the fireworks for this particular rocket... */
466 if(firework_phase[j] > -1)
468 for(i=0; i<particle_values[particles_per_firework]; i++)
470 firework_xpoints[j][i] += firework_xspeed[j][i];
471 firework_ypoints[j][i] += firework_yspeed[j][i];
473 if(gravity != 0)
474 firework_ypoints[j][i] += firework_phase[j]/gravity_values[gravity];
476 #ifdef HAVE_LCD_COLOR
477 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
478 rb->lcd_fillrect(firework_xpoints[j][i]-1,
479 firework_ypoints[j][i]-1,
480 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
482 if(firework_phase[j] < particle_life_values[particle_life]-10)
483 rb->lcd_set_foreground(firework_colors[firework_color[j][i]]);
484 else if(firework_phase[j] < particle_life_values[particle_life]-7)
485 rb->lcd_set_foreground(firework_dark_colors[firework_color[j][i]]);
486 else if(firework_phase[j] < particle_life_values[particle_life]-3)
487 rb->lcd_set_foreground(firework_darker_colors[firework_color[j][i]]);
488 else
489 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
490 #endif
491 rb->lcd_fillrect(firework_xpoints[j][i],
492 firework_ypoints[j][i],
493 FIREWORK_SIZE, FIREWORK_SIZE);
494 /* WIP - currently ugly explosion effect
495 #ifdef HAVE_LCD_COLOR
496 if(firework_phase[j] < 10)
498 rb->lcd_set_foreground(EXPLOSION_COLOR);
499 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
500 rocket_ypos[j]-firework_phase[j],
501 firework_phase[j]*2, firework_phase[j]*2);
503 #endif */
506 #ifdef HAVE_LCD_COLOR
507 rb->lcd_set_foreground(LCD_WHITE);
508 #endif
510 /* firework at its destination age?
511 * no = keep aging; yes = delete it. */
512 if(firework_phase[j] < particle_life_values[particle_life])
513 firework_phase[j]++;
514 else
515 firework_phase[j] = -1;
519 /* is autofire on? */
520 if(autofire_delay != 0)
522 elapsed_tick = *rb->current_tick - start_tick;
524 if(elapsed_tick > autofire_delay_values[autofire_delay])
526 rocket_phase[autofire] = 0;
527 init_rocket(autofire);
529 start_tick = *rb->current_tick;
531 if(autofire < MAX_ROCKETS)
532 autofire++;
533 else
534 autofire = 0;
538 rb->lcd_update();
540 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
541 switch(button)
543 case BTN_MENU: /* back to config menu */
544 fireworks_menu();
545 break;
547 case BTN_FIRE: /* fire off rockets manually */
548 case BTN_FIRE|BUTTON_REPEAT:
549 if(thisrocket < MAX_ROCKETS)
550 thisrocket++;
551 else
552 thisrocket=0;
554 rocket_phase[thisrocket] = 0;
555 init_rocket(thisrocket);
556 break;
559 /* Turn on backlight timeout (revert to settings) */
560 backlight_use_settings(rb); /* backlight control in lib/helper.c */
562 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
563 rb->cpu_boost(false);
564 #endif
566 return PLUGIN_OK;