summary refs log tree commit diff stats
path: root/hslist.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hslist.cpp')
-rw-r--r--hslist.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/hslist.cpp b/hslist.cpp new file mode 100644 index 0000000..d2901b4 --- /dev/null +++ b/hslist.cpp
@@ -0,0 +1,85 @@
1#include "includes.h"
2
3SDL_Surface* HighscoreList::render()
4{
5 SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0);
6 Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255);
7 SDL_FillRect(tmp, NULL, bgColor);
8 SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, bgColor);
9 TTF_Font* posFont = loadFont(40);
10 TTF_Font* dataFont = loadFont(25);
11 SDL_Color fontColor = {0, 0, 0, 0};
12
13 for (int i=0; i<hslist.size(); i++)
14 {
15 Highscore h = hslist[i];
16
17 int posw, posh;
18 char pos[3]; // 2 max characters in rank plus the colon at the end
19 sprintf(pos, "%d:", h.getRank());
20 TTF_SizeText(posFont, pos, &posw, &posh);
21 SDL_Rect posSpace = {0, (i+1)*40, posw, posh};
22 SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace);
23
24 int namew, nameh;
25 char name[26]; // 25 max characters in username plus the space at the beginning
26 sprintf(name, " %s", h.getName());
27 TTF_SizeText(dataFont, name, &namew, &nameh);
28 SDL_Rect nameSpace = {posw, (i+1)*40+((posh/2)-(nameh/2)), namew, nameh};
29 SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, tmp, &nameSpace);
30
31 int lvlw, lvlh;
32 char lvl[10]; // 10 max characters in level (based off the fact that 2^32-1 is 10 characters long, and is the highest int)
33 sprintf(lvl, "%d", h.getLevel());
34 TTF_SizeText(dataFont, lvl, &lvlw, &lvlh);
35 SDL_Rect lvlSpace = {480-lvlw, (i+1)*40+((posh/2)-(nameh/2)), lvlw, lvlh};
36 SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, &lvlSpace);
37 }
38
39 return tmp;
40}
41
42std::vector<Highscore> HighscoreList::getLocalHighscores()
43{
44 std::vector<Highscore> temp = std::vector<Highscore>();
45
46 std::ifstream exists(getDataFile());
47 if (exists)
48 {
49 FILE* hslist = fopen(getDataFile(), "r");
50 int scores;
51 fscanf(hslist, "%d%*c", &scores);
52
53 for (int i=0; i<scores; i++)
54 {
55 int namelen;
56 char namelens[4];
57 char* name = (char*) calloc(25, sizeof(char));
58 int score;
59
60 fscanf(hslist, "%d", &namelen);
61 sprintf(namelens, "%%%dc", namelen);
62 fscanf(hslist, namelens, name);
63 fscanf(hslist, "%d%*c", &score);
64
65 Highscore h = Highscore(name, score);
66 h.setRank(i+1);
67
68 temp.push_back(h);
69 }
70
71 fclose(hslist);
72 }
73
74 return temp;
75}
76
77std::vector<Highscore> HighscoreList::getGlobalHighscores()
78{
79
80}
81
82LocalHighscoreList::LocalHighscoreList()
83{
84 this->hslist = getLocalHighscores();
85}