From fa148bb81260844a77ef2f988f46fb8584bfb3a2 Mon Sep 17 00:00:00 2001 From: Linus Nielsen Feltzing Date: Wed, 8 Jun 2005 13:47:46 +0000 Subject: A new API for saving highscores in game plugins git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6611 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugins/lib/SOURCES | 1 + apps/plugins/lib/highscore.c | 99 ++++++++++++++++++++++++++++++++++++++++++++ apps/plugins/lib/highscore.h | 33 +++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 apps/plugins/lib/highscore.c create mode 100644 apps/plugins/lib/highscore.h diff --git a/apps/plugins/lib/SOURCES b/apps/plugins/lib/SOURCES index 58356af1ec..f8ed798102 100644 --- a/apps/plugins/lib/SOURCES +++ b/apps/plugins/lib/SOURCES @@ -1,4 +1,5 @@ configfile.c +highscore.c #ifdef HAVE_LCD_BITMAP gray_black_display.c gray_blockfuncs.c diff --git a/apps/plugins/lib/highscore.c b/apps/plugins/lib/highscore.c new file mode 100644 index 0000000000..9fbcdf25a3 --- /dev/null +++ b/apps/plugins/lib/highscore.c @@ -0,0 +1,99 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "plugin.h" +#include "highscore.h" + +static struct plugin_api *rb; + +void highscore_init(struct plugin_api* newrb) +{ + rb = newrb; +} + +int highscore_save(char *filename, struct highscore *scores, int num_scores) +{ + int i; + int fd; + int rc; + char buf[80]; + + fd = rb->open(filename, O_WRONLY|O_CREAT); + if(fd < 0) + return -1; + + 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); + rc = rb->write(fd, buf, rb->strlen(buf)); + if(rc < 0) + { + rb->close(fd); + return -2; + } + } + rb->close(fd); + return 0; +} + +int highscore_load(char *filename, struct highscore *scores, int num_scores) +{ + int i; + int fd; + char buf[80]; + char *name, *score, *level; + char *ptr; + + fd = rb->open(filename, O_RDONLY); + if(fd < 0) + return -1; + + rb->memset(scores, 0, sizeof(struct highscore)*num_scores); + + i = -1; + while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores) + { + i++; + + DEBUGF("%s\n", buf); + name = buf; + ptr = rb->strchr(buf, ':'); + if ( !ptr ) + 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 ) + continue; + *ptr = 0; + ptr++; + + scores[i].score = rb->atoi(score); + + level = ptr; + scores[i].level = rb->atoi(level); + } + return 0; +} diff --git a/apps/plugins/lib/highscore.h b/apps/plugins/lib/highscore.h new file mode 100644 index 0000000000..ff192137f3 --- /dev/null +++ b/apps/plugins/lib/highscore.h @@ -0,0 +1,33 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef HIGHSCORE_H +#define HIGHSCORE_H + +struct highscore +{ + char name[32]; + int score; + int level; +}; + +void highscore_init(struct plugin_api* newrb); +int highscore_save(char *filename, struct highscore *scores, int num_scores); +int highscore_load(char *filename, struct highscore *scores, int num_scores); + +#endif -- cgit v1.2.3