summaryrefslogtreecommitdiff
path: root/apps/plugins/snake.c
diff options
context:
space:
mode:
authorTeruaki Kawashima <teru@rockbox.org>2010-02-28 11:27:05 +0000
committerTeruaki Kawashima <teru@rockbox.org>2010-02-28 11:27:05 +0000
commitd4ebc9268e5aa3ac43344707023931e3eba3bb36 (patch)
tree8f6fe6b9cf138f71a9d4177566c03f15edfc1871 /apps/plugins/snake.c
parent0978b427eeb1a661fbbe49dd9c79a1fc270589aa (diff)
downloadrockbox-d4ebc9268e5aa3ac43344707023931e3eba3bb36.tar.gz
rockbox-d4ebc9268e5aa3ac43344707023931e3eba3bb36.zip
snake: use lib/highscore for highscore handling.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24964 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/snake.c')
-rw-r--r--apps/plugins/snake.c115
1 files changed, 57 insertions, 58 deletions
diff --git a/apps/plugins/snake.c b/apps/plugins/snake.c
index a7e75c00c9..1d14efea69 100644
--- a/apps/plugins/snake.c
+++ b/apps/plugins/snake.c
@@ -35,6 +35,7 @@ dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
35#include "plugin.h" 35#include "plugin.h"
36#ifdef HAVE_LCD_BITMAP 36#ifdef HAVE_LCD_BITMAP
37#include "lib/configfile.h" 37#include "lib/configfile.h"
38#include "lib/highscore.h"
38#include "lib/playback_control.h" 39#include "lib/playback_control.h"
39 40
40PLUGIN_HEADER 41PLUGIN_HEADER
@@ -235,15 +236,18 @@ PLUGIN_HEADER
235#define BOARD_WIDTH (LCD_WIDTH/4) 236#define BOARD_WIDTH (LCD_WIDTH/4)
236#define BOARD_HEIGHT (LCD_HEIGHT/4) 237#define BOARD_HEIGHT (LCD_HEIGHT/4)
237 238
239#define CONFIG_FILE_NAME "snake.cfg"
240#define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
241#define NUM_SCORES 5
242
238static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength; 243static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
239static unsigned int score,hiscore=0,level=1; 244static unsigned int score, level = 1;
240static int dir; 245static int dir;
241static bool apple, dead; 246static bool apple, dead, ingame;
247static struct highscore highscores[NUM_SCORES];
242 248
243#define CONFIG_FILE_NAME "snake.cfg"
244static struct configdata config[] = { 249static struct configdata config[] = {
245 {TYPE_INT, 0, 10, { .int_p = &level }, "level", NULL}, 250 {TYPE_INT, 1, 9, { .int_p = &level }, "level", NULL},
246 {TYPE_INT, 0, 10000, { .int_p = &hiscore }, "hiscore", NULL},
247}; 251};
248 252
249static void snake_die (void) 253static void snake_die (void)
@@ -253,12 +257,12 @@ static void snake_die (void)
253 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score); 257 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
254 rb->lcd_puts(0,0,"Oops..."); 258 rb->lcd_puts(0,0,"Oops...");
255 rb->lcd_puts(0,1, pscore); 259 rb->lcd_puts(0,1, pscore);
256 if (score>hiscore) { 260 if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) {
257 hiscore=score;
258 rb->lcd_puts(0,2,"New High Score!"); 261 rb->lcd_puts(0,2,"New High Score!");
259 } 262 }
260 else { 263 else {
261 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore); 264 rb->snprintf(pscore, sizeof(pscore),
265 "High Score: %d", highscores[0].score);
262 rb->lcd_puts(0,2,pscore); 266 rb->lcd_puts(0,2,pscore);
263 } 267 }
264 rb->lcd_update(); 268 rb->lcd_update();
@@ -379,30 +383,20 @@ static void snake_game_init(void) {
379 dead=false; 383 dead=false;
380 snakelength=4; 384 snakelength=4;
381 score=0; 385 score=0;
382 board[11][7]=1; 386 dir=1;
383} 387 board[11][7]=1;
384
385static void snake_choose_level(void)
386{
387 rb->set_int("Snake Speed", "", UNIT_INT, &level, NULL, 1, 1, 9, NULL);
388} 388}
389 389
390static bool _ingame;
391static int snake_menu_cb(int action, const struct menu_item_ex *this_item) 390static int snake_menu_cb(int action, const struct menu_item_ex *this_item)
392{ 391{
393 if(action == ACTION_REQUEST_MENUITEM 392 if(action == ACTION_REQUEST_MENUITEM
394 && !_ingame && ((intptr_t)this_item)==0) 393 && !ingame && ((intptr_t)this_item)==0)
395 return ACTION_EXIT_MENUITEM; 394 return ACTION_EXIT_MENUITEM;
396 return action; 395 return action;
397} 396}
398 397
399static int snake_game_menu(bool ingame) 398static int snake_game_menu(void)
400{ 399{
401 rb->button_clear_queue();
402 int choice = 0;
403
404 _ingame = ingame;
405
406 MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb, 400 MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb,
407 "Resume Game", 401 "Resume Game",
408 "Start New Game", 402 "Start New Game",
@@ -410,10 +404,12 @@ static int snake_game_menu(bool ingame)
410 "High Score", 404 "High Score",
411 "Playback Control", 405 "Playback Control",
412 "Quit"); 406 "Quit");
413 407 int selected = 0;
408
409 rb->button_clear_queue();
410
414 while (true) { 411 while (true) {
415 choice = rb->do_menu(&main_menu, &choice, NULL, false); 412 switch (rb->do_menu(&main_menu, &selected, NULL, false)) {
416 switch (choice) {
417 case 0: 413 case 0:
418 snake_redraw(); 414 snake_redraw();
419 return 0; 415 return 0;
@@ -421,10 +417,12 @@ static int snake_game_menu(bool ingame)
421 snake_game_init(); 417 snake_game_init();
422 return 0; 418 return 0;
423 case 2: 419 case 2:
424 snake_choose_level(); 420 rb->set_int("Snake Speed", "", UNIT_INT, &level,
421 NULL, 1, 1, 9, NULL);
425 break; 422 break;
426 case 3: 423 case 3:
427 rb->splashf(HZ*2, "High Score: %d", hiscore); 424 highscore_show(-1, highscores, NUM_SCORES, true);
425 rb->lcd_setfont(FONT_UI);
428 break; 426 break;
429 case 4: 427 case 4:
430 playback_control(NULL); 428 playback_control(NULL);
@@ -442,17 +440,18 @@ static int snake_game_menu(bool ingame)
442static int snake_game_loop (void) { 440static int snake_game_loop (void) {
443 int button; 441 int button;
444 short x,y; 442 short x,y;
445 bool pause = false; 443 bool pause = false;
446 444
447 if (snake_game_menu(false)==1) 445 if (snake_game_menu())
448 return 1; 446 return 1;
449 447
448 ingame = true;
450 while (true) { 449 while (true) {
451 if (!pause) { 450 if (!pause) {
452 snake_frame(); 451 snake_frame();
453 if (dead) { 452 if (dead) {
454 if (snake_game_menu(false)==1) 453 ingame = false;
455 return 1; 454 return 0;
456 } 455 }
457 if (!apple) { 456 if (!apple) {
458 do { 457 do {
@@ -469,26 +468,24 @@ static int snake_game_loop (void) {
469 } 468 }
470 469
471 button=rb->button_get(false); 470 button=rb->button_get(false);
472 471
473#ifdef HAS_BUTTON_HOLD 472#ifdef HAS_BUTTON_HOLD
474 if (rb->button_hold()) { 473 if (rb->button_hold() && !pause)
475 pause = true; 474 button = SNAKE_PLAYPAUSE;
476 rb->splash (HZ, "Paused");
477 }
478#endif 475#endif
479 switch (button) { 476 switch (button) {
480 case SNAKE_UP: 477 case SNAKE_UP:
481 if (dir!=2) dir=0; 478 if (dir!=2) dir=0;
482 break; 479 break;
483 case SNAKE_RIGHT: 480 case SNAKE_RIGHT:
484 if (dir!=3) dir=1; 481 if (dir!=3) dir=1;
485 break; 482 break;
486 case SNAKE_DOWN: 483 case SNAKE_DOWN:
487 if (dir!=0) dir=2; 484 if (dir!=0) dir=2;
488 break; 485 break;
489 case SNAKE_LEFT: 486 case SNAKE_LEFT:
490 if (dir!=1) dir=3; 487 if (dir!=1) dir=3;
491 break; 488 break;
492 case SNAKE_PLAYPAUSE: 489 case SNAKE_PLAYPAUSE:
493 pause = !pause; 490 pause = !pause;
494 if (pause) 491 if (pause)
@@ -497,16 +494,14 @@ static int snake_game_loop (void) {
497 snake_redraw(); 494 snake_redraw();
498 break; 495 break;
499#ifdef SNAKE_RC_QUIT 496#ifdef SNAKE_RC_QUIT
500 case SNAKE_RC_QUIT: 497 case SNAKE_RC_QUIT:
501#endif 498#endif
502 case SNAKE_QUIT: 499 case SNAKE_QUIT:
503 pause = false; 500 return 0;
504 if (snake_game_menu(true)==1)
505 return 1;
506 break; 501 break;
507 default: 502 default:
508 if (rb->default_event_handler (button) == SYS_USB_CONNECTED) 503 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
509 return PLUGIN_USB_CONNECTED; 504 return 1;
510 break; 505 break;
511 } 506 }
512 } 507 }
@@ -516,10 +511,14 @@ enum plugin_status plugin_start(const void* parameter)
516{ 511{
517 (void)parameter; 512 (void)parameter;
518 513
519 configfile_load(CONFIG_FILE_NAME,config,2,0); 514 configfile_load(CONFIG_FILE_NAME, config, 1, 0);
515 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
520 rb->lcd_clear_display(); 516 rb->lcd_clear_display();
521 snake_game_loop(); 517 ingame = false;
522 configfile_save(CONFIG_FILE_NAME,config,2,0); 518 while(snake_game_loop() == 0)
519 ;
520 configfile_save(CONFIG_FILE_NAME, config, 1, 0);
521 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
523 return PLUGIN_OK; 522 return PLUGIN_OK;
524} 523}
525#endif 524#endif