Fix yellow
[Rockbox.git] / apps / plugins / solitaire.c
blob94ef282698e76b520308b6ff3cb42addf0740147
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 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "playback_control.h"
22 #include "configfile.h"
23 #include "button.h"
24 #include "lcd.h"
25 #include "oldmenuapi.h"
27 #ifdef HAVE_LCD_BITMAP
29 PLUGIN_HEADER
31 static const struct plugin_api* rb;
32 #define min(a,b) (a<b?a:b)
34 /**
35 * Key definitions
38 #if CONFIG_KEYPAD == RECORDER_PAD
39 # define SOL_QUIT BUTTON_OFF
40 # define SOL_UP BUTTON_UP
41 # define SOL_DOWN BUTTON_DOWN
42 # define SOL_LEFT BUTTON_LEFT
43 # define SOL_RIGHT BUTTON_RIGHT
44 # define SOL_MOVE BUTTON_ON
45 # define SOL_DRAW BUTTON_F1
46 # define SOL_REM2CUR BUTTON_PLAY
47 # define SOL_CUR2STACK BUTTON_F2
48 # define SOL_REM2STACK BUTTON_F3
49 # define HK_MOVE "ON"
50 # define HK_DRAW "F1"
51 # define HK_REM2CUR "PLAY"
52 # define HK_CUR2STACK "F2"
53 # define HK_REM2STACK "F3"
55 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
56 # define SOL_QUIT BUTTON_OFF
57 # define SOL_UP BUTTON_UP
58 # define SOL_DOWN BUTTON_DOWN
59 # define SOL_LEFT BUTTON_LEFT
60 # define SOL_RIGHT BUTTON_RIGHT
61 # define SOL_MOVE BUTTON_ON
62 # define SOL_DRAW BUTTON_F1
63 # define SOL_REM2CUR BUTTON_SELECT
64 # define SOL_CUR2STACK BUTTON_F2
65 # define SOL_REM2STACK BUTTON_F3
66 # define HK_MOVE "ON"
67 # define HK_DRAW "F1"
68 # define HK_REM2CUR "SELECT"
69 # define HK_CUR2STACK "F2"
70 # define HK_REM2STACK "F3"
72 #elif CONFIG_KEYPAD == ONDIO_PAD
73 # define SOL_QUIT BUTTON_OFF
74 # define SOL_UP_PRE BUTTON_UP
75 # define SOL_UP (BUTTON_UP | BUTTON_REL)
76 # define SOL_DOWN_PRE BUTTON_DOWN
77 # define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
78 # define SOL_LEFT_PRE BUTTON_LEFT
79 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
80 # define SOL_RIGHT_PRE BUTTON_RIGHT
81 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
82 # define SOL_MOVE_PRE BUTTON_MENU
83 # define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
84 # define SOL_DRAW_PRE BUTTON_MENU
85 # define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
86 # define SOL_REM2CUR_PRE BUTTON_DOWN
87 # define SOL_REM2CUR (BUTTON_DOWN | BUTTON_REPEAT)
88 # define SOL_CUR2STACK_PRE BUTTON_UP
89 # define SOL_CUR2STACK (BUTTON_UP | BUTTON_REPEAT)
90 # define SOL_REM2STACK_PRE BUTTON_RIGHT
91 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
92 # define HK_MOVE "MODE"
93 # define HK_DRAW "MODE.."
94 # define HK_REM2CUR "DOWN.."
95 # define HK_CUR2STACK "UP.."
96 # define HK_REM2STACK "RIGHT.."
98 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
99 (CONFIG_KEYPAD == IRIVER_H300_PAD)
100 # define SOL_QUIT BUTTON_OFF
101 # define SOL_UP BUTTON_UP
102 # define SOL_DOWN BUTTON_DOWN
103 # define SOL_LEFT BUTTON_LEFT
104 # define SOL_RIGHT BUTTON_RIGHT
105 # define SOL_MOVE_PRE BUTTON_SELECT
106 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
107 # define SOL_DRAW BUTTON_MODE
108 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_ON)
109 # define SOL_CUR2STACK_PRE BUTTON_SELECT
110 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
111 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_ON)
112 # define SOL_REM BUTTON_REC
113 # define SOL_RC_QUIT BUTTON_RC_STOP
114 # define HK_MOVE "NAVI"
115 # define HK_DRAW "A-B"
116 # define HK_REM2CUR "PLAY+LEFT"
117 # define HK_CUR2STACK "NAVI.."
118 # define HK_REM2STACK "PLAY+RIGHT"
120 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
121 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
122 # define SOL_QUIT (BUTTON_SELECT | BUTTON_MENU)
123 # define SOL_UP BUTTON_SCROLL_BACK
124 # define SOL_DOWN BUTTON_SCROLL_FWD
125 # define SOL_LEFT_PRE BUTTON_LEFT
126 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
127 # define SOL_RIGHT_PRE BUTTON_RIGHT
128 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
129 # define SOL_MOVE_PRE BUTTON_SELECT
130 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
131 # define SOL_DRAW_PRE BUTTON_MENU
132 # define SOL_DRAW (BUTTON_MENU | BUTTON_REL)
133 # define SOL_REM2CUR BUTTON_PLAY
134 # define SOL_CUR2STACK_PRE BUTTON_MENU
135 # define SOL_CUR2STACK (BUTTON_MENU | BUTTON_REPEAT)
136 # define SOL_REM2STACK_PRE BUTTON_RIGHT
137 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
138 # define HK_UD "SCROLL U/D"
139 # define HK_MOVE "SELECT"
140 # define HK_DRAW "MENU"
141 # define HK_REM2CUR "PLAY"
142 # define HK_CUR2STACK "MENU.."
143 # define HK_REM2STACK "RIGHT.."
145 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
146 # define SOL_QUIT BUTTON_POWER
147 # define SOL_UP BUTTON_UP
148 # define SOL_DOWN BUTTON_DOWN
149 # define SOL_LEFT BUTTON_LEFT
150 # define SOL_RIGHT BUTTON_RIGHT
151 # define SOL_MOVE_PRE BUTTON_SELECT
152 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
153 # define SOL_DRAW_PRE BUTTON_PLAY
154 # define SOL_DRAW (BUTTON_PLAY | BUTTON_REL)
155 # define SOL_REM2CUR_PRE BUTTON_PLAY
156 # define SOL_REM2CUR (BUTTON_PLAY | BUTTON_REPEAT)
157 # define SOL_CUR2STACK_PRE BUTTON_SELECT
158 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
159 # define SOL_REM2STACK BUTTON_REC
160 # define HK_MOVE "SELECT"
161 # define HK_DRAW "PLAY"
162 # define HK_REM2CUR "PLAY.."
163 # define HK_CUR2STACK "SELECT.."
164 # define HK_REM2STACK "REC"
166 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
167 # define SOL_QUIT BUTTON_POWER
168 # define SOL_UP BUTTON_UP
169 # define SOL_DOWN BUTTON_DOWN
170 # define SOL_LEFT BUTTON_LEFT
171 # define SOL_RIGHT BUTTON_RIGHT
172 # define SOL_MOVE_PRE BUTTON_SELECT
173 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
174 # define SOL_DRAW BUTTON_MENU
175 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_A)
176 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
177 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_A)
178 # define HK_MOVE "SELECT"
179 # define HK_DRAW "MENU"
180 # define HK_REM2CUR "A+LEFT"
181 # define HK_CUR2STACK "SELECT.."
182 # define HK_REM2STACK "A+RIGHT"
184 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
185 # define SOL_QUIT BUTTON_POWER
186 # define SOL_UP BUTTON_UP
187 # define SOL_DOWN BUTTON_DOWN
188 # define SOL_LEFT BUTTON_SCROLL_BACK
189 # define SOL_RIGHT BUTTON_SCROLL_FWD
190 # define SOL_MOVE BUTTON_SELECT
191 # define SOL_DRAW BUTTON_REC
192 # define SOL_REM2CUR BUTTON_LEFT
193 # define SOL_CUR2STACK_PRE BUTTON_REC
194 # define SOL_CUR2STACK (BUTTON_REC | BUTTON_RIGHT)
195 # define SOL_REM2STACK BUTTON_RIGHT
196 # define HK_MOVE "SELECT"
197 # define HK_DRAW "REC"
198 # define HK_REM2CUR "LEFT"
199 # define HK_CUR2STACK "DOUBLE SELECT"
200 # define HK_REM2STACK "RIGHT"
202 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
203 # define SOL_QUIT BUTTON_POWER
204 # define SOL_UP BUTTON_UP
205 # define SOL_DOWN BUTTON_DOWN
206 # define SOL_LEFT BUTTON_LEFT
207 # define SOL_RIGHT BUTTON_RIGHT
208 # define SOL_MOVE_PRE BUTTON_SELECT
209 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
210 # define SOL_DRAW BUTTON_VOL_DOWN
211 # define SOL_REM2CUR BUTTON_REC
212 # define SOL_CUR2STACK_PRE BUTTON_SELECT
213 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
214 # define SOL_REM2STACK BUTTON_VOL_UP
215 # define HK_MOVE "SELECT"
216 # define HK_DRAW "REC"
217 # define HK_REM2CUR "LEFT"
218 # define HK_CUR2STACK "DOUBLE SELECT"
219 # define HK_REM2STACK "RIGHT"
221 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
222 # define SOL_QUIT BUTTON_POWER
223 # define SOL_UP BUTTON_SCROLL_UP
224 # define SOL_DOWN BUTTON_SCROLL_DOWN
225 # define SOL_LEFT_PRE BUTTON_LEFT
226 # define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
227 # define SOL_RIGHT_PRE BUTTON_RIGHT
228 # define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
229 # define SOL_MOVE BUTTON_PLAY
230 # define SOL_DRAW_PRE BUTTON_LEFT
231 # define SOL_DRAW (BUTTON_LEFT | BUTTON_REPEAT)
232 # define SOL_REM2CUR BUTTON_FF
233 # define SOL_CUR2STACK BUTTON_REW
234 # define SOL_REM2STACK_PRE BUTTON_RIGHT
235 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
236 # define HK_MOVE "PLAY"
237 # define HK_DRAW "LEFT.."
238 # define HK_REM2CUR "FF"
239 # define HK_CUR2STACK "REW"
240 # define HK_REM2STACK "RIGHT.."
242 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
243 # define SOL_QUIT BUTTON_BACK
244 # define SOL_UP BUTTON_UP
245 # define SOL_DOWN BUTTON_DOWN
246 # define SOL_LEFT BUTTON_LEFT
247 # define SOL_RIGHT BUTTON_RIGHT
248 # define SOL_MOVE_PRE BUTTON_SELECT
249 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
250 # define SOL_DRAW BUTTON_MENU
251 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_SELECT)
252 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
253 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_SELECT)
254 # define HK_MOVE "SELECT"
255 # define HK_DRAW "MENU"
256 # define HK_REM2CUR "SELECT+LEFT"
257 # define HK_CUR2STACK "SELECT.."
258 # define HK_REM2STACK "SELECT+RIGHT"
260 #elif (CONFIG_KEYPAD == MROBE100_PAD)
261 # define SOL_QUIT BUTTON_POWER
262 # define SOL_UP BUTTON_UP
263 # define SOL_DOWN BUTTON_DOWN
264 # define SOL_LEFT BUTTON_LEFT
265 # define SOL_RIGHT BUTTON_RIGHT
266 # define SOL_MOVE_PRE BUTTON_SELECT
267 # define SOL_MOVE (BUTTON_SELECT | BUTTON_REL)
268 # define SOL_DRAW BUTTON_MENU
269 # define SOL_REM2CUR (BUTTON_LEFT | BUTTON_DISPLAY)
270 # define SOL_CUR2STACK (BUTTON_SELECT | BUTTON_REPEAT)
271 # define SOL_REM2STACK (BUTTON_RIGHT | BUTTON_DISPLAY)
272 # define HK_MOVE "SELECT"
273 # define HK_DRAW "MENU"
274 # define HK_REM2CUR "DISPLAY+LEFT"
275 # define HK_CUR2STACK "SELECT.."
276 # define HK_REM2STACK "DISPLAY+RIGHT"
278 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
279 # define SOL_QUIT BUTTON_RC_REC
280 # define SOL_UP BUTTON_RC_VOL_UP
281 # define SOL_DOWN BUTTON_RC_VOL_DOWN
282 # define SOL_LEFT BUTTON_RC_REW
283 # define SOL_RIGHT BUTTON_RC_FF
284 # define SOL_MOVE BUTTON_RC_PLAY
285 # define SOL_DRAW_PRE BUTTON_RC_MENU
286 # define SOL_DRAW (BUTTON_RC_MENU | BUTTON_REL)
287 # define SOL_REM2CUR_PRE BUTTON_RC_MENU
288 # define SOL_REM2CUR (BUTTON_RC_MENU | BUTTON_REPEAT)
289 # define SOL_CUR2STACK_PRE BUTTON_RC_MODE
290 # define SOL_CUR2STACK (BUTTON_RC_MODE | BUTTON_REL)
291 # define SOL_REM2STACK_PRE BUTTON_RC_MODE
292 # define SOL_REM2STACK (BUTTON_RC_MODE | BUTTON_REPEAT)
293 # define HK_LR "REW/FF"
294 # define HK_UD "VOL UP/DOWN"
295 # define HK_MOVE "PLAY"
296 # define HK_DRAW "MENU"
297 # define HK_REM2CUR "MENU.."
298 # define HK_CUR2STACK "MODE"
299 # define HK_REM2STACK "MODE.."
301 #elif (CONFIG_KEYPAD == COWOND2_PAD)
302 # define SOL_QUIT BUTTON_POWER
304 #else
305 #error No keymap defined!
306 #endif
308 #ifdef HAVE_TOUCHPAD
309 //#ifndef SOL_QUIT
310 //# define SOL_QUIT BUTTON_TOPLEFT
311 //endif
312 #ifndef SOL_UP
313 # define SOL_UP BUTTON_TOPMIDDLE
314 #endif
315 #ifndef SOL_DOWN
316 # define SOL_DOWN BUTTON_BOTTOMMIDDLE
317 #endif
318 #ifndef SOL_LEFT
319 # define SOL_LEFT BUTTON_MIDLEFT
320 #endif
321 #ifndef SOL_RIGHT
322 # define SOL_RIGHT BUTTON_MIDRIGHT
323 #endif
324 #ifndef SOL_MOVE
325 # define SOL_MOVE BUTTON_CENTER
326 # define HK_MOVE "CENTRE"
327 #endif
328 #ifndef SOL_DRAW
329 # define SOL_DRAW BUTTON_TOPLEFT
330 # define HK_DRAW "TOPLEFT"
331 #endif
332 #ifndef SOL_REM2CUR
333 # define SOL_REM2CUR BUTTON_TOPRIGHT
334 # define HK_REM2CUR "TOPRIGHT"
335 #endif
336 #ifndef SOL_CUR2STACK
337 # define SOL_CUR2STACK BUTTON_BOTTOMLEFT
338 # define HK_CUR2STACK "BOTTOMLEFT"
339 #endif
340 #ifndef SOL_REM2STACK
341 # define SOL_REM2STACK BUTTON_BOTTOMRIGHT
342 # define HK_REM2STACK "BOTTOMRIGHT"
343 #endif
345 #endif
347 #ifndef HK_LR
348 # define HK_LR "LEFT/RIGHT"
349 #endif
350 #ifndef HK_UD
351 # define HK_UD "UP/DOWN"
352 #endif
355 * Help strings
358 static int helplines;
359 static int displaylines;
361 static char helptext[] =
362 /* Use single spaces only! Close each line with one \0. */
363 "-- Navigation --\0"
364 HK_LR ": Move the cursor to the previous/ next column.\0"
365 HK_UD ": Move the cursor up/ down in the column.\0"
366 HK_MOVE ": Select cards, move cards, reveal hidden cards...\0"
367 HK_DRAW ": Deselect a card if it was selected. Else draw new card(s) from the remains stack.\0"
368 "-- Shortcuts --\0"
369 HK_REM2CUR ": Put the card on top of the remains stack on top of the cursor.\0"
370 HK_CUR2STACK ": Put the card under the cursor on one of the 4 final stacks.\0"
371 HK_REM2STACK ": Put the card on top of the remains stack on one of the 4 final stacks.\0"
375 * Misc constants, graphics and other defines
378 #include "card_back.h"
379 #include "card_deck.h"
380 #include "solitaire_suitsi.h"
382 #define CARD_GFX_WIDTH BMPWIDTH_card_back
383 #define CARD_GFX_HEIGHT BMPHEIGHT_card_back
384 #define CARD_WIDTH (BMPWIDTH_card_back+2)
385 #define CARD_HEIGHT (BMPHEIGHT_card_back+2)
387 #if LCD_WIDTH >= 320
388 # define MARGIN 4
389 # define LARGE_CARD
390 # define SYMBOL_HEIGHT 12
391 #elif LCD_WIDTH >= 220
392 # define MARGIN 3
393 # define LARGE_CARD
394 # define SYMBOL_HEIGHT 12
395 #elif LCD_WIDTH >= 160
396 # define MARGIN 2
397 # define SYMBOL_HEIGHT 11
398 #elif LCD_WIDTH >= 128
399 # define MARGIN 1
400 # define SYMBOL_HEIGHT 10
401 #else
402 # define MARGIN 0
403 # define SYMBOL_HEIGHT 8
404 #endif
406 #define CARD_START (CARD_HEIGHT+2*MARGIN+1)
408 /* background color */
409 #ifdef HAVE_LCD_COLOR
410 # define BACKGROUND_COLOR LCD_RGBPACK(0,157,0)
411 # define FRAME_COLOR LCD_RGBPACK(23,119,218)
412 #elif LCD_DEPTH > 1
413 # define BACKGROUND_COLOR LCD_WHITE
414 # define FRAME_COLOR LCD_BLACK
415 #endif
418 #define CONFIG_FILENAME "sol.cfg"
420 #define NOT_A_CARD -1
422 /* number of cards per suit */
423 #define CARDS_PER_SUIT 13
425 /* number of suits */
426 #define SUITS 4
428 #define NUM_CARDS ( CARDS_PER_SUIT * SUITS )
430 /* number of columns */
431 #define COL_NUM 7
433 /* pseudo column numbers to be used for cursor coordinates */
434 /* columns COL_NUM to COL_NUM + SUITS - 1 correspond to the final stacks */
435 #define STACKS_COL COL_NUM
436 /* column COL_NUM + SUITS corresponds to the remains' stack */
437 #define REM_COL (STACKS_COL + SUITS)
439 #define NOT_A_COL -1
441 typedef struct
443 signed char suit;
444 signed char num;
445 bool known : 1;
446 bool used : 1; /* this is what is used when dealing cards */
447 signed char next;
448 } card_t;
452 * LCD card drawing routines
455 static void draw_cursor( int x, int y )
457 rb->lcd_set_drawmode( DRMODE_COMPLEMENT );
458 rb->lcd_fillrect( x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
459 #ifdef LARGE_CARD
460 rb->lcd_drawpixel( x+1, y+1 );
461 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
462 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
463 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
464 #endif
465 rb->lcd_set_drawmode( DRMODE_SOLID );
468 /* Draw a card's border, select it if it's selected and draw the cursor
469 * if the cursor is currently over the card */
470 static void draw_card_ext( int x, int y, bool selected, bool cursor )
472 #if LCD_DEPTH > 1
473 int oldfg = rb->lcd_get_foreground();
475 rb->lcd_set_foreground( LCD_BLACK );
476 #endif
477 #ifdef LARGE_CARD
478 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y );
479 rb->lcd_hline( x+2, x+CARD_WIDTH-3, y+CARD_HEIGHT-1 );
480 rb->lcd_vline( x, y+2, y+CARD_HEIGHT-3 );
481 rb->lcd_vline( x+CARD_WIDTH-1, y+2, y+CARD_HEIGHT-3 );
482 rb->lcd_drawpixel( x+1, y+1 );
483 rb->lcd_drawpixel( x+1, y+CARD_HEIGHT-2 );
484 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+1 );
485 rb->lcd_drawpixel( x+CARD_WIDTH-2, y+CARD_HEIGHT-2 );
486 #else
487 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y );
488 rb->lcd_hline( x+1, x+CARD_WIDTH-2, y+CARD_HEIGHT-1 );
489 rb->lcd_vline( x, y+1, y+CARD_HEIGHT-2 );
490 rb->lcd_vline( x+CARD_WIDTH-1, y+1, y+CARD_HEIGHT-2 );
491 #endif
493 if( selected )
495 #if LCD_DEPTH > 1
496 rb->lcd_set_foreground( FRAME_COLOR );
497 #endif
498 rb->lcd_drawrect( x+1, y+1, CARD_WIDTH-2, CARD_HEIGHT-2 );
499 #ifdef LARGE_CARD
500 rb->lcd_drawrect( x+2, y+2, CARD_WIDTH-4, CARD_HEIGHT-4 );
501 #endif
503 #if LCD_DEPTH > 1
504 rb->lcd_set_foreground( oldfg );
505 #endif
507 if( cursor )
509 draw_cursor( x, y );
513 /* Draw a card's inner graphics */
514 static void draw_card( card_t *card, int x, int y,
515 bool selected, bool cursor )
517 if( card->known )
519 rb->lcd_bitmap_part( card_deck, CARD_GFX_WIDTH * card->num,
520 CARD_GFX_HEIGHT * card->suit, BMPWIDTH_card_deck,
521 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
523 else
525 rb->lcd_bitmap( card_back, x+1, y+1,
526 CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
528 draw_card_ext( x, y, selected, cursor );
531 /* Draw an empty stack */
532 static void draw_empty_stack( int s, int x, int y, bool cursor )
534 rb->lcd_bitmap_part( solitaire_suitsi, 0,
535 CARD_GFX_HEIGHT * s, BMPWIDTH_solitaire_suitsi,
536 x+1, y+1, CARD_GFX_WIDTH, CARD_GFX_HEIGHT );
538 draw_card_ext( x, y, false, cursor );
542 * Help
545 static void init_help(void)
547 int lines = 0;
548 int w_space, w, h;
549 int para_len;
550 char *para = helptext;
552 rb->lcd_getstringsize(" ", &w_space, &h);
553 displaylines = LCD_HEIGHT / h;
554 para_len = rb->strlen(para);
556 while (para_len)
558 bool first = true;
559 int x = 0;
560 char *next, *store;
562 next = rb->strtok_r(para, " ", &store);
563 while (next)
565 rb->lcd_getstringsize(next, &w, NULL);
566 if (!first)
568 if (x + w > LCD_WIDTH)
570 lines++;
571 x = 0;
573 else
574 next[-1] = ' '; /* re-concatenate string */
576 else
577 first = false;
579 x += w + w_space;
580 next = rb->strtok_r(NULL, " ", &store);
583 lines++;
584 para += para_len + 1;
585 para_len = rb->strlen(para);
587 helplines = lines;
590 enum help { HELP_QUIT, HELP_USB };
592 /* help for the not so intuitive interface */
593 enum help solitaire_help( void )
595 int start = 0;
596 int button;
597 int lastbutton = BUTTON_NONE;
598 bool fixed = (displaylines >= helplines);
600 while( true )
602 char *text = helptext;
603 int line = fixed ? (displaylines - helplines) / 2 : 0;
604 int i;
606 rb->lcd_clear_display();
608 for (i = 0; i < start + displaylines; i++)
610 if (i >= start)
611 rb->lcd_puts(0, line++, text);
612 text += rb->strlen(text) + 1;
614 rb->lcd_update();
616 button = rb->button_get( true );
617 switch( button )
619 case SOL_UP:
620 #ifdef SOL_UP_PRE
621 if( lastbutton != SOL_UP_PRE )
622 break;
623 #else
624 case SOL_UP|BUTTON_REPEAT:
625 #endif
626 if (!fixed && start > 0)
627 start--;
628 break;
630 case SOL_DOWN:
631 #ifdef SOL_DOWN_PRE
632 if( lastbutton != SOL_DOWN_PRE )
633 break;
634 #else
635 case SOL_DOWN|BUTTON_REPEAT:
636 #endif
637 if (!fixed && start < helplines - displaylines)
638 start++;
639 break;
641 #ifdef SOL_RC_QUIT
642 case SOL_RC_QUIT:
643 #endif
644 case SOL_QUIT:
645 return HELP_QUIT;
647 default:
648 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
649 return HELP_USB;
650 break;
652 if( button != BUTTON_NONE )
653 lastbutton = button;
658 * Custom menu / options
661 #define CFGFILE_VERSION 0
663 struct sol_config {
664 int draw_type;
667 struct sol_config sol_disk = {0};
668 struct sol_config sol;
670 static struct configdata config[] = {
671 { TYPE_INT, 0, 1, &sol_disk.draw_type, "draw_type", NULL, NULL },
674 char draw_option_string[32];
676 static void create_draw_option_string(void)
678 if (sol.draw_type == 0)
679 rb->strcpy(draw_option_string, "Draw Three Cards");
680 else
681 rb->strcpy(draw_option_string, "Draw One Card");
684 void solitaire_init(void);
686 /* menu return codes */
687 enum { MENU_RESUME, MENU_SAVE_AND_QUIT, MENU_QUIT, MENU_USB };
689 int solitaire_menu(bool in_game)
691 int m;
692 int result = -1;
693 int i = 0;
695 struct menu_item items[6];
697 if( in_game )
699 items[i++].desc = "Resume Game";
700 items[i++].desc = "Restart Game";
702 else
704 items[i++].desc = "Start Game";
705 items[i++].desc = draw_option_string;
707 items[i++].desc = "Help";
708 items[i++].desc = "Audio Playback";
709 if( in_game )
711 items[i++].desc = "Save and Quit";
713 items[i++].desc = "Quit";
715 create_draw_option_string();
716 m = menu_init(rb, items, i, NULL, NULL, NULL, NULL);
717 while (result < 0)
719 switch (menu_show(m))
721 case MENU_SELECTED_EXIT:
722 result = MENU_RESUME;
723 break;
725 case MENU_ATTACHED_USB:
726 result = MENU_USB;
727 break;
729 case 0:
730 result = MENU_RESUME;
731 break;
733 case 1:
734 if (in_game)
736 solitaire_init();
737 result = MENU_RESUME;
739 else
741 sol.draw_type = (sol.draw_type + 1) % 2;
742 create_draw_option_string();
744 break;
746 case 2:
747 rb->lcd_setmargins(0, 0);
748 if (solitaire_help() == HELP_USB)
749 result = MENU_USB;
750 break;
752 case 3:
753 playback_control(rb, NULL);
754 break;
756 case 4:
757 if( in_game )
758 result = MENU_SAVE_AND_QUIT;
759 else
760 result = MENU_QUIT;
761 break;
763 case 5:
764 result = MENU_QUIT;
765 break;
768 menu_exit(m);
769 rb->lcd_setmargins(0, 0);
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