plugin: implement highscore_show for player and use it in rockblox.
[kugel-rb.git] / apps / plugins / lib / highscore.c
blob9ada06e8fefe62096f79b03546c317382894f218
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "highscore.h"
24 static bool highscore_updated = false;
26 int highscore_save(char *filename, struct highscore *scores, int num_scores)
28 int i;
29 int fd;
30 int rc;
31 char buf[80];
33 if(!highscore_updated)
34 return 1;
36 fd = rb->open(filename, O_WRONLY|O_CREAT);
37 if(fd < 0)
38 return -1;
40 for(i = 0;i < num_scores;i++)
42 rb->snprintf(buf, sizeof(buf), "%d:%d:%s\n",
43 scores[i].score, scores[i].level, scores[i].name);
44 rc = rb->write(fd, buf, rb->strlen(buf));
45 if(rc < 0)
47 rb->close(fd);
48 return -2;
51 rb->close(fd);
52 highscore_updated = false;
53 return 0;
56 int highscore_load(char *filename, struct highscore *scores, int num_scores)
58 int i;
59 int fd;
60 char buf[80];
61 char *score, *level, *name;
63 rb->memset(scores, 0, sizeof(struct highscore)*num_scores);
65 fd = rb->open(filename, O_RDONLY);
66 if(fd < 0)
67 return -1;
69 i = 0;
70 while(rb->read_line(fd, buf, sizeof(buf)) > 0 && i < num_scores)
72 DEBUGF("%s\n", buf);
74 if ( !rb->settings_parseline(buf, &score, &level) )
75 continue;
76 if ( !rb->settings_parseline(level, &level, &name) )
77 continue;
79 scores[i].score = rb->atoi(score);
80 scores[i].level = rb->atoi(level);
81 rb->strlcpy(scores[i].name, name, sizeof(scores[i].name));
82 i++;
84 rb->close(fd);
85 highscore_updated = false;
86 return 0;
89 int highscore_update(int score, int level, const char *name,
90 struct highscore *scores, int num_scores)
92 int pos;
93 struct highscore *entry;
95 if (!highscore_would_update(score, scores, num_scores))
96 return -1;
98 pos = num_scores-1;
99 while (pos > 0 && score > scores[pos-1].score)
101 /* move down one */
102 rb->memcpy((void *)&scores[pos], (void *)&scores[pos-1],
103 sizeof(struct highscore));
104 pos--;
107 entry = scores + pos;
108 entry->score = score;
109 entry->level = level;
110 rb->strlcpy(entry->name, name, sizeof(entry->name));
112 highscore_updated = true;
113 return pos;
116 bool highscore_would_update(int score, struct highscore *scores,
117 int num_scores)
119 return (num_scores > 0) && (score > scores[num_scores-1].score);
122 #ifdef HAVE_LCD_BITMAP
123 #define MARGIN 5
124 void highscore_show(int position, struct highscore *scores, int num_scores,
125 bool show_level)
127 int i, w, h;
128 char str[30];
129 #ifdef HAVE_LCD_COLOR
130 unsigned bgcolor = rb->lcd_get_background();
131 unsigned fgcolor = rb->lcd_get_foreground();
132 rb->lcd_set_background(LCD_BLACK);
133 rb->lcd_set_foreground(LCD_WHITE);
134 #endif
135 rb->lcd_clear_display();
137 rb->lcd_setfont(FONT_UI);
138 rb->lcd_getstringsize("High Scores", &w, &h);
139 /* check wether it fits on screen */
140 if ((4*h + h*(num_scores-1) + MARGIN) > LCD_HEIGHT) {
141 rb->lcd_setfont(FONT_SYSFIXED);
142 rb->lcd_getstringsize("High Scores", &w, &h);
144 rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores");
145 rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score");
147 if(show_level) {
148 rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level");
151 for (i = 0; i<num_scores; i++)
153 #ifdef HAVE_LCD_COLOR
154 if (i == position) {
155 rb->lcd_set_foreground(LCD_RGBPACK(245,0,0));
157 #endif
158 rb->snprintf (str, sizeof (str), "%d)", i+1);
159 rb->lcd_putsxy (MARGIN,3*h + h*i, str);
160 rb->snprintf (str, sizeof (str), "%d", scores[i].score);
161 rb->lcd_putsxy (LCD_WIDTH/4-w/4,3*h + h*i, str);
163 if(show_level) {
164 rb->snprintf (str, sizeof (str), "%d", scores[i].level);
165 rb->lcd_putsxy (LCD_WIDTH*3/4-w/4,3*h + h*i, str);
168 if(i == position) {
169 #ifdef HAVE_LCD_COLOR
170 rb->lcd_set_foreground(LCD_WHITE);
171 #else
172 rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN*2, 3*h + h*(i+1) - 1);
173 #endif
176 rb->lcd_update();
177 rb->sleep(HZ/2);
178 rb->button_clear_queue();
179 rb->button_get(true);
180 rb->lcd_setfont(FONT_SYSFIXED);
181 #ifdef HAVE_LCD_COLOR
182 rb->lcd_set_background(bgcolor);
183 rb->lcd_set_foreground(fgcolor);
184 #endif
186 #else
187 struct scoreinfo {
188 struct highscore *scores;
189 int position;
190 bool show_level;
192 static const char* get_score(int selected, void * data,
193 char * buffer, size_t buffer_len)
195 struct scoreinfo *scoreinfo = (struct scoreinfo *) data;
196 int len;
197 len = rb->snprintf(buffer, buffer_len, "%c%d) %4d",
198 (scoreinfo->position == selected?'*':' '),
199 selected+1, scoreinfo->scores[selected].score);
201 if (scoreinfo->show_level)
202 rb->snprintf(buffer + len, buffer_len - len, " %d",
203 scoreinfo->scores[selected].level);
204 return buffer;
207 void highscore_show(int position, struct highscore *scores, int num_scores,
208 bool show_level)
210 struct simplelist_info info;
211 struct scoreinfo scoreinfo = {scores, position, show_level};
212 rb->simplelist_info_init(&info, "High Scores", num_scores, &scoreinfo);
213 if (position >= 0)
214 info.selection = position;
215 info.hide_selection = true;
216 info.get_name = get_score;
217 rb->simplelist_show_list(&info);
219 #endif /* HAVE_LCD_BITMAP */