A couple more tweaks
[Rockbox.git] / apps / plugins / starfield.c
blob333fd32fcd8cb0b96819842580686f1dc67156e8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2005 Kevin Ferrare
10 * All files in this archive are subject to the GNU General Public License.
11 * See the file COPYING in the source tree root for full license agreement.
13 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
14 * KIND, either express or implied.
16 ****************************************************************************/
18 #include "plugin.h"
19 #include "helper.h"
21 #ifdef HAVE_LCD_BITMAP /* and also not for the Player */
23 PLUGIN_HEADER
25 /******************************* Globals ***********************************/
27 static struct plugin_api* rb; /* global api struct pointer */
29 /* Key assignement */
30 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
31 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
32 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
33 #define STARFIELD_QUIT BUTTON_MENU
34 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_FWD
35 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_BACK
36 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
37 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
38 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
39 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
40 #define STARFIELD_QUIT BUTTON_POWER
41 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
42 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
43 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
44 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
45 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
46 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
47 #define STARFIELD_QUIT BUTTON_POWER
48 #define STARFIELD_INCREASE_ZMOVE BUTTON_SCROLL_UP
49 #define STARFIELD_DECREASE_ZMOVE BUTTON_SCROLL_DOWN
50 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
51 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
52 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
53 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
54 #define STARFIELD_QUIT BUTTON_POWER
55 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
56 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
57 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
58 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
59 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
60 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
61 (CONFIG_KEYPAD == SANSA_C200_PAD)
62 #define STARFIELD_QUIT BUTTON_POWER
63 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
64 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
65 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
66 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
67 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
69 #else
70 #define STARFIELD_QUIT BUTTON_OFF
71 #define STARFIELD_INCREASE_ZMOVE BUTTON_UP
72 #define STARFIELD_DECREASE_ZMOVE BUTTON_DOWN
73 #define STARFIELD_INCREASE_NB_STARS BUTTON_RIGHT
74 #define STARFIELD_DECREASE_NB_STARS BUTTON_LEFT
75 #ifdef BUTTON_SELECT
76 #define STARFIELD_TOGGLE_COLOR BUTTON_SELECT
77 #else
78 #define STARFIELD_TOGGLE_COLOR BUTTON_PLAY
79 #endif
80 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
81 #define STARFIELD_RC_QUIT BUTTON_RC_STOP
82 #endif
84 #endif
86 #define LCD_CENTER_X (LCD_WIDTH/2)
87 #define LCD_CENTER_Y (LCD_HEIGHT/2)
88 #define Z_MAX_DIST 100
91 #define MAX_STARS (LCD_WIDTH*LCD_HEIGHT*20)/100
92 #define INIT_STARS 200
93 #define STARFIELD_INCREASE_STEP 50
94 #define INIT_SPACE_SPEED 1
95 #define STAR_MAX_VELOCITY 2
97 * max 3d coord in the 2d screen :
98 * example with x
99 * x2d=x3d/z+LCD_CENTER_X (+LCD_CENTER_X to center ...)
100 * so
101 * max_x2d=max_x3d/max_z+LCD_CENTER_X
102 * max_x3d=(max_x2d-LCD_CENTER_X)*max_z
103 * with
104 * max_x2d = LCD_WIDTH
105 * max_z = Z_MAX_DIST
106 * we have now
107 * max_x3d=(LCD_WIDTH-LCD_CENTER_X)*Z_MAX_DIST
108 * max_x3d=LCD_CENTER_X*Z_MAX_DIST
111 #define MAX_INIT_STAR_X LCD_CENTER_X*Z_MAX_DIST
112 #define MAX_INIT_STAR_Y LCD_CENTER_Y*Z_MAX_DIST
114 #define MSG_DISP_TIME 30
116 static struct plugin_api* rb; /* global api struct pointer */
119 * Each star's stuffs
121 struct star
123 int x,y,z;
124 int velocity;
125 #ifdef HAVE_LCD_COLOR
126 int color;
127 #endif
130 static inline void star_init(struct star * star, int z_move, bool color)
132 star->velocity=rb->rand() % STAR_MAX_VELOCITY+1;
133 /* choose x between -MAX_INIT_STAR_X and MAX_INIT_STAR_X */
134 star->x=rb->rand() % (2*MAX_INIT_STAR_X)-MAX_INIT_STAR_X;
135 star->y=rb->rand() % (2*MAX_INIT_STAR_Y)-MAX_INIT_STAR_Y;
136 #ifdef HAVE_LCD_COLOR
137 if(color)
138 star->color=LCD_RGBPACK(rb->rand()%128+128,rb->rand()%128+128,
139 rb->rand()%128+128);
140 else
141 star->color=LCD_WHITE;
142 #else
143 (void)color;
144 #endif
145 if(z_move>=0)
146 star->z=Z_MAX_DIST;
147 else
148 star->z=rb->rand() %Z_MAX_DIST/2+1;
151 static inline void star_move(struct star * star, int z_move, bool color)
153 star->z -= z_move*star->velocity;
154 if (star->z <= 0 || star->z > Z_MAX_DIST)
155 star_init(star, z_move, color);
158 static inline void star_draw(struct star * star, int z_move, bool color)
160 int x_draw, y_draw;
162 * 3d -> 2d : projection on the screen : x2d=x3d/z (thales theorem)
163 * we put the star in the center of the screen
165 x_draw = star->x / star->z + LCD_CENTER_X;
166 if (x_draw < 1 || x_draw >= LCD_WIDTH)
168 star_init(star, z_move, color);
169 return;
171 y_draw = star->y / star->z + LCD_CENTER_Y;
172 if (y_draw < 1 || y_draw >= LCD_HEIGHT)
174 star_init(star, z_move, color);
175 return;
178 #ifdef HAVE_LCD_COLOR
179 rb->lcd_set_foreground(star->color);
180 #endif
182 rb->lcd_drawpixel(x_draw, y_draw);
183 if(star->z<5*Z_MAX_DIST/6)
185 rb->lcd_drawpixel(x_draw, y_draw - 1);
186 rb->lcd_drawpixel(x_draw - 1, y_draw);
187 if(star->z<Z_MAX_DIST/2)
189 rb->lcd_drawpixel(x_draw + 1, y_draw);
190 rb->lcd_drawpixel(x_draw, y_draw + 1);
196 * Whole starfield operations
198 struct starfield
200 struct star tab[MAX_STARS];
201 int nb_stars;
202 int z_move;
203 bool color;
206 static inline void starfield_init(struct starfield * starfield)
208 starfield->nb_stars=0;
209 starfield->z_move=INIT_SPACE_SPEED;
210 starfield->color=false;
213 static inline void starfield_add_stars(struct starfield * starfield,
214 int nb_to_add)
216 int i, old_nb_stars;
217 old_nb_stars=starfield->nb_stars;
218 starfield->nb_stars+=nb_to_add;
220 if(starfield->nb_stars > MAX_STARS)
221 starfield->nb_stars=MAX_STARS;
223 for( i=old_nb_stars ; i < starfield->nb_stars ; ++i )
225 star_init( &(starfield->tab[i]), starfield->z_move, starfield->color );
229 static inline void starfield_del_stars(struct starfield * starfield,
230 int nb_to_add)
232 starfield->nb_stars-=nb_to_add;
233 if(starfield->nb_stars<0)
234 starfield->nb_stars=0;
237 static inline void starfield_move_and_draw(struct starfield * starfield)
239 int i;
240 for(i=0;i<starfield->nb_stars;++i)
242 star_move(&(starfield->tab[i]), starfield->z_move, starfield->color);
243 star_draw(&(starfield->tab[i]), starfield->z_move, starfield->color);
247 static struct starfield starfield;
249 int plugin_main(void)
251 char str_buffer[40];
252 int button, avg_peak, t_disp=0;
253 int font_h, font_w;
254 bool pulse=true;
255 rb->lcd_getstringsize("A", &font_w, &font_h);
256 starfield_init(&starfield);
257 starfield_add_stars(&starfield, INIT_STARS);
259 #if LCD_DEPTH > 1
260 rb->lcd_set_backdrop(NULL);
261 #endif
262 #ifdef HAVE_LCD_COLOR
263 rb->lcd_set_background(LCD_BLACK);
264 rb->lcd_set_foreground(LCD_WHITE);
265 #endif
267 while (true)
269 rb->sleep(1);
270 rb->lcd_clear_display();
272 #if ((CONFIG_CODEC == SWCODEC) || !defined(SIMULATOR) && \
273 ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)))
275 /* This will make the stars pulse to the music */
276 if(pulse==true){
278 /* Get the peaks. ( Borrowed from vu_meter ) */
279 #if (CONFIG_CODEC == SWCODEC)
280 int left_peak, right_peak;
281 rb->pcm_calculate_peaks(&left_peak, &right_peak);
282 #else
283 int left_peak = rb->mas_codec_readreg(0xC);
284 int right_peak = rb->mas_codec_readreg(0xD);
285 #endif
286 /* Devide peak data by 4098 to bring the max
287 value down from ~32k to 8 */
288 left_peak = left_peak/0x1000;
289 right_peak = right_peak/0x1000;
291 /* Make sure they dont stop */
292 if(left_peak<0x1)
293 left_peak = 0x1;
294 if(right_peak<0x1)
295 right_peak = 0x1;
297 /* And make sure they dont go over 8 */
298 if(left_peak>0x8)
299 left_peak = 0x8;
300 if(right_peak>0x8)
301 right_peak = 0x8;
303 /* We need the average of both chanels */
304 avg_peak = ( left_peak + right_peak )/2;
306 /* Set the speed to the peak meter */
307 starfield.z_move = avg_peak;
309 } /* if pulse */
310 #else
311 (void) avg_peak;
312 #endif
313 starfield_move_and_draw(&starfield);
315 #ifdef HAVE_LCD_COLOR
316 rb->lcd_set_foreground(LCD_WHITE);
317 #endif
319 /* if a parameter is updated (by the user), we must print it */
320 if (t_disp > 0)
322 --t_disp;
323 rb->snprintf(str_buffer, sizeof(str_buffer),
324 "star:%d speed:%d",
325 starfield.nb_stars,
326 starfield.z_move);
327 #if LCD_DEPTH > 1
328 rb->lcd_set_foreground(LCD_WHITE);
329 #endif
330 rb->lcd_putsxy(0, LCD_HEIGHT-font_h, str_buffer);
332 rb->lcd_update();
334 button = rb->button_get(false);
335 switch(button)
337 case (STARFIELD_INCREASE_ZMOVE):
338 case (STARFIELD_INCREASE_ZMOVE | BUTTON_REPEAT):
339 ++(starfield.z_move);
340 pulse=false;
341 t_disp=MSG_DISP_TIME;
342 break;
343 case (STARFIELD_DECREASE_ZMOVE):
344 case (STARFIELD_DECREASE_ZMOVE | BUTTON_REPEAT):
345 --(starfield.z_move);
346 pulse=false;
347 t_disp=MSG_DISP_TIME;
348 break;
349 case(STARFIELD_INCREASE_NB_STARS):
350 case(STARFIELD_INCREASE_NB_STARS | BUTTON_REPEAT):
351 starfield_add_stars(&starfield, STARFIELD_INCREASE_STEP);
352 t_disp=MSG_DISP_TIME;
353 break;
354 case(STARFIELD_DECREASE_NB_STARS):
355 case(STARFIELD_DECREASE_NB_STARS | BUTTON_REPEAT):
356 starfield_del_stars(&starfield, STARFIELD_INCREASE_STEP);
357 t_disp=MSG_DISP_TIME;
358 break;
359 #ifdef HAVE_LCD_COLOR
360 case(STARFIELD_TOGGLE_COLOR):
361 starfield.color=!starfield.color;
362 break;
363 #endif
364 #ifdef STARFIELD_RC_QUIT
365 case STARFIELD_RC_QUIT:
366 #endif
367 case(STARFIELD_QUIT):
368 case(SYS_USB_CONNECTED):
369 /* Turn on backlight timeout (revert to settings) */
370 backlight_use_settings(rb); /* backlight control in lib/helper.c*/
371 return PLUGIN_OK;
372 break;
377 /*************************** Plugin entry point ****************************/
379 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
381 int ret;
383 rb = api; /* copy to global api pointer */
384 (void)parameter;
385 /* Turn off backlight timeout */
386 backlight_force_on(rb); /* backlight control in lib/helper.c */
388 ret = plugin_main();
390 return ret;
393 #endif /* #ifdef HAVE_LCD_BITMAP */