summaryrefslogtreecommitdiff
path: root/apps/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins')
-rw-r--r--apps/plugins/lib/highscore.c98
-rw-r--r--apps/plugins/lib/highscore.h52
-rw-r--r--apps/plugins/rockblox.c26
3 files changed, 113 insertions, 63 deletions
diff --git a/apps/plugins/lib/highscore.c b/apps/plugins/lib/highscore.c
index 9d3b19e..e8e1c88 100644
--- a/apps/plugins/lib/highscore.c
+++ b/apps/plugins/lib/highscore.c
@@ -34,8 +34,8 @@ int highscore_save(char *filename, struct highscore *scores, int num_scores)
for(i = 0;i < num_scores;i++)
{
- rb->snprintf(buf, sizeof(buf)-1, "%s:%d:%d\n",
- scores[i].name, scores[i].score, scores[i].level);
+ rb->snprintf(buf, sizeof(buf), "%d:%d:%s\n",
+ scores[i].score, scores[i].level, scores[i].name);
rc = rb->write(fd, buf, rb->strlen(buf));
if(rc < 0)
{
@@ -52,72 +52,62 @@ int highscore_load(char *filename, struct highscore *scores, int num_scores)
int i;
int fd;
char buf[80];
- char *name, *score, *level;
- char *ptr;
+ char *score, *level, *name;
- fd = rb->open(filename, O_RDONLY);
+ rb->memset(scores, 0, sizeof(struct highscore)*num_scores);
- rb->memset(scores, 0, sizeof(struct highscore)*(num_scores+1));
-
+ fd = rb->open(filename, O_RDONLY);
if(fd < 0)
return -1;
- i = -1;
- while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores)
+ i = 0;
+ while(rb->read_line(fd, buf, sizeof(buf)) > 0 && i < num_scores)
{
- i++;
-
DEBUGF("%s\n", buf);
- name = buf;
- ptr = rb->strchr(buf, ':');
- if ( !ptr )
+
+ if ( !rb->settings_parseline(buf, &score, &level) )
continue;
- *ptr = 0;
- ptr++;
-
- rb->strncpy(scores[i].name, name, sizeof(scores[i].name));
-
- DEBUGF("%s\n", scores[i].name);
- score = ptr;
-
- ptr = rb->strchr(ptr, ':');
- if ( !ptr )
+ if ( !rb->settings_parseline(level, &level, &name) )
continue;
- *ptr = 0;
- ptr++;
-
+
scores[i].score = rb->atoi(score);
-
- level = ptr;
scores[i].level = rb->atoi(level);
- }
+ rb->strncpy(scores[i].name, name, sizeof(scores[i].name)-1);
+ i++;
+ }
+ rb->close(fd);
return 0;
}
-int highscore_update(int score, int level, struct highscore *scores, int num_scores)
+int highscore_update(int score, int level, const char *name,
+ struct highscore *scores, int num_scores)
{
- int i, j;
- int new = 0;
-
- /* look through the scores and see if this one is in the top ones */
- for(i = num_scores-1;i >= 0; i--)
+ int pos;
+ struct highscore *entry;
+
+ if (!highscore_would_update(score, scores, num_scores))
+ return -1;
+
+ pos = num_scores-1;
+ while (pos > 0 && score > scores[pos-1].score)
{
- if ((score > scores[i].score))
- {
- /* Move the rest down one... */
- if (i > 0)
- {
- for (j=1; j<=i; j++)
- {
- rb->memcpy((void *)&scores[j-1], (void *)&scores[j], sizeof(struct highscore));
- }
- }
- scores[i].score = score;
- scores[i].level = level;
- /* Need to sort out entering a name... maybe old three letter arcade style */
- new = 1;
- break;
- }
- }
- return new;
+ /* move down one */
+ rb->memcpy((void *)&scores[pos], (void *)&scores[pos-1],
+ sizeof(struct highscore));
+ pos--;
+ }
+
+ entry = scores + pos;
+ entry->score = score;
+ entry->level = level;
+ rb->strncpy(entry->name, name, sizeof(entry->name));
+ entry->name[sizeof(entry->name)-1] = '\0';
+
+ return pos;
+}
+
+bool highscore_would_update(int score, struct highscore *scores,
+ int num_scores)
+{
+ return (num_scores > 0) && (score > scores[num_scores-1].score);
}
diff --git a/apps/plugins/lib/highscore.h b/apps/plugins/lib/highscore.h
index db09172..a38a6f7 100644
--- a/apps/plugins/lib/highscore.h
+++ b/apps/plugins/lib/highscore.h
@@ -21,6 +21,8 @@
#ifndef HIGHSCORE_H
#define HIGHSCORE_H
+/* see rockblox.c for the example of usage. */
+
struct highscore
{
char name[32];
@@ -28,8 +30,56 @@ struct highscore
int level;
};
+/* Saves the scores to a file
+ * - filename: name of the file to write the data to
+ * - scores : scores to store
+ * - num_scores: number of the elements in the array 'scores'
+ * Returns 0 on success or a negative value if an error occures
+ */
int highscore_save(char *filename, struct highscore *scores, int num_scores);
+
+/* Reads the scores from a file. The file must be a text file, each line
+ * represents a score entry.
+ *
+ * - filename: name of the file to read the data from
+ * - scores : where to put the read data
+ * - num_scores: max number of the scores to read (array capacity)
+ *
+ * Returns 0 on success or a negative value if an error occures
+ */
int highscore_load(char *filename, struct highscore *scores, int num_scores);
-int highscore_update(int score, int level, struct highscore *scores, int num_scores);
+
+/* Inserts score and level into array of struct highscore in the
+ * descending order of scores, i.e. higher scores are at lower array
+ * indexes.
+ *
+ * - score : the new score value to insert
+ * - level : the game level at which the score was reached
+ * - name : the name of the new entry (whatever it means)
+ * - scores: the array of scores to insert the new value into
+ * - num_scores: number of elements in 'scores'
+ *
+ * Returns the 0-based position of the newly inserted score if it was
+ * inserted. Returns a negative value if the score was not inserted
+ * (i.e. it was less than the lowest score in the array).
+ */
+int highscore_update(int score, int level, const char *name,
+ struct highscore *scores, int num_scores);
+
+/* Checks whether the new score would be inserted into the score table.
+ * This function can be used to find out whether a score with the given
+ * value would be inserted into the score table. If yes, the program
+ * can collect the name of the entry from the user (if it's done that
+ * way) and then really update the score table with 'highscore_update'.
+ *
+ * - score : the score value to check
+ * - scores: the array of existing scores
+ * - num_scores: number of elements in 'scores'
+ *
+ * Returns true iff the given score would be inserted into the score
+ * table by highscore_update.
+ */
+bool highscore_would_update(int score, struct highscore *scores,
+ int num_scores);
#endif
diff --git a/apps/plugins/rockblox.c b/apps/plugins/rockblox.c
index 7aaf26f..9b49877 100644
--- a/apps/plugins/rockblox.c
+++ b/apps/plugins/rockblox.c
@@ -723,7 +723,7 @@ figures[BLOCKS_NUM] = {
#define MAX_HIGH_SCORES 5
/* Default High Scores... */
-struct highscore Highest[MAX_HIGH_SCORES];
+struct highscore highest[MAX_HIGH_SCORES];
/* get random number from (0) to (range-1) */
static int t_rand (int range)
@@ -776,10 +776,11 @@ static void show_highscores (void)
int i;
char str[25]; /* for strings */
- for (i = MAX_HIGH_SCORES-1; i>=0; i--)
+ for (i = 0; i<MAX_HIGH_SCORES; i++)
{
- rb->snprintf (str, sizeof (str), "%06d" _SPACE "L%1d",Highest[i].score, Highest[i].level);
- rb->lcd_putsxy (HIGH_LABEL_X, HIGH_SCORE_Y + (10 * ((MAX_HIGH_SCORES-1) - i)), str);
+ rb->snprintf (str, sizeof (str), "%06d" _SPACE "L%1d",
+ highest[i].score, highest[i].level);
+ rb->lcd_putsxy (HIGH_LABEL_X, HIGH_SCORE_Y + (10 * i), str);
}
}
#endif
@@ -831,8 +832,17 @@ fail:
}
static void init_rockblox (bool resume)
{
- highscore_update(rockblox_status.score, rockblox_status.level, Highest,
- MAX_HIGH_SCORES);
+ char score_name[50];
+ struct tm* tm;
+
+ tm = rb->get_time();
+ rb->snprintf(score_name, sizeof(score_name), "%04d%02d%02d %02d%02d%02d",
+ tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+ highscore_update(rockblox_status.score, rockblox_status.level,
+ score_name, highest, MAX_HIGH_SCORES);
+
#ifdef HAVE_LCD_BITMAP
rb->lcd_bitmap (rockblox_background, 0, 0, LCD_WIDTH, LCD_HEIGHT);
#else /* HAVE_LCD_CHARCELLS */
@@ -1332,7 +1342,7 @@ enum plugin_status plugin_start (const void *parameter)
rb->srand (*rb->current_tick);
/* Load HighScore if any */
- highscore_load(HIGH_SCORE,Highest,MAX_HIGH_SCORES);
+ highscore_load(HIGH_SCORE, highest, MAX_HIGH_SCORES);
#if LCD_DEPTH > 1
rb->lcd_set_backdrop(NULL);
@@ -1357,7 +1367,7 @@ enum plugin_status plugin_start (const void *parameter)
pgfx_release();
#endif
/* Save user's HighScore */
- highscore_save(HIGH_SCORE,Highest,MAX_HIGH_SCORES);
+ highscore_save(HIGH_SCORE, highest, MAX_HIGH_SCORES);
backlight_use_settings(); /* backlight control in lib/helper.c */
dump_resume();