-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.cpp
More file actions
172 lines (147 loc) · 3.87 KB
/
menu.cpp
File metadata and controls
172 lines (147 loc) · 3.87 KB
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
#include <sstream>
#include "menu.h"
bool inputIP = false;
bool loading = false;
string ipText = "";
sf::Texture pButTex;
sf::Sprite pButSprite;
sf::Texture jButTex;
sf::Sprite jButSprite;
sf::Texture mButTex;
sf::Sprite mButSprite;
sf::Texture bButTex;
sf::Sprite bButSprite;
sf::Texture bgTexture;
sf::Sprite bgSprite;
sf::Text ipSprite;
sf::Text loadText;
sf::Text enterIP;
sf::Clock bTimer;
void initMenu(){
//Init all textures and sprites for main menu
bgTexture.loadFromFile("data/textures/bg.png");
bgSprite.setTexture(bgTexture);
pButTex.loadFromFile("data/textures/play.png");
pButSprite.setTexture(pButTex);
pButSprite.setPosition(200,300);
jButTex.loadFromFile("data/textures/join.png");
jButSprite.setTexture(jButTex);
bButTex.loadFromFile("data/textures/back.png");
bButSprite.setTexture(bButTex);
mButTex.loadFromFile("data/textures/multiplayer.png");
mButSprite.setTexture(mButTex);
mButSprite.setPosition(410, 300);
bButSprite.setPosition(10,450);
jButSprite.setPosition(300,450);
ipSprite.setFont(font);
ipSprite.setPosition(400,300);
loadText.setFont(font);
loadText.setPosition(400,300);
enterIP.setFont(font);
enterIP.setPosition(100,200);
bTimer.restart();
}
void updateMenu(){
//get mouse position
enterIP.setString("Enter IP of server: ");
sf::Vector2f mPos;
mPos.x = mousePos.x;
mPos.y = mousePos.y;
//If we are the main part of the menu
if(loading){
if(!connecting){
loading = false;
inputIP = true;
ipText = "Bad IP";
}
loadText.setString("Loading...");
loadText.setOrigin(loadText.getGlobalBounds().width/2,
loadText.getGlobalBounds().height/2);
//cout << "LOADING" << endl;
if(ready){
cout << "CHANGING STATES" << endl;
state = 1;
sf::sleep(sf::milliseconds(500));
}
}else{
if(!inputIP){
if(mouseRight && bTimer.getElapsedTime().asMilliseconds() > 200){
bTimer.restart();
//If user presses play button
if(pButSprite.getGlobalBounds().contains(mPos)){
serverReady = false;
mouseRight = false;
IPad = "localhost";
//initServer();
serverThread->launch();
clientThread->launch();
sf::sleep(sf::milliseconds(500));
loading = true;
connecting = true;
singleplayer = true;
p2Timer.restart();
}
//If user presses Multi-Player button
if(mButSprite.getGlobalBounds().contains(mPos)){
serverReady = false;
mouseRight = false;
IPad = "localhost";
//initServer();
serverThread->launch();
clientThread->launch();
sf::sleep(sf::milliseconds(500));
loading = true;
connecting = true;
singleplayer = false;
p2Timer.restart();
}
//If user presses join button
if(jButSprite.getGlobalBounds().contains(mPos)){
cout << "HIT JOIB BUTTON" << endl;
inputIP = true;
}
}
} else { //If were at the join part of the menu
if(mouseRight && bTimer.getElapsedTime().asMilliseconds() > 200){
bTimer.restart();
//If user presses back button
if(bButSprite.getGlobalBounds().contains(mPos)){
inputIP = false;
}
//If user presses join button
if(jButSprite.getGlobalBounds().contains(mPos)){
mouseRight = false;
inputIP = false;
IPad = ipText;
clientThread->launch();
sf::sleep(sf::milliseconds(500));
loading = true;
}
}
//Set the text for the string showing input
ipSprite.setString(ipText);
ipSprite.setOrigin(ipSprite.getGlobalBounds().width/2,
ipSprite.getGlobalBounds().height/2);
}
}
}
void drawMenu(sf::RenderWindow *screen){
//Draw background
screen->draw(bgSprite);
//Draw the main menu
if(loading){
screen->clear();
screen->draw(loadText);
}else {
if(!inputIP){
screen->draw(pButSprite);
screen->draw(jButSprite);
screen->draw(mButSprite);
} else { //Draw the join menu
screen->draw(ipSprite);
screen->draw(enterIP);
screen->draw(jButSprite);
screen->draw(bButSprite);
}
}
}