summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nielsen Feltzing <linus@haxx.se>2005-06-08 13:47:46 +0000
committerLinus Nielsen Feltzing <linus@haxx.se>2005-06-08 13:47:46 +0000
commitfa148bb81260844a77ef2f988f46fb8584bfb3a2 (patch)
tree97221c629e47f8bc45a56e19654d1f5c6a1b965f
parent9415629f4c3cf7e81aa89e37bcd0b26c24103ed9 (diff)
downloadrockbox-fa148bb81260844a77ef2f988f46fb8584bfb3a2.tar.gz
rockbox-fa148bb81260844a77ef2f988f46fb8584bfb3a2.zip
A new API for saving highscores in game plugins
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6611 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/lib/SOURCES1
-rw-r--r--apps/plugins/lib/highscore.c99
-rw-r--r--apps/plugins/lib/highscore.h33
3 files changed, 133 insertions, 0 deletions
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 @@
1configfile.c 1configfile.c
2highscore.c
2#ifdef HAVE_LCD_BITMAP 3#ifdef HAVE_LCD_BITMAP
3gray_black_display.c 4gray_black_display.c
4gray_blockfuncs.c 5gray_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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "plugin.h"
20#include "highscore.h"
21
22static struct plugin_api *rb;
23
24void highscore_init(struct plugin_api* newrb)
25{
26 rb = newrb;
27}
28
29int highscore_save(char *filename, struct highscore *scores, int num_scores)
30{
31 int i;
32 int fd;
33 int rc;
34 char buf[80];
35
36 fd = rb->open(filename, O_WRONLY|O_CREAT);
37 if(fd < 0)
38 return -1;
39
40 for(i = 0;i < num_scores;i++)
41 {
42 rb->snprintf(buf, sizeof(buf)-1, "%s:%d:%d\n",
43 scores[i].name, scores[i].score, scores[i].level);
44 rc = rb->write(fd, buf, rb->strlen(buf));
45 if(rc < 0)
46 {
47 rb->close(fd);
48 return -2;
49 }
50 }
51 rb->close(fd);
52 return 0;
53}
54
55int highscore_load(char *filename, struct highscore *scores, int num_scores)
56{
57 int i;
58 int fd;
59 char buf[80];
60 char *name, *score, *level;
61 char *ptr;
62
63 fd = rb->open(filename, O_RDONLY);
64 if(fd < 0)
65 return -1;
66
67 rb->memset(scores, 0, sizeof(struct highscore)*num_scores);
68
69 i = -1;
70 while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores)
71 {
72 i++;
73
74 DEBUGF("%s\n", buf);
75 name = buf;
76 ptr = rb->strchr(buf, ':');
77 if ( !ptr )
78 continue;
79 *ptr = 0;
80 ptr++;
81
82 rb->strncpy(scores[i].name, name, sizeof(scores[i].name));
83
84 DEBUGF("%s\n", scores[i].name);
85 score = ptr;
86
87 ptr = rb->strchr(ptr, ':');
88 if ( !ptr )
89 continue;
90 *ptr = 0;
91 ptr++;
92
93 scores[i].score = rb->atoi(score);
94
95 level = ptr;
96 scores[i].level = rb->atoi(level);
97 }
98 return 0;
99}
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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#ifndef HIGHSCORE_H
20#define HIGHSCORE_H
21
22struct highscore
23{
24 char name[32];
25 int score;
26 int level;
27};
28
29void highscore_init(struct plugin_api* newrb);
30int highscore_save(char *filename, struct highscore *scores, int num_scores);
31int highscore_load(char *filename, struct highscore *scores, int num_scores);
32
33#endif