1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "highscore.h"
24 static bool highscore_updated
= false;
26 int highscore_save(char *filename
, struct highscore
*scores
, int num_scores
)
33 if(!highscore_updated
)
36 fd
= rb
->open(filename
, O_WRONLY
|O_CREAT
);
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
));
52 highscore_updated
= false;
56 int highscore_load(char *filename
, struct highscore
*scores
, int num_scores
)
61 char *score
, *level
, *name
;
63 rb
->memset(scores
, 0, sizeof(struct highscore
)*num_scores
);
65 fd
= rb
->open(filename
, O_RDONLY
);
70 while(rb
->read_line(fd
, buf
, sizeof(buf
)) > 0 && i
< num_scores
)
74 if ( !rb
->settings_parseline(buf
, &score
, &level
) )
76 if ( !rb
->settings_parseline(level
, &level
, &name
) )
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
));
85 highscore_updated
= false;
89 int highscore_update(int score
, int level
, const char *name
,
90 struct highscore
*scores
, int num_scores
)
93 struct highscore
*entry
;
95 if (!highscore_would_update(score
, scores
, num_scores
))
99 while (pos
> 0 && score
> scores
[pos
-1].score
)
102 rb
->memcpy((void *)&scores
[pos
], (void *)&scores
[pos
-1],
103 sizeof(struct highscore
));
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;
116 bool highscore_would_update(int score
, struct highscore
*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
)
128 #ifdef HAVE_LCD_COLOR
129 rb
->lcd_set_background(LCD_BLACK
);
130 rb
->lcd_set_foreground(LCD_WHITE
);
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 */
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
153 rb
->lcd_set_foreground(LCD_RGBPACK(245,0,0));
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 */
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
);
168 #ifdef HAVE_LCD_COLOR
169 rb
->lcd_set_foreground(LCD_WHITE
);
171 rb
->lcd_hline(MARGIN
, LCD_WIDTH
-MARGIN
, 3*h
+ h
*(i
+1));
177 rb
->button_clear_queue();
178 rb
->button_get(true);
179 rb
->lcd_setfont(FONT_SYSFIXED
);
181 #endif /* HAVE_LCD_BITMAP */