diff options
author | Richard Burke <rich.g.burke@gmail.com> | 2014-04-23 23:25:05 +0200 |
---|---|---|
committer | Michael Giacomelli <giac2000@hotmail.com> | 2014-05-02 00:56:10 +0200 |
commit | d35be7d0db3a00a9bdab1cdfc9a9b77c714262f7 (patch) | |
tree | 0c0ab1a3d795efd50406fa776696335fed2cb847 | |
parent | fcb835f61abba870e3c648eab9f3728bbe6c890f (diff) | |
download | rockbox-d35be7d0db3a00a9bdab1cdfc9a9b77c714262f7.tar.gz rockbox-d35be7d0db3a00a9bdab1cdfc9a9b77c714262f7.zip |
Added high score and save/load functionality to xobox game.
Change-Id: I5e553a38b9290aeeb3cebaf1abf90ae6fc4ac362
Reviewed-on: http://gerrit.rockbox.org/799
Reviewed-by: Michael Giacomelli <giac2000@hotmail.com>
-rw-r--r-- | apps/plugins/xobox.c | 108 | ||||
-rw-r--r-- | docs/CREDITS | 1 |
2 files changed, 104 insertions, 5 deletions
diff --git a/apps/plugins/xobox.c b/apps/plugins/xobox.c index 6cb46ffa3a..37bb32d2eb 100644 --- a/apps/plugins/xobox.c +++ b/apps/plugins/xobox.c | |||
@@ -23,6 +23,7 @@ | |||
23 | #include "plugin.h" | 23 | #include "plugin.h" |
24 | #include "lib/helper.h" | 24 | #include "lib/helper.h" |
25 | #include "lib/playback_control.h" | 25 | #include "lib/playback_control.h" |
26 | #include "lib/highscore.h" | ||
26 | 27 | ||
27 | 28 | ||
28 | 29 | ||
@@ -417,6 +418,12 @@ static int difficulty = 75; /* Percentage of screen that needs to be filled | |||
417 | * in order to win the game */ | 418 | * in order to win the game */ |
418 | 419 | ||
419 | static bool quit = false; | 420 | static bool quit = false; |
421 | static bool _ingame = false; | ||
422 | |||
423 | #define RESUME_FILE PLUGIN_GAMES_DATA_DIR "/xobox.resume" | ||
424 | #define SCORE_FILE PLUGIN_GAMES_DATA_DIR "/xobox.score" | ||
425 | #define NUM_SCORES 5 | ||
426 | static struct highscore highscores[NUM_SCORES]; | ||
420 | 427 | ||
421 | static unsigned int board[BOARD_H][BOARD_W]; | 428 | static unsigned int board[BOARD_H][BOARD_W]; |
422 | static int testboard[BOARD_H][BOARD_W]; | 429 | static int testboard[BOARD_H][BOARD_W]; |
@@ -1019,12 +1026,66 @@ static void init_game (void) | |||
1019 | rb->splash (HZ * 2, "Ready?"); | 1026 | rb->splash (HZ * 2, "Ready?"); |
1020 | } | 1027 | } |
1021 | 1028 | ||
1029 | static bool load_game(void) | ||
1030 | { | ||
1031 | int fd = rb->open(RESUME_FILE, O_RDONLY); | ||
1032 | |||
1033 | if (fd < 0) { | ||
1034 | return true; | ||
1035 | } | ||
1036 | |||
1037 | bool load_success = | ||
1038 | rb->read(fd, &player, sizeof(player)) == sizeof(player) && | ||
1039 | rb->read(fd, &qixes, sizeof(qixes)) == sizeof(qixes) && | ||
1040 | rb->read(fd, &stack, sizeof(stack)) == sizeof(stack) && | ||
1041 | rb->read(fd, &board, sizeof(board)) == sizeof(board) && | ||
1042 | rb->read(fd, &testboard, sizeof(testboard)) == sizeof(testboard) && | ||
1043 | rb->read(fd, &speed, sizeof(speed)) == sizeof(speed) && | ||
1044 | rb->read(fd, &difficulty, sizeof(difficulty)) == sizeof(difficulty) && | ||
1045 | rb->read(fd, &stackPointer, sizeof(stackPointer)) == sizeof(stackPointer) && | ||
1046 | rb->read(fd, &percentage_cache, | ||
1047 | sizeof(percentage_cache)) == sizeof(percentage_cache); | ||
1048 | |||
1049 | rb->close(fd); | ||
1050 | _ingame = load_success; | ||
1051 | |||
1052 | return load_success; | ||
1053 | } | ||
1054 | |||
1055 | static bool save_game(void) | ||
1056 | { | ||
1057 | int fd = rb->open(RESUME_FILE, O_WRONLY|O_CREAT, 0666); | ||
1058 | |||
1059 | if (fd < 0) { | ||
1060 | return false; | ||
1061 | } | ||
1062 | |||
1063 | bool save_success = | ||
1064 | rb->write(fd, &player, sizeof(player)) > 0 && | ||
1065 | rb->write(fd, &qixes, sizeof(qixes)) > 0 && | ||
1066 | rb->write(fd, &stack, sizeof(stack)) > 0 && | ||
1067 | rb->write(fd, &board, sizeof(board)) > 0 && | ||
1068 | rb->write(fd, &testboard, sizeof(testboard)) > 0 && | ||
1069 | rb->write(fd, &speed, sizeof(speed)) > 0 && | ||
1070 | rb->write(fd, &difficulty, sizeof(difficulty)) > 0 && | ||
1071 | rb->write(fd, &stackPointer, sizeof(stackPointer)) > 0 && | ||
1072 | rb->write(fd, &percentage_cache, sizeof(percentage_cache)) > 0; | ||
1073 | |||
1074 | rb->close(fd); | ||
1075 | |||
1076 | if (!save_success) { | ||
1077 | rb->remove(RESUME_FILE); | ||
1078 | } | ||
1079 | |||
1080 | return save_success; | ||
1081 | } | ||
1082 | |||
1022 | /* the main menu */ | 1083 | /* the main menu */ |
1023 | static bool _ingame; | ||
1024 | static int xobox_menu_cb(int action, const struct menu_item_ex *this_item) | 1084 | static int xobox_menu_cb(int action, const struct menu_item_ex *this_item) |
1025 | { | 1085 | { |
1086 | intptr_t item = (intptr_t)this_item; | ||
1026 | if(action == ACTION_REQUEST_MENUITEM | 1087 | if(action == ACTION_REQUEST_MENUITEM |
1027 | && !_ingame && ((intptr_t)this_item)==0) | 1088 | && !_ingame && (item == 0 || item == 6)) |
1028 | return ACTION_EXIT_MENUITEM; | 1089 | return ACTION_EXIT_MENUITEM; |
1029 | return action; | 1090 | return action; |
1030 | } | 1091 | } |
@@ -1037,12 +1098,16 @@ static int xobox_menu(bool ingame) | |||
1037 | MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb, | 1098 | MENUITEM_STRINGLIST(main_menu, "Xobox Menu", xobox_menu_cb, |
1038 | "Resume Game", "Start New Game", | 1099 | "Resume Game", "Start New Game", |
1039 | "Speed", "Difficulty", | 1100 | "Speed", "Difficulty", |
1040 | "Playback Control", "Quit"); | 1101 | "High Scores", "Playback Control", |
1102 | "Quit Without Saving", "Quit"); | ||
1041 | _ingame = ingame; | 1103 | _ingame = ingame; |
1042 | 1104 | ||
1043 | while (true) { | 1105 | while (true) { |
1044 | switch (rb->do_menu(&main_menu, &selection, NULL, false)) { | 1106 | switch (rb->do_menu(&main_menu, &selection, NULL, false)) { |
1045 | case 0: | 1107 | case 0: |
1108 | rb->remove(RESUME_FILE); | ||
1109 | refresh_board(); | ||
1110 | rb->splash (HZ*2, "Ready?"); | ||
1046 | return 0; | 1111 | return 0; |
1047 | case 1: | 1112 | case 1: |
1048 | init_game (); | 1113 | init_game (); |
@@ -1055,9 +1120,22 @@ static int xobox_menu(bool ingame) | |||
1055 | 5, 50, 95, NULL); | 1120 | 5, 50, 95, NULL); |
1056 | break; | 1121 | break; |
1057 | case 4: | 1122 | case 4: |
1058 | playback_control(NULL); | 1123 | highscore_show(-1, highscores, NUM_SCORES, true); |
1059 | break; | 1124 | break; |
1060 | case 5: | 1125 | case 5: |
1126 | playback_control(NULL); | ||
1127 | break; | ||
1128 | case 6: | ||
1129 | return 1; | ||
1130 | case 7: | ||
1131 | if (_ingame) { | ||
1132 | rb->splash(HZ, "Saving game..."); | ||
1133 | |||
1134 | if (!save_game()) { | ||
1135 | rb->splash(HZ, "Failed to save game"); | ||
1136 | } | ||
1137 | } | ||
1138 | |||
1061 | return 1; | 1139 | return 1; |
1062 | case MENU_ATTACHED_USB: | 1140 | case MENU_ATTACHED_USB: |
1063 | return 1; | 1141 | return 1; |
@@ -1074,7 +1152,7 @@ static int xobox_loop (void) | |||
1074 | bool pause = false; | 1152 | bool pause = false; |
1075 | int end; | 1153 | int end; |
1076 | 1154 | ||
1077 | if (xobox_menu(false)) { | 1155 | if (xobox_menu(_ingame)) { |
1078 | return PLUGIN_OK; | 1156 | return PLUGIN_OK; |
1079 | } | 1157 | } |
1080 | 1158 | ||
@@ -1129,6 +1207,18 @@ static int xobox_loop (void) | |||
1129 | } | 1207 | } |
1130 | if (player.gameover) { | 1208 | if (player.gameover) { |
1131 | rb->splash (HZ, "Game Over!"); | 1209 | rb->splash (HZ, "Game Over!"); |
1210 | |||
1211 | int pos = highscore_update(player.score, player.level, "", | ||
1212 | highscores, NUM_SCORES); | ||
1213 | |||
1214 | if (pos != -1) { | ||
1215 | if (pos == 0) { | ||
1216 | rb->splashf(HZ, "New High Score: %d", player.score); | ||
1217 | } | ||
1218 | |||
1219 | highscore_show(-1, highscores, NUM_SCORES, true); | ||
1220 | } | ||
1221 | |||
1132 | if (xobox_menu(false)) { | 1222 | if (xobox_menu(false)) { |
1133 | quit = true; | 1223 | quit = true; |
1134 | } | 1224 | } |
@@ -1158,6 +1248,12 @@ enum plugin_status plugin_start (const void *parameter) | |||
1158 | /* Turn off backlight timeout */ | 1248 | /* Turn off backlight timeout */ |
1159 | backlight_ignore_timeout(); | 1249 | backlight_ignore_timeout(); |
1160 | 1250 | ||
1251 | highscore_load(SCORE_FILE, highscores, NUM_SCORES); | ||
1252 | |||
1253 | if (!load_game()) { | ||
1254 | rb->splash(HZ, "Failed to load saved game"); | ||
1255 | } | ||
1256 | |||
1161 | randomize (); | 1257 | randomize (); |
1162 | ret = xobox_loop (); | 1258 | ret = xobox_loop (); |
1163 | 1259 | ||
@@ -1165,5 +1261,7 @@ enum plugin_status plugin_start (const void *parameter) | |||
1165 | backlight_use_settings(); | 1261 | backlight_use_settings(); |
1166 | rb->lcd_setfont (FONT_UI); | 1262 | rb->lcd_setfont (FONT_UI); |
1167 | 1263 | ||
1264 | highscore_save(SCORE_FILE, highscores, NUM_SCORES); | ||
1265 | |||
1168 | return ret; | 1266 | return ret; |
1169 | } | 1267 | } |
diff --git a/docs/CREDITS b/docs/CREDITS index 345c30cd34..0329738b1b 100644 --- a/docs/CREDITS +++ b/docs/CREDITS | |||
@@ -636,6 +636,7 @@ Ryan Billing | |||
636 | Dmitry Gamza | 636 | Dmitry Gamza |
637 | Sebastian Leonhardt | 637 | Sebastian Leonhardt |
638 | Avi Eisenberg | 638 | Avi Eisenberg |
639 | Richard Burke | ||
639 | 640 | ||
640 | The libmad team | 641 | The libmad team |
641 | The wavpack team | 642 | The wavpack team |