Index: apps/plugins/bubbles.c =================================================================== --- apps/plugins/bubbles.c (revision 21186) +++ apps/plugins/bubbles.c (working copy) @@ -29,6 +29,7 @@ #include "lib/pluginlib_actions.h" #include "lib/fixedpoint.h" #include "lib/playback_control.h" +#include "lib/highscore.h" PLUGIN_HEADER @@ -1223,15 +1224,6 @@ bool delete; }; -/* the highscore struct - * level is the highscore level - * score is the highscore score - */ -struct highscore { - unsigned int level; - unsigned int score; -}; - /* the game context struct * score is the current score * level is the current level @@ -1254,7 +1246,7 @@ struct game_context { unsigned int score; unsigned int level; - unsigned int highlevel; + struct highscore highlevel; struct highscore highscores[NUM_SCORES]; int angle; int shots; @@ -1288,7 +1280,7 @@ static void bubbles_savescores(struct game_context* bb); static bool bubbles_loadgame(struct game_context* bb); static void bubbles_savegame(struct game_context* bb); -static void bubbles_setcolors(void); +static inline void bubbles_setcolors(void); static void bubbles_callback(void* param); static int bubbles_handlebuttons(struct game_context* bb, bool animblock, int timeout); @@ -2122,79 +2114,44 @@ * returns the high score position. ******************************************************************************/ static int bubbles_recordscore(struct game_context* bb) { - int i; - int position = 0; - unsigned int currentscore, currentlevel; - unsigned int tempscore, templevel; - if(bb->score > 0) { - currentlevel = bb->level-1; - currentscore = bb->score; + int position; - for(i=0; i= bb->highscores[i].score) { - if(!position) { - position = i+1; - bb->dirty = true; - } - templevel = bb->highscores[i].level; - tempscore = bb->highscores[i].score; - bb->highscores[i].level = currentlevel; - bb->highscores[i].score = currentscore; - currentlevel = templevel; - currentscore = tempscore; - } - } + position = highscore_update(bb->score, bb->level-1, + bb->highscores, NUM_SCORES); + if (position) { + bb->dirty = true; + return NUM_SCORES+1-position; } + return 0; +} - return position; - } - /***************************************************************************** * bubbles_loadscores() loads the high scores saved file. ******************************************************************************/ static void bubbles_loadscores(struct game_context* bb) { - int fd; bb->dirty = false; - /* clear high scores */ - bb->highlevel = 0; - rb->memset(bb->highscores, 0, sizeof(bb->highscores)); + /* highlevel and highscores */ + highscore_load(SCORE_FILE, &bb->highlevel, NUM_SCORES+1); - /* open scores file */ - fd = rb->open(SCORE_FILE, O_RDONLY); - if(fd < 0) return; - - /* read in high scores */ - rb->read(fd, &bb->highlevel, sizeof(bb->highlevel)); - if(rb->read(fd, bb->highscores, sizeof(bb->highscores)) <= 0) { - /* scores are bad, reset */ - rb->memset(bb->highscores, 0, sizeof(bb->highscores)); - } - - if( bb->highlevel >= NUM_LEVELS ) - bb->highlevel = NUM_LEVELS - 1; - - rb->close(fd); + if( bb->highlevel.level >= NUM_LEVELS ) + bb->highlevel.level = NUM_LEVELS - 1; } /***************************************************************************** * bubbles_savescores() saves the high scores saved file. ******************************************************************************/ static void bubbles_savescores(struct game_context* bb) { - int fd; - /* write out the high scores to the save file */ - fd = rb->open(SCORE_FILE, O_WRONLY|O_CREAT); - rb->write(fd, &bb->highlevel, sizeof(bb->highlevel)); - rb->write(fd, bb->highscores, sizeof(bb->highscores)); - rb->close(fd); + /* highlevel and highscores */ + highscore_save(SCORE_FILE, &bb->highlevel, NUM_SCORES+1); bb->dirty = false; } /***************************************************************************** -* bubbles_displaycores() displays the high scores +* bubbles_displayscores() displays the high scores ******************************************************************************/ static char * scores_get_name(int selected_item, void * data, char * buffer, size_t buffer_len) @@ -2202,8 +2159,8 @@ struct game_context* bb = (struct game_context*)data; rb->snprintf(buffer, buffer_len, "#%02d: %d, Lvl %d", selected_item+1, - bb->highscores[selected_item].score, - bb->highscores[selected_item].level); + bb->highscores[NUM_SCORES-1-selected_item].score, + bb->highscores[NUM_SCORES-1-selected_item].level); return buffer; } static void bubbles_displayscores(struct game_context* bb) { @@ -2418,7 +2375,8 @@ break; case 2: /* choose level */ startlevel++; - rb->set_int("Choose start level", "", UNIT_INT, &startlevel, NULL, 1, 1, bb->highlevel+1, NULL); + rb->set_int("Choose start level", "", UNIT_INT, &startlevel, + NULL, 1, 1, bb->highlevel.level+1, NULL); startlevel--; break; case 3: /* High scores */ @@ -2507,8 +2465,8 @@ case BB_WIN: rb->splash(HZ*2, "You Win!"); /* record high level */ - if( NUM_LEVELS-1 > bb.highlevel) { - bb.highlevel = NUM_LEVELS-1; + if( NUM_LEVELS-1 > bb.highlevel.level) { + bb.highlevel.level = NUM_LEVELS-1; bb.dirty = true; } @@ -2525,8 +2483,8 @@ case BB_END: if(!bb.resume) { /* record high level */ - if(bb.level-1 > bb.highlevel) { - bb.highlevel = bb.level-1; + if((int)bb.level-1 > bb.highlevel.level) { + bb.highlevel.level = bb.level-1; bb.dirty = true; }