summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Arnold <amiconn@rockbox.org>2008-02-03 14:34:37 +0000
committerJens Arnold <amiconn@rockbox.org>2008-02-03 14:34:37 +0000
commit6f65afd0b757e2111c5b764def30170caa937a6f (patch)
tree6423378a68dee534e1d9ead9c1cb3b56c03a367f
parente686f17ec13f4aa419e394834b34dda8dd36eef7 (diff)
downloadrockbox-6f65afd0b757e2111c5b764def30170caa937a6f.tar.gz
rockbox-6f65afd0b757e2111c5b764def30170caa937a6f.zip
sliding_puzzle improvements: * Use the UI font for numerals and 'moves' display when possible. * Center the numerals and the 'moves' display. * Use a simple line separator. * Use black text on white background for greyscale targets. * Simplify the code a bit.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@16207 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/sliding_puzzle.c145
1 files changed, 83 insertions, 62 deletions
diff --git a/apps/plugins/sliding_puzzle.c b/apps/plugins/sliding_puzzle.c
index f1119614ea..f9a723cc11 100644
--- a/apps/plugins/sliding_puzzle.c
+++ b/apps/plugins/sliding_puzzle.c
@@ -140,8 +140,11 @@ static const char* const picmode_descriptions[] = {
140 140
141static int spots[NUM_SPOTS]; 141static int spots[NUM_SPOTS];
142static int hole = INITIAL_HOLE, moves; 142static int hole = INITIAL_HOLE, moves;
143static char s[5]; 143static unsigned char s[32];
144static enum picmodes picmode = PICMODE_INITIAL_PICTURE; 144static enum picmodes picmode = PICMODE_INITIAL_PICTURE;
145static int num_font = FONT_UI;
146static int moves_font = FONT_UI;
147static int moves_y = 0;
145 148
146static unsigned char img_buf[IMAGE_WIDTH*IMAGE_HEIGHT*sizeof(fb_data)] 149static unsigned char img_buf[IMAGE_WIDTH*IMAGE_HEIGHT*sizeof(fb_data)]
147__attribute__ ((aligned(16))); 150__attribute__ ((aligned(16)));
@@ -155,7 +158,6 @@ static char albumart_path[MAX_PATH+1];
155static char img_buf_path[MAX_PATH+1]; 158static char img_buf_path[MAX_PATH+1];
156 159
157static const fb_data * puzzle_bmp_ptr; 160static const fb_data * puzzle_bmp_ptr;
158extern const fb_data sliding_puzzle[];
159/* initial_bmp_path points to selected bitmap if this game is launched 161/* initial_bmp_path points to selected bitmap if this game is launched
160 as a viewer for a .bmp file, or NULL if game is launched regular way */ 162 as a viewer for a .bmp file, or NULL if game is launched regular way */
161static const char * initial_bmp_path=NULL; 163static const char * initial_bmp_path=NULL;
@@ -265,6 +267,8 @@ static bool load_resize_bitmap(void)
265/* draws a spot at the coordinates (x,y), range of p is 1-20 */ 267/* draws a spot at the coordinates (x,y), range of p is 1-20 */
266static void draw_spot(int p, int x, int y) 268static void draw_spot(int p, int x, int y)
267{ 269{
270 int w, h;
271
268 if (p == HOLE_ID) 272 if (p == HOLE_ID)
269 { 273 {
270#if LCD_DEPTH==1 274#if LCD_DEPTH==1
@@ -275,10 +279,10 @@ static void draw_spot(int p, int x, int y)
275 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT); 279 IMAGE_WIDTH, x, y, SPOTS_WIDTH, SPOTS_HEIGHT);
276#else 280#else
277 /* just draw a black rectangle */ 281 /* just draw a black rectangle */
278 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); 282 int old_fg = rb->lcd_get_foreground();
279 rb->lcd_set_background(LCD_BLACK); 283 rb->lcd_set_foreground(LCD_BLACK);
280 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT); 284 rb->lcd_fillrect(x,y,SPOTS_WIDTH,SPOTS_HEIGHT);
281 rb->lcd_set_drawmode(DRMODE_SOLID); 285 rb->lcd_set_foreground(old_fg);
282#endif 286#endif
283 } 287 }
284 else if (picmode != PICMODE_NUMERALS) 288 else if (picmode != PICMODE_NUMERALS)
@@ -292,7 +296,10 @@ static void draw_spot(int p, int x, int y)
292 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2); 296 rb->lcd_fillrect(x+1, y+1, SPOTS_WIDTH-2, SPOTS_HEIGHT-2);
293 rb->lcd_set_drawmode(DRMODE_SOLID); 297 rb->lcd_set_drawmode(DRMODE_SOLID);
294 rb->snprintf(s, sizeof(s), "%d", p); 298 rb->snprintf(s, sizeof(s), "%d", p);
295 rb->lcd_putsxy(x+2, y+4, (unsigned char *)s); 299 rb->lcd_setfont(num_font);
300 rb->lcd_getstringsize(s, &w, &h);
301 rb->lcd_putsxy(x + (SPOTS_WIDTH/2) - w / 2,
302 y + (SPOTS_HEIGHT/2) - h / 2, s);
296 } 303 }
297} 304}
298 305
@@ -309,16 +316,20 @@ static bool puzzle_finished(void)
309/* move a piece in any direction */ 316/* move a piece in any direction */
310static void move_spot(int x, int y) 317static void move_spot(int x, int y)
311{ 318{
312 int i; 319 int i, w;
313 spots[hole] = spots[hole-x-SPOTS_X*y]; 320 spots[hole] = spots[hole-x-SPOTS_X*y];
314 hole -= (x+SPOTS_X*y); 321 hole -= (x+SPOTS_X*y);
315 moves++; 322 moves++;
323 rb->lcd_setfont(moves_font);
324#if LCD_WIDTH > LCD_HEIGHT
316 rb->snprintf(s, sizeof(s), "%d", moves); 325 rb->snprintf(s, sizeof(s), "%d", moves);
317 s[sizeof(s)-1] = '\0'; 326 w = rb->lcd_getstringsize(s, NULL, NULL);
318#if ((LCD_WIDTH - IMAGE_SIZE) > (LCD_HEIGHT - IMAGE_HEIGHT)) 327 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
319 rb->lcd_putsxy(IMAGE_WIDTH+5, 20, (unsigned char *)s); 328 moves_y, s);
320#else 329#else
321 rb->lcd_putsxy(40, IMAGE_HEIGHT+7, (unsigned char *)s); 330 (void)w;
331 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
332 rb->lcd_putsxy(3, moves_y, s);
322#endif 333#endif
323 for(i=1;i<=4;i++) 334 for(i=1;i<=4;i++)
324 { 335 {
@@ -342,26 +353,42 @@ static void move_spot(int x, int y)
342 spots[hole] = HOLE_ID; 353 spots[hole] = HOLE_ID;
343} 354}
344 355
345/* initializes the puzzle */ 356static void draw_playfield(void)
346static void puzzle_init(void)
347{ 357{
348 int i, r, temp, tsp[NUM_SPOTS]; 358 int i, w;
349 359
350 moves = 0;
351 rb->lcd_clear_display(); 360 rb->lcd_clear_display();
361 rb->lcd_setfont(moves_font);
362#if LCD_WIDTH > LCD_HEIGHT
363 rb->lcd_vline(IMAGE_WIDTH, 0, LCD_HEIGHT-1);
364 w = rb->lcd_getstringsize("Moves", NULL, NULL);
365 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
366 10, "Moves");
352 rb->snprintf(s, sizeof(s), "%d", moves); 367 rb->snprintf(s, sizeof(s), "%d", moves);
353 368 w = rb->lcd_getstringsize(s, NULL, NULL);
354#if ((LCD_WIDTH - IMAGE_SIZE) > (LCD_HEIGHT - IMAGE_HEIGHT)) 369 rb->lcd_putsxy((IMAGE_WIDTH+1+(LCD_WIDTH-IMAGE_WIDTH-1)/2) - w / 2,
355 rb->lcd_drawrect(IMAGE_WIDTH, 0, LCD_WIDTH-IMAGE_WIDTH, IMAGE_HEIGHT); 370 moves_y, s);
356 rb->lcd_putsxy(IMAGE_WIDTH+1, 10, (unsigned char *)"Moves");
357 rb->lcd_putsxy(IMAGE_WIDTH+5, 20, (unsigned char *)s);
358#else 371#else
359 rb->lcd_drawrect(0, IMAGE_HEIGHT, IMAGE_WIDTH, 372 (void)w;
360 (LCD_HEIGHT-IMAGE_HEIGHT)); 373 rb->lcd_hline(0, LCD_WIDTH-1, IMAGE_HEIGHT);
361 rb->lcd_putsxy(3, IMAGE_HEIGHT+7, (unsigned char *)"Moves: "); 374 rb->snprintf(s, sizeof(s), "Moves: %d", moves);
362 rb->lcd_putsxy(40, IMAGE_HEIGHT+7, (unsigned char *)s); 375 rb->lcd_putsxy(3, moves_y, s);
363#endif 376#endif
364 377
378 /* draw spots to the lcd */
379 for (i=0; i<NUM_SPOTS; i++)
380 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
381
382 rb->lcd_update();
383}
384
385/* initializes the puzzle */
386static void puzzle_init(void)
387{
388 int i, r, temp, tsp[NUM_SPOTS];
389
390 moves = 0;
391
365 /* shuffle spots */ 392 /* shuffle spots */
366 for (i=NUM_SPOTS-1; i>=0; i--) { 393 for (i=NUM_SPOTS-1; i>=0; i--) {
367 r = (rb->rand() % (i+1)); 394 r = (rb->rand() % (i+1));
@@ -407,11 +434,7 @@ static void puzzle_init(void)
407 } 434 }
408 } 435 }
409 436
410 /* draw spots to the lcd */ 437 draw_playfield();
411 for (i=0; i<NUM_SPOTS; i++)
412 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
413
414 rb->lcd_update();
415} 438}
416 439
417/* the main game loop */ 440/* the main game loop */
@@ -419,7 +442,6 @@ static int puzzle_loop(void)
419{ 442{
420 int button; 443 int button;
421 int lastbutton = BUTTON_NONE; 444 int lastbutton = BUTTON_NONE;
422 int i;
423 bool load_success; 445 bool load_success;
424 446
425 puzzle_init(); 447 puzzle_init();
@@ -461,22 +483,7 @@ static int puzzle_loop(void)
461 483
462 /* tell the user what mode we picked in the end! */ 484 /* tell the user what mode we picked in the end! */
463 rb->splash(HZ,picmode_descriptions[ picmode ] ); 485 rb->splash(HZ,picmode_descriptions[ picmode ] );
464 rb->lcd_clear_display(); 486 draw_playfield();
465#if ((LCD_WIDTH - IMAGE_SIZE) > (LCD_HEIGHT - IMAGE_HEIGHT))
466 rb->lcd_drawrect(IMAGE_WIDTH, 0, LCD_WIDTH-IMAGE_WIDTH,
467 IMAGE_HEIGHT);
468 rb->lcd_putsxy(IMAGE_WIDTH+1, 10, (unsigned char *)"Moves");
469#else
470 rb->lcd_drawrect(0, IMAGE_HEIGHT, IMAGE_WIDTH,
471 (LCD_HEIGHT-IMAGE_HEIGHT));
472 rb->lcd_putsxy(3, IMAGE_HEIGHT+7, (unsigned char *)"Moves: ");
473#endif
474 for (i=0; i<NUM_SPOTS; i++)
475 draw_spot(spots[i],
476 (i%SPOTS_X)*SPOTS_WIDTH,
477 (i/SPOTS_X)*SPOTS_HEIGHT);
478 rb->lcd_update();
479
480 break; 487 break;
481 488
482 case BUTTON_LEFT: 489 case BUTTON_LEFT:
@@ -594,27 +601,41 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
594 return PLUGIN_OK; 601 return PLUGIN_OK;
595 } 602 }
596 603
597#if LCD_DEPTH>1 604 /* Calculate possible font sizes and text positions */
605 rb->lcd_setfont(FONT_UI);
606 rb->lcd_getstringsize("15", &w, &h);
607 if ((w > (SPOTS_WIDTH-2)) || (h > (SPOTS_HEIGHT-2)))
608 num_font = FONT_SYSFIXED;
609
610#if LCD_WIDTH > LCD_HEIGHT
611 rb->lcd_getstringsize("Moves", &w, &h);
612 if (w > (LCD_WIDTH-IMAGE_WIDTH-1))
613 moves_font = FONT_SYSFIXED;
614 rb->lcd_setfont(moves_font);
615 rb->lcd_getstringsize("Moves", &w, &h);
616 moves_y = 10 + h;
617#else
618 rb->lcd_getstringsize("Moves: 999", &w, &h);
619 if ((w > LCD_WIDTH) || (h > (LCD_HEIGHT-IMAGE_HEIGHT-1)))
620 moves_font = FONT_SYSFIXED;
621 rb->lcd_setfont(moves_font);
622 rb->lcd_getstringsize("Moves: 999", &w, &h);
623 moves_y = (IMAGE_HEIGHT+1+(LCD_HEIGHT-IMAGE_HEIGHT-1)/2) - h / 2;
624#endif
625 for (i=0; i<NUM_SPOTS; i++)
626 spots[i]=(i+1);
627
628#ifdef HAVE_LCD_COLOR
598 rb->lcd_set_background(LCD_BLACK); 629 rb->lcd_set_background(LCD_BLACK);
599 rb->lcd_set_foreground(LCD_WHITE); 630 rb->lcd_set_foreground(LCD_WHITE);
600 rb->lcd_set_backdrop(NULL); 631 rb->lcd_set_backdrop(NULL);
632#elif LCD_DEPTH > 1
633 rb->lcd_set_background(LCD_WHITE);
634 rb->lcd_set_foreground(LCD_BLACK);
635 rb->lcd_set_backdrop(NULL);
601#endif 636#endif
602 637
603 rb->lcd_clear_display(); 638 draw_playfield();
604#if ((LCD_WIDTH - IMAGE_SIZE) > (LCD_HEIGHT - IMAGE_HEIGHT))
605 rb->lcd_drawrect(IMAGE_WIDTH, 0, LCD_WIDTH-IMAGE_WIDTH, IMAGE_HEIGHT);
606 rb->lcd_putsxy(IMAGE_WIDTH+1, 10, (unsigned char *)"Moves");
607#else
608 rb->lcd_drawrect(0, IMAGE_HEIGHT, IMAGE_WIDTH,
609 (LCD_HEIGHT-IMAGE_HEIGHT));
610 rb->lcd_putsxy(3, IMAGE_HEIGHT+7, (unsigned char *)"Moves: ");
611#endif
612 for (i=0; i<NUM_SPOTS; i++) {
613 spots[i]=(i+1);
614 draw_spot(spots[i], (i%SPOTS_X)*SPOTS_WIDTH, (i/SPOTS_X)*SPOTS_HEIGHT);
615 }
616
617 rb->lcd_update();
618 rb->sleep(HZ*2); 639 rb->sleep(HZ*2);
619 640
620 return puzzle_loop(); 641 return puzzle_loop();