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
This commit is contained in:
Johannes Schwarz 2009-07-04 23:05:41 +00:00
parent 54929e8871
commit cacb47e262

View file

@ -29,6 +29,7 @@
#include "lib/pluginlib_actions.h" #include "lib/pluginlib_actions.h"
#include "lib/fixedpoint.h" #include "lib/fixedpoint.h"
#include "lib/playback_control.h" #include "lib/playback_control.h"
#include "lib/highscore.h"
PLUGIN_HEADER PLUGIN_HEADER
@ -50,7 +51,7 @@ PLUGIN_HEADER
#define BB_LEVEL_HEIGHT 10 #define BB_LEVEL_HEIGHT 10
/* various amounts */ /* various amounts */
#define NUM_SCORES 10 #define NUM_SCORES 5
#define NUM_LEVELS 100 #define NUM_LEVELS 100
#define NUM_QUEUE 2 #define NUM_QUEUE 2
#define NUM_BUBBLES 8 #define NUM_BUBBLES 8
@ -1223,15 +1224,6 @@ struct tile {
bool delete; 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 /* the game context struct
* score is the current score * score is the current score
* level is the current level * level is the current level
@ -1254,7 +1246,7 @@ struct highscore {
struct game_context { struct game_context {
unsigned int score; unsigned int score;
unsigned int level; unsigned int level;
unsigned int highlevel; struct highscore highlevel;
struct highscore highscores[NUM_SCORES]; struct highscore highscores[NUM_SCORES];
int angle; int angle;
int shots; int shots;
@ -1284,11 +1276,12 @@ static int bubbles_remove(struct game_context* bb);
static void bubbles_anchored(struct game_context* bb, int row, int col); static void bubbles_anchored(struct game_context* bb, int row, int col);
static int bubbles_fall(struct game_context* bb); static int bubbles_fall(struct game_context* bb);
static int bubbles_checklevel(struct game_context* bb); static int bubbles_checklevel(struct game_context* bb);
static int bubbles_recordscore(struct game_context* bb); static void bubbles_recordscore(struct game_context* bb);
static void bubbles_displayscores(struct game_context* bb, int position);
static void bubbles_savescores(struct game_context* bb); static void bubbles_savescores(struct game_context* bb);
static bool bubbles_loadgame(struct game_context* bb); static bool bubbles_loadgame(struct game_context* bb);
static void bubbles_savegame(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 void bubbles_callback(void* param);
static int bubbles_handlebuttons(struct game_context* bb, bool animblock, static int bubbles_handlebuttons(struct game_context* bb, bool animblock,
int timeout); int timeout);
@ -2121,99 +2114,97 @@ static int bubbles_checklevel(struct game_context* bb) {
* bubbles_recordscore() inserts a high score into the high scores list and * bubbles_recordscore() inserts a high score into the high scores list and
* returns the high score position. * returns the high score position.
******************************************************************************/ ******************************************************************************/
static int bubbles_recordscore(struct game_context* bb) { static void bubbles_recordscore(struct game_context* bb) {
int i;
int position = 0;
unsigned int currentscore, currentlevel;
unsigned int tempscore, templevel;
if(bb->score > 0) { int position;
currentlevel = bb->level-1;
currentscore = bb->score;
for(i=0; i<NUM_SCORES; i++) { if (highscore_would_update(bb->score, bb->highscores, NUM_SCORES)) {
if(currentscore >= bb->highscores[i].score) { bb->dirty = true;
if(!position) { position = highscore_update(bb->score, bb->level, "",
position = i+1; bb->highscores, NUM_SCORES);
bb->dirty = true; if (position==0) {
} rb->splash(HZ*2, "New High Score");
templevel = bb->highscores[i].level;
tempscore = bb->highscores[i].score;
bb->highscores[i].level = currentlevel;
bb->highscores[i].score = currentscore;
currentlevel = templevel;
currentscore = tempscore;
}
} }
bubbles_displayscores(bb, position);
} }
}
return position;
}
/***************************************************************************** /*****************************************************************************
* bubbles_loadscores() loads the high scores saved file. * bubbles_loadscores() loads the high scores saved file.
******************************************************************************/ ******************************************************************************/
static void bubbles_loadscores(struct game_context* bb) { static void bubbles_loadscores(struct game_context* bb) {
int fd;
bb->dirty = false; bb->dirty = false;
/* clear high scores */ /* highlevel and highscores */
bb->highlevel = 0; highscore_load(SCORE_FILE, &bb->highlevel, NUM_SCORES+1);
rb->memset(bb->highscores, 0, sizeof(bb->highscores));
/* open scores file */ if( bb->highlevel.level >= NUM_LEVELS )
fd = rb->open(SCORE_FILE, O_RDONLY); bb->highlevel.level = NUM_LEVELS - 1;
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);
} }
/***************************************************************************** /*****************************************************************************
* bubbles_savescores() saves the high scores saved file. * bubbles_savescores() saves the high scores saved file.
******************************************************************************/ ******************************************************************************/
static void bubbles_savescores(struct game_context* bb) { static void bubbles_savescores(struct game_context* bb) {
int fd;
/* write out the high scores to the save file */ /* highlevel and highscores */
fd = rb->open(SCORE_FILE, O_WRONLY|O_CREAT); highscore_save(SCORE_FILE, &bb->highlevel, NUM_SCORES+1);
rb->write(fd, &bb->highlevel, sizeof(bb->highlevel));
rb->write(fd, bb->highscores, sizeof(bb->highscores));
rb->close(fd);
bb->dirty = false; 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, #define MARGIN 5
char * buffer, size_t buffer_len) static void bubbles_displayscores(struct game_context* bb, int position) {
{ int i, w, h;
struct game_context* bb = (struct game_context*)data; char str[30];
rb->snprintf(buffer, buffer_len, "#%02d: %d, Lvl %d",
selected_item+1,
bb->highscores[selected_item].score,
bb->highscores[selected_item].level);
return buffer;
}
static void bubbles_displayscores(struct game_context* bb) {
struct simplelist_info info;
rb->simplelist_info_init(&info, "High Scores", NUM_SCORES, (void*)bb);
info.hide_selection = true;
info.get_name = scores_get_name;
rb->simplelist_show_list(&info);
}
#ifdef HAVE_LCD_COLOR
rb->lcd_set_background(LCD_BLACK);
rb->lcd_set_foreground(LCD_WHITE);
#endif
rb->button_clear_queue();
rb->lcd_clear_display();
rb->lcd_setfont(FONT_UI);
rb->lcd_getstringsize("High Scores", &w, &h);
/* check wether it fits on screen */
if ((4*h + h*(NUM_SCORES-1) + MARGIN) > LCD_HEIGHT) {
rb->lcd_setfont(FONT_SYSFIXED);
rb->lcd_getstringsize("High Scores", &w, &h);
}
rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores");
rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score");
rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level");
for (i = 0; i<NUM_SCORES; i++)
{
#ifdef HAVE_LCD_COLOR
if(i == position) {
rb->lcd_set_foreground(LCD_RGBPACK(245,0,0));
}
#endif
rb->snprintf (str, sizeof (str), "%d)", i+1);
rb->lcd_putsxy (MARGIN,3*h + h*i, str);
rb->snprintf (str, sizeof (str), "%d", bb->highscores[i].score);
rb->lcd_putsxy (LCD_WIDTH/4-w/4,3*h + h*i, str);
rb->snprintf (str, sizeof (str), "%d", bb->highscores[i].level);
rb->lcd_putsxy (LCD_WIDTH*3/4-w/4,3*h + h*i, str);
if(i == position) {
#ifdef HAVE_LCD_COLOR
rb->lcd_set_foreground(LCD_WHITE);
#else
rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN, 3*h + h*(i+1)-1);
#endif
}
}
rb->lcd_update();
rb->button_get(true);
rb->lcd_setfont(FONT_SYSFIXED);
bubbles_setcolors();
}
/***************************************************************************** /*****************************************************************************
* bubbles_loadgame() loads the saved game and returns load success. * bubbles_loadgame() loads the saved game and returns load success.
@ -2400,7 +2391,7 @@ static int bubbles(struct game_context* bb) {
********************/ ********************/
MENUITEM_STRINGLIST(menu,"Bubbles Menu",NULL, MENUITEM_STRINGLIST(menu,"Bubbles Menu",NULL,
"Start New Game", "Resume Game", "Start New Game", "Resume Game",
"Level", "Display High Scores", "Playback Control", "Level", "High Scores", "Playback Control",
"Quit"); "Quit");
while(!startgame){ while(!startgame){
switch (rb->do_menu(&menu, NULL, NULL, false)) switch (rb->do_menu(&menu, NULL, NULL, false))
@ -2418,11 +2409,12 @@ static int bubbles(struct game_context* bb) {
break; break;
case 2: /* choose level */ case 2: /* choose level */
startlevel++; 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--; startlevel--;
break; break;
case 3: /* High scores */ case 3: /* High scores */
bubbles_displayscores(bb); bubbles_displayscores(bb, 0);
break; break;
case 4: /* Playback Control */ case 4: /* Playback Control */
playback_control(NULL); playback_control(NULL);
@ -2485,14 +2477,12 @@ static int bubbles(struct game_context* bb) {
enum plugin_status plugin_start(const void* parameter) { enum plugin_status plugin_start(const void* parameter) {
struct game_context bb; struct game_context bb;
bool exit = false; bool exit = false;
int position;
/* plugin init */ /* plugin init */
(void)parameter; (void)parameter;
/* end of plugin init */ /* end of plugin init */
/* load files */ /* load files */
rb->splash(0, "Loading...");
bubbles_loadscores(&bb); bubbles_loadscores(&bb);
rb->lcd_clear_display(); rb->lcd_clear_display();
@ -2507,15 +2497,12 @@ enum plugin_status plugin_start(const void* parameter) {
case BB_WIN: case BB_WIN:
rb->splash(HZ*2, "You Win!"); rb->splash(HZ*2, "You Win!");
/* record high level */ /* record high level */
if( NUM_LEVELS-1 > bb.highlevel) { if( NUM_LEVELS-1 > bb.highlevel.level) {
bb.highlevel = NUM_LEVELS-1; bb.highlevel.level = NUM_LEVELS-1;
bb.dirty = true; bb.dirty = true;
} }
/* record high score */ /* record high score */
if((position = bubbles_recordscore(&bb))) { bubbles_recordscore(&bb);
rb->splashf(HZ*2, "New high score #%d!", position);
}
break; break;
case BB_LOSE: case BB_LOSE:
@ -2525,15 +2512,12 @@ enum plugin_status plugin_start(const void* parameter) {
case BB_END: case BB_END:
if(!bb.resume) { if(!bb.resume) {
/* record high level */ /* record high level */
if(bb.level-1 > bb.highlevel) { if((int)bb.level-1 > bb.highlevel.level) {
bb.highlevel = bb.level-1; bb.highlevel.level = bb.level-1;
bb.dirty = true; bb.dirty = true;
} }
/* record high score */ /* record high score */
if((position = bubbles_recordscore(&bb))) { bubbles_recordscore(&bb);
rb->splashf(HZ*2, "New high score #%d!", position);
}
} }
break; break;