Packard Bell Vibe 500: More plugin keymaps (second portion).
[kugel-rb.git] / apps / plugins / sliding_puzzle.c
blob0f5f5fd72262c5a17b254e02ceecb325ce619b90
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Vicentini Martin
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"
23 #ifdef HAVE_LCD_BITMAP
24 PLUGIN_HEADER
26 /* variable button definitions */
27 #if CONFIG_KEYPAD == RECORDER_PAD
28 #define PUZZLE_QUIT BUTTON_OFF
29 #define PUZZLE_LEFT BUTTON_LEFT
30 #define PUZZLE_RIGHT BUTTON_RIGHT
31 #define PUZZLE_UP BUTTON_UP
32 #define PUZZLE_DOWN BUTTON_DOWN
33 #define PUZZLE_SHUFFLE BUTTON_F1
34 #define PUZZLE_PICTURE BUTTON_F2
36 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
37 #define PUZZLE_QUIT BUTTON_OFF
38 #define PUZZLE_LEFT BUTTON_LEFT
39 #define PUZZLE_RIGHT BUTTON_RIGHT
40 #define PUZZLE_UP BUTTON_UP
41 #define PUZZLE_DOWN BUTTON_DOWN
42 #define PUZZLE_SHUFFLE BUTTON_F1
43 #define PUZZLE_PICTURE BUTTON_F2
45 #elif CONFIG_KEYPAD == ONDIO_PAD
46 #define PUZZLE_QUIT BUTTON_OFF
47 #define PUZZLE_LEFT BUTTON_LEFT
48 #define PUZZLE_RIGHT BUTTON_RIGHT
49 #define PUZZLE_UP BUTTON_UP
50 #define PUZZLE_DOWN BUTTON_DOWN
51 #define PUZZLE_SHUFFLE_PICTURE_PRE BUTTON_MENU
52 #define PUZZLE_SHUFFLE (BUTTON_MENU | BUTTON_REPEAT)
53 #define PUZZLE_PICTURE (BUTTON_MENU | BUTTON_REL)
55 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
56 (CONFIG_KEYPAD == IRIVER_H300_PAD)
57 #define PUZZLE_QUIT BUTTON_OFF
58 #define PUZZLE_LEFT BUTTON_LEFT
59 #define PUZZLE_RIGHT BUTTON_RIGHT
60 #define PUZZLE_UP BUTTON_UP
61 #define PUZZLE_DOWN BUTTON_DOWN
62 #define PUZZLE_SHUFFLE BUTTON_SELECT
63 #define PUZZLE_PICTURE BUTTON_ON
65 #define PUZZLE_RC_QUIT BUTTON_RC_STOP
67 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
68 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
69 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
70 #define PUZZLE_QUIT (BUTTON_SELECT | BUTTON_MENU)
71 #define PUZZLE_LEFT BUTTON_LEFT
72 #define PUZZLE_RIGHT BUTTON_RIGHT
73 #define PUZZLE_UP BUTTON_MENU
74 #define PUZZLE_DOWN BUTTON_PLAY
75 #define PUZZLE_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
76 #define PUZZLE_PICTURE (BUTTON_SELECT | BUTTON_RIGHT)
78 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
79 #define PUZZLE_QUIT BUTTON_POWER
80 #define PUZZLE_LEFT BUTTON_LEFT
81 #define PUZZLE_RIGHT BUTTON_RIGHT
82 #define PUZZLE_UP BUTTON_UP
83 #define PUZZLE_DOWN BUTTON_DOWN
84 #define PUZZLE_SHUFFLE BUTTON_REC
85 #define PUZZLE_PICTURE BUTTON_PLAY
87 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
88 #define PUZZLE_QUIT BUTTON_POWER
89 #define PUZZLE_LEFT BUTTON_LEFT
90 #define PUZZLE_RIGHT BUTTON_RIGHT
91 #define PUZZLE_UP BUTTON_UP
92 #define PUZZLE_DOWN BUTTON_DOWN
93 #define PUZZLE_SHUFFLE BUTTON_SELECT
94 #define PUZZLE_PICTURE BUTTON_A
96 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
97 (CONFIG_KEYPAD == SANSA_C200_PAD)
98 #define PUZZLE_QUIT BUTTON_POWER
99 #define PUZZLE_LEFT BUTTON_LEFT
100 #define PUZZLE_RIGHT BUTTON_RIGHT
101 #define PUZZLE_UP BUTTON_UP
102 #define PUZZLE_DOWN BUTTON_DOWN
103 #define PUZZLE_SHUFFLE BUTTON_REC
104 #define PUZZLE_PICTURE BUTTON_SELECT
106 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
107 #define PUZZLE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
108 #define PUZZLE_LEFT BUTTON_LEFT
109 #define PUZZLE_RIGHT BUTTON_RIGHT
110 #define PUZZLE_UP BUTTON_UP
111 #define PUZZLE_DOWN BUTTON_DOWN
112 #define PUZZLE_SHUFFLE BUTTON_SELECT|BUTTON_DOWN
113 #define PUZZLE_PICTURE BUTTON_SELECT
115 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
116 #define PUZZLE_QUIT BUTTON_POWER
117 #define PUZZLE_LEFT BUTTON_LEFT
118 #define PUZZLE_RIGHT BUTTON_RIGHT
119 #define PUZZLE_UP BUTTON_UP
120 #define PUZZLE_DOWN BUTTON_DOWN
121 #define PUZZLE_SHUFFLE BUTTON_HOME
122 #define PUZZLE_PICTURE BUTTON_SELECT
124 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
125 #define PUZZLE_QUIT BUTTON_POWER
126 #define PUZZLE_LEFT BUTTON_LEFT
127 #define PUZZLE_RIGHT BUTTON_RIGHT
128 #define PUZZLE_UP BUTTON_UP
129 #define PUZZLE_DOWN BUTTON_DOWN
130 #define PUZZLE_SHUFFLE (BUTTON_SELECT | BUTTON_UP)
131 #define PUZZLE_PICTURE (BUTTON_SELECT | BUTTON_REL)
133 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
134 #define PUZZLE_QUIT BUTTON_POWER
135 #define PUZZLE_LEFT BUTTON_LEFT
136 #define PUZZLE_RIGHT BUTTON_RIGHT
137 #define PUZZLE_UP BUTTON_SCROLL_UP
138 #define PUZZLE_DOWN BUTTON_SCROLL_DOWN
139 #define PUZZLE_SHUFFLE BUTTON_REW
140 #define PUZZLE_PICTURE BUTTON_PLAY
142 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
143 #define PUZZLE_QUIT BUTTON_BACK
144 #define PUZZLE_LEFT BUTTON_LEFT
145 #define PUZZLE_RIGHT BUTTON_RIGHT
146 #define PUZZLE_UP BUTTON_UP
147 #define PUZZLE_DOWN BUTTON_DOWN
148 #define PUZZLE_SHUFFLE BUTTON_SELECT
149 #define PUZZLE_PICTURE BUTTON_MENU
151 #elif (CONFIG_KEYPAD == MROBE100_PAD)
152 #define PUZZLE_QUIT BUTTON_POWER
153 #define PUZZLE_LEFT BUTTON_LEFT
154 #define PUZZLE_RIGHT BUTTON_RIGHT
155 #define PUZZLE_UP BUTTON_UP
156 #define PUZZLE_DOWN BUTTON_DOWN
157 #define PUZZLE_SHUFFLE BUTTON_SELECT
158 #define PUZZLE_PICTURE BUTTON_DISPLAY
160 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
161 #define PUZZLE_QUIT BUTTON_RC_REC
162 #define PUZZLE_LEFT BUTTON_RC_REW
163 #define PUZZLE_RIGHT BUTTON_RC_FF
164 #define PUZZLE_UP BUTTON_RC_VOL_UP
165 #define PUZZLE_DOWN BUTTON_RC_VOL_DOWN
166 #define PUZZLE_SHUFFLE BUTTON_RC_MODE
167 #define PUZZLE_PICTURE BUTTON_RC_MENU
169 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
170 #define PUZZLE_QUIT BUTTON_POWER
171 #define PUZZLE_QUIT_TEXT "[POWER]"
173 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
174 #define PUZZLE_QUIT BUTTON_BACK
175 #define PUZZLE_LEFT BUTTON_LEFT
176 #define PUZZLE_RIGHT BUTTON_RIGHT
177 #define PUZZLE_UP BUTTON_UP
178 #define PUZZLE_DOWN BUTTON_DOWN
179 #define PUZZLE_SHUFFLE BUTTON_PLAY
180 #define PUZZLE_PICTURE BUTTON_MENU
182 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
183 #define PUZZLE_QUIT BUTTON_POWER
184 #define PUZZLE_LEFT BUTTON_LEFT
185 #define PUZZLE_RIGHT BUTTON_RIGHT
186 #define PUZZLE_UP BUTTON_UP
187 #define PUZZLE_DOWN BUTTON_DOWN
188 #define PUZZLE_SHUFFLE BUTTON_VIEW
189 #define PUZZLE_PICTURE BUTTON_MENU
191 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
192 #define PUZZLE_QUIT BUTTON_POWER
193 #define PUZZLE_LEFT BUTTON_PREV
194 #define PUZZLE_RIGHT BUTTON_NEXT
195 #define PUZZLE_UP BUTTON_UP
196 #define PUZZLE_DOWN BUTTON_DOWN
197 #define PUZZLE_SHUFFLE BUTTON_RIGHT
198 #define PUZZLE_PICTURE BUTTON_MENU
200 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
201 CONFIG_KEYPAD == ONDAVX777_PAD || \
202 CONFIG_KEYPAD == MROBE500_PAD
203 #define PUZZLE_QUIT BUTTON_POWER
204 #define PUZZLE_QUIT_TEXT "[POWER]"
206 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
207 #define PUZZLE_QUIT BUTTON_REC
208 #define PUZZLE_LEFT BUTTON_LEFT
209 #define PUZZLE_RIGHT BUTTON_RIGHT
210 #define PUZZLE_UP BUTTON_UP
211 #define PUZZLE_DOWN BUTTON_DOWN
212 #define PUZZLE_SHUFFLE BUTTON_REW
213 #define PUZZLE_PICTURE BUTTON_PLAY
215 #elif (CONFIG_KEYPAD == PBELL_VIBE500_PAD)
216 #define PUZZLE_QUIT BUTTON_REC
217 #define PUZZLE_LEFT BUTTON_PREV
218 #define PUZZLE_RIGHT BUTTON_NEXT
219 #define PUZZLE_UP BUTTON_UP
220 #define PUZZLE_DOWN BUTTON_DOWN
221 #define PUZZLE_SHUFFLE BUTTON_CANCEL
222 #define PUZZLE_PICTURE BUTTON_MENU
224 #else
225 #error No keymap defined!
226 #endif
228 #ifdef HAVE_TOUCHSCREEN
229 #ifndef PUZZLE_QUIT
230 #define PUZZLE_QUIT BUTTON_TOPLEFT
231 #endif
232 #ifndef PUZZLE_LEFT
233 #define PUZZLE_LEFT BUTTON_MIDLEFT
234 #endif
235 #ifndef PUZZLE_RIGHT
236 #define PUZZLE_RIGHT BUTTON_MIDRIGHT
237 #endif
238 #ifndef PUZZLE_UP
239 #define PUZZLE_UP BUTTON_TOPMIDDLE
240 #endif
241 #ifndef PUZZLE_DOWN
242 #define PUZZLE_DOWN BUTTON_BOTTOMMIDDLE
243 #endif
244 #ifndef PUZZLE_SHUFFLE
245 #define PUZZLE_SHUFFLE BUTTON_BOTTOMLEFT
246 #endif
247 #ifndef PUZZLE_PICTURE
248 #define PUZZLE_PICTURE BUTTON_CENTER
249 #endif
250 #ifndef PUZZLE_QUIT_TEXT
251 #define PUZZLE_QUIT_TEXT "[TOPLEFT]"
252 #endif
253 #ifndef PUZZLE_SHUFFLE_TEXT
254 #define PUZZLE_SHUFFLE_TEXT "[BOTTOMLEFT]"
255 #endif
256 #ifndef PUZZLE_PICTURE_TEXT
257 #define PUZZLE_PICTURE_TEXT "[CENTER]"
258 #endif
259 #endif
261 #ifdef HAVE_ALBUMART
262 #include "lib/read_image.h"
263 #define READ_IMAGE read_image_file
264 #else
265 #define READ_IMAGE rb->read_bmp_file
266 #endif
268 #include "pluginbitmaps/sliding_puzzle.h"
269 #define IMAGE_WIDTH BMPWIDTH_sliding_puzzle
270 #define IMAGE_HEIGHT BMPHEIGHT_sliding_puzzle
271 #define IMAGE_SIZE IMAGE_WIDTH
273 /* use a square image, (the default Archos bitmap looks square on its display)
274 Puzzle image dimension is min(lcd_height,lcd_width)
275 4x4 is more convenient for square puzzles
276 Note: sliding_puzzle.bmp should be evenly divisible by SPOTS_X
277 and SPOTS_Y, otherwise lcd_bitmap_part stride won't be correct */
278 #define SPOTS_X 4
279 #define SPOTS_Y 4
280 #define SPOTS_WIDTH (IMAGE_WIDTH / SPOTS_X)
281 #define SPOTS_HEIGHT (IMAGE_HEIGHT / SPOTS_Y)
282 #define NUM_SPOTS (SPOTS_X*SPOTS_Y)
283 #define HOLE_ID (NUM_SPOTS)
284 #define INITIAL_HOLE (HOLE_ID-1)
286 enum picmodes
288 PICMODE_NUMERALS = 0,
289 PICMODE_INITIAL_PICTURE,
290 PICMODE_DEFAULT_PICTURE,
291 #ifdef HAVE_ALBUMART
292 PICMODE_ALBUM_ART,
293 #endif
294 // PICMODE_RANDOM,
295 PICMODE_LAST_XXX /* placeholder */
298 static const char* const picmode_descriptions[] = {
299 "Numerals",
300 "Viewer Picture",
301 "Default Picture",
302 #ifdef HAVE_ALBUMART
303 "Album Art",
304 #endif
305 "Shouldn't Get Here",
308 static int spots[NUM_SPOTS];
309 static int hole = INITIAL_HOLE, moves;
310 static unsigned char s[32];
311 static enum picmodes picmode = PICMODE_INITIAL_PICTURE;
312 static int num_font = FONT_UI;
313 static int moves_font = FONT_UI;
314 static int moves_y = 0;
316 static unsigned char *img_buf;
317 static size_t img_buf_len;
318 #ifdef HAVE_ALBUMART
319 static char albumart_path[MAX_PATH+1];
320 #endif
321 static char img_buf_path[MAX_PATH+1];
323 static const fb_data * puzzle_bmp_ptr;
324 /* initial_bmp_path points to selected bitmap if this game is launched
325 as a viewer for a .bmp file, or NULL if game is launched regular way */
326 static const char * initial_bmp_path=NULL;
328 #ifdef HAVE_ALBUMART
329 const char * get_albumart_bmp_path(void)
331 struct mp3entry* track = rb->audio_current_track();
333 if (!track || !track->path || track->path[0] == '\0')
334 return NULL;
336 if (!rb->search_albumart_files(track, "", albumart_path, MAX_PATH ) )
337 return NULL;
339 albumart_path[ MAX_PATH ] = '\0';
340 return albumart_path;
342 #endif
344 const char * get_random_bmp_path(void)
346 return(initial_bmp_path);
349 static bool load_resize_bitmap(void)
351 int rc;
352 const char * filename = NULL;
354 /* initially assume using the built-in default */
355 puzzle_bmp_ptr = sliding_puzzle;
357 switch( picmode ){
358 /* some modes don't even need to touch disk and trivially succeed */
359 case PICMODE_NUMERALS:
360 case PICMODE_DEFAULT_PICTURE:
361 default:
362 return(true);
364 #ifdef HAVE_ALBUMART
365 case PICMODE_ALBUM_ART:
366 filename = get_albumart_bmp_path();
367 break;
368 #endif
370 case PICMODE_RANDOM:
371 if(NULL == (filename=get_random_bmp_path()) )
372 filename = initial_bmp_path;
373 break;
375 case PICMODE_INITIAL_PICTURE:
376 filename = initial_bmp_path;
377 break;
380 if( filename != NULL )
382 /* if we already loaded image before, don't touch disk */
383 if( 0 == rb->strcmp( filename, img_buf_path ) )
385 puzzle_bmp_ptr = (const fb_data *)img_buf;
386 return true;
389 struct bitmap main_bitmap;
390 rb->memset(&main_bitmap,0,sizeof(struct bitmap));
391 main_bitmap.data = img_buf;
393 main_bitmap.width = IMAGE_WIDTH;
394 main_bitmap.height = IMAGE_HEIGHT;
396 rc = READ_IMAGE( filename, &main_bitmap,
397 img_buf_len,
398 FORMAT_NATIVE|FORMAT_RESIZE|FORMAT_DITHER,
399 NULL);
400 if( rc > 0 )
402 puzzle_bmp_ptr = (const fb_data *)img_buf;
403 rb->strcpy( img_buf_path, filename );
404 return true;
408 /* something must have failed. get_albumart_bmp_path could return
409 NULL if albumart doesn't exist or couldn't be loaded, or
410 read_bmp_file could have failed. return false and caller should
411 try the next mode (PICMODE_DEFAULT_PICTURE and PICMODE_NUMERALS will
412 always succeed) */
413 return false;
416 /* draws a spot at the coordinates (x,y), range of p is 1-20 */
417 static void draw_spot(int p, int x, int y)
419 int w, h;
421 if (p == HOLE_ID)
423 #if LCD_DEPTH==1
424 /* the bottom-right cell of the default sliding_puzzle image is
425 an appropriate hole graphic */
426 rb->lcd_bitmap_part(sliding_puzzle, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
427 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
428 STRIDE( SCREEN_MAIN,
429 BMPWIDTH_sliding_puzzle, BMPHEIGHT_sliding_puzzle),
430 x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
431 #else
432 /* just draw a black rectangle */
433 int old_fg = rb->lcd_get_foreground();
434 rb->lcd_set_foreground(LCD_BLACK);
435 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
436 rb->lcd_set_foreground(old_fg);
437 #endif
439 else if (picmode != PICMODE_NUMERALS)
441 rb->lcd_bitmap_part( puzzle_bmp_ptr, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
442 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
443 STRIDE( SCREEN_MAIN,
444 BMPWIDTH_sliding_puzzle, BMPHEIGHT_sliding_puzzle),
445 x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
446 } else {
447 rb->lcd_drawrect(x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
448 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
449 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2);
450 rb->lcd_set_drawmode(DRMODE_SOLID);
451 rb->snprintf(s, sizeof(s), "%d", p);
452 rb->lcd_setfont(num_font);
453 rb->lcd_getstringsize(s, &w, &h);
454 rb->lcd_putsxy(x + (SPOTS_WIDTH/2) - w / 2,
455 y + (SPOTS_HEIGHT/2) - h / 2, s);
459 /* check if the puzzle is solved */
460 static bool puzzle_finished(void)
462 int i;
463 for (i=0; i<NUM_SPOTS; i++)
464 if (spots[i] != (i+1))
465 return false;
466 return true;
469 /* move a piece in any direction */
470 static void move_spot(int x, int y)
472 int i, w;
473 spots[hole] = spots[hole-x-SPOTS_X*y];
474 hole -= (x+SPOTS_X*y);
475 moves++;
476 rb->lcd_setfont(moves_font);
477 #if LCD_WIDTH > LCD_HEIGHT
478 rb->snprintf(s, sizeof(s), "%d", moves);
479 w = rb->lcd_getstringsize(s, NULL, NULL);
480 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
481 moves_y, s);
482 #else
483 (void)w;
484 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
485 rb->lcd_putsxy(3, moves_y, s);
486 #endif
487 for(i=1;i<=4;i++)
489 draw_spot(HOLE_ID,
490 (hole%SPOTS_X)*SPOTS_WIDTH,
491 (hole/SPOTS_X)*SPOTS_HEIGHT);
492 draw_spot(spots[hole],
493 (hole%SPOTS_X)*SPOTS_WIDTH + (i*x*SPOTS_WIDTH)/5,
494 (hole/SPOTS_X)*SPOTS_HEIGHT + (i*y*SPOTS_HEIGHT)/5);
495 rb->lcd_update();
496 rb->sleep(HZ/50);
498 draw_spot(HOLE_ID,
499 (hole%SPOTS_X)*SPOTS_WIDTH,
500 (hole/SPOTS_X)*SPOTS_HEIGHT);
501 draw_spot(spots[hole],
502 ((hole%SPOTS_X)+x)*SPOTS_WIDTH,
503 ((hole/SPOTS_X)+y)*SPOTS_HEIGHT);
504 rb->lcd_update();
506 spots[hole] = HOLE_ID;
509 static void draw_playfield(void)
511 int i, w;
513 rb->lcd_clear_display();
514 rb->lcd_setfont(moves_font);
515 #if LCD_WIDTH > LCD_HEIGHT
516 rb->lcd_vline(IMAGE_WIDTH, 0, LCD_HEIGHT-1);
517 w = rb->lcd_getstringsize("Moves", NULL, NULL);
518 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
519 10, "Moves");
520 rb->snprintf(s, sizeof(s), "%d", moves);
521 w = rb->lcd_getstringsize(s, NULL, NULL);
522 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
523 moves_y, s);
524 #else
525 (void)w;
526 rb->lcd_hline(0, LCD_WIDTH-1, IMAGE_HEIGHT);
527 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
528 rb->lcd_putsxy(3, moves_y, s);
529 #endif
531 /* draw spots to the lcd */
532 for (i=0; i<NUM_SPOTS; i++)
533 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
535 rb->lcd_update();
538 /* initializes the puzzle */
539 static void puzzle_init(void)
541 int i, r, temp, tsp[NUM_SPOTS];
543 moves = 0;
545 /* shuffle spots */
546 for (i=NUM_SPOTS-1; i>=0; i--) {
547 r = (rb->rand() % (i+1));
549 temp = spots[r];
550 spots[r] = spots[i];
551 spots[i] = temp;
553 if (spots[i]==HOLE_ID)
554 hole = i;
557 /* test if the puzzle is solvable */
558 for (i=0; i<NUM_SPOTS; i++)
559 tsp[i] = spots[i];
560 r=0;
562 /* First, check if the problem has even or odd parity,
563 depending on where the empty square is */
564 if ((((SPOTS_X-1)-hole%SPOTS_X) + ((SPOTS_Y-1)-hole/SPOTS_X))%2 == 1)
565 ++r;
567 /* Now check how many swaps we need to solve it */
568 for (i=0; i<NUM_SPOTS-1; i++) {
569 while (tsp[i] != (i+1)) {
570 temp = tsp[i];
571 tsp[i] = tsp[temp-1];
572 tsp[temp-1] = temp;
573 ++r;
577 /* if the random puzzle isn't solvable just change two spots */
578 if (r%2 == 1) {
579 if (spots[0]!=HOLE_ID && spots[1]!=HOLE_ID) {
580 temp = spots[0];
581 spots[0] = spots[1];
582 spots[1] = temp;
583 } else {
584 temp = spots[2];
585 spots[2] = spots[3];
586 spots[3] = temp;
590 draw_playfield();
593 /* the main game loop */
594 static int puzzle_loop(void)
596 int button;
597 int lastbutton = BUTTON_NONE;
598 bool load_success;
600 puzzle_init();
601 while(true) {
602 button = rb->button_get(true);
603 switch (button) {
604 #ifdef PUZZLE_RC_QUIT
605 case PUZZLE_RC_QUIT:
606 #endif
607 case PUZZLE_QUIT:
608 /* get out of here */
609 return PLUGIN_OK;
611 case PUZZLE_SHUFFLE:
612 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
613 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
614 break;
615 #endif
616 /* mix up the pieces */
617 puzzle_init();
618 break;
620 case PUZZLE_PICTURE:
621 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
622 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
623 break;
624 #endif
625 /* change picture */
626 picmode = (picmode+1)%PICMODE_LAST_XXX;
628 /* if load_resize_bitmap fails to load bitmap, try next picmode
632 load_success = load_resize_bitmap();
633 if( !load_success )
634 picmode = (picmode+1)%PICMODE_LAST_XXX;
636 while( !load_success );
638 /* tell the user what mode we picked in the end! */
639 rb->splash(HZ,picmode_descriptions[ picmode ] );
640 draw_playfield();
641 break;
643 case PUZZLE_LEFT:
644 if ((hole%SPOTS_X)<(SPOTS_X-1) && !puzzle_finished())
645 move_spot(-1, 0);
646 break;
648 case PUZZLE_RIGHT:
649 if ((hole%SPOTS_X)>0 && !puzzle_finished())
650 move_spot(1, 0);
651 break;
653 case PUZZLE_UP:
654 if ((hole/SPOTS_X)<(SPOTS_Y-1) && !puzzle_finished())
655 move_spot(0, -1);
656 break;
658 case PUZZLE_DOWN:
659 if ((hole/SPOTS_X)>0 && !puzzle_finished())
660 move_spot(0, 1);
661 break;
663 default:
664 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
665 return PLUGIN_USB_CONNECTED;
666 break;
668 if (button != BUTTON_NONE)
669 lastbutton = button;
673 enum plugin_status plugin_start(
674 const void* parameter)
676 int i, w, h;
678 initial_bmp_path=(const char *)parameter;
679 img_buf = rb->plugin_get_buffer(&img_buf_len);
680 picmode = PICMODE_INITIAL_PICTURE;
681 img_buf_path[0] = '\0';
683 /* If launched as a viewer, just go straight to the game without
684 bothering with the splash or instructions page */
685 if(parameter==NULL)
687 /* if not launched as a viewer, use default puzzle, and show help */
688 picmode = PICMODE_DEFAULT_PICTURE;
690 /* print title */
691 rb->lcd_getstringsize((unsigned char *)"Sliding Puzzle", &w, &h);
692 w = (w+1)/2;
693 h = (h+1)/2;
694 rb->lcd_clear_display();
695 rb->lcd_putsxy(LCD_WIDTH/2-w, (LCD_HEIGHT/2)-h,
696 (unsigned char *)"Sliding Puzzle");
697 rb->lcd_update();
698 rb->sleep(HZ);
700 /* print instructions */
701 rb->lcd_clear_display();
702 rb->lcd_setfont(FONT_SYSFIXED);
703 #if CONFIG_KEYPAD == RECORDER_PAD || CONFIG_KEYPAD == ARCHOS_AV300_PAD
704 rb->lcd_putsxy(3, 18, "[OFF] to stop");
705 rb->lcd_putsxy(3, 28, "[F1] shuffle");
706 rb->lcd_putsxy(3, 38, "[F2] change pic");
707 #elif CONFIG_KEYPAD == ONDIO_PAD
708 rb->lcd_putsxy(0, 18, "[OFF] to stop");
709 rb->lcd_putsxy(0, 28, "[MODE..] shuffle");
710 rb->lcd_putsxy(0, 38, "[MODE] change pic");
711 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
712 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
713 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
714 rb->lcd_putsxy(0, 18, "[S-MENU] to stop");
715 rb->lcd_putsxy(0, 28, "[S-LEFT] shuffle");
716 rb->lcd_putsxy(0, 38, "[S-RIGHT] change pic");
717 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
718 (CONFIG_KEYPAD == IRIVER_H300_PAD)
719 rb->lcd_putsxy(0, 18, "[STOP] to stop");
720 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
721 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
722 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
723 rb->lcd_putsxy(0, 18, "[OFF] to stop");
724 rb->lcd_putsxy(0, 28, "[REC] shuffle");
725 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
726 #elif CONFIG_KEYPAD == GIGABEAT_PAD
727 rb->lcd_putsxy(0, 18, "[POWER] to stop");
728 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
729 rb->lcd_putsxy(0, 38, "[A] change pic");
730 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
731 (CONFIG_KEYPAD == SANSA_C200_PAD)
732 rb->lcd_putsxy(0, 18, "[OFF] to stop");
733 rb->lcd_putsxy(0, 28, "[REC] shuffle");
734 rb->lcd_putsxy(0, 38, "[SELECT] change pic");
735 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
736 rb->lcd_putsxy(0, 18, "[OFF] to stop");
737 rb->lcd_putsxy(0, 28, "[REW] shuffle");
738 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
739 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
740 rb->lcd_putsxy(0, 18, "[BACK] to stop");
741 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
742 rb->lcd_putsxy(0, 38, "[MENU] change pic");
743 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
744 rb->lcd_putsxy(0, 18, "[REC] to stop");
745 rb->lcd_putsxy(0, 28, "[MODE] shuffle");
746 rb->lcd_putsxy(0, 38, "[MENU] change pic");
747 #endif
748 #ifdef HAVE_TOUCHSCREEN
749 rb->lcd_putsxy(0, 18, PUZZLE_QUIT_TEXT " to stop");
750 rb->lcd_putsxy(0, 28, PUZZLE_SHUFFLE_TEXT " shuffle");
751 rb->lcd_putsxy(0, 38, PUZZLE_PICTURE_TEXT " change pic");
752 #endif
753 #ifdef HAVE_ALBUMART
754 rb->lcd_putsxy(0,48," pic->albumart->num");
755 #else
756 rb->lcd_putsxy(0,48," pic<->num");
757 #endif
758 rb->lcd_update();
759 rb->button_get_w_tmo(HZ*2);
762 hole = INITIAL_HOLE;
764 if( !load_resize_bitmap() )
766 rb->lcd_clear_display();
767 rb->splash(HZ*2,"Failed to load bitmap!");
768 return PLUGIN_OK;
771 /* Calculate possible font sizes and text positions */
772 rb->lcd_setfont(FONT_UI);
773 rb->lcd_getstringsize("15", &w, &h);
774 if ((w > (SPOTS_WIDTH-2)) || (h > (SPOTS_HEIGHT-2)))
775 num_font = FONT_SYSFIXED;
777 #if LCD_WIDTH > LCD_HEIGHT
778 rb->lcd_getstringsize("Moves", &w, &h);
779 if (w > (LCD_WIDTH-IMAGE_WIDTH-1))
780 moves_font = FONT_SYSFIXED;
781 rb->lcd_setfont(moves_font);
782 rb->lcd_getstringsize("Moves", &w, &h);
783 moves_y = 10 + h;
784 #else
785 rb->lcd_getstringsize("Moves: 999", &w, &h);
786 if ((w > LCD_WIDTH) || (h > (LCD_HEIGHT-IMAGE_HEIGHT-1)))
787 moves_font = FONT_SYSFIXED;
788 rb->lcd_setfont(moves_font);
789 rb->lcd_getstringsize("Moves: 999", &w, &h);
790 moves_y = (IMAGE_HEIGHT+1+(LCD_HEIGHT-IMAGE_HEIGHT-1)/2) - h / 2;
791 #endif
792 for (i=0; i<NUM_SPOTS; i++)
793 spots[i]=(i+1);
795 #ifdef HAVE_LCD_COLOR
796 rb->lcd_set_background(LCD_BLACK);
797 rb->lcd_set_foreground(LCD_WHITE);
798 rb->lcd_set_backdrop(NULL);
799 #elif LCD_DEPTH > 1
800 rb->lcd_set_background(LCD_WHITE);
801 rb->lcd_set_foreground(LCD_BLACK);
802 rb->lcd_set_backdrop(NULL);
803 #endif
805 draw_playfield();
806 rb->sleep(HZ*2);
808 return puzzle_loop();
811 #endif