diff options
author | Johannes Schwarz <ubuntuxer@rockbox.org> | 2009-07-04 23:05:41 +0000 |
---|---|---|
committer | Johannes Schwarz <ubuntuxer@rockbox.org> | 2009-07-04 23:05:41 +0000 |
commit | cacb47e26243293534f49ee1001748d129596836 (patch) | |
tree | 42b2502b468517ca0ae8f85d951068240b2afe22 /apps/plugins | |
parent | 54929e8871aa672a92dadbc0ad693ffd497c541f (diff) | |
download | rockbox-cacb47e26243293534f49ee1001748d129596836.tar.gz rockbox-cacb47e26243293534f49ee1001748d129596836.zip |
FS#10291: use lib highscore.h and add a new highscore table
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21643 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/bubbles.c | 182 |
1 files changed, 83 insertions, 99 deletions
diff --git a/apps/plugins/bubbles.c b/apps/plugins/bubbles.c index 25f6b893d8..4146b45b36 100644 --- a/apps/plugins/bubbles.c +++ b/apps/plugins/bubbles.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "lib/pluginlib_actions.h" | 29 | #include "lib/pluginlib_actions.h" |
30 | #include "lib/fixedpoint.h" | 30 | #include "lib/fixedpoint.h" |
31 | #include "lib/playback_control.h" | 31 | #include "lib/playback_control.h" |
32 | #include "lib/highscore.h" | ||
32 | 33 | ||
33 | PLUGIN_HEADER | 34 | PLUGIN_HEADER |
34 | 35 | ||
@@ -50,7 +51,7 @@ PLUGIN_HEADER | |||
50 | #define BB_LEVEL_HEIGHT 10 | 51 | #define BB_LEVEL_HEIGHT 10 |
51 | 52 | ||
52 | /* various amounts */ | 53 | /* various amounts */ |
53 | #define NUM_SCORES 10 | 54 | #define NUM_SCORES 5 |
54 | #define NUM_LEVELS 100 | 55 | #define NUM_LEVELS 100 |
55 | #define NUM_QUEUE 2 | 56 | #define NUM_QUEUE 2 |
56 | #define NUM_BUBBLES 8 | 57 | #define NUM_BUBBLES 8 |
@@ -1223,15 +1224,6 @@ struct tile { | |||
1223 | bool delete; | 1224 | bool delete; |
1224 | }; | 1225 | }; |
1225 | 1226 | ||
1226 | /* the highscore struct | ||
1227 | * level is the highscore level | ||
1228 | * score is the highscore score | ||
1229 | */ | ||
1230 | struct highscore { | ||
1231 | unsigned int level; | ||
1232 | unsigned int score; | ||
1233 | }; | ||
1234 | |||
1235 | /* the game context struct | 1227 | /* the game context struct |
1236 | * score is the current score | 1228 | * score is the current score |
1237 | * level is the current level | 1229 | * level is the current level |
@@ -1254,7 +1246,7 @@ struct highscore { | |||
1254 | struct game_context { | 1246 | struct game_context { |
1255 | unsigned int score; | 1247 | unsigned int score; |
1256 | unsigned int level; | 1248 | unsigned int level; |
1257 | unsigned int highlevel; | 1249 | struct highscore highlevel; |
1258 | struct highscore highscores[NUM_SCORES]; | 1250 | struct highscore highscores[NUM_SCORES]; |
1259 | int angle; | 1251 | int angle; |
1260 | int shots; | 1252 | int shots; |
@@ -1284,11 +1276,12 @@ static int bubbles_remove(struct game_context* bb); | |||
1284 | static void bubbles_anchored(struct game_context* bb, int row, int col); | 1276 | static void bubbles_anchored(struct game_context* bb, int row, int col); |
1285 | static int bubbles_fall(struct game_context* bb); | 1277 | static int bubbles_fall(struct game_context* bb); |
1286 | static int bubbles_checklevel(struct game_context* bb); | 1278 | static int bubbles_checklevel(struct game_context* bb); |
1287 | static int bubbles_recordscore(struct game_context* bb); | 1279 | static void bubbles_recordscore(struct game_context* bb); |
1280 | static void bubbles_displayscores(struct game_context* bb, int position); | ||
1288 | static void bubbles_savescores(struct game_context* bb); | 1281 | static void bubbles_savescores(struct game_context* bb); |
1289 | static bool bubbles_loadgame(struct game_context* bb); | 1282 | static bool bubbles_loadgame(struct game_context* bb); |
1290 | static void bubbles_savegame(struct game_context* bb); | 1283 | static void bubbles_savegame(struct game_context* bb); |
1291 | static void bubbles_setcolors(void); | 1284 | static inline void bubbles_setcolors(void); |
1292 | static void bubbles_callback(void* param); | 1285 | static void bubbles_callback(void* param); |
1293 | static int bubbles_handlebuttons(struct game_context* bb, bool animblock, | 1286 | static int bubbles_handlebuttons(struct game_context* bb, bool animblock, |
1294 | int timeout); | 1287 | int timeout); |
@@ -2121,99 +2114,97 @@ static int bubbles_checklevel(struct game_context* bb) { | |||
2121 | * bubbles_recordscore() inserts a high score into the high scores list and | 2114 | * bubbles_recordscore() inserts a high score into the high scores list and |
2122 | * returns the high score position. | 2115 | * returns the high score position. |
2123 | ******************************************************************************/ | 2116 | ******************************************************************************/ |
2124 | static int bubbles_recordscore(struct game_context* bb) { | 2117 | static void bubbles_recordscore(struct game_context* bb) { |
2125 | int i; | 2118 | |
2126 | int position = 0; | 2119 | int position; |
2127 | unsigned int currentscore, currentlevel; | 2120 | |
2128 | unsigned int tempscore, templevel; | 2121 | if (highscore_would_update(bb->score, bb->highscores, NUM_SCORES)) { |
2129 | 2122 | bb->dirty = true; | |
2130 | if(bb->score > 0) { | 2123 | position = highscore_update(bb->score, bb->level, "", |
2131 | currentlevel = bb->level-1; | 2124 | bb->highscores, NUM_SCORES); |
2132 | currentscore = bb->score; | 2125 | if (position==0) { |
2133 | 2126 | rb->splash(HZ*2, "New High Score"); | |
2134 | for(i=0; i<NUM_SCORES; i++) { | ||
2135 | if(currentscore >= bb->highscores[i].score) { | ||
2136 | if(!position) { | ||
2137 | position = i+1; | ||
2138 | bb->dirty = true; | ||
2139 | } | ||
2140 | templevel = bb->highscores[i].level; | ||
2141 | tempscore = bb->highscores[i].score; | ||
2142 | bb->highscores[i].level = currentlevel; | ||
2143 | bb->highscores[i].score = currentscore; | ||
2144 | currentlevel = templevel; | ||
2145 | currentscore = tempscore; | ||
2146 | } | ||
2147 | } | 2127 | } |
2128 | bubbles_displayscores(bb, position); | ||
2148 | } | 2129 | } |
2149 | 2130 | } | |
2150 | return position; | ||
2151 | } | ||
2152 | 2131 | ||
2153 | /***************************************************************************** | 2132 | /***************************************************************************** |
2154 | * bubbles_loadscores() loads the high scores saved file. | 2133 | * bubbles_loadscores() loads the high scores saved file. |
2155 | ******************************************************************************/ | 2134 | ******************************************************************************/ |
2156 | static void bubbles_loadscores(struct game_context* bb) { | 2135 | static void bubbles_loadscores(struct game_context* bb) { |
2157 | int fd; | ||
2158 | 2136 | ||
2159 | bb->dirty = false; | 2137 | bb->dirty = false; |
2160 | 2138 | ||
2161 | /* clear high scores */ | 2139 | /* highlevel and highscores */ |
2162 | bb->highlevel = 0; | 2140 | highscore_load(SCORE_FILE, &bb->highlevel, NUM_SCORES+1); |
2163 | rb->memset(bb->highscores, 0, sizeof(bb->highscores)); | ||
2164 | |||
2165 | /* open scores file */ | ||
2166 | fd = rb->open(SCORE_FILE, O_RDONLY); | ||
2167 | if(fd < 0) return; | ||
2168 | |||
2169 | /* read in high scores */ | ||
2170 | rb->read(fd, &bb->highlevel, sizeof(bb->highlevel)); | ||
2171 | if(rb->read(fd, bb->highscores, sizeof(bb->highscores)) <= 0) { | ||
2172 | /* scores are bad, reset */ | ||
2173 | rb->memset(bb->highscores, 0, sizeof(bb->highscores)); | ||
2174 | } | ||
2175 | |||
2176 | if( bb->highlevel >= NUM_LEVELS ) | ||
2177 | bb->highlevel = NUM_LEVELS - 1; | ||
2178 | 2141 | ||
2179 | rb->close(fd); | 2142 | if( bb->highlevel.level >= NUM_LEVELS ) |
2143 | bb->highlevel.level = NUM_LEVELS - 1; | ||
2180 | } | 2144 | } |
2181 | 2145 | ||
2182 | /***************************************************************************** | 2146 | /***************************************************************************** |
2183 | * bubbles_savescores() saves the high scores saved file. | 2147 | * bubbles_savescores() saves the high scores saved file. |
2184 | ******************************************************************************/ | 2148 | ******************************************************************************/ |
2185 | static void bubbles_savescores(struct game_context* bb) { | 2149 | static void bubbles_savescores(struct game_context* bb) { |
2186 | int fd; | ||
2187 | 2150 | ||
2188 | /* write out the high scores to the save file */ | 2151 | /* highlevel and highscores */ |
2189 | fd = rb->open(SCORE_FILE, O_WRONLY|O_CREAT); | 2152 | highscore_save(SCORE_FILE, &bb->highlevel, NUM_SCORES+1); |
2190 | rb->write(fd, &bb->highlevel, sizeof(bb->highlevel)); | ||
2191 | rb->write(fd, bb->highscores, sizeof(bb->highscores)); | ||
2192 | rb->close(fd); | ||
2193 | bb->dirty = false; | 2153 | bb->dirty = false; |
2194 | } | 2154 | } |
2195 | 2155 | ||
2196 | /***************************************************************************** | 2156 | /***************************************************************************** |
2197 | * bubbles_displaycores() displays the high scores | 2157 | * bubbles_displayscores() displays the high scores |
2198 | ******************************************************************************/ | 2158 | ******************************************************************************/ |
2199 | static char * scores_get_name(int selected_item, void * data, | 2159 | #define MARGIN 5 |
2200 | char * buffer, size_t buffer_len) | 2160 | static void bubbles_displayscores(struct game_context* bb, int position) { |
2201 | { | 2161 | int i, w, h; |
2202 | struct game_context* bb = (struct game_context*)data; | 2162 | char str[30]; |
2203 | rb->snprintf(buffer, buffer_len, "#%02d: %d, Lvl %d", | ||
2204 | selected_item+1, | ||
2205 | bb->highscores[selected_item].score, | ||
2206 | bb->highscores[selected_item].level); | ||
2207 | return buffer; | ||
2208 | } | ||
2209 | static void bubbles_displayscores(struct game_context* bb) { | ||
2210 | struct simplelist_info info; | ||
2211 | rb->simplelist_info_init(&info, "High Scores", NUM_SCORES, (void*)bb); | ||
2212 | info.hide_selection = true; | ||
2213 | info.get_name = scores_get_name; | ||
2214 | rb->simplelist_show_list(&info); | ||
2215 | } | ||
2216 | 2163 | ||
2164 | #ifdef HAVE_LCD_COLOR | ||
2165 | rb->lcd_set_background(LCD_BLACK); | ||
2166 | rb->lcd_set_foreground(LCD_WHITE); | ||
2167 | #endif | ||
2168 | rb->button_clear_queue(); | ||
2169 | rb->lcd_clear_display(); | ||
2170 | |||
2171 | rb->lcd_setfont(FONT_UI); | ||
2172 | rb->lcd_getstringsize("High Scores", &w, &h); | ||
2173 | /* check wether it fits on screen */ | ||
2174 | if ((4*h + h*(NUM_SCORES-1) + MARGIN) > LCD_HEIGHT) { | ||
2175 | rb->lcd_setfont(FONT_SYSFIXED); | ||
2176 | rb->lcd_getstringsize("High Scores", &w, &h); | ||
2177 | } | ||
2178 | rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores"); | ||
2179 | rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score"); | ||
2180 | rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level"); | ||
2181 | |||
2182 | for (i = 0; i<NUM_SCORES; i++) | ||
2183 | { | ||
2184 | #ifdef HAVE_LCD_COLOR | ||
2185 | if(i == position) { | ||
2186 | rb->lcd_set_foreground(LCD_RGBPACK(245,0,0)); | ||
2187 | } | ||
2188 | #endif | ||
2189 | rb->snprintf (str, sizeof (str), "%d)", i+1); | ||
2190 | rb->lcd_putsxy (MARGIN,3*h + h*i, str); | ||
2191 | rb->snprintf (str, sizeof (str), "%d", bb->highscores[i].score); | ||
2192 | rb->lcd_putsxy (LCD_WIDTH/4-w/4,3*h + h*i, str); | ||
2193 | rb->snprintf (str, sizeof (str), "%d", bb->highscores[i].level); | ||
2194 | rb->lcd_putsxy (LCD_WIDTH*3/4-w/4,3*h + h*i, str); | ||
2195 | if(i == position) { | ||
2196 | #ifdef HAVE_LCD_COLOR | ||
2197 | rb->lcd_set_foreground(LCD_WHITE); | ||
2198 | #else | ||
2199 | rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN, 3*h + h*(i+1)-1); | ||
2200 | #endif | ||
2201 | } | ||
2202 | } | ||
2203 | rb->lcd_update(); | ||
2204 | rb->button_get(true); | ||
2205 | rb->lcd_setfont(FONT_SYSFIXED); | ||
2206 | bubbles_setcolors(); | ||
2207 | } | ||
2217 | 2208 | ||
2218 | /***************************************************************************** | 2209 | /***************************************************************************** |
2219 | * bubbles_loadgame() loads the saved game and returns load success. | 2210 | * bubbles_loadgame() loads the saved game and returns load success. |
@@ -2400,7 +2391,7 @@ static int bubbles(struct game_context* bb) { | |||
2400 | ********************/ | 2391 | ********************/ |
2401 | MENUITEM_STRINGLIST(menu,"Bubbles Menu",NULL, | 2392 | MENUITEM_STRINGLIST(menu,"Bubbles Menu",NULL, |
2402 | "Start New Game", "Resume Game", | 2393 | "Start New Game", "Resume Game", |
2403 | "Level", "Display High Scores", "Playback Control", | 2394 | "Level", "High Scores", "Playback Control", |
2404 | "Quit"); | 2395 | "Quit"); |
2405 | while(!startgame){ | 2396 | while(!startgame){ |
2406 | switch (rb->do_menu(&menu, NULL, NULL, false)) | 2397 | switch (rb->do_menu(&menu, NULL, NULL, false)) |
@@ -2418,11 +2409,12 @@ static int bubbles(struct game_context* bb) { | |||
2418 | break; | 2409 | break; |
2419 | case 2: /* choose level */ | 2410 | case 2: /* choose level */ |
2420 | startlevel++; | 2411 | startlevel++; |
2421 | rb->set_int("Choose start level", "", UNIT_INT, &startlevel, NULL, 1, 1, bb->highlevel+1, NULL); | 2412 | rb->set_int("Choose start level", "", UNIT_INT, &startlevel, |
2413 | NULL, 1, 1, bb->highlevel.level+1, NULL); | ||
2422 | startlevel--; | 2414 | startlevel--; |
2423 | break; | 2415 | break; |
2424 | case 3: /* High scores */ | 2416 | case 3: /* High scores */ |
2425 | bubbles_displayscores(bb); | 2417 | bubbles_displayscores(bb, 0); |
2426 | break; | 2418 | break; |
2427 | case 4: /* Playback Control */ | 2419 | case 4: /* Playback Control */ |
2428 | playback_control(NULL); | 2420 | playback_control(NULL); |
@@ -2485,14 +2477,12 @@ static int bubbles(struct game_context* bb) { | |||
2485 | enum plugin_status plugin_start(const void* parameter) { | 2477 | enum plugin_status plugin_start(const void* parameter) { |
2486 | struct game_context bb; | 2478 | struct game_context bb; |
2487 | bool exit = false; | 2479 | bool exit = false; |
2488 | int position; | ||
2489 | 2480 | ||
2490 | /* plugin init */ | 2481 | /* plugin init */ |
2491 | (void)parameter; | 2482 | (void)parameter; |
2492 | /* end of plugin init */ | 2483 | /* end of plugin init */ |
2493 | 2484 | ||
2494 | /* load files */ | 2485 | /* load files */ |
2495 | rb->splash(0, "Loading..."); | ||
2496 | bubbles_loadscores(&bb); | 2486 | bubbles_loadscores(&bb); |
2497 | rb->lcd_clear_display(); | 2487 | rb->lcd_clear_display(); |
2498 | 2488 | ||
@@ -2507,15 +2497,12 @@ enum plugin_status plugin_start(const void* parameter) { | |||
2507 | case BB_WIN: | 2497 | case BB_WIN: |
2508 | rb->splash(HZ*2, "You Win!"); | 2498 | rb->splash(HZ*2, "You Win!"); |
2509 | /* record high level */ | 2499 | /* record high level */ |
2510 | if( NUM_LEVELS-1 > bb.highlevel) { | 2500 | if( NUM_LEVELS-1 > bb.highlevel.level) { |
2511 | bb.highlevel = NUM_LEVELS-1; | 2501 | bb.highlevel.level = NUM_LEVELS-1; |
2512 | bb.dirty = true; | 2502 | bb.dirty = true; |
2513 | } | 2503 | } |
2514 | |||
2515 | /* record high score */ | 2504 | /* record high score */ |
2516 | if((position = bubbles_recordscore(&bb))) { | 2505 | bubbles_recordscore(&bb); |
2517 | rb->splashf(HZ*2, "New high score #%d!", position); | ||
2518 | } | ||
2519 | break; | 2506 | break; |
2520 | 2507 | ||
2521 | case BB_LOSE: | 2508 | case BB_LOSE: |
@@ -2525,15 +2512,12 @@ enum plugin_status plugin_start(const void* parameter) { | |||
2525 | case BB_END: | 2512 | case BB_END: |
2526 | if(!bb.resume) { | 2513 | if(!bb.resume) { |
2527 | /* record high level */ | 2514 | /* record high level */ |
2528 | if(bb.level-1 > bb.highlevel) { | 2515 | if((int)bb.level-1 > bb.highlevel.level) { |
2529 | bb.highlevel = bb.level-1; | 2516 | bb.highlevel.level = bb.level-1; |
2530 | bb.dirty = true; | 2517 | bb.dirty = true; |
2531 | } | 2518 | } |
2532 | |||
2533 | /* record high score */ | 2519 | /* record high score */ |
2534 | if((position = bubbles_recordscore(&bb))) { | 2520 | bubbles_recordscore(&bb); |
2535 | rb->splashf(HZ*2, "New high score #%d!", position); | ||
2536 | } | ||
2537 | } | 2521 | } |
2538 | break; | 2522 | break; |
2539 | 2523 | ||