2005-06-08 13:47:46 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Linus Nielsen Feltzing
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-06-08 13:47:46 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "highscore.h"
|
|
|
|
|
2009-07-18 15:16:24 +00:00
|
|
|
static bool highscore_updated = false;
|
|
|
|
|
2005-06-08 13:47:46 +00:00
|
|
|
int highscore_save(char *filename, struct highscore *scores, int num_scores)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
int rc;
|
|
|
|
char buf[80];
|
|
|
|
|
2009-07-18 15:16:24 +00:00
|
|
|
if(!highscore_updated)
|
|
|
|
return 1;
|
|
|
|
|
2005-06-08 13:47:46 +00:00
|
|
|
fd = rb->open(filename, O_WRONLY|O_CREAT);
|
|
|
|
if(fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for(i = 0;i < num_scores;i++)
|
|
|
|
{
|
2009-06-30 20:00:46 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d:%d:%s\n",
|
|
|
|
scores[i].score, scores[i].level, scores[i].name);
|
2005-06-08 13:47:46 +00:00
|
|
|
rc = rb->write(fd, buf, rb->strlen(buf));
|
|
|
|
if(rc < 0)
|
|
|
|
{
|
|
|
|
rb->close(fd);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->close(fd);
|
2009-07-18 15:16:24 +00:00
|
|
|
highscore_updated = false;
|
2005-06-08 13:47:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int highscore_load(char *filename, struct highscore *scores, int num_scores)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
char buf[80];
|
2009-06-30 20:00:46 +00:00
|
|
|
char *score, *level, *name;
|
2005-06-08 13:47:46 +00:00
|
|
|
|
2009-06-30 20:00:46 +00:00
|
|
|
rb->memset(scores, 0, sizeof(struct highscore)*num_scores);
|
2007-01-05 16:32:20 +00:00
|
|
|
|
2009-06-30 20:00:46 +00:00
|
|
|
fd = rb->open(filename, O_RDONLY);
|
2005-06-08 13:47:46 +00:00
|
|
|
if(fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-06-30 20:00:46 +00:00
|
|
|
i = 0;
|
|
|
|
while(rb->read_line(fd, buf, sizeof(buf)) > 0 && i < num_scores)
|
2005-06-08 13:47:46 +00:00
|
|
|
{
|
|
|
|
DEBUGF("%s\n", buf);
|
2009-06-30 20:00:46 +00:00
|
|
|
|
|
|
|
if ( !rb->settings_parseline(buf, &score, &level) )
|
2005-06-08 13:47:46 +00:00
|
|
|
continue;
|
2009-06-30 20:00:46 +00:00
|
|
|
if ( !rb->settings_parseline(level, &level, &name) )
|
2005-06-08 13:47:46 +00:00
|
|
|
continue;
|
2009-06-30 20:00:46 +00:00
|
|
|
|
2005-06-08 13:47:46 +00:00
|
|
|
scores[i].score = rb->atoi(score);
|
|
|
|
scores[i].level = rb->atoi(level);
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(scores[i].name, name, sizeof(scores[i].name));
|
2009-06-30 20:00:46 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
rb->close(fd);
|
2009-07-18 15:16:24 +00:00
|
|
|
highscore_updated = false;
|
2005-06-08 13:47:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-01-05 16:32:20 +00:00
|
|
|
|
2009-06-30 20:00:46 +00:00
|
|
|
int highscore_update(int score, int level, const char *name,
|
|
|
|
struct highscore *scores, int num_scores)
|
2007-01-05 16:32:20 +00:00
|
|
|
{
|
2009-06-30 20:00:46 +00:00
|
|
|
int pos;
|
|
|
|
struct highscore *entry;
|
|
|
|
|
|
|
|
if (!highscore_would_update(score, scores, num_scores))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pos = num_scores-1;
|
|
|
|
while (pos > 0 && score > scores[pos-1].score)
|
2007-01-05 16:32:20 +00:00
|
|
|
{
|
2009-06-30 20:00:46 +00:00
|
|
|
/* move down one */
|
|
|
|
rb->memcpy((void *)&scores[pos], (void *)&scores[pos-1],
|
|
|
|
sizeof(struct highscore));
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = scores + pos;
|
|
|
|
entry->score = score;
|
|
|
|
entry->level = level;
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(entry->name, name, sizeof(entry->name));
|
2009-06-30 20:00:46 +00:00
|
|
|
|
2009-07-18 15:16:24 +00:00
|
|
|
highscore_updated = true;
|
2009-06-30 20:00:46 +00:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool highscore_would_update(int score, struct highscore *scores,
|
|
|
|
int num_scores)
|
|
|
|
{
|
|
|
|
return (num_scores > 0) && (score > scores[num_scores-1].score);
|
2007-01-05 16:32:20 +00:00
|
|
|
}
|
2009-07-18 15:16:24 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
2010-02-22 13:45:24 +00:00
|
|
|
#define MARGIN 5
|
|
|
|
void highscore_show(int position, struct highscore *scores, int num_scores,
|
|
|
|
bool show_level)
|
2009-07-18 15:16:24 +00:00
|
|
|
{
|
|
|
|
int i, w, h;
|
|
|
|
char str[30];
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
2010-02-22 13:45:24 +00:00
|
|
|
unsigned bgcolor = rb->lcd_get_background();
|
|
|
|
unsigned fgcolor = rb->lcd_get_foreground();
|
2009-07-18 15:16:24 +00:00
|
|
|
rb->lcd_set_background(LCD_BLACK);
|
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
#endif
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
|
|
|
rb->lcd_getstringsize("High Scores", &w, &h);
|
|
|
|
/* check wether it fits on screen */
|
|
|
|
if ((4*h + h*(num_scores-1) + MARGIN) > LCD_HEIGHT) {
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
rb->lcd_getstringsize("High Scores", &w, &h);
|
|
|
|
}
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores");
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score");
|
2009-08-03 01:07:58 +00:00
|
|
|
|
|
|
|
if(show_level) {
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level");
|
|
|
|
}
|
2009-07-18 15:16:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i<num_scores; i++)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
if (i == position) {
|
|
|
|
rb->lcd_set_foreground(LCD_RGBPACK(245,0,0));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
rb->snprintf (str, sizeof (str), "%d)", i+1);
|
|
|
|
rb->lcd_putsxy (MARGIN,3*h + h*i, str);
|
|
|
|
rb->snprintf (str, sizeof (str), "%d", scores[i].score);
|
|
|
|
rb->lcd_putsxy (LCD_WIDTH/4-w/4,3*h + h*i, str);
|
2009-08-03 01:07:58 +00:00
|
|
|
|
|
|
|
if(show_level) {
|
|
|
|
rb->snprintf (str, sizeof (str), "%d", scores[i].level);
|
|
|
|
rb->lcd_putsxy (LCD_WIDTH*3/4-w/4,3*h + h*i, str);
|
|
|
|
}
|
|
|
|
|
2009-07-18 15:16:24 +00:00
|
|
|
if(i == position) {
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
#else
|
2010-02-22 13:45:24 +00:00
|
|
|
rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN*2, 3*h + h*(i+1) - 1);
|
2009-07-18 15:16:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->lcd_update();
|
2009-08-03 16:30:08 +00:00
|
|
|
rb->sleep(HZ/2);
|
|
|
|
rb->button_clear_queue();
|
2009-07-18 15:16:24 +00:00
|
|
|
rb->button_get(true);
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2010-02-22 13:45:24 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_background(bgcolor);
|
|
|
|
rb->lcd_set_foreground(fgcolor);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct scoreinfo {
|
|
|
|
struct highscore *scores;
|
|
|
|
int position;
|
|
|
|
bool show_level;
|
|
|
|
};
|
|
|
|
static const char* get_score(int selected, void * data,
|
|
|
|
char * buffer, size_t buffer_len)
|
|
|
|
{
|
|
|
|
struct scoreinfo *scoreinfo = (struct scoreinfo *) data;
|
|
|
|
int len;
|
|
|
|
len = rb->snprintf(buffer, buffer_len, "%c%d) %4d",
|
|
|
|
(scoreinfo->position == selected?'*':' '),
|
|
|
|
selected+1, scoreinfo->scores[selected].score);
|
|
|
|
|
|
|
|
if (scoreinfo->show_level)
|
|
|
|
rb->snprintf(buffer + len, buffer_len - len, " %d",
|
|
|
|
scoreinfo->scores[selected].level);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void highscore_show(int position, struct highscore *scores, int num_scores,
|
|
|
|
bool show_level)
|
|
|
|
{
|
|
|
|
struct simplelist_info info;
|
|
|
|
struct scoreinfo scoreinfo = {scores, position, show_level};
|
|
|
|
rb->simplelist_info_init(&info, "High Scores", num_scores, &scoreinfo);
|
|
|
|
if (position >= 0)
|
|
|
|
info.selection = position;
|
|
|
|
info.hide_selection = true;
|
|
|
|
info.get_name = get_score;
|
|
|
|
rb->simplelist_show_list(&info);
|
2009-07-18 15:16:24 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_LCD_BITMAP */
|