diff options
author | Teruaki Kawashima <teru@rockbox.org> | 2009-12-20 14:00:38 +0000 |
---|---|---|
committer | Teruaki Kawashima <teru@rockbox.org> | 2009-12-20 14:00:38 +0000 |
commit | ed1acf037c3d1e6ec9e1cf098f6242cb5a655550 (patch) | |
tree | 140e1121833b7dbb613fce67a17a5da2204c41d3 /apps/plugins/codebuster.c | |
parent | 0cbf210d76dbc8f01a2da944e76f05fdb050003b (diff) | |
download | rockbox-ed1acf037c3d1e6ec9e1cf098f6242cb5a655550.tar.gz rockbox-ed1acf037c3d1e6ec9e1cf098f6242cb5a655550.zip |
codebuster: save settings only if they are changed.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24082 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/codebuster.c')
-rw-r--r-- | apps/plugins/codebuster.c | 119 |
1 files changed, 62 insertions, 57 deletions
diff --git a/apps/plugins/codebuster.c b/apps/plugins/codebuster.c index 2abf5ec81b..bfcc25f9b5 100644 --- a/apps/plugins/codebuster.c +++ b/apps/plugins/codebuster.c | |||
@@ -64,31 +64,38 @@ struct mm_line { | |||
64 | }; | 64 | }; |
65 | 65 | ||
66 | const int colors[MAX_COLORS_COUNT] = { | 66 | const int colors[MAX_COLORS_COUNT] = { |
67 | LCD_RGBPACK(252, 233, 79), | 67 | LCD_RGBPACK(252, 233, 79), |
68 | LCD_RGBPACK(206, 92, 0), | 68 | LCD_RGBPACK(206, 92, 0), |
69 | LCD_RGBPACK(143, 89, 2), | 69 | LCD_RGBPACK(143, 89, 2), |
70 | LCD_RGBPACK( 78, 154, 6), | 70 | LCD_RGBPACK( 78, 154, 6), |
71 | /* LCD_RGBPACK( 32, 74, 135), */ | 71 | /* LCD_RGBPACK( 32, 74, 135), */ |
72 | LCD_RGBPACK( 52, 101, 164), | 72 | LCD_RGBPACK( 52, 101, 164), |
73 | /* LCD_RGBPACK(114, 159, 207), */ | 73 | /* LCD_RGBPACK(114, 159, 207), */ |
74 | LCD_RGBPACK(117, 80, 123), | 74 | LCD_RGBPACK(117, 80, 123), |
75 | /* LCD_RGBPACK(173, 127, 168), */ | 75 | /* LCD_RGBPACK(173, 127, 168), */ |
76 | LCD_RGBPACK(164, 0, 0), | 76 | LCD_RGBPACK(164, 0, 0), |
77 | LCD_RGBPACK(238, 238, 236), | 77 | LCD_RGBPACK(238, 238, 236), |
78 | }; | 78 | }; |
79 | 79 | ||
80 | /* Flags */ | 80 | /* Flags */ |
81 | static bool quit, leave, usb; | 81 | static bool quit, leave, usb; |
82 | static bool found, game_ended; | 82 | static bool found, game_ended; |
83 | 83 | ||
84 | /* Settings */ | 84 | /* Settings */ |
85 | struct settings { | ||
86 | int pieces; | ||
87 | int colors; | ||
88 | int guesses; | ||
89 | bool labeling; | ||
90 | bool framing; | ||
91 | }; | ||
92 | static struct settings settings = { | ||
93 | 5, 7, 10, false, false, | ||
94 | }; | ||
95 | static struct settings old_settings; | ||
85 | static int pieces_count; | 96 | static int pieces_count; |
86 | static int colors_count; | 97 | static int colors_count; |
87 | static int guesses_count; | 98 | static int guesses_count; |
88 | static int pieces_tmp = 5; | ||
89 | static int colors_tmp = 7; | ||
90 | static int guesses_tmp = 10; | ||
91 | static bool labeling = false, framing = false; | ||
92 | 99 | ||
93 | /* Display */ | 100 | /* Display */ |
94 | #define ALUMINIUM LCD_RGBPACK(136, 138, 133) | 101 | #define ALUMINIUM LCD_RGBPACK(136, 138, 133) |
@@ -102,13 +109,12 @@ static bool labeling = false, framing = false; | |||
102 | #define CONFIG_FILE_NAME "codebuster.cfg" | 109 | #define CONFIG_FILE_NAME "codebuster.cfg" |
103 | 110 | ||
104 | static struct configdata config[] = { | 111 | static struct configdata config[] = { |
105 | {TYPE_INT, 0, MAX_PIECES_COUNT, { .int_p = &pieces_tmp }, "pieces", NULL}, | 112 | {TYPE_INT, 0, MAX_PIECES_COUNT, { .int_p = &settings.pieces }, "pieces", NULL}, |
106 | {TYPE_INT, 0, MAX_COLORS_COUNT, { .int_p = &colors_tmp }, "colors", NULL}, | 113 | {TYPE_INT, 0, MAX_COLORS_COUNT, { .int_p = &settings.colors }, "colors", NULL}, |
107 | {TYPE_INT, 0, MAX_GUESSES_COUNT, { .int_p = &guesses_tmp }, "guesses", NULL}, | 114 | {TYPE_INT, 0, MAX_GUESSES_COUNT, { .int_p = &settings.guesses }, "guesses", NULL}, |
108 | {TYPE_BOOL, 0, 1, { .bool_p = &labeling }, "labeling", NULL}, | 115 | {TYPE_BOOL, 0, 1, { .bool_p = &settings.labeling }, "labeling", NULL}, |
109 | {TYPE_BOOL, 0, 1, { .bool_p = &framing }, "framing", NULL}, | 116 | {TYPE_BOOL, 0, 1, { .bool_p = &settings.framing }, "framing", NULL}, |
110 | }; | 117 | }; |
111 | static bool settings_changed = false; | ||
112 | 118 | ||
113 | static int line_h; | 119 | static int line_h; |
114 | static int piece_w, tick_w; | 120 | static int piece_w, tick_w; |
@@ -158,22 +164,22 @@ static void draw_piece(int x, int y, int w, int h, int color_id, bool emph) { | |||
158 | else | 164 | else |
159 | fill_color_rect(x, y, w, h, color); | 165 | fill_color_rect(x, y, w, h, color); |
160 | 166 | ||
161 | if (!emph && framing) | 167 | if (!emph && settings.framing) |
162 | rb->lcd_drawrect(x, y, w, h); | 168 | rb->lcd_drawrect(x, y, w, h); |
163 | 169 | ||
164 | if (labeling && color_id >= 0) { | 170 | if (settings.labeling && color_id >= 0) { |
165 | char text[2]; | 171 | char text[2]; |
166 | rb->snprintf(text, 2, "%d", color_id); | 172 | rb->snprintf(text, 2, "%d", color_id); |
167 | 173 | ||
168 | int fw, fh; rb->font_getstringsize(text, &fw, &fh, FONT_SYSFIXED); | 174 | int fw, fh; rb->font_getstringsize(text, &fw, &fh, FONT_SYSFIXED); |
169 | rb->lcd_putsxy(x + get_margin(fw, w), y + get_margin(fh, h), text); | 175 | rb->lcd_putsxy(x + get_margin(fw, w), y + get_margin(fh, h), text); |
170 | } | 176 | } |
171 | } | 177 | } |
172 | 178 | ||
173 | /* Compute the score for a given guess (expressed in ticks) */ | 179 | /* Compute the score for a given guess (expressed in ticks) */ |
174 | static void validate_guess(struct mm_line* guess) { | 180 | static void validate_guess(struct mm_line* guess) { |
175 | bool solution_match[pieces_count]; | 181 | bool solution_match[MAX_PIECES_COUNT]; |
176 | bool guess_match[pieces_count]; | 182 | bool guess_match[MAX_PIECES_COUNT]; |
177 | 183 | ||
178 | guess->score.misplaced = 0; | 184 | guess->score.misplaced = 0; |
179 | guess->score.correct = 0; | 185 | guess->score.correct = 0; |
@@ -289,10 +295,10 @@ static void init_vars(void) { | |||
289 | } | 295 | } |
290 | 296 | ||
291 | static void init_board(void) { | 297 | static void init_board(void) { |
292 | 298 | ||
293 | pieces_count = pieces_tmp; | 299 | pieces_count = settings.pieces; |
294 | colors_count = colors_tmp; | 300 | colors_count = settings.colors; |
295 | guesses_count = guesses_tmp; | 301 | guesses_count = settings.guesses; |
296 | 302 | ||
297 | line_h = GAME_H / (2 * (guesses_count + 2) - 1); | 303 | line_h = GAME_H / (2 * (guesses_count + 2) - 1); |
298 | 304 | ||
@@ -316,31 +322,31 @@ static void randomize_solution(void) { | |||
316 | static void settings_menu(void) { | 322 | static void settings_menu(void) { |
317 | MENUITEM_STRINGLIST(settings_menu, "Settings", NULL, | 323 | MENUITEM_STRINGLIST(settings_menu, "Settings", NULL, |
318 | "Number of colours", "Number of pegs", | 324 | "Number of colours", "Number of pegs", |
319 | "Number of guesses", "Labels", "Frames"); | 325 | "Number of guesses", |
320 | 326 | "Display labels", "Display frames"); | |
321 | int cur_item =0; | 327 | int cur_item = 0; |
322 | |||
323 | bool menu_quit = false; | 328 | bool menu_quit = false; |
329 | |||
324 | while(!menu_quit) { | 330 | while(!menu_quit) { |
325 | 331 | ||
326 | switch(rb->do_menu(&settings_menu, &cur_item, NULL, false)) { | 332 | switch(rb->do_menu(&settings_menu, &cur_item, NULL, false)) { |
327 | case 0: | 333 | case 0: |
328 | rb->set_int("Number of colours", "", UNIT_INT, &colors_tmp, | 334 | rb->set_int("Number of colours", "", UNIT_INT, &settings.colors, |
329 | NULL, -1, MAX_COLORS_COUNT, 1, NULL); | 335 | NULL, -1, MAX_COLORS_COUNT, 1, NULL); |
330 | break; | 336 | break; |
331 | case 1: | 337 | case 1: |
332 | rb->set_int("Number of pegs", "", UNIT_INT, &pieces_tmp, | 338 | rb->set_int("Number of pegs", "", UNIT_INT, &settings.pieces, |
333 | NULL, -1, MAX_PIECES_COUNT, 1, NULL); | 339 | NULL, -1, MAX_PIECES_COUNT, 1, NULL); |
334 | break; | 340 | break; |
335 | case 2: | 341 | case 2: |
336 | rb->set_int("Number of guesses", "", UNIT_INT, &guesses_tmp, | 342 | rb->set_int("Number of guesses", "", UNIT_INT, &settings.guesses, |
337 | NULL, -1, MAX_GUESSES_COUNT, 1, NULL); | 343 | NULL, -1, MAX_GUESSES_COUNT, 1, NULL); |
338 | break; | 344 | break; |
339 | case 3: | 345 | case 3: |
340 | rb->set_bool("Display labels", &labeling); | 346 | rb->set_bool("Display labels", &settings.labeling); |
341 | break; | 347 | break; |
342 | case 4: | 348 | case 4: |
343 | rb->set_bool("Display frames", &framing); | 349 | rb->set_bool("Display frames", &settings.framing); |
344 | break; | 350 | break; |
345 | case GO_TO_PREVIOUS: | 351 | case GO_TO_PREVIOUS: |
346 | menu_quit = true; | 352 | menu_quit = true; |
@@ -364,10 +370,9 @@ static void main_menu(void) { | |||
364 | MENUITEM_STRINGLIST(main_menu, "Codebuster Menu", menu_cb, | 370 | MENUITEM_STRINGLIST(main_menu, "Codebuster Menu", menu_cb, |
365 | "Resume Game", "Start New Game", "Settings", | 371 | "Resume Game", "Start New Game", "Settings", |
366 | "Playback Control", "Quit"); | 372 | "Playback Control", "Quit"); |
367 | 373 | int cur_item = 0; | |
368 | int cur_item =0; | ||
369 | |||
370 | bool menu_quit = false; | 374 | bool menu_quit = false; |
375 | |||
371 | while(!menu_quit) { | 376 | while(!menu_quit) { |
372 | 377 | ||
373 | switch(rb->do_menu(&main_menu, &cur_item, NULL, false)) { | 378 | switch(rb->do_menu(&main_menu, &cur_item, NULL, false)) { |
@@ -381,7 +386,6 @@ static void main_menu(void) { | |||
381 | break; | 386 | break; |
382 | case 2: | 387 | case 2: |
383 | settings_menu(); | 388 | settings_menu(); |
384 | settings_changed = true; | ||
385 | break; | 389 | break; |
386 | case 3: | 390 | case 3: |
387 | playback_control(NULL); | 391 | playback_control(NULL); |
@@ -406,15 +410,16 @@ enum plugin_status plugin_start(const void* parameter) { | |||
406 | rb->lcd_set_backdrop(NULL); | 410 | rb->lcd_set_backdrop(NULL); |
407 | rb->lcd_set_foreground(LCD_WHITE); | 411 | rb->lcd_set_foreground(LCD_WHITE); |
408 | rb->lcd_set_background(LCD_BLACK); | 412 | rb->lcd_set_background(LCD_BLACK); |
409 | 413 | ||
410 | configfile_load(CONFIG_FILE_NAME,config,5,0); | 414 | configfile_load(CONFIG_FILE_NAME, config, ARRAYLEN(config), 0); |
415 | rb->memcpy(&old_settings, &settings, sizeof(settings)); | ||
411 | 416 | ||
412 | main_menu(); | 417 | main_menu(); |
413 | while (!quit) { | 418 | while (!quit) { |
414 | init_board(); | 419 | init_board(); |
415 | randomize_solution(); | 420 | randomize_solution(); |
416 | init_vars(); | 421 | init_vars(); |
417 | 422 | ||
418 | draw_board(0, 0); | 423 | draw_board(0, 0); |
419 | int button = 0, guess = 0, piece = 0; | 424 | int button = 0, guess = 0, piece = 0; |
420 | for (guess = 0; guess < guesses_count && !stop_game(); guess++) { | 425 | for (guess = 0; guess < guesses_count && !stop_game(); guess++) { |
@@ -491,19 +496,19 @@ enum plugin_status plugin_start(const void* parameter) { | |||
491 | rb->splash(HZ, "Well done :)"); | 496 | rb->splash(HZ, "Well done :)"); |
492 | else | 497 | else |
493 | rb->splash(HZ, "Wooops :("); | 498 | rb->splash(HZ, "Wooops :("); |
494 | do { | 499 | do { |
495 | button = rb->button_get(true); | 500 | button = rb->button_get(true); |
496 | if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { | 501 | if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { |
497 | quit = usb = true; | 502 | quit = usb = true; |
498 | } | 503 | } |
499 | } while( ( button == BUTTON_NONE ) | 504 | } while( ( button == BUTTON_NONE ) |
500 | || ( button & (BUTTON_REL|BUTTON_REPEAT) ) ); | 505 | || ( button & (BUTTON_REL|BUTTON_REPEAT) ) ); |
501 | main_menu(); | 506 | main_menu(); |
502 | } | 507 | } |
503 | } | 508 | } |
504 | if (settings_changed) | 509 | if (rb->memcmp(&old_settings, &settings, sizeof(settings))) |
505 | configfile_save(CONFIG_FILE_NAME,config,5,0); | 510 | configfile_save(CONFIG_FILE_NAME, config, ARRAYLEN(config), 0); |
506 | 511 | ||
507 | rb->lcd_setfont(FONT_UI); | 512 | rb->lcd_setfont(FONT_UI); |
508 | return (usb) ? PLUGIN_USB_CONNECTED : PLUGIN_OK; | 513 | return (usb) ? PLUGIN_USB_CONNECTED : PLUGIN_OK; |
509 | } | 514 | } |