New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / plugins / fireworks.c
blob5134d0153880208486c6ca7cf9e3cb701127ec8b
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/oldmenuapi.h"
23 #include "lib/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 #elif CONFIG_KEYPAD == IAUDIO67_PAD
77 #define BTN_MENU BUTTON_MENU
78 #define BTN_FIRE BUTTON_PLAY
79 #else
80 #error No keymap defined!
81 #endif
83 #ifdef HAVE_TOUCHSCREEN
84 #ifndef BTN_MENU
85 #define BTN_MENU (BUTTON_TOPLEFT|BUTTON_REL)
86 #endif
87 #ifndef BTN_FIRE
88 #define BTN_FIRE BUTTON_CENTER
89 #endif
90 #endif
92 /* The lowdown on source terminology:
93 * a ROCKET is launched from the LCD bottom.
94 * FIREWORKs are ejected from the rocket when it explodes. */
96 #define MAX_ROCKETS 40
97 #define ROCKET_LIFE (LCD_HEIGHT/2)
98 #define ROCKET_LIFE_VAR (LCD_HEIGHT/4)
99 #define ROCKET_SIZE 2
100 #define ROCKET_MOVEMENT_RANGE 4
101 #define ROCKET_TRAIL_PARTICLES 50
103 #define MAX_FIREWORKS 40
104 #define FIREWORK_MOVEMENT_RANGE 6
105 #define FIREWORK_SIZE 2
107 /* position, speed, "phase" (age), color of all fireworks */
108 int firework_xpoints[MAX_ROCKETS+1][MAX_FIREWORKS];
109 int firework_ypoints[MAX_ROCKETS+1][MAX_FIREWORKS];
110 int firework_xspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
111 int firework_yspeed[MAX_ROCKETS+1][MAX_FIREWORKS];
112 int firework_phase[MAX_ROCKETS+1];
113 #ifdef HAVE_LCD_COLOR
114 int firework_color[MAX_ROCKETS+1][MAX_FIREWORKS];
115 #endif
117 /* position, speed, "phase" (age) of all rockets */
118 int rocket_xpos[MAX_ROCKETS+1];
119 int rocket_ypos[MAX_ROCKETS+1];
120 int rocket_xspeed[MAX_ROCKETS+1];
121 int rocket_yspeed[MAX_ROCKETS+1];
122 int rocket_phase[MAX_ROCKETS+1];
123 int rocket_targetphase[MAX_ROCKETS+1];
125 /* settings values. these should eventually be saved to
126 * disk. maybe a preset loading/saving system? */
127 int autofire_delay = 0;
128 int particles_per_firework = 2;
129 int particle_life = 1;
130 int gravity = 1;
131 int show_rockets = 1;
132 int frames_per_second = 4;
133 bool quit_plugin = false;
135 /* firework colors:
136 * firework_colors = brightest firework color, used most of the time.
137 * DARK colors = fireworks are nearly burnt out.
138 * DARKER colors = fireworks are several frames away from burning out.
139 * DARKEST colors = fireworks are a couple frames from burning out. */
140 #ifdef HAVE_LCD_COLOR
141 static const unsigned firework_colors[] = {
142 LCD_RGBPACK(0,255,64), LCD_RGBPACK(61,255,249), LCD_RGBPACK(255,200,61),
143 LCD_RGBPACK(217,22,217), LCD_RGBPACK(22,217,132), LCD_RGBPACK(67,95,254),
144 LCD_RGBPACK(151,84,213) };
146 static const unsigned firework_dark_colors[] = {
147 LCD_RGBPACK(0,128,32), LCD_RGBPACK(30,128,128), LCD_RGBPACK(128,100,30),
148 LCD_RGBPACK(109,11,109), LCD_RGBPACK(11,109,66), LCD_RGBPACK(33,47,128),
149 LCD_RGBPACK(75,42,105) };
151 static const unsigned firework_darker_colors[] = {
152 LCD_RGBPACK(0,64,16), LCD_RGBPACK(15,64,64), LCD_RGBPACK(64,50,15),
153 LCD_RGBPACK(55,5,55), LCD_RGBPACK(5,55,33), LCD_RGBPACK(16,24,64),
154 LCD_RGBPACK(38,21,52) };
156 static const unsigned firework_darkest_colors[] = {
157 LCD_RGBPACK(0,32,8), LCD_RGBPACK(7,32,32), LCD_RGBPACK(32,25,7),
158 LCD_RGBPACK(27,2,27), LCD_RGBPACK(2,27,16), LCD_RGBPACK(8,12,32),
159 LCD_RGBPACK(19,10,26) };
161 #define EXPLOSION_COLOR LCD_RGBPACK(255,240,0)
163 #endif
165 static const struct opt_items autofire_delay_settings[15] = {
166 { "Off", -1 },
167 { "50ms", -1 },
168 { "100ms", -1 },
169 { "200ms", -1 },
170 { "300ms", -1 },
171 { "400ms", -1 },
172 { "500ms", -1 },
173 { "600ms", -1 },
174 { "700ms", -1 },
175 { "800ms", -1 },
176 { "900ms", -1 },
177 { "1s", -1 },
178 { "2s", -1 },
179 { "3s", -1 },
180 { "4s", -1 }
183 int autofire_delay_values[15] = {
184 0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400 };
186 static const struct opt_items particle_settings[8] = {
187 { "5", -1 },
188 { "10", -1 },
189 { "15", -1 },
190 { "20", -1 },
191 { "25", -1 },
192 { "30", -1 },
193 { "35", -1 },
194 { "40", -1 },
197 int particle_values[8] = {
198 5, 10, 15, 20, 25, 30, 35, 40 };
200 static const struct opt_items particle_life_settings[9] = {
201 { "20 cycles", -1 },
202 { "30 cycles", -1 },
203 { "40 cycles", -1 },
204 { "50 cycles", -1 },
205 { "60 cycles", -1 },
206 { "70 cycles", -1 },
207 { "80 cycles", -1 },
208 { "90 cycles", -1 },
209 { "100 cycles", -1 }
212 int particle_life_values[9] = {
213 20, 30, 40, 50, 60, 70, 80, 90, 100 };
215 static const struct opt_items gravity_settings[4] = {
216 { "Off", -1 },
217 { "Weak", -1 },
218 { "Moderate", -1 },
219 { "Strong", -1 },
222 int gravity_values[4] = {
223 0, 30, 20, 10 };
225 #ifdef HAVE_LCD_COLOR
227 static const struct opt_items rocket_settings[3] = {
228 { "No", -1 },
229 { "Yes (no trails)", -1 },
230 { "Yes (with trails)", -1 },
232 int rocket_values[4] = {
233 2, 1, 0 };
235 #else
237 static const struct opt_items rocket_settings[2] = {
238 { "No", -1 },
239 { "Yes", -1 },
241 int rocket_values[4] = {
242 1, 0 };
244 #endif
246 static const struct opt_items fps_settings[9] = {
247 { "20 FPS", -1 },
248 { "25 FPS", -1 },
249 { "30 FPS", -1 },
250 { "35 FPS", -1 },
251 { "40 FPS", -1 },
252 { "45 FPS", -1 },
253 { "50 FPS", -1 },
254 { "55 FPS", -1 },
255 { "60 FPS", -1 }
258 int fps_values[9] = {
259 20, 25, 30, 35, 40, 45, 50, 55, 60 };
261 static const struct menu_item items[] = {
262 { "Start Demo", NULL },
263 { "Auto-Fire", NULL },
264 { "Particles Per Firework", NULL },
265 { "Particle Life", NULL },
266 { "Gravity", NULL },
267 { "Show Rockets", NULL },
268 { "FPS (Speed)", NULL },
269 { "Quit", NULL }
272 /* called on startup. initializes all variables, etc */
273 void init_all(void)
275 int j;
277 for(j=0; j<MAX_ROCKETS; j++)
278 firework_phase[j] = -1;
281 /* called when a rocket hits its destination height.
282 * prepares all associated fireworks to be expelled. */
283 void init_explode(int x, int y, int firework, int points)
285 int i;
287 for(i=0; i<points; i++)
289 rb->srand(*rb->current_tick * i);
291 firework_xpoints[firework][i] = x;
292 firework_ypoints[firework][i] = y;
294 firework_xspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
295 firework_yspeed[firework][i] = (rb->rand() % FIREWORK_MOVEMENT_RANGE) - FIREWORK_MOVEMENT_RANGE/2;
297 #ifdef HAVE_LCD_COLOR
298 firework_color[firework][i] = rb->rand() % 7;
299 #endif
303 /* called when a rocket is launched.
304 * prepares said rocket to start moving towards its destination. */
305 void init_rocket(int rocket)
307 rb->srand(*rb->current_tick);
309 rocket_xpos[rocket] = rb->rand() % LCD_WIDTH;
310 rocket_ypos[rocket] = LCD_HEIGHT;
312 rocket_xspeed[rocket] = (rb->rand() % ROCKET_MOVEMENT_RANGE) - ROCKET_MOVEMENT_RANGE/2;
313 rocket_yspeed[rocket] = 3;
315 rocket_targetphase[rocket] = (ROCKET_LIFE + (rb->rand() % ROCKET_LIFE_VAR)) / rocket_yspeed[rocket];
318 /* startup/configuration menu. */
319 void fireworks_menu(void)
321 int m, result;
322 bool menu_quit = false;
324 rb->lcd_setfont(FONT_UI);
325 #ifdef HAVE_LCD_COLOR
326 rb->lcd_set_background(LCD_BLACK);
327 rb->lcd_set_foreground(LCD_WHITE);
328 #endif
329 rb->lcd_clear_display();
330 rb->lcd_update();
332 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
333 NULL, NULL, NULL, NULL);
335 rb->button_clear_queue();
337 while(!menu_quit)
339 result = menu_show(m);
341 switch(result)
343 case 0:
344 rb->lcd_setfont(FONT_SYSFIXED);
346 #ifdef HAVE_LCD_COLOR
347 rb->lcd_set_background(LCD_BLACK);
348 rb->lcd_set_foreground(LCD_WHITE);
349 #endif
351 rb->lcd_clear_display();
352 rb->lcd_update();
354 init_all();
355 menu_quit = true;
356 break;
358 case 1:
359 rb->set_option("Auto-Fire", &autofire_delay, INT, autofire_delay_settings, 15, NULL);
360 break;
362 case 2:
363 rb->set_option("Particles Per Firework", &particles_per_firework, INT, particle_settings, 8, NULL);
364 break;
366 case 3:
367 rb->set_option("Particle Life", &particle_life, INT, particle_life_settings, 9, NULL);
368 break;
370 case 4:
371 rb->set_option("Gravity", &gravity, INT, gravity_settings, 4, NULL);
372 break;
374 case 5:
375 rb->set_option("Show Rockets", &show_rockets, INT, rocket_settings, 3, NULL);
376 break;
378 case 6:
379 rb->set_option("FPS (Speed)", &frames_per_second, INT, fps_settings, 9, NULL);
380 break;
382 case 7:
383 quit_plugin = true;
384 menu_quit = true;
385 break;
389 menu_exit(m);
392 /* this is the plugin entry point */
393 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
395 (void)parameter;
397 rb = api;
399 int j, i, autofire=0;
400 int thisrocket=0;
401 int start_tick, elapsed_tick;
402 int button;
404 /* set everything up.. no BL timeout, no backdrop,
405 white-text-on-black-background. */
406 backlight_force_on(rb); /* backlight control in lib/helper.c */
407 #if LCD_DEPTH > 1
408 rb->lcd_set_backdrop(NULL);
409 rb->lcd_set_background(LCD_BLACK);
410 rb->lcd_set_foreground(LCD_WHITE);
411 #endif
413 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
414 rb->cpu_boost(true);
415 #endif
417 fireworks_menu();
419 start_tick = *rb->current_tick;
421 while(!quit_plugin)
423 rb->lcd_clear_display();
425 /* loop through every possible rocket */
426 for(j=0; j<MAX_ROCKETS; j++)
428 /* if the current rocket is actually moving/"alive" then go on and
429 * move/update/explode it */
430 if(rocket_phase[j] > -1)
432 #ifdef HAVE_LCD_COLOR /* draw trail, if requested */
433 if(show_rockets==2)
435 rb->lcd_set_foreground(LCD_RGBPACK(128,128,128));
436 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
437 ROCKET_SIZE, ROCKET_SIZE);
438 rb->lcd_set_foreground(LCD_RGBPACK(64,64,64));
439 rb->lcd_fillrect(rocket_xpos[j]-rocket_xspeed[j], rocket_ypos[j]+rocket_yspeed[j],
440 ROCKET_SIZE, ROCKET_SIZE);
442 #endif
444 /* move rocket */
445 rocket_xpos[j] += rocket_xspeed[j];
446 rocket_ypos[j] -= rocket_yspeed[j];
448 #ifdef HAVE_LCD_COLOR
449 rb->lcd_set_foreground(LCD_WHITE);
450 #endif
451 if(show_rockets==2 || show_rockets==1)
452 rb->lcd_fillrect(rocket_xpos[j], rocket_ypos[j],
453 ROCKET_SIZE, ROCKET_SIZE);
455 /* if(rocket isn't "there" yet) keep moving
456 * if(rocket IS there) explode it. */
457 if(rocket_phase[j] < rocket_targetphase[j])
458 rocket_phase[j]++;
459 else
461 rocket_phase[j] = -1;
463 firework_phase[j] = 0;
464 init_explode(rocket_xpos[j], rocket_ypos[j], j, particle_values[particles_per_firework]);
468 /* and now onto the fireworks for this particular rocket... */
469 if(firework_phase[j] > -1)
471 for(i=0; i<particle_values[particles_per_firework]; i++)
473 firework_xpoints[j][i] += firework_xspeed[j][i];
474 firework_ypoints[j][i] += firework_yspeed[j][i];
476 if(gravity != 0)
477 firework_ypoints[j][i] += firework_phase[j]/gravity_values[gravity];
479 #ifdef HAVE_LCD_COLOR
480 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
481 rb->lcd_fillrect(firework_xpoints[j][i]-1,
482 firework_ypoints[j][i]-1,
483 FIREWORK_SIZE+2, FIREWORK_SIZE+2);
485 if(firework_phase[j] < particle_life_values[particle_life]-10)
486 rb->lcd_set_foreground(firework_colors[firework_color[j][i]]);
487 else if(firework_phase[j] < particle_life_values[particle_life]-7)
488 rb->lcd_set_foreground(firework_dark_colors[firework_color[j][i]]);
489 else if(firework_phase[j] < particle_life_values[particle_life]-3)
490 rb->lcd_set_foreground(firework_darker_colors[firework_color[j][i]]);
491 else
492 rb->lcd_set_foreground(firework_darkest_colors[firework_color[j][i]]);
493 #endif
494 rb->lcd_fillrect(firework_xpoints[j][i],
495 firework_ypoints[j][i],
496 FIREWORK_SIZE, FIREWORK_SIZE);
497 /* WIP - currently ugly explosion effect
498 #ifdef HAVE_LCD_COLOR
499 if(firework_phase[j] < 10)
501 rb->lcd_set_foreground(EXPLOSION_COLOR);
502 rb->lcd_fillrect(rocket_xpos[j]-firework_phase[j],
503 rocket_ypos[j]-firework_phase[j],
504 firework_phase[j]*2, firework_phase[j]*2);
506 #endif */
509 #ifdef HAVE_LCD_COLOR
510 rb->lcd_set_foreground(LCD_WHITE);
511 #endif
513 /* firework at its destination age?
514 * no = keep aging; yes = delete it. */
515 if(firework_phase[j] < particle_life_values[particle_life])
516 firework_phase[j]++;
517 else
518 firework_phase[j] = -1;
522 /* is autofire on? */
523 if(autofire_delay != 0)
525 elapsed_tick = *rb->current_tick - start_tick;
527 if(elapsed_tick > autofire_delay_values[autofire_delay])
529 rocket_phase[autofire] = 0;
530 init_rocket(autofire);
532 start_tick = *rb->current_tick;
534 if(autofire < MAX_ROCKETS)
535 autofire++;
536 else
537 autofire = 0;
541 rb->lcd_update();
543 button = rb->button_get_w_tmo(HZ/fps_values[frames_per_second]);
544 switch(button)
546 case BTN_MENU: /* back to config menu */
547 fireworks_menu();
548 break;
550 case BTN_FIRE: /* fire off rockets manually */
551 case BTN_FIRE|BUTTON_REPEAT:
552 if(thisrocket < MAX_ROCKETS)
553 thisrocket++;
554 else
555 thisrocket=0;
557 rocket_phase[thisrocket] = 0;
558 init_rocket(thisrocket);
559 break;
562 /* Turn on backlight timeout (revert to settings) */
563 backlight_use_settings(rb); /* backlight control in lib/helper.c */
565 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
566 rb->cpu_boost(false);
567 #endif
569 return PLUGIN_OK;