Meizu M3: initial version of battery readout and (uncalibrated) charge/discharge...
[kugel-rb.git] / apps / plugins / sliding_puzzle.c
blob7087fd64a583cfe7471dc54d5a6f41269e614904
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 == COWOND2_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 == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
192 #define PUZZLE_QUIT BUTTON_POWER
193 #define PUZZLE_QUIT_TEXT "[POWER]"
195 #else
196 #error No keymap defined!
197 #endif
199 #ifdef HAVE_TOUCHSCREEN
200 #ifndef PUZZLE_QUIT
201 #define PUZZLE_QUIT BUTTON_TOPLEFT
202 #endif
203 #ifndef PUZZLE_LEFT
204 #define PUZZLE_LEFT BUTTON_MIDLEFT
205 #endif
206 #ifndef PUZZLE_RIGHT
207 #define PUZZLE_RIGHT BUTTON_MIDRIGHT
208 #endif
209 #ifndef PUZZLE_UP
210 #define PUZZLE_UP BUTTON_TOPMIDDLE
211 #endif
212 #ifndef PUZZLE_DOWN
213 #define PUZZLE_DOWN BUTTON_BOTTOMMIDDLE
214 #endif
215 #ifndef PUZZLE_SHUFFLE
216 #define PUZZLE_SHUFFLE BUTTON_BOTTOMLEFT
217 #endif
218 #ifndef PUZZLE_PICTURE
219 #define PUZZLE_PICTURE BUTTON_CENTER
220 #endif
221 #ifndef PUZZLE_QUIT_TEXT
222 #define PUZZLE_QUIT_TEXT "[TOPLEFT]"
223 #endif
224 #ifndef PUZZLE_SHUFFLE_TEXT
225 #define PUZZLE_SHUFFLE_TEXT "[BOTTOMLEFT]"
226 #endif
227 #ifndef PUZZLE_PICTURE_TEXT
228 #define PUZZLE_PICTURE_TEXT "[CENTER]"
229 #endif
230 #endif
232 #ifdef HAVE_ALBUMART
233 #include "lib/read_image.h"
234 #define READ_IMAGE read_image_file
235 #else
236 #define READ_IMAGE rb->read_bmp_file
237 #endif
239 #include "pluginbitmaps/sliding_puzzle.h"
240 #define IMAGE_WIDTH BMPWIDTH_sliding_puzzle
241 #define IMAGE_HEIGHT BMPHEIGHT_sliding_puzzle
242 #define IMAGE_SIZE IMAGE_WIDTH
244 /* use a square image, (the default Archos bitmap looks square on its display)
245 Puzzle image dimension is min(lcd_height,lcd_width)
246 4x4 is more convenient for square puzzles
247 Note: sliding_puzzle.bmp should be evenly divisible by SPOTS_X
248 and SPOTS_Y, otherwise lcd_bitmap_part stride won't be correct */
249 #define SPOTS_X 4
250 #define SPOTS_Y 4
251 #define SPOTS_WIDTH (IMAGE_WIDTH / SPOTS_X)
252 #define SPOTS_HEIGHT (IMAGE_HEIGHT / SPOTS_Y)
253 #define NUM_SPOTS (SPOTS_X*SPOTS_Y)
254 #define HOLE_ID (NUM_SPOTS)
255 #define INITIAL_HOLE (HOLE_ID-1)
257 enum picmodes
259 PICMODE_NUMERALS = 0,
260 PICMODE_INITIAL_PICTURE,
261 PICMODE_DEFAULT_PICTURE,
262 #ifdef HAVE_ALBUMART
263 PICMODE_ALBUM_ART,
264 #endif
265 // PICMODE_RANDOM,
266 PICMODE_LAST_XXX /* placeholder */
269 static const char* const picmode_descriptions[] = {
270 "Numerals",
271 "Viewer Picture",
272 "Default Picture",
273 #ifdef HAVE_ALBUMART
274 "Album Art",
275 #endif
276 "Shouldn't Get Here",
279 static int spots[NUM_SPOTS];
280 static int hole = INITIAL_HOLE, moves;
281 static unsigned char s[32];
282 static enum picmodes picmode = PICMODE_INITIAL_PICTURE;
283 static int num_font = FONT_UI;
284 static int moves_font = FONT_UI;
285 static int moves_y = 0;
287 static unsigned char *img_buf;
288 static size_t img_buf_len;
289 #ifdef HAVE_ALBUMART
290 static char albumart_path[MAX_PATH+1];
291 #endif
292 static char img_buf_path[MAX_PATH+1];
294 static const fb_data * puzzle_bmp_ptr;
295 /* initial_bmp_path points to selected bitmap if this game is launched
296 as a viewer for a .bmp file, or NULL if game is launched regular way */
297 static const char * initial_bmp_path=NULL;
299 #ifdef HAVE_ALBUMART
300 const char * get_albumart_bmp_path(void)
302 struct mp3entry* track = rb->audio_current_track();
304 if (!track || !track->path || track->path[0] == '\0')
305 return NULL;
307 if (!rb->search_albumart_files(track, "", albumart_path, MAX_PATH ) )
308 return NULL;
310 albumart_path[ MAX_PATH ] = '\0';
311 return albumart_path;
313 #endif
315 const char * get_random_bmp_path(void)
317 return(initial_bmp_path);
320 static bool load_resize_bitmap(void)
322 int rc;
323 const char * filename = NULL;
325 /* initially assume using the built-in default */
326 puzzle_bmp_ptr = sliding_puzzle;
328 switch( picmode ){
329 /* some modes don't even need to touch disk and trivially succeed */
330 case PICMODE_NUMERALS:
331 case PICMODE_DEFAULT_PICTURE:
332 default:
333 return(true);
335 #ifdef HAVE_ALBUMART
336 case PICMODE_ALBUM_ART:
337 filename = get_albumart_bmp_path();
338 break;
339 #endif
341 case PICMODE_RANDOM:
342 if(NULL == (filename=get_random_bmp_path()) )
343 filename = initial_bmp_path;
344 break;
346 case PICMODE_INITIAL_PICTURE:
347 filename = initial_bmp_path;
348 break;
351 if( filename != NULL )
353 /* if we already loaded image before, don't touch disk */
354 if( 0 == rb->strcmp( filename, img_buf_path ) )
356 puzzle_bmp_ptr = (const fb_data *)img_buf;
357 return true;
360 struct bitmap main_bitmap;
361 rb->memset(&main_bitmap,0,sizeof(struct bitmap));
362 main_bitmap.data = img_buf;
364 main_bitmap.width = IMAGE_WIDTH;
365 main_bitmap.height = IMAGE_HEIGHT;
367 rc = READ_IMAGE( filename, &main_bitmap,
368 img_buf_len,
369 FORMAT_NATIVE|FORMAT_RESIZE|FORMAT_DITHER,
370 NULL);
371 if( rc > 0 )
373 puzzle_bmp_ptr = (const fb_data *)img_buf;
374 rb->strcpy( img_buf_path, filename );
375 return true;
379 /* something must have failed. get_albumart_bmp_path could return
380 NULL if albumart doesn't exist or couldn't be loaded, or
381 read_bmp_file could have failed. return false and caller should
382 try the next mode (PICMODE_DEFAULT_PICTURE and PICMODE_NUMERALS will
383 always succeed) */
384 return false;
387 /* draws a spot at the coordinates (x,y), range of p is 1-20 */
388 static void draw_spot(int p, int x, int y)
390 int w, h;
392 if (p == HOLE_ID)
394 #if LCD_DEPTH==1
395 /* the bottom-right cell of the default sliding_puzzle image is
396 an appropriate hole graphic */
397 rb->lcd_bitmap_part(sliding_puzzle, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
398 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
399 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
400 #else
401 /* just draw a black rectangle */
402 int old_fg = rb->lcd_get_foreground();
403 rb->lcd_set_foreground(LCD_BLACK);
404 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
405 rb->lcd_set_foreground(old_fg);
406 #endif
408 else if (picmode != PICMODE_NUMERALS)
410 rb->lcd_bitmap_part( puzzle_bmp_ptr, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
411 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
412 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
413 } else {
414 rb->lcd_drawrect(x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
415 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
416 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2);
417 rb->lcd_set_drawmode(DRMODE_SOLID);
418 rb->snprintf(s, sizeof(s), "%d", p);
419 rb->lcd_setfont(num_font);
420 rb->lcd_getstringsize(s, &w, &h);
421 rb->lcd_putsxy(x + (SPOTS_WIDTH/2) - w / 2,
422 y + (SPOTS_HEIGHT/2) - h / 2, s);
426 /* check if the puzzle is solved */
427 static bool puzzle_finished(void)
429 int i;
430 for (i=0; i<NUM_SPOTS; i++)
431 if (spots[i] != (i+1))
432 return false;
433 return true;
436 /* move a piece in any direction */
437 static void move_spot(int x, int y)
439 int i, w;
440 spots[hole] = spots[hole-x-SPOTS_X*y];
441 hole -= (x+SPOTS_X*y);
442 moves++;
443 rb->lcd_setfont(moves_font);
444 #if LCD_WIDTH > LCD_HEIGHT
445 rb->snprintf(s, sizeof(s), "%d", moves);
446 w = rb->lcd_getstringsize(s, NULL, NULL);
447 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
448 moves_y, s);
449 #else
450 (void)w;
451 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
452 rb->lcd_putsxy(3, moves_y, s);
453 #endif
454 for(i=1;i<=4;i++)
456 draw_spot(HOLE_ID,
457 (hole%SPOTS_X)*SPOTS_WIDTH,
458 (hole/SPOTS_X)*SPOTS_HEIGHT);
459 draw_spot(spots[hole],
460 (hole%SPOTS_X)*SPOTS_WIDTH + (i*x*SPOTS_WIDTH)/5,
461 (hole/SPOTS_X)*SPOTS_HEIGHT + (i*y*SPOTS_HEIGHT)/5);
462 rb->lcd_update();
463 rb->sleep(HZ/50);
465 draw_spot(HOLE_ID,
466 (hole%SPOTS_X)*SPOTS_WIDTH,
467 (hole/SPOTS_X)*SPOTS_HEIGHT);
468 draw_spot(spots[hole],
469 ((hole%SPOTS_X)+x)*SPOTS_WIDTH,
470 ((hole/SPOTS_X)+y)*SPOTS_HEIGHT);
471 rb->lcd_update();
473 spots[hole] = HOLE_ID;
476 static void draw_playfield(void)
478 int i, w;
480 rb->lcd_clear_display();
481 rb->lcd_setfont(moves_font);
482 #if LCD_WIDTH > LCD_HEIGHT
483 rb->lcd_vline(IMAGE_WIDTH, 0, LCD_HEIGHT-1);
484 w = rb->lcd_getstringsize("Moves", NULL, NULL);
485 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
486 10, "Moves");
487 rb->snprintf(s, sizeof(s), "%d", moves);
488 w = rb->lcd_getstringsize(s, NULL, NULL);
489 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
490 moves_y, s);
491 #else
492 (void)w;
493 rb->lcd_hline(0, LCD_WIDTH-1, IMAGE_HEIGHT);
494 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
495 rb->lcd_putsxy(3, moves_y, s);
496 #endif
498 /* draw spots to the lcd */
499 for (i=0; i<NUM_SPOTS; i++)
500 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
502 rb->lcd_update();
505 /* initializes the puzzle */
506 static void puzzle_init(void)
508 int i, r, temp, tsp[NUM_SPOTS];
510 moves = 0;
512 /* shuffle spots */
513 for (i=NUM_SPOTS-1; i>=0; i--) {
514 r = (rb->rand() % (i+1));
516 temp = spots[r];
517 spots[r] = spots[i];
518 spots[i] = temp;
520 if (spots[i]==HOLE_ID)
521 hole = i;
524 /* test if the puzzle is solvable */
525 for (i=0; i<NUM_SPOTS; i++)
526 tsp[i] = spots[i];
527 r=0;
529 /* First, check if the problem has even or odd parity,
530 depending on where the empty square is */
531 if ((((SPOTS_X-1)-hole%SPOTS_X) + ((SPOTS_Y-1)-hole/SPOTS_X))%2 == 1)
532 ++r;
534 /* Now check how many swaps we need to solve it */
535 for (i=0; i<NUM_SPOTS-1; i++) {
536 while (tsp[i] != (i+1)) {
537 temp = tsp[i];
538 tsp[i] = tsp[temp-1];
539 tsp[temp-1] = temp;
540 ++r;
544 /* if the random puzzle isn't solvable just change two spots */
545 if (r%2 == 1) {
546 if (spots[0]!=HOLE_ID && spots[1]!=HOLE_ID) {
547 temp = spots[0];
548 spots[0] = spots[1];
549 spots[1] = temp;
550 } else {
551 temp = spots[2];
552 spots[2] = spots[3];
553 spots[3] = temp;
557 draw_playfield();
560 /* the main game loop */
561 static int puzzle_loop(void)
563 int button;
564 int lastbutton = BUTTON_NONE;
565 bool load_success;
567 puzzle_init();
568 while(true) {
569 button = rb->button_get(true);
570 switch (button) {
571 #ifdef PUZZLE_RC_QUIT
572 case PUZZLE_RC_QUIT:
573 #endif
574 case PUZZLE_QUIT:
575 /* get out of here */
576 return PLUGIN_OK;
578 case PUZZLE_SHUFFLE:
579 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
580 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
581 break;
582 #endif
583 /* mix up the pieces */
584 puzzle_init();
585 break;
587 case PUZZLE_PICTURE:
588 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
589 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
590 break;
591 #endif
592 /* change picture */
593 picmode = (picmode+1)%PICMODE_LAST_XXX;
595 /* if load_resize_bitmap fails to load bitmap, try next picmode
599 load_success = load_resize_bitmap();
600 if( !load_success )
601 picmode = (picmode+1)%PICMODE_LAST_XXX;
603 while( !load_success );
605 /* tell the user what mode we picked in the end! */
606 rb->splash(HZ,picmode_descriptions[ picmode ] );
607 draw_playfield();
608 break;
610 case PUZZLE_LEFT:
611 if ((hole%SPOTS_X)<(SPOTS_X-1) && !puzzle_finished())
612 move_spot(-1, 0);
613 break;
615 case PUZZLE_RIGHT:
616 if ((hole%SPOTS_X)>0 && !puzzle_finished())
617 move_spot(1, 0);
618 break;
620 case PUZZLE_UP:
621 if ((hole/SPOTS_X)<(SPOTS_Y-1) && !puzzle_finished())
622 move_spot(0, -1);
623 break;
625 case PUZZLE_DOWN:
626 if ((hole/SPOTS_X)>0 && !puzzle_finished())
627 move_spot(0, 1);
628 break;
630 default:
631 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
632 return PLUGIN_USB_CONNECTED;
633 break;
635 if (button != BUTTON_NONE)
636 lastbutton = button;
640 enum plugin_status plugin_start(
641 const void* parameter)
643 int i, w, h;
645 initial_bmp_path=(const char *)parameter;
646 img_buf = rb->plugin_get_buffer(&img_buf_len);
647 picmode = PICMODE_INITIAL_PICTURE;
648 img_buf_path[0] = '\0';
650 /* If launched as a viewer, just go straight to the game without
651 bothering with the splash or instructions page */
652 if(parameter==NULL)
654 /* if not launched as a viewer, use default puzzle, and show help */
655 picmode = PICMODE_DEFAULT_PICTURE;
657 /* print title */
658 rb->lcd_getstringsize((unsigned char *)"Sliding Puzzle", &w, &h);
659 w = (w+1)/2;
660 h = (h+1)/2;
661 rb->lcd_clear_display();
662 rb->lcd_putsxy(LCD_WIDTH/2-w, (LCD_HEIGHT/2)-h,
663 (unsigned char *)"Sliding Puzzle");
664 rb->lcd_update();
665 rb->sleep(HZ);
667 /* print instructions */
668 rb->lcd_clear_display();
669 rb->lcd_setfont(FONT_SYSFIXED);
670 #if CONFIG_KEYPAD == RECORDER_PAD || CONFIG_KEYPAD == ARCHOS_AV300_PAD
671 rb->lcd_putsxy(3, 18, "[OFF] to stop");
672 rb->lcd_putsxy(3, 28, "[F1] shuffle");
673 rb->lcd_putsxy(3, 38, "[F2] change pic");
674 #elif CONFIG_KEYPAD == ONDIO_PAD
675 rb->lcd_putsxy(0, 18, "[OFF] to stop");
676 rb->lcd_putsxy(0, 28, "[MODE..] shuffle");
677 rb->lcd_putsxy(0, 38, "[MODE] change pic");
678 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
679 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
680 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
681 rb->lcd_putsxy(0, 18, "[S-MENU] to stop");
682 rb->lcd_putsxy(0, 28, "[S-LEFT] shuffle");
683 rb->lcd_putsxy(0, 38, "[S-RIGHT] change pic");
684 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
685 (CONFIG_KEYPAD == IRIVER_H300_PAD)
686 rb->lcd_putsxy(0, 18, "[STOP] to stop");
687 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
688 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
689 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
690 rb->lcd_putsxy(0, 18, "[OFF] to stop");
691 rb->lcd_putsxy(0, 28, "[REC] shuffle");
692 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
693 #elif CONFIG_KEYPAD == GIGABEAT_PAD
694 rb->lcd_putsxy(0, 18, "[POWER] to stop");
695 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
696 rb->lcd_putsxy(0, 38, "[A] change pic");
697 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
698 (CONFIG_KEYPAD == SANSA_C200_PAD)
699 rb->lcd_putsxy(0, 18, "[OFF] to stop");
700 rb->lcd_putsxy(0, 28, "[REC] shuffle");
701 rb->lcd_putsxy(0, 38, "[SELECT] change pic");
702 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
703 rb->lcd_putsxy(0, 18, "[OFF] to stop");
704 rb->lcd_putsxy(0, 28, "[REW] shuffle");
705 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
706 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
707 rb->lcd_putsxy(0, 18, "[BACK] to stop");
708 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
709 rb->lcd_putsxy(0, 38, "[MENU] change pic");
710 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
711 rb->lcd_putsxy(0, 18, "[REC] to stop");
712 rb->lcd_putsxy(0, 28, "[MODE] shuffle");
713 rb->lcd_putsxy(0, 38, "[MENU] change pic");
714 #endif
715 #ifdef HAVE_TOUCHSCREEN
716 rb->lcd_putsxy(0, 18, PUZZLE_QUIT_TEXT " to stop");
717 rb->lcd_putsxy(0, 28, PUZZLE_SHUFFLE_TEXT " shuffle");
718 rb->lcd_putsxy(0, 38, PUZZLE_PICTURE_TEXT " change pic");
719 #endif
720 #ifdef HAVE_ALBUMART
721 rb->lcd_putsxy(0,48," pic->albumart->num");
722 #else
723 rb->lcd_putsxy(0,48," pic<->num");
724 #endif
725 rb->lcd_update();
726 rb->button_get_w_tmo(HZ*2);
729 hole = INITIAL_HOLE;
731 if( !load_resize_bitmap() )
733 rb->lcd_clear_display();
734 rb->splash(HZ*2,"Failed to load bitmap!");
735 return PLUGIN_OK;
738 /* Calculate possible font sizes and text positions */
739 rb->lcd_setfont(FONT_UI);
740 rb->lcd_getstringsize("15", &w, &h);
741 if ((w > (SPOTS_WIDTH-2)) || (h > (SPOTS_HEIGHT-2)))
742 num_font = FONT_SYSFIXED;
744 #if LCD_WIDTH > LCD_HEIGHT
745 rb->lcd_getstringsize("Moves", &w, &h);
746 if (w > (LCD_WIDTH-IMAGE_WIDTH-1))
747 moves_font = FONT_SYSFIXED;
748 rb->lcd_setfont(moves_font);
749 rb->lcd_getstringsize("Moves", &w, &h);
750 moves_y = 10 + h;
751 #else
752 rb->lcd_getstringsize("Moves: 999", &w, &h);
753 if ((w > LCD_WIDTH) || (h > (LCD_HEIGHT-IMAGE_HEIGHT-1)))
754 moves_font = FONT_SYSFIXED;
755 rb->lcd_setfont(moves_font);
756 rb->lcd_getstringsize("Moves: 999", &w, &h);
757 moves_y = (IMAGE_HEIGHT+1+(LCD_HEIGHT-IMAGE_HEIGHT-1)/2) - h / 2;
758 #endif
759 for (i=0; i<NUM_SPOTS; i++)
760 spots[i]=(i+1);
762 #ifdef HAVE_LCD_COLOR
763 rb->lcd_set_background(LCD_BLACK);
764 rb->lcd_set_foreground(LCD_WHITE);
765 rb->lcd_set_backdrop(NULL);
766 #elif LCD_DEPTH > 1
767 rb->lcd_set_background(LCD_WHITE);
768 rb->lcd_set_foreground(LCD_BLACK);
769 rb->lcd_set_backdrop(NULL);
770 #endif
772 draw_playfield();
773 rb->sleep(HZ*2);
775 return puzzle_loop();
778 #endif