as3525: fix r26308
[kugel-rb.git] / apps / plugins / sliding_puzzle.c
blob7bf49c123d90dfc269c22ab6d087ae03b9a4c800
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 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
225 #define PUZZLE_QUIT (BUTTON_REC | BUTTON_PLAY)
226 #define PUZZLE_LEFT BUTTON_VOL_DOWN
227 #define PUZZLE_RIGHT BUTTON_VOL_UP
228 #define PUZZLE_UP BUTTON_PREV
229 #define PUZZLE_DOWN BUTTON_NEXT
230 #define PUZZLE_SHUFFLE BUTTON_REC
231 #define PUZZLE_PICTURE BUTTON_PLAY
233 #else
234 #error No keymap defined!
235 #endif
237 #ifdef HAVE_TOUCHSCREEN
238 #ifndef PUZZLE_QUIT
239 #define PUZZLE_QUIT BUTTON_TOPLEFT
240 #endif
241 #ifndef PUZZLE_LEFT
242 #define PUZZLE_LEFT BUTTON_MIDLEFT
243 #endif
244 #ifndef PUZZLE_RIGHT
245 #define PUZZLE_RIGHT BUTTON_MIDRIGHT
246 #endif
247 #ifndef PUZZLE_UP
248 #define PUZZLE_UP BUTTON_TOPMIDDLE
249 #endif
250 #ifndef PUZZLE_DOWN
251 #define PUZZLE_DOWN BUTTON_BOTTOMMIDDLE
252 #endif
253 #ifndef PUZZLE_SHUFFLE
254 #define PUZZLE_SHUFFLE BUTTON_BOTTOMLEFT
255 #endif
256 #ifndef PUZZLE_PICTURE
257 #define PUZZLE_PICTURE BUTTON_CENTER
258 #endif
259 #ifndef PUZZLE_QUIT_TEXT
260 #define PUZZLE_QUIT_TEXT "[TOPLEFT]"
261 #endif
262 #ifndef PUZZLE_SHUFFLE_TEXT
263 #define PUZZLE_SHUFFLE_TEXT "[BOTTOMLEFT]"
264 #endif
265 #ifndef PUZZLE_PICTURE_TEXT
266 #define PUZZLE_PICTURE_TEXT "[CENTER]"
267 #endif
268 #endif
270 #ifdef HAVE_ALBUMART
271 #include "lib/read_image.h"
272 #define READ_IMAGE read_image_file
273 #else
274 #define READ_IMAGE rb->read_bmp_file
275 #endif
277 #include "pluginbitmaps/sliding_puzzle.h"
278 #define IMAGE_WIDTH BMPWIDTH_sliding_puzzle
279 #define IMAGE_HEIGHT BMPHEIGHT_sliding_puzzle
280 #define IMAGE_SIZE IMAGE_WIDTH
282 /* use a square image, (the default Archos bitmap looks square on its display)
283 Puzzle image dimension is min(lcd_height,lcd_width)
284 4x4 is more convenient for square puzzles
285 Note: sliding_puzzle.bmp should be evenly divisible by SPOTS_X
286 and SPOTS_Y, otherwise lcd_bitmap_part stride won't be correct */
287 #define SPOTS_X 4
288 #define SPOTS_Y 4
289 #define SPOTS_WIDTH (IMAGE_WIDTH / SPOTS_X)
290 #define SPOTS_HEIGHT (IMAGE_HEIGHT / SPOTS_Y)
291 #define NUM_SPOTS (SPOTS_X*SPOTS_Y)
292 #define HOLE_ID (NUM_SPOTS)
293 #define INITIAL_HOLE (HOLE_ID-1)
295 enum picmodes
297 PICMODE_NUMERALS = 0,
298 PICMODE_INITIAL_PICTURE,
299 PICMODE_DEFAULT_PICTURE,
300 #ifdef HAVE_ALBUMART
301 PICMODE_ALBUM_ART,
302 #endif
303 // PICMODE_RANDOM,
304 PICMODE_LAST_XXX /* placeholder */
307 static const char* const picmode_descriptions[] = {
308 "Numerals",
309 "Viewer Picture",
310 "Default Picture",
311 #ifdef HAVE_ALBUMART
312 "Album Art",
313 #endif
314 "Shouldn't Get Here",
317 static int spots[NUM_SPOTS];
318 static int hole = INITIAL_HOLE, moves;
319 static unsigned char s[32];
320 static enum picmodes picmode = PICMODE_INITIAL_PICTURE;
321 static int num_font = FONT_UI;
322 static int moves_font = FONT_UI;
323 static int moves_y = 0;
325 static unsigned char *img_buf;
326 static size_t img_buf_len;
327 #ifdef HAVE_ALBUMART
328 static char albumart_path[MAX_PATH+1];
329 #endif
330 static char img_buf_path[MAX_PATH+1];
332 static const fb_data * puzzle_bmp_ptr;
333 /* initial_bmp_path points to selected bitmap if this game is launched
334 as a viewer for a .bmp file, or NULL if game is launched regular way */
335 static const char * initial_bmp_path=NULL;
337 #ifdef HAVE_ALBUMART
338 const char * get_albumart_bmp_path(void)
340 struct mp3entry* track = rb->audio_current_track();
342 if (!track || !track->path || track->path[0] == '\0')
343 return NULL;
345 if (!rb->search_albumart_files(track, "", albumart_path, MAX_PATH ) )
346 return NULL;
348 albumart_path[ MAX_PATH ] = '\0';
349 return albumart_path;
351 #endif
353 const char * get_random_bmp_path(void)
355 return(initial_bmp_path);
358 static bool load_resize_bitmap(void)
360 int rc;
361 const char * filename = NULL;
363 /* initially assume using the built-in default */
364 puzzle_bmp_ptr = sliding_puzzle;
366 switch( picmode ){
367 /* some modes don't even need to touch disk and trivially succeed */
368 case PICMODE_NUMERALS:
369 case PICMODE_DEFAULT_PICTURE:
370 default:
371 return(true);
373 #ifdef HAVE_ALBUMART
374 case PICMODE_ALBUM_ART:
375 filename = get_albumart_bmp_path();
376 break;
377 #endif
379 case PICMODE_RANDOM:
380 if(NULL == (filename=get_random_bmp_path()) )
381 filename = initial_bmp_path;
382 break;
384 case PICMODE_INITIAL_PICTURE:
385 filename = initial_bmp_path;
386 break;
389 if( filename != NULL )
391 /* if we already loaded image before, don't touch disk */
392 if( 0 == rb->strcmp( filename, img_buf_path ) )
394 puzzle_bmp_ptr = (const fb_data *)img_buf;
395 return true;
398 struct bitmap main_bitmap;
399 rb->memset(&main_bitmap,0,sizeof(struct bitmap));
400 main_bitmap.data = img_buf;
402 main_bitmap.width = IMAGE_WIDTH;
403 main_bitmap.height = IMAGE_HEIGHT;
405 rc = READ_IMAGE( filename, &main_bitmap,
406 img_buf_len,
407 FORMAT_NATIVE|FORMAT_RESIZE|FORMAT_DITHER,
408 NULL);
409 if( rc > 0 )
411 puzzle_bmp_ptr = (const fb_data *)img_buf;
412 rb->strcpy( img_buf_path, filename );
413 return true;
417 /* something must have failed. get_albumart_bmp_path could return
418 NULL if albumart doesn't exist or couldn't be loaded, or
419 read_bmp_file could have failed. return false and caller should
420 try the next mode (PICMODE_DEFAULT_PICTURE and PICMODE_NUMERALS will
421 always succeed) */
422 return false;
425 /* draws a spot at the coordinates (x,y), range of p is 1-20 */
426 static void draw_spot(int p, int x, int y)
428 int w, h;
430 if (p == HOLE_ID)
432 #if LCD_DEPTH==1
433 /* the bottom-right cell of the default sliding_puzzle image is
434 an appropriate hole graphic */
435 rb->lcd_bitmap_part(sliding_puzzle, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
436 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
437 STRIDE( SCREEN_MAIN,
438 BMPWIDTH_sliding_puzzle, BMPHEIGHT_sliding_puzzle),
439 x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
440 #else
441 /* just draw a black rectangle */
442 int old_fg = rb->lcd_get_foreground();
443 rb->lcd_set_foreground(LCD_BLACK);
444 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
445 rb->lcd_set_foreground(LCD_WHITE);
446 rb->lcd_drawrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
447 rb->lcd_set_foreground(old_fg);
448 #endif
450 else if (picmode != PICMODE_NUMERALS)
452 rb->lcd_bitmap_part( puzzle_bmp_ptr, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
453 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
454 STRIDE( SCREEN_MAIN,
455 BMPWIDTH_sliding_puzzle, BMPHEIGHT_sliding_puzzle),
456 x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
457 } else {
458 rb->lcd_drawrect(x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
459 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
460 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2);
461 rb->lcd_set_drawmode(DRMODE_SOLID);
462 rb->snprintf(s, sizeof(s), "%d", p);
463 rb->lcd_setfont(num_font);
464 rb->lcd_getstringsize(s, &w, &h);
465 rb->lcd_putsxy(x + (SPOTS_WIDTH/2) - w / 2,
466 y + (SPOTS_HEIGHT/2) - h / 2, s);
470 /* check if the puzzle is solved */
471 static bool puzzle_finished(void)
473 int i;
474 for (i=0; i<NUM_SPOTS; i++)
475 if (spots[i] != (i+1))
476 return false;
477 return true;
480 /* move a piece in any direction */
481 static void move_spot(int x, int y)
483 int i, w;
484 spots[hole] = spots[hole-x-SPOTS_X*y];
485 hole -= (x+SPOTS_X*y);
486 moves++;
487 rb->lcd_setfont(moves_font);
488 #if LCD_WIDTH > LCD_HEIGHT
489 rb->snprintf(s, sizeof(s), "%d", moves);
490 w = rb->lcd_getstringsize(s, NULL, NULL);
491 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
492 moves_y, s);
493 #else
494 (void)w;
495 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
496 rb->lcd_putsxy(3, moves_y, s);
497 #endif
498 for(i=1;i<=4;i++)
500 draw_spot(HOLE_ID,
501 (hole%SPOTS_X)*SPOTS_WIDTH,
502 (hole/SPOTS_X)*SPOTS_HEIGHT);
503 draw_spot(spots[hole],
504 (hole%SPOTS_X)*SPOTS_WIDTH + (i*x*SPOTS_WIDTH)/5,
505 (hole/SPOTS_X)*SPOTS_HEIGHT + (i*y*SPOTS_HEIGHT)/5);
506 rb->lcd_update();
507 rb->sleep(HZ/50);
509 draw_spot(HOLE_ID,
510 (hole%SPOTS_X)*SPOTS_WIDTH,
511 (hole/SPOTS_X)*SPOTS_HEIGHT);
512 draw_spot(spots[hole],
513 ((hole%SPOTS_X)+x)*SPOTS_WIDTH,
514 ((hole/SPOTS_X)+y)*SPOTS_HEIGHT);
515 rb->lcd_update();
517 spots[hole] = HOLE_ID;
520 static void draw_playfield(void)
522 int i, w;
524 rb->lcd_clear_display();
525 rb->lcd_setfont(moves_font);
526 #if LCD_WIDTH > LCD_HEIGHT
527 rb->lcd_vline(IMAGE_WIDTH, 0, LCD_HEIGHT-1);
528 w = rb->lcd_getstringsize("Moves", NULL, NULL);
529 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
530 10, "Moves");
531 rb->snprintf(s, sizeof(s), "%d", moves);
532 w = rb->lcd_getstringsize(s, NULL, NULL);
533 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
534 moves_y, s);
535 #else
536 (void)w;
537 rb->lcd_hline(0, LCD_WIDTH-1, IMAGE_HEIGHT);
538 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
539 rb->lcd_putsxy(3, moves_y, s);
540 #endif
542 /* draw spots to the lcd */
543 for (i=0; i<NUM_SPOTS; i++)
544 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
546 rb->lcd_update();
549 /* initializes the puzzle */
550 static void puzzle_init(void)
552 int i, r, temp, tsp[NUM_SPOTS];
554 moves = 0;
556 /* shuffle spots */
557 for (i=NUM_SPOTS-1; i>=0; i--) {
558 r = (rb->rand() % (i+1));
560 temp = spots[r];
561 spots[r] = spots[i];
562 spots[i] = temp;
564 if (spots[i]==HOLE_ID)
565 hole = i;
568 /* test if the puzzle is solvable */
569 for (i=0; i<NUM_SPOTS; i++)
570 tsp[i] = spots[i];
571 r=0;
573 /* First, check if the problem has even or odd parity,
574 depending on where the empty square is */
575 if ((((SPOTS_X-1)-hole%SPOTS_X) + ((SPOTS_Y-1)-hole/SPOTS_X))%2 == 1)
576 ++r;
578 /* Now check how many swaps we need to solve it */
579 for (i=0; i<NUM_SPOTS-1; i++) {
580 while (tsp[i] != (i+1)) {
581 temp = tsp[i];
582 tsp[i] = tsp[temp-1];
583 tsp[temp-1] = temp;
584 ++r;
588 /* if the random puzzle isn't solvable just change two spots */
589 if (r%2 == 1) {
590 if (spots[0]!=HOLE_ID && spots[1]!=HOLE_ID) {
591 temp = spots[0];
592 spots[0] = spots[1];
593 spots[1] = temp;
594 } else {
595 temp = spots[2];
596 spots[2] = spots[3];
597 spots[3] = temp;
601 draw_playfield();
604 /* the main game loop */
605 static int puzzle_loop(void)
607 int button;
608 int lastbutton = BUTTON_NONE;
609 bool load_success;
611 puzzle_init();
612 while(true) {
613 button = rb->button_get(true);
614 switch (button) {
615 #ifdef PUZZLE_RC_QUIT
616 case PUZZLE_RC_QUIT:
617 #endif
618 case PUZZLE_QUIT:
619 /* get out of here */
620 return PLUGIN_OK;
622 case PUZZLE_SHUFFLE:
623 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
624 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
625 break;
626 #endif
627 /* mix up the pieces */
628 puzzle_init();
629 break;
631 case PUZZLE_PICTURE:
632 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
633 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
634 break;
635 #endif
636 /* change picture */
637 picmode = (picmode+1)%PICMODE_LAST_XXX;
639 /* if load_resize_bitmap fails to load bitmap, try next picmode
643 load_success = load_resize_bitmap();
644 if( !load_success )
645 picmode = (picmode+1)%PICMODE_LAST_XXX;
647 while( !load_success );
649 /* tell the user what mode we picked in the end! */
650 rb->splash(HZ,picmode_descriptions[ picmode ] );
651 draw_playfield();
652 break;
654 case PUZZLE_LEFT:
655 if ((hole%SPOTS_X)<(SPOTS_X-1) && !puzzle_finished())
656 move_spot(-1, 0);
657 break;
659 case PUZZLE_RIGHT:
660 if ((hole%SPOTS_X)>0 && !puzzle_finished())
661 move_spot(1, 0);
662 break;
664 case PUZZLE_UP:
665 if ((hole/SPOTS_X)<(SPOTS_Y-1) && !puzzle_finished())
666 move_spot(0, -1);
667 break;
669 case PUZZLE_DOWN:
670 if ((hole/SPOTS_X)>0 && !puzzle_finished())
671 move_spot(0, 1);
672 break;
674 default:
675 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
676 return PLUGIN_USB_CONNECTED;
677 break;
679 if (button != BUTTON_NONE)
680 lastbutton = button;
684 enum plugin_status plugin_start(
685 const void* parameter)
687 int i, w, h;
689 initial_bmp_path=(const char *)parameter;
690 img_buf = rb->plugin_get_buffer(&img_buf_len);
691 picmode = PICMODE_INITIAL_PICTURE;
692 img_buf_path[0] = '\0';
694 /* If launched as a viewer, just go straight to the game without
695 bothering with the splash or instructions page */
696 if(parameter==NULL)
698 /* if not launched as a viewer, use default puzzle, and show help */
699 picmode = PICMODE_DEFAULT_PICTURE;
701 /* print title */
702 rb->lcd_getstringsize((unsigned char *)"Sliding Puzzle", &w, &h);
703 w = (w+1)/2;
704 h = (h+1)/2;
705 rb->lcd_clear_display();
706 rb->lcd_putsxy(LCD_WIDTH/2-w, (LCD_HEIGHT/2)-h,
707 (unsigned char *)"Sliding Puzzle");
708 rb->lcd_update();
709 rb->sleep(HZ);
711 /* print instructions */
712 rb->lcd_clear_display();
713 rb->lcd_setfont(FONT_SYSFIXED);
714 #if CONFIG_KEYPAD == RECORDER_PAD || CONFIG_KEYPAD == ARCHOS_AV300_PAD
715 rb->lcd_putsxy(3, 18, "[OFF] to stop");
716 rb->lcd_putsxy(3, 28, "[F1] shuffle");
717 rb->lcd_putsxy(3, 38, "[F2] change pic");
718 #elif CONFIG_KEYPAD == ONDIO_PAD
719 rb->lcd_putsxy(0, 18, "[OFF] to stop");
720 rb->lcd_putsxy(0, 28, "[MODE..] shuffle");
721 rb->lcd_putsxy(0, 38, "[MODE] change pic");
722 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
723 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
724 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
725 rb->lcd_putsxy(0, 18, "[S-MENU] to stop");
726 rb->lcd_putsxy(0, 28, "[S-LEFT] shuffle");
727 rb->lcd_putsxy(0, 38, "[S-RIGHT] change pic");
728 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
729 (CONFIG_KEYPAD == IRIVER_H300_PAD)
730 rb->lcd_putsxy(0, 18, "[STOP] to stop");
731 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
732 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
733 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
734 rb->lcd_putsxy(0, 18, "[OFF] to stop");
735 rb->lcd_putsxy(0, 28, "[REC] shuffle");
736 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
737 #elif CONFIG_KEYPAD == GIGABEAT_PAD
738 rb->lcd_putsxy(0, 18, "[POWER] to stop");
739 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
740 rb->lcd_putsxy(0, 38, "[A] change pic");
741 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
742 (CONFIG_KEYPAD == SANSA_C200_PAD)
743 rb->lcd_putsxy(0, 18, "[OFF] to stop");
744 rb->lcd_putsxy(0, 28, "[REC] shuffle");
745 rb->lcd_putsxy(0, 38, "[SELECT] change pic");
746 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
747 rb->lcd_putsxy(0, 18, "[OFF] to stop");
748 rb->lcd_putsxy(0, 28, "[REW] shuffle");
749 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
750 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
751 rb->lcd_putsxy(0, 18, "[BACK] to stop");
752 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
753 rb->lcd_putsxy(0, 38, "[MENU] change pic");
754 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
755 rb->lcd_putsxy(0, 18, "[REC] to stop");
756 rb->lcd_putsxy(0, 28, "[MODE] shuffle");
757 rb->lcd_putsxy(0, 38, "[MENU] change pic");
758 #endif
759 #ifdef HAVE_TOUCHSCREEN
760 rb->lcd_putsxy(0, 18, PUZZLE_QUIT_TEXT " to stop");
761 rb->lcd_putsxy(0, 28, PUZZLE_SHUFFLE_TEXT " shuffle");
762 rb->lcd_putsxy(0, 38, PUZZLE_PICTURE_TEXT " change pic");
763 #endif
764 #ifdef HAVE_ALBUMART
765 rb->lcd_putsxy(0,48," pic->albumart->num");
766 #else
767 rb->lcd_putsxy(0,48," pic<->num");
768 #endif
769 rb->lcd_update();
770 rb->button_get_w_tmo(HZ*2);
773 hole = INITIAL_HOLE;
775 if( !load_resize_bitmap() )
777 rb->lcd_clear_display();
778 rb->splash(HZ*2,"Failed to load bitmap!");
779 return PLUGIN_OK;
782 /* Calculate possible font sizes and text positions */
783 rb->lcd_setfont(FONT_UI);
784 rb->lcd_getstringsize("15", &w, &h);
785 if ((w > (SPOTS_WIDTH-2)) || (h > (SPOTS_HEIGHT-2)))
786 num_font = FONT_SYSFIXED;
788 #if LCD_WIDTH > LCD_HEIGHT
789 rb->lcd_getstringsize("Moves", &w, &h);
790 if (w > (LCD_WIDTH-IMAGE_WIDTH-1))
791 moves_font = FONT_SYSFIXED;
792 rb->lcd_setfont(moves_font);
793 rb->lcd_getstringsize("Moves", &w, &h);
794 moves_y = 10 + h;
795 #else
796 rb->lcd_getstringsize("Moves: 999", &w, &h);
797 if ((w > LCD_WIDTH) || (h > (LCD_HEIGHT-IMAGE_HEIGHT-1)))
798 moves_font = FONT_SYSFIXED;
799 rb->lcd_setfont(moves_font);
800 rb->lcd_getstringsize("Moves: 999", &w, &h);
801 moves_y = (IMAGE_HEIGHT+1+(LCD_HEIGHT-IMAGE_HEIGHT-1)/2) - h / 2;
802 #endif
803 for (i=0; i<NUM_SPOTS; i++)
804 spots[i]=(i+1);
806 #ifdef HAVE_LCD_COLOR
807 rb->lcd_set_background(LCD_BLACK);
808 rb->lcd_set_foreground(LCD_WHITE);
809 rb->lcd_set_backdrop(NULL);
810 #elif LCD_DEPTH > 1
811 rb->lcd_set_background(LCD_WHITE);
812 rb->lcd_set_foreground(LCD_BLACK);
813 rb->lcd_set_backdrop(NULL);
814 #endif
816 draw_playfield();
817 rb->sleep(HZ*2);
819 return puzzle_loop();
822 #endif