1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include "topscores.h"
#include "storage.h"
#include "utils.h"
#include "font.h"
#include "convert.h"
#include "messages.h"
TopScores::TopScores()
{
Storage *storage = getStorage();
for (int i = 0; i < MAX_SCORES; i++) {
int score = storage->get(L"top_score_" + toString(i), -1);
if (score < 0)
break;
std::wstring name = storage->get(L"top_name_" + toString(i), L"");
add(name, score);
}
modifed = false;
}
TopScores::~TopScores()
{
save();
}
int TopScores::add(const std::wstring &name, int score)
{
if (score >= getMaxScore() || (scores.size() < 1)) {
if (! isFull()) {
Entry e = { name, score };
scores.push_back(e);
modifed = true;
return scores.size() - 1;
}
return -1;
}
int pos = 0;
for (ScoresList::iterator i = scores.begin(); i != scores.end(); i++) {
Entry &e = *i;
if (e.score > score) {
Entry ne = { name, score };
scores.insert(i, ne);
modifed = true;
break;
}
pos++;
}
while (scores.size() > MAX_SCORES) {
modifed = true;
scores.erase(--scores.end());
}
return modifed ? pos : -1;
}
void TopScores::save()
{
if (! modifed)
return;
Storage *storage = getStorage();
int no = 0;
for (ScoresList::iterator i = scores.begin(); i != scores.end(); i++) {
Entry &e = *i;
storage->set(L"top_name_" + toString(no), e.name);
storage->set(L"top_score_" + toString(no), e.score);
no++;
}
storage->flush();
modifed = false;
}
TopScores::ScoresList& TopScores::getScores()
{
return scores;
}
int TopScores::getMaxScore()
{
if (scores.size() < 1)
return -1;
ScoresList::iterator i = scores.end();
i--;
return (*i).score;
}
class ScoresWindow: public Window
{
public:
ScoresWindow(int x, int y, TopScores *scores, int highlight);
};
ScoresWindow::ScoresWindow(int x, int y, TopScores *scores, int highlight):
Window(x, y, 320, 350, L"blue.bmp")
{
Font titleFont(L"DejaVuSans.ttf", 26);
Font entryFont(L"DejaVuSans.ttf", 14);
Font timeFont(L"DejaVuSans.ttf", 14);
std::wstring txt = msg(L"topScores");
int w = titleFont.getWidth(txt);
titleFont.draw(background, (320 - w) / 2, 15, 255,255,0, true, txt);
TopScores::ScoresList &list = scores->getScores();
int no = 1;
int pos = 70;
for (TopScores::ScoresList::iterator i = list.begin();
i != list.end(); i++)
{
TopScores::Entry &e = *i;
std::wstring s(toString(no) + L".");
int w = entryFont.getWidth(s);
int c = ((no - 1) == highlight) ? 0 : 255;
entryFont.draw(background, 30 - w, pos, 255,255,c, true, s);
SDL_Rect rect = { 40, pos-20, 180, 40 };
SDL_SetClipRect(background, &rect);
entryFont.draw(background, 40, pos, 255,255,c, true, e.name);
SDL_SetClipRect(background, NULL);
s = secToStr(e.score);
w = timeFont.getWidth(s);
timeFont.draw(background, 305-w, pos, 255,255,c, true, s);
pos += 20;
no++;
}
}
void showScoresWindow(Area *parentArea, TopScores *scores, int highlight)
{
Area area;
Font font(L"DejaVuSans.ttf", 16);
area.add(parentArea);
area.add(new ScoresWindow(240, 125, scores, highlight));
ExitCommand exitCmd(area);
area.add(new Button(348, 430, 90, 25, &font, 255,255,0, L"blue.bmp",
msg(L"ok"), &exitCmd));
area.add(new KeyAccel(SDLK_ESCAPE, &exitCmd));
area.run();
}
std::wstring enterNameDialog(Area *parentArea)
{
Area area;
Font font(L"DejaVuSans.ttf", 16);
area.add(parentArea);
area.add(new Window(170, 280, 460, 100, L"blue.bmp"));
Storage *storage = getStorage();
std::wstring name = storage->get(L"lastName", msg(L"anonymous"));
area.add(new Label(&font, 180, 300, 255,255,0, msg(L"enterName")));
area.add(new InputField(350, 300, 270, 26, L"blue.bmp", name, 20,
255,255,0, &font));
ExitCommand exitCmd(area);
area.add(new Button(348, 340, 90, 25, &font, 255,255,0, L"blue.bmp",
msg(L"ok"), &exitCmd));
area.add(new KeyAccel(SDLK_ESCAPE, &exitCmd));
area.add(new KeyAccel(SDLK_RETURN, &exitCmd));
area.run();
storage->set(L"lastName", name);
return name;
}
|