-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelMenu.cpp
executable file
·125 lines (102 loc) · 4.42 KB
/
LevelMenu.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
#include "LevelMenu.hpp"
LevelMenu::LevelMenu() {
open = true;
actualLvl = 0;
forward.setPosition(0,0);
forward.setSize(100,100);
forward.setTextResizeText(" > ");
backward.setPosition(0,0);
backward.setSize(100,100);
backward.setTextResizeText(" < ");
centralImage.setText("");
}
LevelMenu::~LevelMenu(){}
int LevelMenu::display(sf::RenderWindow* window){
forward.setSize(100,100);
forward.setPosition(window->getSize().x - backward.getSize().x -10, window->getSize().y/2);
backward.setSize(100,100);
backward.setPosition(0 +10, window->getSize().y/2);
centralImage.setSize(window->getSize().x*3/4, window->getSize().y*3/4);
centralImage.setPosition(window->getSize().x/2 - centralImage.getSize().x/2, window->getSize().y/2 - centralImage.getSize().y/2);
centralImage.disableClickEffect();
open = true;
bool clicked = false;
bool reloadImage = true;
while(open){
sf::Event event;
while (window->pollEvent(event)) {
forward.handleEvent(event);
backward.handleEvent(event);
centralImage.handleEvent(event);
switch (event.type) {
case sf::Event::Closed:
window->close();
exit(0);
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape) { window->close(); exit(0);}
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left) {
clicked = true;
}
default:
break;
}
}
if (forward.hasBeenClicked()) {
++actualLvl;
std::stringstream str; str << "res/board" << actualLvl << ".txt";
std::string filePath = str.str();
std::ifstream myfile;
myfile.open(filePath.c_str());
std::string actual;
getline (myfile,actual);
if(actual == "1") {
reloadImage = true;
getline (myfile,maxScore);
getline (myfile,bestTime);
}
else {
reloadImage = false;
--actualLvl;
}
myfile.close();
}
if (backward.hasBeenClicked()) {
if(actualLvl > 0) {
--actualLvl;
std::stringstream str; str << "res/board" << actualLvl << ".txt";
std::string filePath = str.str();
std::ifstream myfile;
myfile.open(filePath.c_str());
std::string actual;
getline (myfile,actual);
if(actual == "1") {
reloadImage = true;
getline (myfile,maxScore);
getline (myfile,bestTime);
}
else {
reloadImage = false;
++actualLvl;
}
myfile.close();
}
}
if (centralImage.hasBeenClicked()) return actualLvl;
if(reloadImage){
std::stringstream st;
st << "board" << actualLvl;
std::string board = st.str();
centralImage.setTexture("res/"+board+".png");
}
window->clear();
forward.draw(*window);
backward.draw(*window);
text.drawTextPos(window, "Max Score: " + maxScore, sf::Color::White, 0, 0);
text.drawTextPos(window, "Best Time: " + bestTime, sf::Color::White, 0, 50);
centralImage.draw(*window);
window->display();
}
}