New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / plugins / sliding_puzzle.c
blob0d125566961ab8787dd312083b9ecfec4016afd2
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"
22 #include "lib/bmp.h"
24 #ifdef HAVE_LCD_BITMAP
25 PLUGIN_HEADER
27 /* variable button definitions */
28 #if CONFIG_KEYPAD == RECORDER_PAD
29 #define PUZZLE_QUIT BUTTON_OFF
30 #define PUZZLE_LEFT BUTTON_LEFT
31 #define PUZZLE_RIGHT BUTTON_RIGHT
32 #define PUZZLE_UP BUTTON_UP
33 #define PUZZLE_DOWN BUTTON_DOWN
34 #define PUZZLE_SHUFFLE BUTTON_F1
35 #define PUZZLE_PICTURE BUTTON_F2
37 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
38 #define PUZZLE_QUIT BUTTON_OFF
39 #define PUZZLE_LEFT BUTTON_LEFT
40 #define PUZZLE_RIGHT BUTTON_RIGHT
41 #define PUZZLE_UP BUTTON_UP
42 #define PUZZLE_DOWN BUTTON_DOWN
43 #define PUZZLE_SHUFFLE BUTTON_F1
44 #define PUZZLE_PICTURE BUTTON_F2
46 #elif CONFIG_KEYPAD == ONDIO_PAD
47 #define PUZZLE_QUIT BUTTON_OFF
48 #define PUZZLE_LEFT BUTTON_LEFT
49 #define PUZZLE_RIGHT BUTTON_RIGHT
50 #define PUZZLE_UP BUTTON_UP
51 #define PUZZLE_DOWN BUTTON_DOWN
52 #define PUZZLE_SHUFFLE_PICTURE_PRE BUTTON_MENU
53 #define PUZZLE_SHUFFLE (BUTTON_MENU | BUTTON_REPEAT)
54 #define PUZZLE_PICTURE (BUTTON_MENU | BUTTON_REL)
56 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
57 (CONFIG_KEYPAD == IRIVER_H300_PAD)
58 #define PUZZLE_QUIT BUTTON_OFF
59 #define PUZZLE_LEFT BUTTON_LEFT
60 #define PUZZLE_RIGHT BUTTON_RIGHT
61 #define PUZZLE_UP BUTTON_UP
62 #define PUZZLE_DOWN BUTTON_DOWN
63 #define PUZZLE_SHUFFLE BUTTON_SELECT
64 #define PUZZLE_PICTURE BUTTON_ON
66 #define PUZZLE_RC_QUIT BUTTON_RC_STOP
68 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
69 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
70 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
71 #define PUZZLE_QUIT (BUTTON_SELECT | BUTTON_MENU)
72 #define PUZZLE_LEFT BUTTON_LEFT
73 #define PUZZLE_RIGHT BUTTON_RIGHT
74 #define PUZZLE_UP BUTTON_MENU
75 #define PUZZLE_DOWN BUTTON_PLAY
76 #define PUZZLE_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
77 #define PUZZLE_PICTURE (BUTTON_SELECT | BUTTON_RIGHT)
79 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
80 #define PUZZLE_QUIT BUTTON_POWER
81 #define PUZZLE_LEFT BUTTON_LEFT
82 #define PUZZLE_RIGHT BUTTON_RIGHT
83 #define PUZZLE_UP BUTTON_UP
84 #define PUZZLE_DOWN BUTTON_DOWN
85 #define PUZZLE_SHUFFLE BUTTON_REC
86 #define PUZZLE_PICTURE BUTTON_PLAY
88 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
89 #define PUZZLE_QUIT BUTTON_POWER
90 #define PUZZLE_LEFT BUTTON_LEFT
91 #define PUZZLE_RIGHT BUTTON_RIGHT
92 #define PUZZLE_UP BUTTON_UP
93 #define PUZZLE_DOWN BUTTON_DOWN
94 #define PUZZLE_SHUFFLE BUTTON_SELECT
95 #define PUZZLE_PICTURE BUTTON_A
97 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
98 (CONFIG_KEYPAD == SANSA_C200_PAD)
99 #define PUZZLE_QUIT BUTTON_POWER
100 #define PUZZLE_LEFT BUTTON_LEFT
101 #define PUZZLE_RIGHT BUTTON_RIGHT
102 #define PUZZLE_UP BUTTON_UP
103 #define PUZZLE_DOWN BUTTON_DOWN
104 #define PUZZLE_SHUFFLE BUTTON_REC
105 #define PUZZLE_PICTURE BUTTON_SELECT
107 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
108 #define PUZZLE_QUIT BUTTON_POWER
109 #define PUZZLE_LEFT BUTTON_LEFT
110 #define PUZZLE_RIGHT BUTTON_RIGHT
111 #define PUZZLE_UP BUTTON_SCROLL_UP
112 #define PUZZLE_DOWN BUTTON_SCROLL_DOWN
113 #define PUZZLE_SHUFFLE BUTTON_REW
114 #define PUZZLE_PICTURE BUTTON_PLAY
116 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
117 #define PUZZLE_QUIT BUTTON_BACK
118 #define PUZZLE_LEFT BUTTON_LEFT
119 #define PUZZLE_RIGHT BUTTON_RIGHT
120 #define PUZZLE_UP BUTTON_UP
121 #define PUZZLE_DOWN BUTTON_DOWN
122 #define PUZZLE_SHUFFLE BUTTON_SELECT
123 #define PUZZLE_PICTURE BUTTON_MENU
125 #elif (CONFIG_KEYPAD == MROBE100_PAD)
126 #define PUZZLE_QUIT BUTTON_POWER
127 #define PUZZLE_LEFT BUTTON_LEFT
128 #define PUZZLE_RIGHT BUTTON_RIGHT
129 #define PUZZLE_UP BUTTON_UP
130 #define PUZZLE_DOWN BUTTON_DOWN
131 #define PUZZLE_SHUFFLE BUTTON_SELECT
132 #define PUZZLE_PICTURE BUTTON_DISPLAY
134 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
135 #define PUZZLE_QUIT BUTTON_RC_REC
136 #define PUZZLE_LEFT BUTTON_RC_REW
137 #define PUZZLE_RIGHT BUTTON_RC_FF
138 #define PUZZLE_UP BUTTON_RC_VOL_UP
139 #define PUZZLE_DOWN BUTTON_RC_VOL_DOWN
140 #define PUZZLE_SHUFFLE BUTTON_RC_MODE
141 #define PUZZLE_PICTURE BUTTON_RC_MENU
143 #elif (CONFIG_KEYPAD == COWOND2_PAD)
144 #define PUZZLE_QUIT BUTTON_POWER
145 #define PUZZLE_QUIT_TEXT "[POWER]"
147 #else
148 #error No keymap defined!
149 #endif
151 #ifdef HAVE_TOUCHSCREEN
152 #ifndef PUZZLE_QUIT
153 #define PUZZLE_QUIT BUTTON_TOPLEFT
154 #endif
155 #ifndef PUZZLE_LEFT
156 #define PUZZLE_LEFT BUTTON_MIDLEFT
157 #endif
158 #ifndef PUZZLE_RIGHT
159 #define PUZZLE_RIGHT BUTTON_MIDRIGHT
160 #endif
161 #ifndef PUZZLE_UP
162 #define PUZZLE_UP BUTTON_TOPMIDDLE
163 #endif
164 #ifndef PUZZLE_DOWN
165 #define PUZZLE_DOWN BUTTON_BOTTOMMIDDLE
166 #endif
167 #ifndef PUZZLE_SHUFFLE
168 #define PUZZLE_SHUFFLE BUTTON_BOTTOMLEFT
169 #endif
170 #ifndef PUZZLE_PICTURE
171 #define PUZZLE_PICTURE BUTTON_CENTER
172 #endif
173 #ifndef PUZZLE_QUIT_TEXT
174 #define PUZZLE_QUIT_TEXT "[TOPLEFT]"
175 #endif
176 #ifndef PUZZLE_SHUFFLE_TEXT
177 #define PUZZLE_SHUFFLE_TEXT "[BOTTOMLEFT]"
178 #endif
179 #ifndef PUZZLE_PICTURE_TEXT
180 #define PUZZLE_PICTURE_TEXT "[CENTER]"
181 #endif
182 #endif
184 #include "pluginbitmaps/sliding_puzzle.h"
185 #define IMAGE_WIDTH BMPWIDTH_sliding_puzzle
186 #define IMAGE_HEIGHT BMPHEIGHT_sliding_puzzle
187 #define IMAGE_SIZE IMAGE_WIDTH
189 static const struct plugin_api* rb;
191 /* use a square image, (the default Archos bitmap looks square on its display)
192 Puzzle image dimension is min(lcd_height,lcd_width)
193 4x4 is more convenient for square puzzles
194 Note: sliding_puzzle.bmp should be evenly divisible by SPOTS_X
195 and SPOTS_Y, otherwise lcd_bitmap_part stride won't be correct */
196 #define SPOTS_X 4
197 #define SPOTS_Y 4
198 #define SPOTS_WIDTH (IMAGE_WIDTH / SPOTS_X)
199 #define SPOTS_HEIGHT (IMAGE_HEIGHT / SPOTS_Y)
200 #define NUM_SPOTS (SPOTS_X*SPOTS_Y)
201 #define HOLE_ID (NUM_SPOTS)
202 #define INITIAL_HOLE (HOLE_ID-1)
204 enum picmodes
206 PICMODE_NUMERALS = 0,
207 PICMODE_INITIAL_PICTURE,
208 PICMODE_DEFAULT_PICTURE,
209 #ifdef HAVE_ALBUMART
210 PICMODE_ALBUM_ART,
211 #endif
212 // PICMODE_RANDOM,
213 PICMODE_LAST_XXX /* placeholder */
216 static const char* const picmode_descriptions[] = {
217 "Numerals",
218 "Viewer Picture",
219 "Default Picture",
220 #ifdef HAVE_ALBUMART
221 "Album Art",
222 #endif
223 "Shouldn't Get Here",
226 static int spots[NUM_SPOTS];
227 static int hole = INITIAL_HOLE, moves;
228 static unsigned char s[32];
229 static enum picmodes picmode = PICMODE_INITIAL_PICTURE;
230 static int num_font = FONT_UI;
231 static int moves_font = FONT_UI;
232 static int moves_y = 0;
234 static unsigned char img_buf[IMAGE_WIDTH*IMAGE_HEIGHT*sizeof(fb_data)]
235 __attribute__ ((aligned(16)));
236 #if LCD_DEPTH>1
237 static unsigned char temp_img_buf[LCD_WIDTH*LCD_HEIGHT*sizeof(fb_data)]
238 __attribute__ ((aligned(16)));
239 #endif
240 #ifdef HAVE_ALBUMART
241 static char albumart_path[MAX_PATH+1];
242 #endif
243 static char img_buf_path[MAX_PATH+1];
245 static const fb_data * puzzle_bmp_ptr;
246 /* initial_bmp_path points to selected bitmap if this game is launched
247 as a viewer for a .bmp file, or NULL if game is launched regular way */
248 static const char * initial_bmp_path=NULL;
250 #ifdef HAVE_ALBUMART
251 const char * get_albumart_bmp_path(void)
253 struct mp3entry* track = rb->audio_current_track();
255 if (!track || !track->path || track->path[0] == '\0')
256 return NULL;
258 if (!rb->search_albumart_files(track, "", albumart_path, MAX_PATH ) )
259 return NULL;
261 albumart_path[ MAX_PATH ] = '\0';
262 return albumart_path;
264 #endif
266 const char * get_random_bmp_path(void)
268 return(initial_bmp_path);
271 static bool load_resize_bitmap(void)
273 int rc;
274 const char * filename = NULL;
276 /* initially assume using the built-in default */
277 puzzle_bmp_ptr = sliding_puzzle;
279 switch( picmode ){
280 /* some modes don't even need to touch disk and trivially succeed */
281 case PICMODE_NUMERALS:
282 case PICMODE_DEFAULT_PICTURE:
283 default:
284 return(true);
286 #ifdef HAVE_ALBUMART
287 case PICMODE_ALBUM_ART:
288 filename = get_albumart_bmp_path();
289 break;
290 #endif
292 case PICMODE_RANDOM:
293 if(NULL == (filename=get_random_bmp_path()) )
294 filename = initial_bmp_path;
295 break;
297 case PICMODE_INITIAL_PICTURE:
298 filename = initial_bmp_path;
299 break;
302 if( filename != NULL )
304 /* if we already loaded image before, don't touch disk */
305 if( 0 == rb->strcmp( filename, img_buf_path ) )
307 puzzle_bmp_ptr = (const fb_data *)img_buf;
308 return true;
311 struct bitmap main_bitmap;
312 rb->memset(&main_bitmap,0,sizeof(struct bitmap));
313 main_bitmap.data = img_buf;
315 #if LCD_DEPTH>1
316 struct bitmap temp_bitmap;
317 rb->memset(&temp_bitmap,0,sizeof(struct bitmap));
318 temp_bitmap.data = temp_img_buf;
320 main_bitmap.width = IMAGE_WIDTH;
321 main_bitmap.height = IMAGE_HEIGHT;
323 rc = rb->read_bmp_file( filename, &temp_bitmap, sizeof(temp_img_buf),
324 FORMAT_NATIVE );
325 if( rc > 0 )
327 #ifdef HAVE_LCD_COLOR
328 smooth_resize_bitmap( &temp_bitmap, &main_bitmap );
329 #else
330 simple_resize_bitmap( &temp_bitmap, &main_bitmap );
331 #endif
332 puzzle_bmp_ptr = (const fb_data *)img_buf;
333 rb->strcpy( img_buf_path, filename );
334 return true;
336 #else
337 rc = rb->read_bmp_file( filename, &main_bitmap, sizeof(img_buf),
338 FORMAT_NATIVE );
339 if( rc > 0 )
341 puzzle_bmp_ptr = (const fb_data *)img_buf;
342 rb->strcpy( img_buf_path, filename );
343 return true;
345 #endif
348 /* something must have failed. get_albumart_bmp_path could return
349 NULL if albumart doesn't exist or couldn't be loaded, or
350 read_bmp_file could have failed. return false and caller should
351 try the next mode (PICMODE_DEFAULT_PICTURE and PICMODE_NUMERALS will
352 always succeed) */
353 return false;
356 /* draws a spot at the coordinates (x,y), range of p is 1-20 */
357 static void draw_spot(int p, int x, int y)
359 int w, h;
361 if (p == HOLE_ID)
363 #if LCD_DEPTH==1
364 /* the bottom-right cell of the default sliding_puzzle image is
365 an appropriate hole graphic */
366 rb->lcd_bitmap_part(sliding_puzzle, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
367 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
368 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
369 #else
370 /* just draw a black rectangle */
371 int old_fg = rb->lcd_get_foreground();
372 rb->lcd_set_foreground(LCD_BLACK);
373 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
374 rb->lcd_set_foreground(old_fg);
375 #endif
377 else if (picmode != PICMODE_NUMERALS)
379 rb->lcd_bitmap_part( puzzle_bmp_ptr, ((p-1)%SPOTS_X)*SPOTS_WIDTH,
380 ((p-1)/SPOTS_X)*SPOTS_HEIGHT,
381 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
382 } else {
383 rb->lcd_drawrect(x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
384 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
385 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2);
386 rb->lcd_set_drawmode(DRMODE_SOLID);
387 rb->snprintf(s, sizeof(s), "%d", p);
388 rb->lcd_setfont(num_font);
389 rb->lcd_getstringsize(s, &w, &h);
390 rb->lcd_putsxy(x + (SPOTS_WIDTH/2) - w / 2,
391 y + (SPOTS_HEIGHT/2) - h / 2, s);
395 /* check if the puzzle is solved */
396 static bool puzzle_finished(void)
398 int i;
399 for (i=0; i<NUM_SPOTS; i++)
400 if (spots[i] != (i+1))
401 return false;
402 return true;
405 /* move a piece in any direction */
406 static void move_spot(int x, int y)
408 int i, w;
409 spots[hole] = spots[hole-x-SPOTS_X*y];
410 hole -= (x+SPOTS_X*y);
411 moves++;
412 rb->lcd_setfont(moves_font);
413 #if LCD_WIDTH > LCD_HEIGHT
414 rb->snprintf(s, sizeof(s), "%d", moves);
415 w = rb->lcd_getstringsize(s, NULL, NULL);
416 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
417 moves_y, s);
418 #else
419 (void)w;
420 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
421 rb->lcd_putsxy(3, moves_y, s);
422 #endif
423 for(i=1;i<=4;i++)
425 draw_spot(HOLE_ID,
426 (hole%SPOTS_X)*SPOTS_WIDTH,
427 (hole/SPOTS_X)*SPOTS_HEIGHT);
428 draw_spot(spots[hole],
429 (hole%SPOTS_X)*SPOTS_WIDTH + (i*x*SPOTS_WIDTH)/5,
430 (hole/SPOTS_X)*SPOTS_HEIGHT + (i*y*SPOTS_HEIGHT)/5);
431 rb->lcd_update();
432 rb->sleep(HZ/50);
434 draw_spot(HOLE_ID,
435 (hole%SPOTS_X)*SPOTS_WIDTH,
436 (hole/SPOTS_X)*SPOTS_HEIGHT);
437 draw_spot(spots[hole],
438 ((hole%SPOTS_X)+x)*SPOTS_WIDTH,
439 ((hole/SPOTS_X)+y)*SPOTS_HEIGHT);
440 rb->lcd_update();
442 spots[hole] = HOLE_ID;
445 static void draw_playfield(void)
447 int i, w;
449 rb->lcd_clear_display();
450 rb->lcd_setfont(moves_font);
451 #if LCD_WIDTH > LCD_HEIGHT
452 rb->lcd_vline(IMAGE_WIDTH, 0, LCD_HEIGHT-1);
453 w = rb->lcd_getstringsize("Moves", NULL, NULL);
454 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
455 10, "Moves");
456 rb->snprintf(s, sizeof(s), "%d", moves);
457 w = rb->lcd_getstringsize(s, NULL, NULL);
458 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
459 moves_y, s);
460 #else
461 (void)w;
462 rb->lcd_hline(0, LCD_WIDTH-1, IMAGE_HEIGHT);
463 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
464 rb->lcd_putsxy(3, moves_y, s);
465 #endif
467 /* draw spots to the lcd */
468 for (i=0; i<NUM_SPOTS; i++)
469 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
471 rb->lcd_update();
474 /* initializes the puzzle */
475 static void puzzle_init(void)
477 int i, r, temp, tsp[NUM_SPOTS];
479 moves = 0;
481 /* shuffle spots */
482 for (i=NUM_SPOTS-1; i>=0; i--) {
483 r = (rb->rand() % (i+1));
485 temp = spots[r];
486 spots[r] = spots[i];
487 spots[i] = temp;
489 if (spots[i]==HOLE_ID)
490 hole = i;
493 /* test if the puzzle is solvable */
494 for (i=0; i<NUM_SPOTS; i++)
495 tsp[i] = spots[i];
496 r=0;
498 /* First, check if the problem has even or odd parity,
499 depending on where the empty square is */
500 if ((((SPOTS_X-1)-hole%SPOTS_X) + ((SPOTS_Y-1)-hole/SPOTS_X))%2 == 1)
501 ++r;
503 /* Now check how many swaps we need to solve it */
504 for (i=0; i<NUM_SPOTS-1; i++) {
505 while (tsp[i] != (i+1)) {
506 temp = tsp[i];
507 tsp[i] = tsp[temp-1];
508 tsp[temp-1] = temp;
509 ++r;
513 /* if the random puzzle isn't solvable just change two spots */
514 if (r%2 == 1) {
515 if (spots[0]!=HOLE_ID && spots[1]!=HOLE_ID) {
516 temp = spots[0];
517 spots[0] = spots[1];
518 spots[1] = temp;
519 } else {
520 temp = spots[2];
521 spots[2] = spots[3];
522 spots[3] = temp;
526 draw_playfield();
529 /* the main game loop */
530 static int puzzle_loop(void)
532 int button;
533 int lastbutton = BUTTON_NONE;
534 bool load_success;
536 puzzle_init();
537 while(true) {
538 button = rb->button_get(true);
539 switch (button) {
540 #ifdef PUZZLE_RC_QUIT
541 case PUZZLE_RC_QUIT:
542 #endif
543 case PUZZLE_QUIT:
544 /* get out of here */
545 return PLUGIN_OK;
547 case PUZZLE_SHUFFLE:
548 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
549 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
550 break;
551 #endif
552 /* mix up the pieces */
553 puzzle_init();
554 break;
556 case PUZZLE_PICTURE:
557 #ifdef PUZZLE_SHUFFLE_PICTURE_PRE
558 if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
559 break;
560 #endif
561 /* change picture */
562 picmode = (picmode+1)%PICMODE_LAST_XXX;
564 /* if load_resize_bitmap fails to load bitmap, try next picmode */
567 load_success = load_resize_bitmap();
568 if( !load_success )
569 picmode = (picmode+1)%PICMODE_LAST_XXX;
571 while( !load_success );
573 /* tell the user what mode we picked in the end! */
574 rb->splash(HZ,picmode_descriptions[ picmode ] );
575 draw_playfield();
576 break;
578 case PUZZLE_LEFT:
579 if ((hole%SPOTS_X)<(SPOTS_X-1) && !puzzle_finished())
580 move_spot(-1, 0);
581 break;
583 case PUZZLE_RIGHT:
584 if ((hole%SPOTS_X)>0 && !puzzle_finished())
585 move_spot(1, 0);
586 break;
588 case PUZZLE_UP:
589 if ((hole/SPOTS_X)<(SPOTS_Y-1) && !puzzle_finished())
590 move_spot(0, -1);
591 break;
593 case PUZZLE_DOWN:
594 if ((hole/SPOTS_X)>0 && !puzzle_finished())
595 move_spot(0, 1);
596 break;
598 default:
599 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
600 return PLUGIN_USB_CONNECTED;
601 break;
603 if (button != BUTTON_NONE)
604 lastbutton = button;
608 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
610 int i, w, h;
612 rb = api;
614 initial_bmp_path=(const char *)parameter;
615 picmode = PICMODE_INITIAL_PICTURE;
616 img_buf_path[0] = '\0';
618 /* If launched as a viewer, just go straight to the game without
619 bothering with the splash or instructions page */
620 if(parameter==NULL)
622 /* if not launched as a viewer, use default puzzle, and show help */
623 picmode = PICMODE_DEFAULT_PICTURE;
625 /* print title */
626 rb->lcd_getstringsize((unsigned char *)"Sliding Puzzle", &w, &h);
627 w = (w+1)/2;
628 h = (h+1)/2;
629 rb->lcd_clear_display();
630 rb->lcd_putsxy(LCD_WIDTH/2-w, (LCD_HEIGHT/2)-h,
631 (unsigned char *)"Sliding Puzzle");
632 rb->lcd_update();
633 rb->sleep(HZ);
635 /* print instructions */
636 rb->lcd_clear_display();
637 rb->lcd_setfont(FONT_SYSFIXED);
638 #if CONFIG_KEYPAD == RECORDER_PAD || CONFIG_KEYPAD == ARCHOS_AV300_PAD
639 rb->lcd_putsxy(3, 18, "[OFF] to stop");
640 rb->lcd_putsxy(3, 28, "[F1] shuffle");
641 rb->lcd_putsxy(3, 38, "[F2] change pic");
642 #elif CONFIG_KEYPAD == ONDIO_PAD
643 rb->lcd_putsxy(0, 18, "[OFF] to stop");
644 rb->lcd_putsxy(0, 28, "[MODE..] shuffle");
645 rb->lcd_putsxy(0, 38, "[MODE] change pic");
646 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
647 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
648 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
649 rb->lcd_putsxy(0, 18, "[S-MENU] to stop");
650 rb->lcd_putsxy(0, 28, "[S-LEFT] shuffle");
651 rb->lcd_putsxy(0, 38, "[S-RIGHT] change pic");
652 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
653 (CONFIG_KEYPAD == IRIVER_H300_PAD)
654 rb->lcd_putsxy(0, 18, "[STOP] to stop");
655 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
656 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
657 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
658 rb->lcd_putsxy(0, 18, "[OFF] to stop");
659 rb->lcd_putsxy(0, 28, "[REC] shuffle");
660 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
661 #elif CONFIG_KEYPAD == GIGABEAT_PAD
662 rb->lcd_putsxy(0, 18, "[OFF] to stop");
663 rb->lcd_putsxy(0, 28, "[SELECT] shuffle");
664 rb->lcd_putsxy(0, 38, "[A] change pic");
665 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
666 (CONFIG_KEYPAD == SANSA_C200_PAD)
667 rb->lcd_putsxy(0, 18, "[OFF] to stop");
668 rb->lcd_putsxy(0, 28, "[REC] shuffle");
669 rb->lcd_putsxy(0, 38, "[SELECT] change pic");
670 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
671 rb->lcd_putsxy(0, 18, "[OFF] to stop");
672 rb->lcd_putsxy(0, 28, "[REW] shuffle");
673 rb->lcd_putsxy(0, 38, "[PLAY] change pic");
674 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
675 rb->lcd_putsxy(0, 18, "[REC] to stop");
676 rb->lcd_putsxy(0, 28, "[MODE] shuffle");
677 rb->lcd_putsxy(0, 38, "[MENU] change pic");
678 #endif
679 #ifdef HAVE_TOUCHSCREEN
680 rb->lcd_putsxy(0, 18, PUZZLE_QUIT_TEXT " to stop");
681 rb->lcd_putsxy(0, 28, PUZZLE_SHUFFLE_TEXT " shuffle");
682 rb->lcd_putsxy(0, 38, PUZZLE_PICTURE_TEXT " change pic");
683 #endif
684 #ifdef HAVE_ALBUMART
685 rb->lcd_putsxy(0,48," pic->albumart->num");
686 #else
687 rb->lcd_putsxy(0,48," pic<->num");
688 #endif
689 rb->lcd_update();
690 rb->button_get_w_tmo(HZ*2);
693 hole = INITIAL_HOLE;
695 if( !load_resize_bitmap() )
697 rb->lcd_clear_display();
698 rb->splash(HZ*2,"Failed to load bitmap!");
699 return PLUGIN_OK;
702 /* Calculate possible font sizes and text positions */
703 rb->lcd_setfont(FONT_UI);
704 rb->lcd_getstringsize("15", &w, &h);
705 if ((w > (SPOTS_WIDTH-2)) || (h > (SPOTS_HEIGHT-2)))
706 num_font = FONT_SYSFIXED;
708 #if LCD_WIDTH > LCD_HEIGHT
709 rb->lcd_getstringsize("Moves", &w, &h);
710 if (w > (LCD_WIDTH-IMAGE_WIDTH-1))
711 moves_font = FONT_SYSFIXED;
712 rb->lcd_setfont(moves_font);
713 rb->lcd_getstringsize("Moves", &w, &h);
714 moves_y = 10 + h;
715 #else
716 rb->lcd_getstringsize("Moves: 999", &w, &h);
717 if ((w > LCD_WIDTH) || (h > (LCD_HEIGHT-IMAGE_HEIGHT-1)))
718 moves_font = FONT_SYSFIXED;
719 rb->lcd_setfont(moves_font);
720 rb->lcd_getstringsize("Moves: 999", &w, &h);
721 moves_y = (IMAGE_HEIGHT+1+(LCD_HEIGHT-IMAGE_HEIGHT-1)/2) - h / 2;
722 #endif
723 for (i=0; i<NUM_SPOTS; i++)
724 spots[i]=(i+1);
726 #ifdef HAVE_LCD_COLOR
727 rb->lcd_set_background(LCD_BLACK);
728 rb->lcd_set_foreground(LCD_WHITE);
729 rb->lcd_set_backdrop(NULL);
730 #elif LCD_DEPTH > 1
731 rb->lcd_set_background(LCD_WHITE);
732 rb->lcd_set_foreground(LCD_BLACK);
733 rb->lcd_set_backdrop(NULL);
734 #endif
736 draw_playfield();
737 rb->sleep(HZ*2);
739 return puzzle_loop();
742 #endif