Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / plugins / solitaire.c
blob385aa11a410ccfaa8df83affe7ecd3d6ac06ef91
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004-2007 Antoine Cellerier <dionoea @t videolan d.t org>
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 ****************************************************************************/
22 #include "plugin.h"
23 #include "playback_control.h"
24 #include "configfile.h"
25 #include "button.h"
26 #include "lcd.h"
27 #include "oldmenuapi.h"
29 #ifdef HAVE_LCD_BITMAP
31 PLUGIN_HEADER
33 static const struct plugin_api* rb;
34 #define min(a,b) (a<b?a:b)
36 /**
37 * Key definitions
40 #if CONFIG_KEYPAD == RECORDER_PAD
41 # define SOL_QUIT BUTTON_OFF
42 # define SOL_UP BUTTON_UP
43 # define SOL_DOWN BUTTON_DOWN
44 # define SOL_LEFT BUTTON_LEFT
45 # define SOL_RIGHT BUTTON_RIGHT
46 # define SOL_MOVE BUTTON_ON
47 # define SOL_DRAW BUTTON_F1
48 # define SOL_REM2CUR BUTTON_PLAY
49 # define SOL_CUR2STACK BUTTON_F2
50 # define SOL_REM2STACK BUTTON_F3
51 # define HK_MOVE "ON"
52 # define HK_DRAW "F1"
53 # define HK_REM2CUR "PLAY"
54 # define HK_CUR2STACK "F2"
55 # define HK_REM2STACK "F3"
57 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
58 # define SOL_QUIT BUTTON_OFF
59 # define SOL_UP BUTTON_UP
60 # define SOL_DOWN BUTTON_DOWN
61 # define SOL_LEFT BUTTON_LEFT
62 # define SOL_RIGHT BUTTON_RIGHT
63 # define SOL_MOVE BUTTON_ON
64 # define SOL_DRAW BUTTON_F1
65 # define SOL_REM2CUR BUTTON_SELECT
66 # define SOL_CUR2STACK BUTTON_F2
67 # define SOL_REM2STACK BUTTON_F3
68 # define HK_MOVE "ON"
69 # define HK_DRAW "F1"
70 # define HK_REM2CUR "SELECT"
71 # define HK_CUR2STACK "F2"
72 # define HK_REM2STACK "F3"
74 #elif CONFIG_KEYPAD == ONDIO_PAD
75 # define SOL_QUIT BUTTON_OFF
76 # define SOL_UP_PRE BUTTON_UP
77 # define SOL_UP (BUTTON_UP | BUTTON_REL)
78 # define SOL_DOWN_PRE BUTTON_DOWN
79 # define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
80 # define SOL_LEFT_PRE BUTTON_LEFT
81 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
82 # define SOL_RIGHT_PRE BUTTON_RIGHT
83 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
84 # define SOL_MOVE_PRE BUTTON_MENU
85 # define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
86 # define SOL_DRAW_PRE BUTTON_MENU
87 # define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
88 # define SOL_REM2CUR_PRE BUTTON_DOWN
89 # define SOL_REM2CUR (BUTTON_DOWN | BUTTON_REPEAT)
90 # define SOL_CUR2STACK_PRE BUTTON_UP
91 # define SOL_CUR2STACK (BUTTON_UP | BUTTON_REPEAT)
92 # define SOL_REM2STACK_PRE BUTTON_RIGHT
93 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
94 # define HK_MOVE "MODE"
95 # define HK_DRAW "MODE.."
96 # define HK_REM2CUR "DOWN.."
97 # define HK_CUR2STACK "UP.."
98 # define HK_REM2STACK "RIGHT.."
100 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
101 (CONFIG_KEYPAD == IRIVER_H300_PAD)
102 # define SOL_QUIT BUTTON_OFF
103 # define SOL_UP BUTTON_UP
104 # define SOL_DOWN BUTTON_DOWN
105 # define SOL_LEFT BUTTON_LEFT
106 # define SOL_RIGHT BUTTON_RIGHT
107 # define SOL_MOVE_PRE BUTTON_SELECT
108 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
109 # define SOL_DRAW BUTTON_MODE
110 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_ON)
111 # define SOL_CUR2STACK_PRE BUTTON_SELECT
112 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
113 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_ON)
114 # define SOL_REM BUTTON_REC
115 # define SOL_RC_QUIT BUTTON_RC_STOP
116 # define HK_MOVE "NAVI"
117 # define HK_DRAW "A-B"
118 # define HK_REM2CUR "PLAY+LEFT"
119 # define HK_CUR2STACK "NAVI.."
120 # define HK_REM2STACK "PLAY+RIGHT"
122 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
123 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
124 # define SOL_QUIT (BUTTON_SELECT | BUTTON_MENU)
125 # define SOL_UP BUTTON_SCROLL_BACK
126 # define SOL_DOWN BUTTON_SCROLL_FWD
127 # define SOL_LEFT_PRE BUTTON_LEFT
128 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
129 # define SOL_RIGHT_PRE BUTTON_RIGHT
130 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
131 # define SOL_MOVE_PRE BUTTON_SELECT
132 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
133 # define SOL_DRAW_PRE BUTTON_MENU
134 # define SOL_DRAW (BUTTON_MENU | BUTTON_REL)
135 # define SOL_REM2CUR BUTTON_PLAY
136 # define SOL_CUR2STACK_PRE BUTTON_MENU
137 # define SOL_CUR2STACK (BUTTON_MENU | BUTTON_REPEAT)
138 # define SOL_REM2STACK_PRE BUTTON_RIGHT
139 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
140 # define HK_UD "SCROLL U/D"
141 # define HK_MOVE "SELECT"
142 # define HK_DRAW "MENU"
143 # define HK_REM2CUR "PLAY"
144 # define HK_CUR2STACK "MENU.."
145 # define HK_REM2STACK "RIGHT.."
147 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
148 # define SOL_QUIT BUTTON_POWER
149 # define SOL_UP BUTTON_UP
150 # define SOL_DOWN BUTTON_DOWN
151 # define SOL_LEFT BUTTON_LEFT
152 # define SOL_RIGHT BUTTON_RIGHT
153 # define SOL_MOVE_PRE BUTTON_SELECT
154 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
155 # define SOL_DRAW_PRE BUTTON_PLAY
156 # define SOL_DRAW (BUTTON_PLAY | BUTTON_REL)
157 # define SOL_REM2CUR_PRE BUTTON_PLAY
158 # define SOL_REM2CUR (BUTTON_PLAY | BUTTON_REPEAT)
159 # define SOL_CUR2STACK_PRE BUTTON_SELECT
160 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
161 # define SOL_REM2STACK BUTTON_REC
162 # define HK_MOVE "SELECT"
163 # define HK_DRAW "PLAY"
164 # define HK_REM2CUR "PLAY.."
165 # define HK_CUR2STACK "SELECT.."
166 # define HK_REM2STACK "REC"
168 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
169 # define SOL_QUIT BUTTON_POWER
170 # define SOL_UP BUTTON_UP
171 # define SOL_DOWN BUTTON_DOWN
172 # define SOL_LEFT BUTTON_LEFT
173 # define SOL_RIGHT BUTTON_RIGHT
174 # define SOL_MOVE_PRE BUTTON_SELECT
175 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
176 # define SOL_DRAW BUTTON_MENU
177 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_A)
178 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
179 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_A)
180 # define HK_MOVE "SELECT"
181 # define HK_DRAW "MENU"
182 # define HK_REM2CUR "A+LEFT"
183 # define HK_CUR2STACK "SELECT.."
184 # define HK_REM2STACK "A+RIGHT"
186 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
187 # define SOL_QUIT BUTTON_POWER
188 # define SOL_UP BUTTON_UP
189 # define SOL_DOWN BUTTON_DOWN
190 # define SOL_LEFT BUTTON_SCROLL_BACK
191 # define SOL_RIGHT BUTTON_SCROLL_FWD
192 # define SOL_MOVE BUTTON_SELECT
193 # define SOL_DRAW BUTTON_REC
194 # define SOL_REM2CUR BUTTON_LEFT
195 # define SOL_CUR2STACK_PRE BUTTON_REC
196 # define SOL_CUR2STACK (BUTTON_REC | BUTTON_RIGHT)
197 # define SOL_REM2STACK BUTTON_RIGHT
198 # define HK_MOVE "SELECT"
199 # define HK_DRAW "REC"
200 # define HK_REM2CUR "LEFT"
201 # define HK_CUR2STACK "DOUBLE SELECT"
202 # define HK_REM2STACK "RIGHT"
204 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
205 # define SOL_QUIT BUTTON_POWER
206 # define SOL_UP BUTTON_UP
207 # define SOL_DOWN BUTTON_DOWN
208 # define SOL_LEFT BUTTON_LEFT
209 # define SOL_RIGHT BUTTON_RIGHT
210 # define SOL_MOVE_PRE BUTTON_SELECT
211 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
212 # define SOL_DRAW BUTTON_VOL_DOWN
213 # define SOL_REM2CUR BUTTON_REC
214 # define SOL_CUR2STACK_PRE BUTTON_SELECT
215 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
216 # define SOL_REM2STACK BUTTON_VOL_UP
217 # define HK_MOVE "SELECT"
218 # define HK_DRAW "REC"
219 # define HK_REM2CUR "LEFT"
220 # define HK_CUR2STACK "DOUBLE SELECT"
221 # define HK_REM2STACK "RIGHT"
223 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
224 # define SOL_QUIT BUTTON_POWER
225 # define SOL_UP BUTTON_SCROLL_UP
226 # define SOL_DOWN BUTTON_SCROLL_DOWN
227 # define SOL_LEFT_PRE BUTTON_LEFT
228 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
229 # define SOL_RIGHT_PRE BUTTON_RIGHT
230 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
231 # define SOL_MOVE BUTTON_PLAY
232 # define SOL_DRAW_PRE BUTTON_LEFT
233 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
234 # define SOL_REM2CUR BUTTON_FF
235 # define SOL_CUR2STACK BUTTON_REW
236 # define SOL_REM2STACK_PRE BUTTON_RIGHT
237 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
238 # define HK_MOVE "PLAY"
239 # define HK_DRAW "LEFT.."
240 # define HK_REM2CUR "FF"
241 # define HK_CUR2STACK "REW"
242 # define HK_REM2STACK "RIGHT.."
244 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
245 # define SOL_QUIT BUTTON_BACK
246 # define SOL_UP BUTTON_UP
247 # define SOL_DOWN BUTTON_DOWN
248 # define SOL_LEFT BUTTON_LEFT
249 # define SOL_RIGHT BUTTON_RIGHT
250 # define SOL_MOVE_PRE BUTTON_SELECT
251 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
252 # define SOL_DRAW BUTTON_MENU
253 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
254 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
255 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
256 # define HK_MOVE "SELECT"
257 # define HK_DRAW "MENU"
258 # define HK_REM2CUR "SELECT+LEFT"
259 # define HK_CUR2STACK "SELECT.."
260 # define HK_REM2STACK "SELECT+RIGHT"
262 #elif (CONFIG_KEYPAD == MROBE100_PAD)
263 # define SOL_QUIT BUTTON_POWER
264 # define SOL_UP BUTTON_UP
265 # define SOL_DOWN BUTTON_DOWN
266 # define SOL_LEFT BUTTON_LEFT
267 # define SOL_RIGHT BUTTON_RIGHT
268 # define SOL_MOVE_PRE BUTTON_SELECT
269 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
270 # define SOL_DRAW BUTTON_MENU
271 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_DISPLAY)
272 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
273 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_DISPLAY)
274 # define HK_MOVE "SELECT"
275 # define HK_DRAW "MENU"
276 # define HK_REM2CUR "DISPLAY+LEFT"
277 # define HK_CUR2STACK "SELECT.."
278 # define HK_REM2STACK "DISPLAY+RIGHT"
280 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
281 # define SOL_QUIT BUTTON_RC_REC
282 # define SOL_UP BUTTON_RC_VOL_UP
283 # define SOL_DOWN BUTTON_RC_VOL_DOWN
284 # define SOL_LEFT BUTTON_RC_REW
285 # define SOL_RIGHT BUTTON_RC_FF
286 # define SOL_MOVE BUTTON_RC_PLAY
287 # define SOL_DRAW_PRE BUTTON_RC_MENU
288 # define SOL_DRAW (BUTTON_RC_MENU | BUTTON_REL)
289 # define SOL_REM2CUR_PRE BUTTON_RC_MENU
290 # define SOL_REM2CUR (BUTTON_RC_MENU | BUTTON_REPEAT)
291 # define SOL_CUR2STACK_PRE BUTTON_RC_MODE
292 # define SOL_CUR2STACK (BUTTON_RC_MODE | BUTTON_REL)
293 # define SOL_REM2STACK_PRE BUTTON_RC_MODE
294 # define SOL_REM2STACK (BUTTON_RC_MODE | BUTTON_REPEAT)
295 # define HK_LR "REW/FF"
296 # define HK_UD "VOL UP/DOWN"
297 # define HK_MOVE "PLAY"
298 # define HK_DRAW "MENU"
299 # define HK_REM2CUR "MENU.."
300 # define HK_CUR2STACK "MODE"
301 # define HK_REM2STACK "MODE.."
303 #elif (CONFIG_KEYPAD == COWOND2_PAD)
304 # define SOL_QUIT BUTTON_POWER
306 #else
307 #error No keymap defined!
308 #endif
310 #ifdef HAVE_TOUCHPAD
311 //#ifndef SOL_QUIT
312 //# define SOL_QUIT BUTTON_TOPLEFT
313 //endif
314 #ifndef SOL_UP
315 # define SOL_UP BUTTON_TOPMIDDLE
316 #endif
317 #ifndef SOL_DOWN
318 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
319 #endif
320 #ifndef SOL_LEFT
321 # define SOL_LEFT BUTTON_MIDLEFT
322 #endif
323 #ifndef SOL_RIGHT
324 # define SOL_RIGHT BUTTON_MIDRIGHT
325 #endif
326 #ifndef SOL_MOVE
327 # define SOL_MOVE BUTTON_CENTER
328 # define HK_MOVE "CENTRE"
329 #endif
330 #ifndef SOL_DRAW
331 # define SOL_DRAW BUTTON_TOPLEFT
332 # define HK_DRAW "TOPLEFT"
333 #endif
334 #ifndef SOL_REM2CUR
335 # define SOL_REM2CUR BUTTON_TOPRIGHT
336 # define HK_REM2CUR "TOPRIGHT"
337 #endif
338 #ifndef SOL_CUR2STACK
339 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
340 # define HK_CUR2STACK "BOTTOMLEFT"
341 #endif
342 #ifndef SOL_REM2STACK
343 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
344 # define HK_REM2STACK "BOTTOMRIGHT"
345 #endif
347 #endif
349 #ifndef HK_LR
350 # define HK_LR "LEFT/RIGHT"
351 #endif
352 #ifndef HK_UD
353 # define HK_UD "UP/DOWN"
354 #endif
357 * Help strings
360 static int helplines;
361 static int displaylines;
363 static char helptext[] =
364 /* Use single spaces only! Close each line with one \0. */
365 "-- Navigation --\0"
366 HK_LR ": Move the cursor to the previous/ next column.\0"
367 HK_UD ": Move the cursor up/ down in the column.\0"
368 HK_MOVE ": Select cards, move cards, reveal hidden cards...\0"
369 HK_DRAW ": Deselect a card if it was selected. Else draw new card(s) from the remains stack.\0"
370 "-- Shortcuts --\0"
371 HK_REM2CUR ": Put the card on top of the remains stack on top of the cursor.\0"
372 HK_CUR2STACK ": Put the card under the cursor on one of the 4 final stacks.\0"
373 HK_REM2STACK ": Put the card on top of the remains stack on one of the 4 final stacks.\0"
377 * Misc constants, graphics and other defines
380 #include "card_back.h"
381 #include "card_deck.h"
382 #include "solitaire_suitsi.h"
384 #define CARD_GFX_WIDTH BMPWIDTH_card_back
385 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
386 #define CARD_WIDTH (BMPWIDTH_card_back+2)
387 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
389 #if LCD_WIDTH >= 320
390 # define MARGIN 4
391 # define LARGE_CARD
392 # define SYMBOL_HEIGHT 12
393 #elif LCD_WIDTH >= 220
394 # define MARGIN 3
395 # define LARGE_CARD
396 # define SYMBOL_HEIGHT 12
397 #elif LCD_WIDTH >= 160
398 # define MARGIN 2
399 # define SYMBOL_HEIGHT 11
400 #elif LCD_WIDTH >= 128
401 # define MARGIN 1
402 # define SYMBOL_HEIGHT 10
403 #else
404 # define MARGIN 0
405 # define SYMBOL_HEIGHT 8
406 #endif
408 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
410 /* background color */
411 #ifdef HAVE_LCD_COLOR
412 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
413 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
414 #elif LCD_DEPTH > 1
415 # define BACKGROUND_COLOR LCD_WHITE
416 # define FRAME_COLOR LCD_BLACK
417 #endif
420 #define CONFIG_FILENAME "sol.cfg"
422 #define NOT_A_CARD -1
424 /* number of cards per suit */
425 #define CARDS_PER_SUIT 13
427 /* number of suits */
428 #define SUITS 4
430 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
432 /* number of columns */
433 #define COL_NUM 7
435 /* pseudo column numbers to be used for cursor coordinates */
436 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
437 #define STACKS_COL COL_NUM
438 /* column COL_NUM + SUITS corresponds to the remains' stack */
439 #define REM_COL (STACKS_COL + SUITS)
441 #define NOT_A_COL -1
443 typedef struct
445 signed char suit;
446 signed char num;
447 bool known : 1;
448 bool used : 1; /* this is what is used when dealing cards */
449 signed char next;
450 } card_t;
454 * LCD card drawing routines
457 static void draw_cursor( int x, int y )
459 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
460 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
461 #ifdef LARGE_CARD
462 rb->lcd_drawpixel( x+1, y+1 );
463 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
464 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
465 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
466 #endif
467 rb->lcd_set_drawmode( DRMODE_SOLID );
470 /* Draw a card's border, select it if it's selected and draw the cursor
471 * if the cursor is currently over the card */
472 static void draw_card_ext( int x, int y, bool selected, bool cursor )
474 #if LCD_DEPTH > 1
475 int oldfg = rb->lcd_get_foreground();
477 rb->lcd_set_foreground( LCD_BLACK );
478 #endif
479 #ifdef LARGE_CARD
480 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
481 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
482 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
483 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
484 rb->lcd_drawpixel( x+1, y+1 );
485 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
486 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
487 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
488 #else
489 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
490 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
491 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
492 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
493 #endif
495 if( selected )
497 #if LCD_DEPTH > 1
498 rb->lcd_set_foreground( FRAME_COLOR );
499 #endif
500 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
501 #ifdef LARGE_CARD
502 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
503 #endif
505 #if LCD_DEPTH > 1
506 rb->lcd_set_foreground( oldfg );
507 #endif
509 if( cursor )
511 draw_cursor( x, y );
515 /* Draw a card's inner graphics */
516 static void draw_card( card_t *card, int x, int y,
517 bool selected, bool cursor )
519 if( card->known )
521 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
522 CARD_GFX_HEIGHT * card->suit, BMPWIDTH_card_deck,
523 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
525 else
527 rb->lcd_bitmap( card_back, x+1, y+1,
528 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
530 draw_card_ext( x, y, selected, cursor );
533 /* Draw an empty stack */
534 static void draw_empty_stack( int s, int x, int y, bool cursor )
536 rb->lcd_bitmap_part( solitaire_suitsi, 0,
537 CARD_GFX_HEIGHT * s, BMPWIDTH_solitaire_suitsi,
538 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
540 draw_card_ext( x, y, false, cursor );
544 * Help
547 static void init_help(void)
549 int lines = 0;
550 int w_space, w, h;
551 int para_len;
552 char *para = helptext;
554 rb->lcd_getstringsize(" ", &w_space, &h);
555 displaylines = LCD_HEIGHT / h;
556 para_len = rb->strlen(para);
558 while (para_len)
560 bool first = true;
561 int x = 0;
562 char *next, *store;
564 next = rb->strtok_r(para, " ", &store);
565 while (next)
567 rb->lcd_getstringsize(next, &w, NULL);
568 if (!first)
570 if (x + w > LCD_WIDTH)
572 lines++;
573 x = 0;
575 else
576 next[-1] = ' '; /* re-concatenate string */
578 else
579 first = false;
581 x += w + w_space;
582 next = rb->strtok_r(NULL, " ", &store);
585 lines++;
586 para += para_len + 1;
587 para_len = rb->strlen(para);
589 helplines = lines;
592 enum help { HELP_QUIT, HELP_USB };
594 /* help for the not so intuitive interface */
595 enum help solitaire_help( void )
597 int start = 0;
598 int button;
599 int lastbutton = BUTTON_NONE;
600 bool fixed = (displaylines >= helplines);
602 while( true )
604 char *text = helptext;
605 int line = fixed ? (displaylines - helplines) / 2 : 0;
606 int i;
608 rb->lcd_clear_display();
610 for (i = 0; i < start + displaylines; i++)
612 if (i >= start)
613 rb->lcd_puts(0, line++, text);
614 text += rb->strlen(text) + 1;
616 rb->lcd_update();
618 button = rb->button_get( true );
619 switch( button )
621 case SOL_UP:
622 #ifdef SOL_UP_PRE
623 if( lastbutton != SOL_UP_PRE )
624 break;
625 #else
626 case SOL_UP|BUTTON_REPEAT:
627 #endif
628 if (!fixed && start > 0)
629 start--;
630 break;
632 case SOL_DOWN:
633 #ifdef SOL_DOWN_PRE
634 if( lastbutton != SOL_DOWN_PRE )
635 break;
636 #else
637 case SOL_DOWN|BUTTON_REPEAT:
638 #endif
639 if (!fixed && start < helplines - displaylines)
640 start++;
641 break;
643 #ifdef SOL_RC_QUIT
644 case SOL_RC_QUIT:
645 #endif
646 case SOL_QUIT:
647 return HELP_QUIT;
649 default:
650 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
651 return HELP_USB;
652 break;
654 if( button != BUTTON_NONE )
655 lastbutton = button;
660 * Custom menu / options
663 #define CFGFILE_VERSION 0
665 struct sol_config {
666 int draw_type;
669 struct sol_config sol_disk = {0};
670 struct sol_config sol;
672 static struct configdata config[] = {
673 { TYPE_INT, 0, 1, &sol_disk.draw_type, "draw_type", NULL, NULL },
676 char draw_option_string[32];
678 static void create_draw_option_string(void)
680 if (sol.draw_type == 0)
681 rb->strcpy(draw_option_string, "Draw Three Cards");
682 else
683 rb->strcpy(draw_option_string, "Draw One Card");
686 void solitaire_init(void);
688 /* menu return codes */
689 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
691 int solitaire_menu(bool in_game)
693 int m;
694 int result = -1;
695 int i = 0;
697 struct menu_item items[6];
699 if( in_game )
701 items[i++].desc = "Resume Game";
702 items[i++].desc = "Restart Game";
704 else
706 items[i++].desc = "Start Game";
707 items[i++].desc = draw_option_string;
709 items[i++].desc = "Help";
710 items[i++].desc = "Audio Playback";
711 if( in_game )
713 items[i++].desc = "Save and Quit";
715 items[i++].desc = "Quit";
717 create_draw_option_string();
718 m = menu_init(rb, items, i, NULL, NULL, NULL, NULL);
719 while (result < 0)
721 switch (menu_show(m))
723 case MENU_SELECTED_EXIT:
724 result = MENU_RESUME;
725 break;
727 case MENU_ATTACHED_USB:
728 result = MENU_USB;
729 break;
731 case 0:
732 result = MENU_RESUME;
733 break;
735 case 1:
736 if (in_game)
738 solitaire_init();
739 result = MENU_RESUME;
741 else
743 sol.draw_type = (sol.draw_type + 1) % 2;
744 create_draw_option_string();
746 break;
748 case 2:
749 if (solitaire_help() == HELP_USB)
750 result = MENU_USB;
751 break;
753 case 3:
754 playback_control(rb, NULL);
755 break;
757 case 4:
758 if( in_game )
759 result = MENU_SAVE_AND_QUIT;
760 else
761 result = MENU_QUIT;
762 break;
764 case 5:
765 result = MENU_QUIT;
766 break;
769 menu_exit(m);
770 return result;
774 * Global variables
777 /* player's cursor */
778 int cur_card;
779 /* player's cursor column num */
780 int cur_col;
782 /* selected card */
783 int sel_card;
785 /* the deck */
786 card_t deck[ NUM_CARDS ];
788 /* the remaining cards */
789 /* first card of the remains' stack */
790 int rem;
791 /* upper visible card from the remains' stack */
792 int cur_rem;
793 /* number of cards drawn from the remains stack - 1 */
794 int count_rem;
795 /* number of cards per draw of the remains' stack */
796 int cards_per_draw;
798 /* the 7 game columns */
799 int cols[COL_NUM];
800 /* the 4 final stacks */
801 int stacks[SUITS];
804 * Card handling routines
807 int next_random_card( card_t *deck )
809 int i,r;
811 r = rb->rand()%(NUM_CARDS)+1;
812 i = 0;
814 while( r>0 )
816 i = (i + 1)%(NUM_CARDS);
817 if( !deck[i].used ) r--;
820 deck[i].used = true;
822 return i;
826 /* initialize the game */
827 void solitaire_init( void )
830 int c;
831 int i, j;
833 /* number of cards that are drawn on the remains' stack (by pressing F2) */
834 if( sol.draw_type == 0 )
836 cards_per_draw = 3;
838 else
840 cards_per_draw = 1;
843 /* init deck */
844 for( i=0; i<SUITS; i++ )
846 for( j=0; j<CARDS_PER_SUIT; j++ )
848 #define card deck[i*CARDS_PER_SUIT+j]
849 card.suit = i;
850 card.num = j;
851 card.known = true;
852 card.used = false;
853 card.next = NOT_A_CARD;
854 #undef card
858 /* deal the cards ... */
859 /* ... in the columns */
860 for( i=0; i<COL_NUM; i++ )
862 c = NOT_A_CARD;
863 for( j=0; j<=i; j++ )
865 if( c == NOT_A_CARD )
867 cols[i] = next_random_card( deck );
868 c = cols[i];
870 else
872 deck[c].next = next_random_card( deck );
873 c = deck[c].next;
875 if( j < i )
876 deck[c].known = false;
880 /* ... shuffle what's left of the deck */
881 rem = next_random_card(deck);
882 c = rem;
884 for( i=1; i < NUM_CARDS - COL_NUM * (COL_NUM + 1)/2; i++ )
886 deck[c].next = next_random_card( deck );
887 c = deck[c].next;
890 /* we now finished dealing the cards. The game can start ! (at last) */
892 /* init the stack */
893 for( i = 0; i<SUITS; i++ )
895 stacks[i] = NOT_A_CARD;
898 /* the cursor starts on upper left card */
899 cur_card = cols[0];
900 cur_col = 0;
902 /* no card is selected */
903 sel_card = NOT_A_CARD;
905 /* init the remainder */
906 cur_rem = NOT_A_CARD;
908 count_rem = -1;
911 /* find the column number in which 'card' can be found */
912 int find_card_col( int card )
914 int i;
915 int c;
917 if( card == NOT_A_CARD ) return NOT_A_COL;
919 for( i=0; i<COL_NUM; i++ )
921 c = cols[i];
922 while( c != NOT_A_CARD )
924 if( c == card ) return i;
925 c = deck[c].next;
929 for( i=0; i<SUITS; i++ )
931 c = stacks[i];
932 while( c != NOT_A_CARD )
934 if( c == card ) return STACKS_COL + i;
935 c = deck[c].next;
939 return REM_COL;
942 /* find the card preceding 'card' */
943 /* if it doesn't exist, return NOT_A_CARD */
944 int find_prev_card( int card ){
945 int i;
947 for( i=0; i < NUM_CARDS; i++ )
949 if( deck[i].next == card ) return i;
952 return NOT_A_CARD;
955 /* find the last card of a given column */
956 int find_last_card( int col )
958 int c;
960 if( col < COL_NUM )
962 c = cols[col];
964 else if( col < REM_COL )
966 c = stacks[col - STACKS_COL];
968 else
970 c = cur_rem;
973 if(c == NOT_A_CARD)
974 return c;
975 else
977 while(deck[c].next != NOT_A_CARD)
978 c = deck[c].next;
979 return c;
983 enum move { MOVE_OK, MOVE_NOT_OK };
985 enum move move_card( int dest_col, int src_card )
987 /* the column on which to take src_card */
988 int src_col;
990 /* the last card of dest_col */
991 int dest_card;
993 /* the card under src_card */
994 int src_card_prev;
996 /* you can't move no card (at least, it doesn't have any consequence) */
997 if( src_card == NOT_A_CARD ) return MOVE_NOT_OK;
998 /* you can't put a card back on the remains' stack */
999 if( dest_col == REM_COL ) return MOVE_NOT_OK;
1000 /* you can't move an unknown card */
1001 if( !deck[src_card].known ) return MOVE_NOT_OK;
1003 src_col = find_card_col( src_card );
1004 dest_card = find_last_card( dest_col );
1005 src_card_prev = find_prev_card( src_card );
1007 /* you can't move more than one card at a time from the final stack */
1008 /* to the rest of the game */
1009 if( src_col >= COL_NUM && src_col < REM_COL
1010 && deck[src_card].next != NOT_A_CARD )
1012 return MOVE_NOT_OK;
1015 /* if we (that means dest) are on one of the 7 columns ... */
1016 if( dest_col < COL_NUM )
1018 /* ... check is we are on an empty color and that the src is a king */
1019 if( dest_card == NOT_A_CARD
1020 && deck[src_card].num == CARDS_PER_SUIT - 1 )
1022 /* this is a winning combination */
1023 cols[dest_col] = src_card;
1025 /* ... or check if the cards follow one another and have
1026 * different colorsuit */
1027 else if(( deck[dest_card].suit + deck[src_card].suit)%2==1
1028 && deck[dest_card].num == deck[src_card].num + 1 )
1030 /* this is a winning combination */
1031 deck[dest_card].next = src_card;
1033 /* ... or, humpf, well that's not good news */
1034 else
1036 /* this is not a winning combination */
1037 return MOVE_NOT_OK;
1040 /* if we are on one of the 4 final stacks ... */
1041 else if( dest_col < REM_COL )
1043 /* ... check if we are on an empty stack... */
1044 if( dest_card == NOT_A_CARD )
1046 /* ... and the src is an ace and this is the correct final stack */
1047 if( deck[src_card].num == 0
1048 && deck[src_card].suit == dest_col - STACKS_COL )
1050 /* this is a winning combination */
1051 stacks[dest_col - STACKS_COL] = src_card;
1053 else
1055 /* this is not a winning combination */
1056 return MOVE_NOT_OK;
1059 else /* non-empty stack */
1061 /* ...check if the cards follow one another, have the same suit and
1062 * {that src has no .next element or is from the remains' stack} */
1063 if( deck[dest_card].suit == deck[src_card].suit
1064 && deck[dest_card].num + 1 == deck[src_card].num
1065 && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) )
1067 /* this is a winning combination */
1068 deck[dest_card].next = src_card;
1070 /* ... or, well that's not good news */
1071 else
1073 /* this is not a winning combination */
1074 return MOVE_NOT_OK;
1078 /* if we are on the remains' stack */
1079 else
1081 /* you can't move a card back to the remains' stack */
1082 return MOVE_NOT_OK;
1085 /* if the src card is from the remains' stack, we don't want to take
1086 * the following cards */
1087 if( src_col == REM_COL )
1089 /* if src card is the first card from the stack */
1090 if( src_card_prev == NOT_A_CARD )
1092 rem = deck[src_card].next;
1094 /* if src card is not the first card from the stack */
1095 else
1097 deck[src_card_prev].next = deck[src_card].next;
1099 deck[src_card].next = NOT_A_CARD;
1100 cur_rem = src_card_prev;
1101 count_rem--;
1103 /* if the src card is from somewhere else, just take everything */
1104 else
1106 if( src_card_prev == NOT_A_CARD )
1108 if( src_col < COL_NUM )
1110 cols[src_col] = NOT_A_CARD;
1112 else
1114 stacks[src_col - STACKS_COL] = NOT_A_CARD;
1117 else
1119 deck[src_card_prev].next = NOT_A_CARD;
1120 deck[src_card_prev].known = true;
1123 sel_card = NOT_A_CARD;
1124 /* tada ! */
1125 return MOVE_OK;
1128 enum { SOLITAIRE_WIN, SOLITAIRE_SAVE_AND_QUIT, SOLITAIRE_QUIT, SOLITAIRE_USB };
1131 * Bouncing cards at the end of the game
1134 #define BC_ACCEL ((1<<16)*LCD_HEIGHT/128)
1135 #define BC_MYSPEED (6*BC_ACCEL)
1136 #define BC_MXSPEED (6*LCD_HEIGHT/128)
1138 int bouncing_cards( void )
1140 int i, j, x, vx, y, fp_y, fp_vy, button;
1142 /* flush the button queue */
1143 while( ( button = rb->button_get( false ) ) != BUTTON_NONE )
1145 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1146 return SOLITAIRE_USB;
1149 /* fun stuff :) */
1150 for( i = CARDS_PER_SUIT-1; i>=0; i-- )
1152 for( j = 0; j < SUITS; j++ )
1154 x = LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*j+j+1;
1155 fp_y = MARGIN<<16;
1157 #if LCD_WIDTH > 200
1158 vx = rb->rand() % (4*BC_MXSPEED/3-2) - BC_MXSPEED;
1159 if( vx >= -1 )
1160 vx += 3;
1161 #else
1162 vx = rb->rand() % (4*BC_MXSPEED/3) - BC_MXSPEED;
1163 if( vx >= 0 )
1164 vx++;
1165 #endif
1167 fp_vy = -rb->rand() % BC_MYSPEED;
1169 while( x < LCD_WIDTH && x + CARD_WIDTH > 0 )
1171 fp_vy += BC_ACCEL;
1172 x += vx;
1173 fp_y += fp_vy;
1174 if( fp_y >= (LCD_HEIGHT-CARD_HEIGHT) << 16 )
1176 fp_vy = -fp_vy*4/5;
1177 fp_y = (LCD_HEIGHT-CARD_HEIGHT) << 16;
1179 y = fp_y >> 16;
1180 draw_card( &deck[j*CARDS_PER_SUIT+i], x, y,
1181 false, false );
1182 rb->lcd_update_rect( x<0?0:x, y<0?0:y,
1183 CARD_WIDTH, CARD_HEIGHT );
1185 button = rb->button_get_w_tmo( 2 );
1186 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1187 return SOLITAIRE_USB;
1188 if( button == SOL_QUIT || button == SOL_MOVE )
1189 return SOLITAIRE_WIN;
1193 return SOLITAIRE_WIN;
1197 * Game save/load routines
1199 void get_save_filename( char *buf )
1201 char *s;
1202 rb->strcpy( buf, rb->plugin_get_current_filename() );
1203 s = rb->strrchr( buf, '/' ) + 1;
1204 *s = '\0';
1205 rb->strcat( s, "sol.save" );
1208 int open_save_file( int flags )
1210 char buf[MAX_PATH];
1211 get_save_filename( buf );
1212 return rb->open( buf, flags );
1215 void delete_save_file( void )
1217 char buf[MAX_PATH];
1218 get_save_filename( buf );
1219 rb->remove( buf );
1222 #ifdef write
1223 # undef write
1224 #endif
1225 int save_write( int fd, const void *buf, size_t count, int *checksum )
1227 size_t i;
1228 if( rb->PREFIX(write)( fd, buf, count ) < (ssize_t)count )
1229 return 1;
1230 for( i = 0; i < count; i++ )
1231 *checksum += (int)(((const char *)buf)[i]);
1232 return 0;
1235 #ifdef read
1236 # undef read
1237 #endif
1238 int save_read( int fd, void *buf, size_t count, int *checksum )
1240 size_t i;
1241 if( rb->PREFIX(read)( fd, buf, count ) < (ssize_t)count )
1242 return 1;
1243 for( i = 0; i < count; i++ )
1244 *checksum -= (int)(((const char *)buf)[i]);
1245 return 0;
1248 int save_game( void )
1250 int fd = open_save_file( O_CREAT|O_WRONLY|O_TRUNC );
1251 int checksum = 42;
1252 if( fd < 0 )
1253 return -1;
1254 if( save_write( fd, &cur_card, sizeof( int ), &checksum )
1255 || save_write( fd, &cur_col, sizeof( int ), &checksum )
1256 || save_write( fd, &sel_card, sizeof( int ), &checksum )
1257 || save_write( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1258 || save_write( fd, &rem, sizeof( int ), &checksum )
1259 || save_write( fd, &cur_rem, sizeof( int ), &checksum )
1260 || save_write( fd, &count_rem, sizeof( int ), &checksum )
1261 || save_write( fd, &cards_per_draw, sizeof( int ), &checksum )
1262 || save_write( fd, cols, COL_NUM * sizeof( int ), &checksum )
1263 || save_write( fd, stacks, SUITS * sizeof( int ), &checksum )
1264 || ( rb->PREFIX(write)( fd, &checksum, sizeof( int ) ) < (ssize_t)(sizeof( int ) ) ) )
1266 rb->close( fd );
1267 rb->splash( 2*HZ, "Error while saving game. Aborting." );
1268 return -2;
1270 rb->close( fd );
1271 return 0;
1274 int load_game( void )
1276 int fd = open_save_file( O_RDONLY );
1277 int checksum;
1278 if( fd < 0 )
1279 return -1;
1280 if( ( rb->PREFIX(lseek)( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 )
1281 || ( rb->PREFIX(read)( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) )
1282 || ( rb->PREFIX(lseek)( fd, 0, SEEK_SET ) == -1 )
1283 || save_read( fd, &cur_card, sizeof( int ), &checksum )
1284 || save_read( fd, &cur_col, sizeof( int ), &checksum )
1285 || save_read( fd, &sel_card, sizeof( int ), &checksum )
1286 || save_read( fd, deck, NUM_CARDS * sizeof( card_t ), &checksum )
1287 || save_read( fd, &rem, sizeof( int ), &checksum )
1288 || save_read( fd, &cur_rem, sizeof( int ), &checksum )
1289 || save_read( fd, &count_rem, sizeof( int ), &checksum )
1290 || save_read( fd, &cards_per_draw, sizeof( int ), &checksum )
1291 || save_read( fd, cols, COL_NUM * sizeof( int ), &checksum )
1292 || save_read( fd, stacks, SUITS * sizeof( int ), &checksum ) )
1294 rb->close( fd );
1295 rb->splash( 2*HZ, "Error while loading saved game. Aborting." );
1296 delete_save_file();
1297 return -2;
1299 rb->close( fd );
1300 if( checksum != 42 )
1302 rb->splash( 2*HZ, "Save file was corrupted. Aborting." );
1303 delete_save_file();
1304 return -3;
1306 return 0;
1310 * The main game loop
1312 * If skipmenu is defined to SOLITAIRE_QUIT, the menu will be skipped and
1313 * game will resume.
1316 int solitaire( int skipmenu )
1319 int i,j;
1320 int button, lastbutton = 0;
1321 int c,h,prevcard;
1322 int biggest_col_length;
1324 rb->srand( *rb->current_tick );
1325 if( skipmenu != SOLITAIRE_QUIT )
1327 switch( solitaire_menu(false) )
1329 case MENU_QUIT:
1330 return SOLITAIRE_QUIT;
1332 case MENU_USB:
1333 return SOLITAIRE_USB;
1335 solitaire_init();
1338 while( true )
1340 rb->lcd_clear_display();
1342 /* get the biggest column length so that display can be "optimized" */
1343 biggest_col_length = 0;
1345 for(i=0;i<COL_NUM;i++)
1347 j = 0;
1348 c = cols[i];
1350 if( c != NOT_A_CARD )
1352 while( true )
1354 /* don't include the last card in the column length. */
1355 if( deck[c].next == NOT_A_CARD )
1357 break; /* no successor: get outta here. */
1359 else
1361 if( deck[c].known )
1362 j += 2;
1363 else
1364 j++;
1366 c = deck[c].next;
1368 /* make column distinguishable from an empty column,
1369 * and avoid division by zero while displaying */
1370 if( j == 0 )
1371 j = 1;
1373 if( j > biggest_col_length )
1374 biggest_col_length = j;
1377 /* check if there are cards remaining in the game. */
1378 /* if there aren't any, that means you won :) */
1379 if( biggest_col_length == 0 && rem == NOT_A_CARD )
1381 rb->splash( HZ, "You Won :)" );
1382 return bouncing_cards();
1385 /* draw the columns */
1386 for( i = 0; i < COL_NUM; i++ )
1388 c = cols[i];
1389 j = CARD_START;
1390 while( true )
1392 if( c == NOT_A_CARD )
1394 /* draw the cursor on empty columns */
1395 if( cur_col == i )
1397 draw_cursor( MARGIN + i * (CARD_WIDTH
1398 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1399 j );
1401 break;
1404 draw_card( &deck[c], MARGIN + i * (CARD_WIDTH
1405 +(LCD_WIDTH-COL_NUM*CARD_WIDTH-2*MARGIN)/(COL_NUM-1)),
1406 j, c == sel_card, c == cur_card );
1408 h = c;
1409 c = deck[c].next;
1410 if( c == NOT_A_CARD ) break;
1412 /* This is where we change the spacing between cards so that
1413 * they don't overflow out of the LCD */
1414 if( h == cur_card )
1415 j += SYMBOL_HEIGHT;
1416 else if( deck[h].known )
1417 j += min( SYMBOL_HEIGHT,
1418 2 * (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1419 / biggest_col_length );
1420 else
1421 j += min( SYMBOL_HEIGHT / 2,
1422 (LCD_HEIGHT - CARD_START - CARD_HEIGHT - MARGIN)
1423 / biggest_col_length );
1427 /* draw the stacks */
1428 for( i=0; i<SUITS; i++ )
1430 c = find_last_card( STACKS_COL + i );
1432 if( c != NOT_A_CARD )
1434 draw_card( &deck[c],
1435 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1436 MARGIN,
1437 c == sel_card, cur_col == STACKS_COL + i );
1439 else
1441 draw_empty_stack( i,
1442 LCD_WIDTH-(CARD_WIDTH*4+4+MARGIN)+CARD_WIDTH*i+i+1,
1443 MARGIN, cur_col == STACKS_COL + i );
1447 /* draw the remains */
1448 if( rem != NOT_A_CARD &&
1449 ( cur_rem == NOT_A_CARD || deck[cur_rem].next != NOT_A_CARD ) )
1451 /* gruik ! (we want to display a card back) */
1452 deck[rem].known = false;
1453 draw_card( &deck[rem], MARGIN, MARGIN, false, false );
1454 deck[rem].known = true;
1457 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1459 if( count_rem < 0 )
1461 prevcard = rem;
1462 count_rem = 0;
1463 while( prevcard != cur_rem && count_rem < cards_per_draw-1 )
1465 prevcard = deck[prevcard].next;
1466 count_rem++;
1469 prevcard = cur_rem;
1470 j = CARD_WIDTH+2*MARGIN+1;
1471 for( i = 0; i < count_rem; i++ )
1472 prevcard = find_prev_card(prevcard);
1473 for( i = 0; i <= count_rem; i++ )
1475 draw_card( &deck[prevcard], j,
1476 MARGIN, sel_card == prevcard,
1477 cur_card == prevcard );
1478 prevcard = deck[prevcard].next;
1479 j += CARD_WIDTH/2;
1482 if( ( cur_rem == NOT_A_CARD || rem == NOT_A_CARD )
1483 && cur_col == REM_COL )
1485 draw_cursor( CARD_WIDTH+2*MARGIN+1, MARGIN );
1488 rb->lcd_update();
1490 /* what to do when a key is pressed ... */
1491 button = rb->button_get( true );
1492 #if (CONFIG_KEYPAD == SANSA_E200_PAD)
1493 if (button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD))
1494 button = button & (~BUTTON_REPEAT);
1495 #endif
1496 switch( button )
1498 /* move cursor to the last card of the previous column
1499 * or to the previous final stack
1500 * or to the remains stack */
1501 case SOL_RIGHT:
1502 #ifdef SOL_RIGHT_PRE
1503 if( lastbutton != SOL_RIGHT_PRE )
1504 break;
1505 #endif
1506 if( cur_col >= COL_NUM )
1508 cur_col = 0;
1510 else if( cur_col == COL_NUM - 1 )
1512 cur_col = REM_COL;
1514 else
1516 cur_col = (cur_col+1)%(REM_COL+1);
1518 if(cur_col == REM_COL)
1520 cur_card = cur_rem;
1521 break;
1523 cur_card = find_last_card( cur_col );
1524 break;
1526 /* move cursor to the last card of the next column
1527 * or to the next final stack
1528 * or to the remains stack */
1529 case SOL_LEFT:
1530 #ifdef SOL_LEFT_PRE
1531 if( lastbutton != SOL_LEFT_PRE )
1532 break;
1533 #endif
1534 if( cur_col == 0 )
1536 cur_col = REM_COL;
1538 else if( cur_col >= COL_NUM )
1540 cur_col = COL_NUM - 1;
1542 else
1544 cur_col = (cur_col + REM_COL)%(REM_COL+1);
1546 if( cur_col == REM_COL )
1548 cur_card = cur_rem;
1549 break;
1551 cur_card = find_last_card( cur_col );
1552 break;
1554 /* move cursor to card that's bellow */
1555 case SOL_DOWN:
1556 #ifdef SOL_DOWN_PRE
1557 if( lastbutton != SOL_DOWN_PRE )
1558 break;
1559 #else
1560 case SOL_DOWN|BUTTON_REPEAT:
1561 #endif
1562 if( cur_col >= COL_NUM )
1564 cur_col = (cur_col - COL_NUM + 1)%(SUITS + 1) + COL_NUM;
1565 if( cur_col == REM_COL )
1567 cur_card = cur_rem;
1569 else
1571 cur_card = find_last_card( cur_col );
1573 break;
1575 if( cur_card == NOT_A_CARD ) break;
1576 if( deck[cur_card].next != NOT_A_CARD )
1578 cur_card = deck[cur_card].next;
1580 else
1582 cur_card = cols[cur_col];
1583 while( !deck[ cur_card].known
1584 && deck[cur_card].next != NOT_A_CARD )
1586 cur_card = deck[cur_card].next;
1589 break;
1591 /* move cursor to card that's above */
1592 case SOL_UP:
1593 #ifdef SOL_UP_PRE
1594 if( lastbutton != SOL_UP_PRE )
1595 break;
1596 #else
1597 case SOL_UP|BUTTON_REPEAT:
1598 #endif
1599 if( cur_col >= COL_NUM )
1601 cur_col = (cur_col - COL_NUM + SUITS)%(SUITS + 1) + COL_NUM;
1602 if( cur_col == REM_COL )
1604 cur_card = cur_rem;
1606 else
1608 cur_card = find_last_card( cur_col );
1610 break;
1612 if( cur_card == NOT_A_CARD ) break;
1613 do {
1614 cur_card = find_prev_card( cur_card );
1615 if( cur_card == NOT_A_CARD )
1617 cur_card = find_last_card( cur_col );
1619 } while( deck[cur_card].next != NOT_A_CARD
1620 && !deck[cur_card].known );
1621 break;
1623 /* Try to put card under cursor on one of the stacks */
1624 case SOL_CUR2STACK:
1625 #ifdef SOL_CUR2STACK_PRE
1626 if( lastbutton != SOL_CUR2STACK_PRE )
1627 break;
1628 #endif
1629 move_card( deck[cur_card].suit + STACKS_COL, cur_card );
1630 break;
1632 /* Move cards arround, Uncover cards, ... */
1633 case SOL_MOVE:
1634 #ifdef SOL_MOVE_PRE
1635 if( lastbutton != SOL_MOVE_PRE )
1636 break;
1637 #endif
1639 if( sel_card == NOT_A_CARD )
1641 if( cur_card != NOT_A_CARD )
1643 if( deck[cur_card].next == NOT_A_CARD
1644 && !deck[cur_card].known )
1646 /* reveal a hidden card */
1647 deck[cur_card].known = true;
1649 else if( cur_col == REM_COL && cur_rem == NOT_A_CARD )
1651 break;
1653 else
1655 /* select a card */
1656 sel_card = cur_card;
1660 else if( sel_card == cur_card )
1662 /* unselect card or try putting card on
1663 * one of the 4 stacks */
1664 if( move_card( deck[sel_card].suit + COL_NUM, sel_card )
1665 == MOVE_OK && cur_col == REM_COL )
1667 cur_card = cur_rem;
1669 sel_card = NOT_A_CARD;
1671 else
1673 /* try moving cards */
1674 /* The flexible move must not be used from the remains stack. */
1675 if (find_card_col(sel_card) == REM_COL)
1677 if (move_card( cur_col, sel_card ) == MOVE_NOT_OK)
1678 sel_card = NOT_A_CARD;
1680 else
1682 do {
1683 if (move_card( cur_col, sel_card) == MOVE_OK)
1684 break;
1685 sel_card = find_prev_card(sel_card);
1686 } while (sel_card != NOT_A_CARD);
1689 break;
1691 /* If the card on the top of the remains can be put where
1692 * the cursor is, go ahead */
1693 case SOL_REM2CUR:
1694 #ifdef SOL_REM2CUR_PRE
1695 if( lastbutton != SOL_REM2CUR_PRE )
1696 break;
1697 #endif
1698 move_card( cur_col, cur_rem );
1699 break;
1701 /* If the card on top of the remains can be put on one
1702 * of the stacks, do so */
1703 case SOL_REM2STACK:
1704 #ifdef SOL_REM2STACK_PRE
1705 if( lastbutton != SOL_REM2STACK_PRE )
1706 break;
1707 #endif
1708 move_card( deck[cur_rem].suit + COL_NUM, cur_rem );
1709 break;
1711 #ifdef SOL_REM
1712 case SOL_REM:
1713 if( sel_card != NOT_A_CARD )
1715 /* unselect selected card */
1716 sel_card = NOT_A_CARD;
1717 break;
1719 if( rem != NOT_A_CARD && cur_rem != NOT_A_CARD )
1721 sel_card = cur_rem;
1722 break;
1724 break;
1725 #endif
1727 /* unselect selected card or ...
1728 * draw new cards from the remains of the deck */
1729 case SOL_DRAW:
1730 #ifdef SOL_DRAW_PRE
1731 if( lastbutton != SOL_DRAW_PRE )
1732 break;
1733 #endif
1734 if( sel_card != NOT_A_CARD )
1736 /* unselect selected card */
1737 sel_card = NOT_A_CARD;
1738 break;
1740 if( rem != NOT_A_CARD )
1742 int cur_rem_old = cur_rem;
1743 count_rem = -1;
1744 /* draw new cards form the remains of the deck */
1745 if( cur_rem == NOT_A_CARD )
1747 /*if the cursor card is null*/
1748 cur_rem = rem;
1749 i = cards_per_draw - 1;
1750 count_rem++;
1752 else
1754 i = cards_per_draw;
1757 while( i > 0 && deck[cur_rem].next != NOT_A_CARD )
1759 cur_rem = deck[cur_rem].next;
1760 i--;
1761 count_rem++;
1763 /* test if any cards are really left on
1764 * the remains' stack */
1765 if( i == cards_per_draw )
1767 cur_rem = NOT_A_CARD;
1768 count_rem = -1;
1770 /* if cursor was on remains' stack when new cards were
1771 * drawn, put cursor on top of remains' stack */
1772 if( cur_col == REM_COL && cur_card == cur_rem_old )
1774 cur_card = cur_rem;
1775 sel_card = NOT_A_CARD;
1778 break;
1780 /* Show the menu */
1781 #ifdef SOL_RC_QUIT
1782 case SOL_RC_QUIT:
1783 #endif
1784 case SOL_QUIT:
1785 switch( solitaire_menu(true) )
1787 case MENU_SAVE_AND_QUIT:
1788 return SOLITAIRE_SAVE_AND_QUIT;
1790 case MENU_QUIT:
1791 return SOLITAIRE_QUIT;
1793 case MENU_USB:
1794 return SOLITAIRE_USB;
1796 break;
1798 case SYS_POWEROFF:
1799 return SOLITAIRE_SAVE_AND_QUIT;
1801 default:
1802 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
1803 return SOLITAIRE_USB;
1804 break;
1807 if( button != BUTTON_NONE )
1808 lastbutton = button;
1810 /* fix incoherences concerning cur_col and cur_card */
1811 c = find_card_col( cur_card );
1812 if( c != NOT_A_COL && c != cur_col )
1813 cur_card = find_last_card( cur_col );
1815 if( cur_card == NOT_A_CARD
1816 && find_last_card( cur_col ) != NOT_A_CARD )
1817 cur_card = find_last_card( cur_col );
1822 * Plugin entry point
1825 enum plugin_status plugin_start( const struct plugin_api* api, const void* parameter )
1827 int result;
1829 /* plugin init */
1830 (void)parameter;
1831 rb = api;
1833 rb->splash( HZ, "Welcome to Solitaire!" );
1835 configfile_init(rb);
1836 configfile_load(CONFIG_FILENAME, config,
1837 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1838 rb->memcpy(&sol, &sol_disk, sizeof(sol)); /* copy to running config */
1840 if( load_game() == 0 )
1842 rb->splash( HZ, "Resuming saved game." );
1843 result = SOLITAIRE_QUIT;
1845 else
1846 result = SOLITAIRE_WIN;
1848 init_help();
1850 /* play the game :)
1851 * Keep playing if a game was won (that means display the menu after
1852 * winning instead of quiting) */
1853 while( ( result = solitaire( result ) ) == SOLITAIRE_WIN );
1855 if( result == SOLITAIRE_QUIT )
1856 delete_save_file();
1857 else /* result == SOLITAIRE_USB || result == SOLITAIRE_SAVE_AND_QUIT */
1858 save_game();
1860 if (rb->memcmp(&sol, &sol_disk, sizeof(sol))) /* save settings if changed */
1862 rb->memcpy(&sol_disk, &sol, sizeof(sol));
1863 configfile_save(CONFIG_FILENAME, config,
1864 sizeof(config) / sizeof(config[0]), CFGFILE_VERSION);
1867 /* Exit the plugin */
1868 return ( result == SOLITAIRE_USB ) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
1871 #endif