calendar: fix bug when add new event text longer than last entry.
[kugel-rb.git] / apps / plugins / lib / highscore.c
blob20e472f9dc12db78efafb1441393dec399403efc
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 void highscore_show(int position, struct highscore *scores, int num_scores, bool show_level)
125 int i, w, h;
126 char str[30];
127 #define MARGIN 5
128 #ifdef HAVE_LCD_COLOR
129 rb->lcd_set_background(LCD_BLACK);
130 rb->lcd_set_foreground(LCD_WHITE);
131 #endif
132 rb->lcd_clear_display();
134 rb->lcd_setfont(FONT_UI);
135 rb->lcd_getstringsize("High Scores", &w, &h);
136 /* check wether it fits on screen */
137 if ((4*h + h*(num_scores-1) + MARGIN) > LCD_HEIGHT) {
138 rb->lcd_setfont(FONT_SYSFIXED);
139 rb->lcd_getstringsize("High Scores", &w, &h);
141 rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores");
142 rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score");
144 /* Decide whether to display the level column or not */
145 if(show_level) {
146 rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level");
149 for (i = 0; i<num_scores; i++)
151 #ifdef HAVE_LCD_COLOR
152 if (i == position) {
153 rb->lcd_set_foreground(LCD_RGBPACK(245,0,0));
155 #endif
156 rb->snprintf (str, sizeof (str), "%d)", i+1);
157 rb->lcd_putsxy (MARGIN,3*h + h*i, str);
158 rb->snprintf (str, sizeof (str), "%d", scores[i].score);
159 rb->lcd_putsxy (LCD_WIDTH/4-w/4,3*h + h*i, str);
161 /* Decide whether to display the level column or not */
162 if(show_level) {
163 rb->snprintf (str, sizeof (str), "%d", scores[i].level);
164 rb->lcd_putsxy (LCD_WIDTH*3/4-w/4,3*h + h*i, str);
167 if(i == position) {
168 #ifdef HAVE_LCD_COLOR
169 rb->lcd_set_foreground(LCD_WHITE);
170 #else
171 rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN, 3*h + h*(i+1));
172 #endif
175 rb->lcd_update();
176 rb->sleep(HZ/2);
177 rb->button_clear_queue();
178 rb->button_get(true);
179 rb->lcd_setfont(FONT_SYSFIXED);
181 #endif /* HAVE_LCD_BITMAP */