/
highscores.cpp
136 lines (109 loc) · 3.63 KB
/
highscores.cpp
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
#include "highscores.h"
bool hsComp(score *first, score *second)
{
return (*first > *second);
}
void highscore::reloadHighScore()
{
string playerNameTMP;
int scoreTMP;
// Clear highscore list first
this->highScoreData.clear();
// Initialize XML reader
XMLDocument xmlHighScore;
XMLElement *nodeTransversal;
xmlHighScore.LoadFile("score.xml");
// Get first child
nodeTransversal = xmlHighScore.FirstChildElement("hs");
playerNameTMP = string(nodeTransversal->FirstChildElement("name")->GetText());
scoreTMP = atoi(nodeTransversal->FirstChildElement("score")->GetText());
this->addScore(playerNameTMP, scoreTMP);
// Get all node
while(true)
{
nodeTransversal = nodeTransversal->NextSiblingElement("hs");
if(nodeTransversal)
{
playerNameTMP = string(nodeTransversal->FirstChildElement("name")->GetText());
scoreTMP = atoi(nodeTransversal->FirstChildElement("score")->GetText());
this->addScore(playerNameTMP, scoreTMP);
}
else
{
break;
}
}
// Sort highscore depend on its score
this->highScoreData.sort(hsComp);
}
void highscore::newHighScore(score *newHighScore)
{
// SAVE NEW SCORE DIRECTLY INTO XML FILE
// Load XML file
char scoreStr[10];
XMLDocument doc;
XMLElement *nodeTransversal;
doc.LoadFile("score.xml");
// Write its child first
XMLText *nameText = doc.NewText(newHighScore->readPlayerName().c_str());
XMLElement *name = doc.NewElement("name");
name->InsertEndChild(nameText);
sprintf(scoreStr, "%d", newHighScore->readScore());
XMLText *scoreText = doc.NewText(scoreStr);
XMLElement *score = doc.NewElement("score");
score->InsertEndChild(scoreText);
// Create new node
XMLElement* hs = doc.NewElement("hs");
hs->InsertEndChild(name);
hs->InsertEndChild(score);
doc.InsertEndChild(hs);
doc.SaveFile("score.xml");
}
void highscore::saveHighScore()
{
}
void highscore::addScore(string playerName, int scorePoint)
{
score *newScore = new score(playerName, scorePoint);
this->highScoreData.push_back(newScore);
}
highscore::~highscore()
{
// Delete all of score in highScoreData
list<score *>::iterator it = this->highScoreData.begin();
for(int i=0; i < this->highScoreData.size(); i++)
{
if((*it) != NULL)
{
delete (*it);
}
++it;
}
}
// Interface High Score part
interfaceHighScore::interfaceHighScore()
{
// Load background bitmap
this->hsBackground = load_bitmap("highscore.pcx", NULL);
// Load font
hsFont = load_font("font-highscore.pcx", pallete, NULL);
}
void interfaceHighScore::displayHighScore()
{
// Buffer hsBackground
BITMAP *buffer;
buffer = create_bitmap(640, 480);
draw_sprite(buffer, this->hsBackground, 0, 0);
// Display highscore title.
textout_centre_ex(buffer, this->hsFont, "SKOR TERTINGGI", 320, 50, makecol(0,0,0), -1);
// Iterate highScoreData, display each name and score
list<score *>::iterator it = this->highScoreData.begin();
for(int i=0; i < this->highScoreData.size(); i++)
{
// Display each name
textprintf_ex(buffer, this->hsFont, HIGHSCORE_ITEM_START_X, HIGHSCORE_ITEM_START_Y + i * HIGHSCORE_ITEM_HEIGHT, makecol(255,255,255), -1, (*it)->readPlayerName().c_str());
textprintf_ex(buffer, this->hsFont, HIGHSCORE_ITEM_START_X + HIGHSCORE_SCORENUMBER, HIGHSCORE_ITEM_START_Y + i * HIGHSCORE_ITEM_HEIGHT, makecol(255,255,255), -1, "%05d", (*it)->readScore());
++it;
}
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
}