FS#12756 by Marek Salaba - update Czech translation
[maemo-rb.git] / apps / plugins / lib / highscore.c
blobff7a16622212350876170d86fe669ebb8f41926f
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, 0666);
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 #ifdef HAVE_LCD_COLOR
129 unsigned bgcolor = rb->lcd_get_background();
130 unsigned fgcolor = rb->lcd_get_foreground();
131 rb->lcd_set_background(LCD_BLACK);
132 rb->lcd_set_foreground(LCD_WHITE);
133 #endif
134 rb->lcd_clear_display();
136 rb->lcd_setfont(FONT_UI);
137 rb->lcd_getstringsize("High Scores", &w, &h);
138 /* check wether it fits on screen */
139 if ((4*h + h*(num_scores-1) + MARGIN) > LCD_HEIGHT) {
140 rb->lcd_setfont(FONT_SYSFIXED);
141 rb->lcd_getstringsize("High Scores", &w, &h);
143 rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores");
144 rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score");
146 if(show_level) {
147 rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level");
150 for (i = 0; i<num_scores; i++)
152 #ifdef HAVE_LCD_COLOR
153 if (i == position) {
154 rb->lcd_set_foreground(LCD_RGBPACK(245,0,0));
156 #endif
157 rb->lcd_putsxyf (MARGIN,3*h + h*i, "%d)", i+1);
158 rb->lcd_putsxyf (LCD_WIDTH/4-w/4,3*h + h*i, "%d", scores[i].score);
160 if(show_level) {
161 rb->lcd_putsxyf (LCD_WIDTH*3/4-w/4,3*h + h*i, "%d", scores[i].level);
164 if(i == position) {
165 #ifdef HAVE_LCD_COLOR
166 rb->lcd_set_foreground(LCD_WHITE);
167 #else
168 rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN*2, 3*h + h*(i+1) - 1);
169 #endif
172 rb->lcd_update();
173 rb->sleep(HZ/2);
174 rb->button_clear_queue();
175 rb->button_get(true);
176 rb->lcd_setfont(FONT_SYSFIXED);
177 #ifdef HAVE_LCD_COLOR
178 rb->lcd_set_background(bgcolor);
179 rb->lcd_set_foreground(fgcolor);
180 #endif
182 #else
183 struct scoreinfo {
184 struct highscore *scores;
185 int position;
186 bool show_level;
188 static const char* get_score(int selected, void * data,
189 char * buffer, size_t buffer_len)
191 struct scoreinfo *scoreinfo = (struct scoreinfo *) data;
192 int len;
193 len = rb->snprintf(buffer, buffer_len, "%c%d) %4d",
194 (scoreinfo->position == selected?'*':' '),
195 selected+1, scoreinfo->scores[selected].score);
197 if (scoreinfo->show_level)
198 rb->snprintf(buffer + len, buffer_len - len, " %d",
199 scoreinfo->scores[selected].level);
200 return buffer;
203 void highscore_show(int position, struct highscore *scores, int num_scores,
204 bool show_level)
206 struct simplelist_info info;
207 struct scoreinfo scoreinfo = {scores, position, show_level};
208 rb->simplelist_info_init(&info, "High Scores", num_scores, &scoreinfo);
209 if (position >= 0)
210 info.selection = position;
211 info.hide_selection = true;
212 info.get_name = get_score;
213 rb->simplelist_show_list(&info);
215 #endif /* HAVE_LCD_BITMAP */