Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / apps / plugins / fireworks.c
blob3af9bdf39d49ea54e6f1d59cfb32e4f98b442e66
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Zakk Roberts
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "oldmenuapi.h"
21 #include "helper.h"
23 PLUGIN_HEADER
25 static struct plugin_api* rb;
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
38 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
39 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
40 #define BTN_MENU BUTTON_MENU
41 #define BTN_FIRE BUTTON_SELECT
42 #elif (CONFIG_KEYPAD == RECORDER_PAD)
43 #define BTN_MENU BUTTON_OFF
44 #define BTN_FIRE BUTTON_PLAY
45 #elif (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
46 #define BTN_MENU BUTTON_OFF
47 #define BTN_FIRE BUTTON_SELECT
48 #elif (CONFIG_KEYPAD == ONDIO_PAD)
49 #define BTN_MENU BUTTON_MENU
50 #define BTN_FIRE BUTTON_UP
51 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
52 #define BTN_MENU BUTTON_POWER
53 #define BTN_FIRE BUTTON_SELECT
54 #elif (CONFIG_KEYPAD == IRIVER_IFP7XX_PAD)
55 #define BTN_MENU BUTTON_MODE
56 #define BTN_FIRE BUTTON_SELECT
57 #elif (CONFIG_KEYPAD == GIGABEAT_PAD) || \
58 (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \
59 (CONFIG_KEYPAD == MROBE100_PAD)
60 #define BTN_MENU BUTTON_MENU
61 #define BTN_FIRE BUTTON_SELECT
62 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
63 (CONFIG_KEYPAD == SANSA_C200_PAD)
64 #define BTN_MENU BUTTON_POWER
65 #define BTN_FIRE BUTTON_SELECT
66 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
67 #define BTN_MENU BUTTON_POWER
68 #define BTN_FIRE BUTTON_PLAY
69 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
70 #define BTN_MENU BUTTON_RC_REC
71 #define BTN_FIRE BUTTON_RC_PLAY
72 #elif (CONFIG_KEYPAD == COWOND2_PAD)
73 #define BTN_MENU BUTTON_MENU
74 #define BTN_FIRE BUTTON_SELECT
75 #else
76 #error No keymap defined!
77 #endif
79 /* The lowdown on source terminology:
80 * a ROCKET is launched from the LCD bottom.
81 * FIREWORKs are ejected from the rocket when it explodes. */
83 #define MAX_ROCKETS 40
84 #define ROCKET_LIFE (LCD_HEIGHT/2)
85 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
86 #define ROCKET_SIZE 2
87 #define ROCKET_MOVEMENT_RANGE 4
88 #define ROCKET_TRAIL_PARTICLES 50
90 #define MAX_FIREWORKS 40
91 #define FIREWORK_MOVEMENT_RANGE 6
92 #define FIREWORK_SIZE 2
94 /* position, speed, "phase" (age), color of all fireworks */
95 int firework_xpoints[MAX_ROCKETS+1][MAX_FIREWORKS];
96 int firework_ypoints[MAX_ROCKETS+1][MAX_FIREWORKS];
97 int firework_xspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
98 int firework_yspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
99 int firework_phase[MAX_ROCKETS+1];
100 #ifdef HAVE_LCD_COLOR
101 int firework_color[MAX_ROCKETS+1][MAX_FIREWORKS];
102 #endif
104 /* position, speed, "phase" (age) of all rockets */
105 int rocket_xpos[MAX_ROCKETS+1];
106 int rocket_ypos[MAX_ROCKETS+1];
107 int rocket_xspeed[MAX_ROCKETS+1];
108 int rocket_yspeed[MAX_ROCKETS+1];
109 int rocket_phase[MAX_ROCKETS+1];
110 int rocket_targetphase[MAX_ROCKETS+1];
112 /* settings values. these should eventually be saved to
113 * disk. maybe a preset loading/saving system? */
114 int autofire_delay = 0;
115 int particles_per_firework = 2;
116 int particle_life = 1;
117 int gravity = 1;
118 int show_rockets = 1;
119 int frames_per_second = 4;
120 bool quit_plugin = false;
122 /* firework colors:
123 * firework_colors = brightest firework color, used most of the time.
124 * DARK colors = fireworks are nearly burnt out.
125 * DARKER colors = fireworks are several frames away from burning out.
126 * DARKEST colors = fireworks are a couple frames from burning out. */
127 #ifdef HAVE_LCD_COLOR
128 static const unsigned firework_colors[] = {
129 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
130 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
131 LCD_RGBPACK(151,84,213) };
133 static const unsigned firework_dark_colors[] = {
134 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
135 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
136 LCD_RGBPACK(75,42,105) };
138 static const unsigned firework_darker_colors[] = {
139 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
140 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
141 LCD_RGBPACK(38,21,52) };
143 static const unsigned firework_darkest_colors[] = {
144 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
145 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
146 LCD_RGBPACK(19,10,26) };
148 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
150 #endif
152 static const struct opt_items autofire_delay_settings[15] = {
153 { "Off", -1 },
154 { "50ms", -1 },
155 { "100ms", -1 },
156 { "200ms", -1 },
157 { "300ms", -1 },
158 { "400ms", -1 },
159 { "500ms", -1 },
160 { "600ms", -1 },
161 { "700ms", -1 },
162 { "800ms", -1 },
163 { "900ms", -1 },
164 { "1s", -1 },
165 { "2s", -1 },
166 { "3s", -1 },
167 { "4s", -1 }
170 int autofire_delay_values[15] = {
171 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
173 static const struct opt_items particle_settings[8] = {
174 { "5", -1 },
175 { "10", -1 },
176 { "15", -1 },
177 { "20", -1 },
178 { "25", -1 },
179 { "30", -1 },
180 { "35", -1 },
181 { "40", -1 },
184 int particle_values[8] = {
185 5, 10, 15, 20, 25, 30, 35, 40 };
187 static const struct opt_items particle_life_settings[9] = {
188 { "20 cycles", -1 },
189 { "30 cycles", -1 },
190 { "40 cycles", -1 },
191 { "50 cycles", -1 },
192 { "60 cycles", -1 },
193 { "70 cycles", -1 },
194 { "80 cycles", -1 },
195 { "90 cycles", -1 },
196 { "100 cycles", -1 }
199 int particle_life_values[9] = {
200 20, 30, 40, 50, 60, 70, 80, 90, 100 };
202 static const struct opt_items gravity_settings[4] = {
203 { "Off", -1 },
204 { "Weak", -1 },
205 { "Moderate", -1 },
206 { "Strong", -1 },
209 int gravity_values[4] = {
210 0, 30, 20, 10 };
212 #ifdef HAVE_LCD_COLOR
214 static const struct opt_items rocket_settings[3] = {
215 { "No", -1 },
216 { "Yes (no trails)", -1 },
217 { "Yes (with trails)", -1 },
219 int rocket_values[4] = {
220 2, 1, 0 };
222 #else
224 static const struct opt_items rocket_settings[2] = {
225 { "No", -1 },
226 { "Yes", -1 },
228 int rocket_values[4] = {
229 1, 0 };
231 #endif
233 static const struct opt_items fps_settings[9] = {
234 { "20 FPS", -1 },
235 { "25 FPS", -1 },
236 { "30 FPS", -1 },
237 { "35 FPS", -1 },
238 { "40 FPS", -1 },
239 { "45 FPS", -1 },
240 { "50 FPS", -1 },
241 { "55 FPS", -1 },
242 { "60 FPS", -1 }
245 int fps_values[9] = {
246 20, 25, 30, 35, 40, 45, 50, 55, 60 };
248 static const struct menu_item items[] = {
249 { "Start Demo", NULL },
250 { "Auto-Fire", NULL },
251 { "Particles Per Firework", NULL },
252 { "Particle Life", NULL },
253 { "Gravity", NULL },
254 { "Show Rockets", NULL },
255 { "FPS (Speed)", NULL },
256 { "Quit", NULL }
259 /* called on startup. initializes all variables, etc */
260 void init_all(void)
262 int j;
264 for(j=0; j<MAX_ROCKETS; j++)
265 firework_phase[j] = -1;
268 /* called when a rocket hits its destination height.
269 * prepares all associated fireworks to be expelled. */
270 void init_explode(int x, int y, int firework, int points)
272 int i;
274 for(i=0; i<points; i++)
276 rb->srand(*rb->current_tick * i);
278 firework_xpoints[firework][i] = x;
279 firework_ypoints[firework][i] = y;
281 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
282 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
284 #ifdef HAVE_LCD_COLOR
285 firework_color[firework][i] = rb->rand() % 7;
286 #endif
290 /* called when a rocket is launched.
291 * prepares said rocket to start moving towards its destination. */
292 void init_rocket(int rocket)
294 rb->srand(*rb->current_tick);
296 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
297 rocket_ypos[rocket] = LCD_HEIGHT;
299 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE) - ROCKET_MOVEMENT_RANGE/2;
300 rocket_yspeed[rocket] = 3;
302 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR)) / rocket_yspeed[rocket];
305 /* startup/configuration menu. */
306 void fireworks_menu(void)
308 int m, result;
309 bool menu_quit = false;
311 rb->lcd_setfont(FONT_UI);
312 #ifdef HAVE_LCD_COLOR
313 rb->lcd_set_background(LCD_BLACK);
314 rb->lcd_set_foreground(LCD_WHITE);
315 #endif
316 rb->lcd_clear_display();
317 rb->lcd_update();
319 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
320 NULL, NULL, NULL, NULL);
322 rb->button_clear_queue();
324 while(!menu_quit)
326 result = menu_show(m);
328 switch(result)
330 case 0:
331 rb->lcd_setfont(FONT_SYSFIXED);
333 #ifdef HAVE_LCD_COLOR
334 rb->lcd_set_background(LCD_BLACK);
335 rb->lcd_set_foreground(LCD_WHITE);
336 #endif
338 rb->lcd_clear_display();
339 rb->lcd_update();
341 init_all();
342 menu_quit = true;
343 break;
345 case 1:
346 rb->set_option("Auto-Fire", &autofire_delay, INT, autofire_delay_settings, 15, NULL);
347 break;
349 case 2:
350 rb->set_option("Particles Per Firework", &particles_per_firework, INT, particle_settings, 8, NULL);
351 break;
353 case 3:
354 rb->set_option("Particle Life", &particle_life, INT, particle_life_settings, 9, NULL);
355 break;
357 case 4:
358 rb->set_option("Gravity", &gravity, INT, gravity_settings, 4, NULL);
359 break;
361 case 5:
362 rb->set_option("Show Rockets", &show_rockets, INT, rocket_settings, 3, NULL);
363 break;
365 case 6:
366 rb->set_option("FPS (Speed)", &frames_per_second, INT, fps_settings, 9, NULL);
367 break;
369 case 7:
370 quit_plugin = true;
371 menu_quit = true;
372 break;
376 menu_exit(m);
379 /* this is the plugin entry point */
380 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
382 (void)parameter;
384 rb = api;
386 int j, i, autofire=0;
387 int thisrocket=0;
388 int start_tick, elapsed_tick;
389 int button;
391 /* set everything up.. no BL timeout, no backdrop,
392 white-text-on-black-background. */
393 backlight_force_on(rb); /* backlight control in lib/helper.c */
394 #if LCD_DEPTH > 1
395 rb->lcd_set_backdrop(NULL);
396 rb->lcd_set_background(LCD_BLACK);
397 rb->lcd_set_foreground(LCD_WHITE);
398 #endif
400 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
401 rb->cpu_boost(true);
402 #endif
404 fireworks_menu();
406 start_tick = *rb->current_tick;
408 while(!quit_plugin)
410 rb->lcd_clear_display();
412 /* loop through every possible rocket */
413 for(j=0; j<MAX_ROCKETS; j++)
415 /* if the current rocket is actually moving/"alive" then go on and
416 * move/update/explode it */
417 if(rocket_phase[j] > -1)
419 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
420 if(show_rockets==2)
422 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
423 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
424 ROCKET_SIZE, ROCKET_SIZE);
425 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
426 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j], rocket_ypos[j]+rocket_yspeed[j],
427 ROCKET_SIZE, ROCKET_SIZE);
429 #endif
431 /* move rocket */
432 rocket_xpos[j] += rocket_xspeed[j];
433 rocket_ypos[j] -= rocket_yspeed[j];
435 #ifdef HAVE_LCD_COLOR
436 rb->lcd_set_foreground(LCD_WHITE);
437 #endif
438 if(show_rockets==2 || show_rockets==1)
439 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
440 ROCKET_SIZE, ROCKET_SIZE);
442 /* if(rocket isn't "there" yet) keep moving
443 * if(rocket IS there) explode it. */
444 if(rocket_phase[j] < rocket_targetphase[j])
445 rocket_phase[j]++;
446 else
448 rocket_phase[j] = -1;
450 firework_phase[j] = 0;
451 init_explode(rocket_xpos[j], rocket_ypos[j], j, particle_values[particles_per_firework]);
455 /* and now onto the fireworks for this particular rocket... */
456 if(firework_phase[j] > -1)
458 for(i=0; i<particle_values[particles_per_firework]; i++)
460 firework_xpoints[j][i] += firework_xspeed[j][i];
461 firework_ypoints[j][i] += firework_yspeed[j][i];
463 if(gravity != 0)
464 firework_ypoints[j][i] += firework_phase[j]/gravity_values[gravity];
466 #ifdef HAVE_LCD_COLOR
467 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
468 rb->lcd_fillrect(firework_xpoints[j][i]-1,
469 firework_ypoints[j][i]-1,
470 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
472 if(firework_phase[j] < particle_life_values[particle_life]-10)
473 rb->lcd_set_foreground(firework_colors[firework_color[j][i]]);
474 else if(firework_phase[j] < particle_life_values[particle_life]-7)
475 rb->lcd_set_foreground(firework_dark_colors[firework_color[j][i]]);
476 else if(firework_phase[j] < particle_life_values[particle_life]-3)
477 rb->lcd_set_foreground(firework_darker_colors[firework_color[j][i]]);
478 else
479 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
480 #endif
481 rb->lcd_fillrect(firework_xpoints[j][i],
482 firework_ypoints[j][i],
483 FIREWORK_SIZE, FIREWORK_SIZE);
484 /* WIP - currently ugly explosion effect
485 #ifdef HAVE_LCD_COLOR
486 if(firework_phase[j] < 10)
488 rb->lcd_set_foreground(EXPLOSION_COLOR);
489 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
490 rocket_ypos[j]-firework_phase[j],
491 firework_phase[j]*2, firework_phase[j]*2);
493 #endif */
496 #ifdef HAVE_LCD_COLOR
497 rb->lcd_set_foreground(LCD_WHITE);
498 #endif
500 /* firework at its destination age?
501 * no = keep aging; yes = delete it. */
502 if(firework_phase[j] < particle_life_values[particle_life])
503 firework_phase[j]++;
504 else
505 firework_phase[j] = -1;
509 /* is autofire on? */
510 if(autofire_delay != 0)
512 elapsed_tick = *rb->current_tick - start_tick;
514 if(elapsed_tick > autofire_delay_values[autofire_delay])
516 rocket_phase[autofire] = 0;
517 init_rocket(autofire);
519 start_tick = *rb->current_tick;
521 if(autofire < MAX_ROCKETS)
522 autofire++;
523 else
524 autofire = 0;
528 rb->lcd_update();
530 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
531 switch(button)
533 case BTN_MENU: /* back to config menu */
534 fireworks_menu();
535 break;
537 case BTN_FIRE: /* fire off rockets manually */
538 case BTN_FIRE|BUTTON_REPEAT:
539 if(thisrocket < MAX_ROCKETS)
540 thisrocket++;
541 else
542 thisrocket=0;
544 rocket_phase[thisrocket] = 0;
545 init_rocket(thisrocket);
546 break;
549 /* Turn on backlight timeout (revert to settings) */
550 backlight_use_settings(rb); /* backlight control in lib/helper.c */
552 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
553 rb->cpu_boost(false);
554 #endif
556 return PLUGIN_OK;