/
CardHandler.cpp
executable file
·331 lines (274 loc) · 9.72 KB
/
CardHandler.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "CardHandler.h"
CardHandler::CardHandler(std::string mediaPath, std::string storyPath, SDL_Renderer* r)
:allStories(StoryHandler(r))
{
readXml(mediaPath, r);
readXmlSettings(xmlSettings, r);
currentCards = catCard[0];
allStories.readXml(storyPath, r, swedishNames, englishNames);
theMenu = Menu(SCREEN_HEIGHT/22, r, vecCat, swedishNames, englishNames);
addedTime = 2.0;
isStory = false;
}
std::vector<Card*> CardHandler::getCurrentCard()
{
return currentCards;
}
std::vector<Card*> CardHandler::getAll()
{
return catCard[0];
}
Menu CardHandler::getMenu()
{
return theMenu;
}
// loops over all the cards and then loops over all the events and handles them
void CardHandler::HandleEvents(bool &lang)
{
for (int indo = 0; indo < currentCards.size(); indo++)
{
for (int indi = 0; indi < frameEvents.size(); indi++) {
//removing an event if used so only one card can be affected by a specific event
if (theMenu.handleEvent(&frameEvents[indi], lang, isStory, allStories.getFlipp())) {
currentCards = catCard[theMenu.getCat()];
allStories.setStory(theMenu.getStory());
frameEvents.erase(frameEvents.begin() + indi);
addedTime = 2.0;
}
else if(!isStory && currentCards[indo]->handleEvent(&frameEvents[indi])) {
frameEvents.erase(frameEvents.begin() + indi);
addedTime = 2.0;
}
else if (isStory && allStories.handleEvent(&frameEvents[indi])) {
frameEvents.erase(frameEvents.begin() + indi);
addedTime = 2.0;
}
}
}
}
std::vector<SDL_Event> CardHandler::getFrameEvents()
{
return frameEvents;
}
void CardHandler::addEvent(SDL_Event e)
{
frameEvents.push_back(e);
}
bool CardHandler::readXml(std::string filePath, SDL_Renderer* r)
{
// getting the string through an ifstream
std::ifstream ifs(filePath, std::ios::in);
if(!ifs.is_open())
{
return false;
}
// making it the right format (char) for the parsing to xml doc
std::string xml_str;
xml_str.assign(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
// creating a xml_document and parsing the xml-file, making a DOM-tree
rapidxml::xml_document<> doc;
try
{
doc.parse<0>((char*)xml_str.c_str());
}
catch (...)
{
return false;
}
// accesing the first node in the file
rapidxml::xml_node<>* content = doc.first_node("content");
///--------CATEGORY----------///
// accesing all category nodes in content
for (rapidxml::xml_node<>* second = content->first_node("category"); second <= content->last_node("category"); second = second->next_sibling())
{
std::string cName = second->first_attribute("name")->value();
// Indicra alla variabler som category behöver
std::string texSv;
std::string texEn;
std::string catPath;
std::stringstream ss1;
std::stringstream ss2;
std::stringstream ss3;
ss1 << second->first_attribute("tex_se")->value();
getline( ss1, texSv);
ss2 << second->first_attribute("tex_en")->value();
getline( ss2, texEn);
ss3 << second->first_attribute("path")->value();
ss3 >> catPath;
vecCat.push_back(Category(cName, texSv, texEn, catPath));
catCard.push_back(std::vector<Card*>());
}
///--------------MEDIA--------------///
// accesing all media nodes
for (rapidxml::xml_node<>* second = content->first_node("media"); second; second = second->next_sibling())
{
// writes out inforamtion about the media to the terminal and creates variables to use for creation of cards
std::string mediaPath = second->first_attribute("path")->value();
std::stringstream ss;
int scaleExp;
ss << second->first_attribute("scale_exp")->value();
ss >> scaleExp;
std::vector<std::string> cardCat;
std::string seHeader;
std::string seText;
std::string enHeader;
std::string enText;
std::stringstream sSv;
std::stringstream sEn;
for (rapidxml::xml_node<>* inside = second->first_node(); inside ; inside = inside->next_sibling())
{
std::string b = inside->name();
if (b == "category")
{
std::string cat = inside->first_attribute()->value();
cardCat.push_back(cat);
}
else if (b == "se")
{
seHeader = inside->first_node()->value() ;
sSv << inside->first_node()->next_sibling()->value();
std::string get;
getline(sSv, get);
while (sSv) {
seText.append(get);
//seText.append("\n\n");
getline(sSv, get);
}
}
else
{
enHeader = inside->first_node()->value() ;
sEn << inside->first_node()->next_sibling()->value();
std::string get;
getline(sEn, get);
while (sEn) {
enText.append(get);
//enText.append("\n\n");
getline(sEn, get);
}
}
}
// create card with variables
glm::vec3 position = glm::vec3(rand() % rand() % (SCREEN_WIDTH /2) + (SCREEN_WIDTH /4),rand() % (SCREEN_HEIGHT /2) + (SCREEN_HEIGHT /4),0);
glm::vec2 velocity;
int random = rand() % 4;
int fact = 100;
if (random == 0)
{
velocity = glm::vec2(fact*(rand() % 4 + 1) , fact*(rand() % 4 + 1));
}
else if (random == 1)
{
velocity = glm::vec2(-fact*(rand() % 4 + 1), fact*(rand() % 4 + 1));
}
else if (random == 2)
{
velocity = glm::vec2(fact*(rand() % 4 + 1) , -fact*(rand() % 4 + 1));
}
else
{
velocity = glm::vec2(-fact*(rand() % 4 + 1), -fact*(rand() % 4 + 1));
}
std::vector<std::string> svCat;
std::vector<std::string> enCat;
for (int i = 0; i < cardCat.size(); i++) {
for (int j = 0; j < vecCat.size(); j++) {
if (vecCat[j].getCatName() == cardCat[i]) {
svCat.push_back(vecCat[j].getTextSv());
enCat.push_back(vecCat[j].getTextEn());
}
}
}
PhotoCard* newCard = new PhotoCard(svCat, enCat, seHeader, seText, enHeader, enText, position, velocity, mediaPath, r);
// adding the card to categories vector
for (int i = 0; i < cardCat.size(); i++) {
for (int j = 0; j < vecCat.size(); j++) {
if (vecCat[j].getCatName() == cardCat[i]) {
catCard[j].push_back(newCard);
}
}
}
}
return true;
}
bool CardHandler::readXmlSettings(std::string filePath, SDL_Renderer* r)
{
// getting the string through an ifstream
std::ifstream ifs(filePath, std::ios::in);
if(!ifs.is_open())
{
return false;
}
// making it the right format (char) for the parsing to xml doc
std::string xml_str;
xml_str.assign(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
// creating a xml_document and parsing the xml-file, making a DOM-tree
rapidxml::xml_document<> doc;
try
{
doc.parse<0>((char*)xml_str.c_str());
}
catch (...)
{
return false;
}
// accesing the first node in the file
rapidxml::xml_node<>* content = doc.first_node("content");
///-------SETTINGS------------///
rapidxml::xml_node<>* second = content->first_node("CardHandler");
// Backgorundcolors
unsigned int bColorR;
std::stringstream sR;
sR << std::hex << second->first_attribute("background_color_r")->value();
sR >> std::hex >> bColorR;
backgorundColor[0] = bColorR;
unsigned int bColorG;
std::stringstream sG;
sG << std::hex << second->first_attribute("background_color_g")->value();
sG >> std::hex >> bColorG;
backgorundColor[1] = bColorG;
unsigned int bColorB;
std::stringstream sB;
sB << std::hex << second->first_attribute("background_color_b")->value();
sB >> std::hex >> bColorB;
backgorundColor[2] = bColorB;
unsigned int bColorA;
std::stringstream sA;
sA << std::hex << second->first_attribute("background_color_a")->value() << std::hex;
sA >> std::hex >>bColorA;
backgorundColor[3] = bColorA;
return true;
}
void CardHandler::changeCat(float t) {
addedTime += t;
if (static_cast<int>(addedTime)%20 == 0) {
isStory = false;
theMenu.changeCategory();
currentCards = catCard[theMenu.getCat()];
allStories.resetStory();
addedTime = 2.0;
}
}
bool compCard( Card* a, Card* b) {return a->getLifeTime() > b->getLifeTime(); }
void CardHandler::sort(){
std::sort(currentCards.begin(), currentCards.end(), compCard);
}
void CardHandler::render(SDL_Renderer* r, bool swede) {
if (!isStory) {
sort();
for (int i = currentCards.size() - 1; i >= 0; i--) {
if (currentCards[i]->getLifeTime() < clock()) {
currentCards[i]->move(0.001); // (start - clock())/(CLOCKS_PER_SEC*2)
}
currentCards[i]->render(r, swede); // theTextures[theCards[i].texIndex]
}
}
else {
allStories.render(r, swede);
}
theMenu.renderMenu(r, swede, isStory);
}
void CardHandler::clearEvents()
{
frameEvents.clear();
}