"remote_control: don't use goto.
[kugel-rb.git] / apps / plugins / fireworks.c
blob8a858d8d4fec794899eb37fb983ee41f3597b7cd
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 defined(HAVE_TOUCHSCREEN)
114 /* This is a touchscreen target */
115 #else
116 #error No keymap defined!
117 #endif
119 #ifdef HAVE_TOUCHSCREEN
120 #ifndef BTN_MENU
121 #define BTN_MENU (BUTTON_TOPLEFT|BUTTON_REL)
122 #endif
123 #ifndef BTN_FIRE
124 #define BTN_FIRE BUTTON_CENTER
125 #endif
126 #endif
128 /* The lowdown on source terminology:
129 * a ROCKET is launched from the LCD bottom.
130 * FIREWORKs are ejected from the rocket when it explodes. */
132 #define MAX_ROCKETS 40
133 #define ROCKET_LIFE (LCD_HEIGHT/2)
134 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
135 #define ROCKET_SIZE 2
136 #define ROCKET_MOVEMENT_RANGE 4
137 #define ROCKET_TRAIL_PARTICLES 50
139 #define MAX_FIREWORKS 40
140 #define FIREWORK_MOVEMENT_RANGE 6
141 #define FIREWORK_SIZE 2
143 /* position, speed, "phase" (age), color of all fireworks */
144 int firework_xpoints[MAX_ROCKETS][MAX_FIREWORKS];
145 int firework_ypoints[MAX_ROCKETS][MAX_FIREWORKS];
146 int firework_xspeed[MAX_ROCKETS][MAX_FIREWORKS];
147 int firework_yspeed[MAX_ROCKETS][MAX_FIREWORKS];
148 int firework_phase[MAX_ROCKETS];
149 #ifdef HAVE_LCD_COLOR
150 int firework_color[MAX_ROCKETS][MAX_FIREWORKS];
151 #endif
153 /* position, speed, "phase" (age) of all rockets */
154 int rocket_xpos[MAX_ROCKETS];
155 int rocket_ypos[MAX_ROCKETS];
156 int rocket_xspeed[MAX_ROCKETS];
157 int rocket_yspeed[MAX_ROCKETS];
158 int rocket_phase[MAX_ROCKETS];
159 int rocket_targetphase[MAX_ROCKETS];
161 /* settings values. these should eventually be saved to
162 * disk. maybe a preset loading/saving system? */
163 int autofire_delay = 0;
164 int particles_per_firework = 2;
165 int particle_life = 1;
166 int gravity = 1;
167 int show_rockets = 1;
168 int frames_per_second = 4;
169 bool quit_plugin = false;
171 /* firework colors:
172 * firework_colors = brightest firework color, used most of the time.
173 * DARK colors = fireworks are nearly burnt out.
174 * DARKER colors = fireworks are several frames away from burning out.
175 * DARKEST colors = fireworks are a couple frames from burning out. */
176 #ifdef HAVE_LCD_COLOR
177 static const unsigned firework_colors[] = {
178 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
179 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
180 LCD_RGBPACK(151,84,213) };
182 static const unsigned firework_dark_colors[] = {
183 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
184 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
185 LCD_RGBPACK(75,42,105) };
187 static const unsigned firework_darker_colors[] = {
188 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
189 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
190 LCD_RGBPACK(38,21,52) };
192 static const unsigned firework_darkest_colors[] = {
193 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
194 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
195 LCD_RGBPACK(19,10,26) };
197 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
199 #endif
201 static const struct opt_items autofire_delay_settings[15] = {
202 { "Off", -1 },
203 { "50ms", -1 },
204 { "100ms", -1 },
205 { "200ms", -1 },
206 { "300ms", -1 },
207 { "400ms", -1 },
208 { "500ms", -1 },
209 { "600ms", -1 },
210 { "700ms", -1 },
211 { "800ms", -1 },
212 { "900ms", -1 },
213 { "1s", -1 },
214 { "2s", -1 },
215 { "3s", -1 },
216 { "4s", -1 }
219 int autofire_delay_values[15] = {
220 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
222 static const struct opt_items particle_settings[8] = {
223 { "5", -1 },
224 { "10", -1 },
225 { "15", -1 },
226 { "20", -1 },
227 { "25", -1 },
228 { "30", -1 },
229 { "35", -1 },
230 { "40", -1 },
233 int particle_values[8] = {
234 5, 10, 15, 20, 25, 30, 35, 40 };
236 static const struct opt_items particle_life_settings[9] = {
237 { "20 cycles", -1 },
238 { "30 cycles", -1 },
239 { "40 cycles", -1 },
240 { "50 cycles", -1 },
241 { "60 cycles", -1 },
242 { "70 cycles", -1 },
243 { "80 cycles", -1 },
244 { "90 cycles", -1 },
245 { "100 cycles", -1 }
248 int particle_life_values[9] = {
249 20, 30, 40, 50, 60, 70, 80, 90, 100 };
251 static const struct opt_items gravity_settings[4] = {
252 { "Off", -1 },
253 { "Weak", -1 },
254 { "Moderate", -1 },
255 { "Strong", -1 },
258 int gravity_values[4] = {
259 0, 30, 20, 10 };
261 #ifdef HAVE_LCD_COLOR
263 static const struct opt_items rocket_settings[3] = {
264 { "No", -1 },
265 { "Yes (no trails)", -1 },
266 { "Yes (with trails)", -1 },
268 int rocket_values[4] = {
269 2, 1, 0 };
271 #else
273 static const struct opt_items rocket_settings[2] = {
274 { "No", -1 },
275 { "Yes", -1 },
277 int rocket_values[4] = {
278 1, 0 };
280 #endif
282 static const struct opt_items fps_settings[9] = {
283 { "20 FPS", -1 },
284 { "25 FPS", -1 },
285 { "30 FPS", -1 },
286 { "35 FPS", -1 },
287 { "40 FPS", -1 },
288 { "45 FPS", -1 },
289 { "50 FPS", -1 },
290 { "55 FPS", -1 },
291 { "60 FPS", -1 }
294 int fps_values[9] = {
295 20, 25, 30, 35, 40, 45, 50, 55, 60 };
297 MENUITEM_STRINGLIST(menu, "Fireworks Menu", NULL,
298 "Start Demo", "Auto-Fire", "Particles Per Firework",
299 "Particle Life", "Gravity", "Show Rockets",
300 "FPS (Speed)", "Playback Control", "Quit");
302 /* called on startup. initializes all variables, etc */
303 void init_all(void)
305 int j;
307 for(j=0; j<MAX_ROCKETS; j++)
309 rocket_phase[j] = -1;
310 firework_phase[j] = -1;
314 /* called when a rocket hits its destination height.
315 * prepares all associated fireworks to be expelled. */
316 void init_explode(int x, int y, int firework, int points)
318 int i;
320 for(i=0; i<points; i++)
322 rb->srand(*rb->current_tick * i);
324 firework_xpoints[firework][i] = x;
325 firework_ypoints[firework][i] = y;
327 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE)
328 - FIREWORK_MOVEMENT_RANGE/2;
329 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE)
330 - FIREWORK_MOVEMENT_RANGE/2;
332 #ifdef HAVE_LCD_COLOR
333 firework_color[firework][i] = rb->rand() % 7;
334 #endif
338 /* called when a rocket is launched.
339 * prepares said rocket to start moving towards its destination. */
340 void init_rocket(int rocket)
342 rb->srand(*rb->current_tick);
344 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
345 rocket_ypos[rocket] = LCD_HEIGHT;
347 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE)
348 - ROCKET_MOVEMENT_RANGE/2;
349 rocket_yspeed[rocket] = 3;
351 rocket_phase[rocket] = 0;
352 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR))
353 / rocket_yspeed[rocket];
356 /* startup/configuration menu. */
357 void fireworks_menu(void)
359 int selected = 0, result;
360 bool menu_quit = false;
362 rb->lcd_setfont(FONT_UI);
363 #ifdef HAVE_LCD_COLOR
364 rb->lcd_set_background(LCD_BLACK);
365 rb->lcd_set_foreground(LCD_WHITE);
366 #endif
367 rb->lcd_clear_display();
368 rb->lcd_update();
370 rb->button_clear_queue();
372 while(!menu_quit)
374 result = rb->do_menu(&menu, &selected, NULL, false);
376 switch(result)
378 case 0:
379 rb->lcd_setfont(FONT_SYSFIXED);
381 #ifdef HAVE_LCD_COLOR
382 rb->lcd_set_background(LCD_BLACK);
383 rb->lcd_set_foreground(LCD_WHITE);
384 #endif
386 rb->lcd_clear_display();
387 rb->lcd_update();
389 init_all();
390 menu_quit = true;
391 break;
393 case 1:
394 rb->set_option("Auto-Fire", &autofire_delay, INT,
395 autofire_delay_settings, 15, NULL);
396 break;
398 case 2:
399 rb->set_option("Particles Per Firework", &particles_per_firework,
400 INT, particle_settings, 8, NULL);
401 break;
403 case 3:
404 rb->set_option("Particle Life", &particle_life, INT,
405 particle_life_settings, 9, NULL);
406 break;
408 case 4:
409 rb->set_option("Gravity", &gravity, INT,
410 gravity_settings, 4, NULL);
411 break;
413 case 5:
414 rb->set_option("Show Rockets", &show_rockets, INT,
415 rocket_settings, 3, NULL);
416 break;
418 case 6:
419 rb->set_option("FPS (Speed)", &frames_per_second, INT,
420 fps_settings, 9, NULL);
421 break;
423 case 7:
424 playback_control(NULL);
425 break;
427 case 8:
428 quit_plugin = true;
429 menu_quit = true;
430 break;
435 /* this is the plugin entry point */
436 enum plugin_status plugin_start(const void* parameter)
438 (void)parameter;
440 int j, i;
441 int thisrocket=0;
442 int start_tick, elapsed_tick;
443 int button;
445 /* set everything up.. no BL timeout, no backdrop,
446 white-text-on-black-background. */
447 backlight_force_on(); /* backlight control in lib/helper.c */
448 #if LCD_DEPTH > 1
449 rb->lcd_set_backdrop(NULL);
450 rb->lcd_set_background(LCD_BLACK);
451 rb->lcd_set_foreground(LCD_WHITE);
452 #endif
454 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
455 rb->cpu_boost(true);
456 #endif
458 fireworks_menu();
460 start_tick = *rb->current_tick;
462 while(!quit_plugin)
464 rb->lcd_clear_display();
466 /* loop through every possible rocket */
467 for(j=0; j<MAX_ROCKETS; j++)
469 /* if the current rocket is actually moving/"alive" then go on and
470 * move/update/explode it */
471 if(rocket_phase[j] > -1)
473 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
474 if(show_rockets==2)
476 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
477 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
478 ROCKET_SIZE, ROCKET_SIZE);
479 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
480 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j],
481 rocket_ypos[j]+rocket_yspeed[j],
482 ROCKET_SIZE, ROCKET_SIZE);
484 #endif
486 /* move rocket */
487 rocket_xpos[j] += rocket_xspeed[j];
488 rocket_ypos[j] -= rocket_yspeed[j];
490 #ifdef HAVE_LCD_COLOR
491 rb->lcd_set_foreground(LCD_WHITE);
492 #endif
493 if(show_rockets==2 || show_rockets==1)
494 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
495 ROCKET_SIZE, ROCKET_SIZE);
497 /* if(rocket isn't "there" yet) keep moving
498 * if(rocket IS there) explode it. */
499 if(rocket_phase[j] < rocket_targetphase[j])
500 rocket_phase[j]++;
501 else
503 rocket_phase[j] = -1;
505 firework_phase[j] = 0;
506 init_explode(rocket_xpos[j], rocket_ypos[j], j,
507 particle_values[particles_per_firework]);
511 /* and now onto the fireworks for this particular rocket... */
512 if(firework_phase[j] > -1)
514 for(i=0; i<particle_values[particles_per_firework]; i++)
516 firework_xpoints[j][i] += firework_xspeed[j][i];
517 firework_ypoints[j][i] += firework_yspeed[j][i];
519 if(gravity != 0)
520 firework_ypoints[j][i] += firework_phase[j]
521 /gravity_values[gravity];
523 #ifdef HAVE_LCD_COLOR
524 rb->lcd_set_foreground(
525 firework_darkest_colors[firework_color[j][i]]);
526 rb->lcd_fillrect(firework_xpoints[j][i]-1,
527 firework_ypoints[j][i]-1,
528 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
530 int phase_left = particle_life_values[particle_life]
531 - firework_phase[j];
532 if(phase_left > 10)
533 rb->lcd_set_foreground(
534 firework_colors[firework_color[j][i]]);
535 else if(phase_left > 7)
536 rb->lcd_set_foreground(
537 firework_dark_colors[firework_color[j][i]]);
538 else if(phase_left > 3)
539 rb->lcd_set_foreground(
540 firework_darker_colors[firework_color[j][i]]);
541 else
542 rb->lcd_set_foreground(
543 firework_darkest_colors[firework_color[j][i]]);
544 #endif
545 rb->lcd_fillrect(firework_xpoints[j][i],
546 firework_ypoints[j][i],
547 FIREWORK_SIZE, FIREWORK_SIZE);
548 /* WIP - currently ugly explosion effect
549 #ifdef HAVE_LCD_COLOR
550 if(firework_phase[j] < 10)
552 rb->lcd_set_foreground(EXPLOSION_COLOR);
553 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
554 rocket_ypos[j]-firework_phase[j],
555 firework_phase[j]*2, firework_phase[j]*2);
557 #endif */
560 #ifdef HAVE_LCD_COLOR
561 rb->lcd_set_foreground(LCD_WHITE);
562 #endif
564 /* firework at its destination age?
565 * no = keep aging; yes = delete it. */
566 if(firework_phase[j] < particle_life_values[particle_life])
567 firework_phase[j]++;
568 else
569 firework_phase[j] = -1;
573 /* is autofire on? */
574 if(autofire_delay != 0)
576 elapsed_tick = *rb->current_tick - start_tick;
578 if(elapsed_tick > autofire_delay_values[autofire_delay])
580 init_rocket(thisrocket);
581 if(++thisrocket == MAX_ROCKETS)
582 thisrocket = 0;
584 start_tick = *rb->current_tick;
588 rb->lcd_update();
590 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
591 switch(button)
593 case BTN_MENU: /* back to config menu */
594 fireworks_menu();
595 break;
597 case BTN_FIRE: /* fire off rockets manually */
598 case BTN_FIRE|BUTTON_REPEAT:
599 init_rocket(thisrocket);
600 if(++thisrocket == MAX_ROCKETS)
601 thisrocket=0;
602 break;
605 /* Turn on backlight timeout (revert to settings) */
606 backlight_use_settings(); /* backlight control in lib/helper.c */
608 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
609 rb->cpu_boost(false);
610 #endif
612 return PLUGIN_OK;