Accept FS#10094 by Teruaki Kawashima:
[kugel-rb.git] / apps / plugins / fireworks.c
blobd23700746008c0e80a7369e486ba794b00594d7b
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 == COWOND2_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 == ONDAVX747_PAD)
103 #define BTN_MENU (BUTTON_MENU|BUTTON_REL)
105 #elif defined(HAVE_TOUCHSCREEN)
106 /* This is a touchscreen target */
107 #else
108 #error No keymap defined!
109 #endif
111 #ifdef HAVE_TOUCHSCREEN
112 #ifndef BTN_MENU
113 #define BTN_MENU (BUTTON_TOPLEFT|BUTTON_REL)
114 #endif
115 #ifndef BTN_FIRE
116 #define BTN_FIRE BUTTON_CENTER
117 #endif
118 #endif
120 /* The lowdown on source terminology:
121 * a ROCKET is launched from the LCD bottom.
122 * FIREWORKs are ejected from the rocket when it explodes. */
124 #define MAX_ROCKETS 40
125 #define ROCKET_LIFE (LCD_HEIGHT/2)
126 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
127 #define ROCKET_SIZE 2
128 #define ROCKET_MOVEMENT_RANGE 4
129 #define ROCKET_TRAIL_PARTICLES 50
131 #define MAX_FIREWORKS 40
132 #define FIREWORK_MOVEMENT_RANGE 6
133 #define FIREWORK_SIZE 2
135 /* position, speed, "phase" (age), color of all fireworks */
136 int firework_xpoints[MAX_ROCKETS+1][MAX_FIREWORKS];
137 int firework_ypoints[MAX_ROCKETS+1][MAX_FIREWORKS];
138 int firework_xspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
139 int firework_yspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
140 int firework_phase[MAX_ROCKETS+1];
141 #ifdef HAVE_LCD_COLOR
142 int firework_color[MAX_ROCKETS+1][MAX_FIREWORKS];
143 #endif
145 /* position, speed, "phase" (age) of all rockets */
146 int rocket_xpos[MAX_ROCKETS+1];
147 int rocket_ypos[MAX_ROCKETS+1];
148 int rocket_xspeed[MAX_ROCKETS+1];
149 int rocket_yspeed[MAX_ROCKETS+1];
150 int rocket_phase[MAX_ROCKETS+1];
151 int rocket_targetphase[MAX_ROCKETS+1];
153 /* settings values. these should eventually be saved to
154 * disk. maybe a preset loading/saving system? */
155 int autofire_delay = 0;
156 int particles_per_firework = 2;
157 int particle_life = 1;
158 int gravity = 1;
159 int show_rockets = 1;
160 int frames_per_second = 4;
161 bool quit_plugin = false;
163 /* firework colors:
164 * firework_colors = brightest firework color, used most of the time.
165 * DARK colors = fireworks are nearly burnt out.
166 * DARKER colors = fireworks are several frames away from burning out.
167 * DARKEST colors = fireworks are a couple frames from burning out. */
168 #ifdef HAVE_LCD_COLOR
169 static const unsigned firework_colors[] = {
170 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
171 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
172 LCD_RGBPACK(151,84,213) };
174 static const unsigned firework_dark_colors[] = {
175 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
176 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
177 LCD_RGBPACK(75,42,105) };
179 static const unsigned firework_darker_colors[] = {
180 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
181 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
182 LCD_RGBPACK(38,21,52) };
184 static const unsigned firework_darkest_colors[] = {
185 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
186 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
187 LCD_RGBPACK(19,10,26) };
189 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
191 #endif
193 static const struct opt_items autofire_delay_settings[15] = {
194 { "Off", -1 },
195 { "50ms", -1 },
196 { "100ms", -1 },
197 { "200ms", -1 },
198 { "300ms", -1 },
199 { "400ms", -1 },
200 { "500ms", -1 },
201 { "600ms", -1 },
202 { "700ms", -1 },
203 { "800ms", -1 },
204 { "900ms", -1 },
205 { "1s", -1 },
206 { "2s", -1 },
207 { "3s", -1 },
208 { "4s", -1 }
211 int autofire_delay_values[15] = {
212 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
214 static const struct opt_items particle_settings[8] = {
215 { "5", -1 },
216 { "10", -1 },
217 { "15", -1 },
218 { "20", -1 },
219 { "25", -1 },
220 { "30", -1 },
221 { "35", -1 },
222 { "40", -1 },
225 int particle_values[8] = {
226 5, 10, 15, 20, 25, 30, 35, 40 };
228 static const struct opt_items particle_life_settings[9] = {
229 { "20 cycles", -1 },
230 { "30 cycles", -1 },
231 { "40 cycles", -1 },
232 { "50 cycles", -1 },
233 { "60 cycles", -1 },
234 { "70 cycles", -1 },
235 { "80 cycles", -1 },
236 { "90 cycles", -1 },
237 { "100 cycles", -1 }
240 int particle_life_values[9] = {
241 20, 30, 40, 50, 60, 70, 80, 90, 100 };
243 static const struct opt_items gravity_settings[4] = {
244 { "Off", -1 },
245 { "Weak", -1 },
246 { "Moderate", -1 },
247 { "Strong", -1 },
250 int gravity_values[4] = {
251 0, 30, 20, 10 };
253 #ifdef HAVE_LCD_COLOR
255 static const struct opt_items rocket_settings[3] = {
256 { "No", -1 },
257 { "Yes (no trails)", -1 },
258 { "Yes (with trails)", -1 },
260 int rocket_values[4] = {
261 2, 1, 0 };
263 #else
265 static const struct opt_items rocket_settings[2] = {
266 { "No", -1 },
267 { "Yes", -1 },
269 int rocket_values[4] = {
270 1, 0 };
272 #endif
274 static const struct opt_items fps_settings[9] = {
275 { "20 FPS", -1 },
276 { "25 FPS", -1 },
277 { "30 FPS", -1 },
278 { "35 FPS", -1 },
279 { "40 FPS", -1 },
280 { "45 FPS", -1 },
281 { "50 FPS", -1 },
282 { "55 FPS", -1 },
283 { "60 FPS", -1 }
286 int fps_values[9] = {
287 20, 25, 30, 35, 40, 45, 50, 55, 60 };
289 MENUITEM_STRINGLIST(menu, "Fireworks Menu", NULL,
290 "Start Demo", "Auto-Fire", "Particles Per Firework",
291 "Particle Life", "Gravity", "Show Rockets",
292 "FPS (Speed)", "Playback Control", "Quit");
294 /* called on startup. initializes all variables, etc */
295 void init_all(void)
297 int j;
299 for(j=0; j<MAX_ROCKETS; j++)
300 firework_phase[j] = -1;
303 /* called when a rocket hits its destination height.
304 * prepares all associated fireworks to be expelled. */
305 void init_explode(int x, int y, int firework, int points)
307 int i;
309 for(i=0; i<points; i++)
311 rb->srand(*rb->current_tick * i);
313 firework_xpoints[firework][i] = x;
314 firework_ypoints[firework][i] = y;
316 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
317 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
319 #ifdef HAVE_LCD_COLOR
320 firework_color[firework][i] = rb->rand() % 7;
321 #endif
325 /* called when a rocket is launched.
326 * prepares said rocket to start moving towards its destination. */
327 void init_rocket(int rocket)
329 rb->srand(*rb->current_tick);
331 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
332 rocket_ypos[rocket] = LCD_HEIGHT;
334 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE) - ROCKET_MOVEMENT_RANGE/2;
335 rocket_yspeed[rocket] = 3;
337 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR)) / rocket_yspeed[rocket];
340 /* startup/configuration menu. */
341 void fireworks_menu(void)
343 int selected = 0, result;
344 bool menu_quit = false;
346 rb->lcd_setfont(FONT_UI);
347 #ifdef HAVE_LCD_COLOR
348 rb->lcd_set_background(LCD_BLACK);
349 rb->lcd_set_foreground(LCD_WHITE);
350 #endif
351 rb->lcd_clear_display();
352 rb->lcd_update();
354 rb->button_clear_queue();
356 while(!menu_quit)
358 result = rb->do_menu(&menu, &selected, NULL, false);
360 switch(result)
362 case 0:
363 rb->lcd_setfont(FONT_SYSFIXED);
365 #ifdef HAVE_LCD_COLOR
366 rb->lcd_set_background(LCD_BLACK);
367 rb->lcd_set_foreground(LCD_WHITE);
368 #endif
370 rb->lcd_clear_display();
371 rb->lcd_update();
373 init_all();
374 menu_quit = true;
375 break;
377 case 1:
378 rb->set_option("Auto-Fire", &autofire_delay, INT, autofire_delay_settings, 15, NULL);
379 break;
381 case 2:
382 rb->set_option("Particles Per Firework", &particles_per_firework, INT, particle_settings, 8, NULL);
383 break;
385 case 3:
386 rb->set_option("Particle Life", &particle_life, INT, particle_life_settings, 9, NULL);
387 break;
389 case 4:
390 rb->set_option("Gravity", &gravity, INT, gravity_settings, 4, NULL);
391 break;
393 case 5:
394 rb->set_option("Show Rockets", &show_rockets, INT, rocket_settings, 3, NULL);
395 break;
397 case 6:
398 rb->set_option("FPS (Speed)", &frames_per_second, INT, fps_settings, 9, NULL);
399 break;
401 case 7:
402 playback_control(NULL);
403 break;
405 case 8:
406 quit_plugin = true;
407 menu_quit = true;
408 break;
413 /* this is the plugin entry point */
414 enum plugin_status plugin_start(const void* parameter)
416 (void)parameter;
418 int j, i, autofire=0;
419 int thisrocket=0;
420 int start_tick, elapsed_tick;
421 int button;
423 /* set everything up.. no BL timeout, no backdrop,
424 white-text-on-black-background. */
425 backlight_force_on(); /* backlight control in lib/helper.c */
426 #if LCD_DEPTH > 1
427 rb->lcd_set_backdrop(NULL);
428 rb->lcd_set_background(LCD_BLACK);
429 rb->lcd_set_foreground(LCD_WHITE);
430 #endif
432 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
433 rb->cpu_boost(true);
434 #endif
436 fireworks_menu();
438 start_tick = *rb->current_tick;
440 while(!quit_plugin)
442 rb->lcd_clear_display();
444 /* loop through every possible rocket */
445 for(j=0; j<MAX_ROCKETS; j++)
447 /* if the current rocket is actually moving/"alive" then go on and
448 * move/update/explode it */
449 if(rocket_phase[j] > -1)
451 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
452 if(show_rockets==2)
454 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
455 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
456 ROCKET_SIZE, ROCKET_SIZE);
457 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
458 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j], rocket_ypos[j]+rocket_yspeed[j],
459 ROCKET_SIZE, ROCKET_SIZE);
461 #endif
463 /* move rocket */
464 rocket_xpos[j] += rocket_xspeed[j];
465 rocket_ypos[j] -= rocket_yspeed[j];
467 #ifdef HAVE_LCD_COLOR
468 rb->lcd_set_foreground(LCD_WHITE);
469 #endif
470 if(show_rockets==2 || show_rockets==1)
471 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
472 ROCKET_SIZE, ROCKET_SIZE);
474 /* if(rocket isn't "there" yet) keep moving
475 * if(rocket IS there) explode it. */
476 if(rocket_phase[j] < rocket_targetphase[j])
477 rocket_phase[j]++;
478 else
480 rocket_phase[j] = -1;
482 firework_phase[j] = 0;
483 init_explode(rocket_xpos[j], rocket_ypos[j], j, particle_values[particles_per_firework]);
487 /* and now onto the fireworks for this particular rocket... */
488 if(firework_phase[j] > -1)
490 for(i=0; i<particle_values[particles_per_firework]; i++)
492 firework_xpoints[j][i] += firework_xspeed[j][i];
493 firework_ypoints[j][i] += firework_yspeed[j][i];
495 if(gravity != 0)
496 firework_ypoints[j][i] += firework_phase[j]/gravity_values[gravity];
498 #ifdef HAVE_LCD_COLOR
499 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
500 rb->lcd_fillrect(firework_xpoints[j][i]-1,
501 firework_ypoints[j][i]-1,
502 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
504 if(firework_phase[j] < particle_life_values[particle_life]-10)
505 rb->lcd_set_foreground(firework_colors[firework_color[j][i]]);
506 else if(firework_phase[j] < particle_life_values[particle_life]-7)
507 rb->lcd_set_foreground(firework_dark_colors[firework_color[j][i]]);
508 else if(firework_phase[j] < particle_life_values[particle_life]-3)
509 rb->lcd_set_foreground(firework_darker_colors[firework_color[j][i]]);
510 else
511 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
512 #endif
513 rb->lcd_fillrect(firework_xpoints[j][i],
514 firework_ypoints[j][i],
515 FIREWORK_SIZE, FIREWORK_SIZE);
516 /* WIP - currently ugly explosion effect
517 #ifdef HAVE_LCD_COLOR
518 if(firework_phase[j] < 10)
520 rb->lcd_set_foreground(EXPLOSION_COLOR);
521 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
522 rocket_ypos[j]-firework_phase[j],
523 firework_phase[j]*2, firework_phase[j]*2);
525 #endif */
528 #ifdef HAVE_LCD_COLOR
529 rb->lcd_set_foreground(LCD_WHITE);
530 #endif
532 /* firework at its destination age?
533 * no = keep aging; yes = delete it. */
534 if(firework_phase[j] < particle_life_values[particle_life])
535 firework_phase[j]++;
536 else
537 firework_phase[j] = -1;
541 /* is autofire on? */
542 if(autofire_delay != 0)
544 elapsed_tick = *rb->current_tick - start_tick;
546 if(elapsed_tick > autofire_delay_values[autofire_delay])
548 rocket_phase[autofire] = 0;
549 init_rocket(autofire);
551 start_tick = *rb->current_tick;
553 if(autofire < MAX_ROCKETS)
554 autofire++;
555 else
556 autofire = 0;
560 rb->lcd_update();
562 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
563 switch(button)
565 case BTN_MENU: /* back to config menu */
566 fireworks_menu();
567 break;
569 case BTN_FIRE: /* fire off rockets manually */
570 case BTN_FIRE|BUTTON_REPEAT:
571 if(thisrocket < MAX_ROCKETS)
572 thisrocket++;
573 else
574 thisrocket=0;
576 rocket_phase[thisrocket] = 0;
577 init_rocket(thisrocket);
578 break;
581 /* Turn on backlight timeout (revert to settings) */
582 backlight_use_settings(); /* backlight control in lib/helper.c */
584 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
585 rb->cpu_boost(false);
586 #endif
588 return PLUGIN_OK;