-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.cpp
More file actions
375 lines (324 loc) · 12.4 KB
/
Copy pathMainMenu.cpp
File metadata and controls
375 lines (324 loc) · 12.4 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "pch.h"
void MainMenu::initVariables() {
this->videoMode = sf::VideoMode::getDesktopMode();
this->font.loadFromFile("Fonts/metropolis/Metropolis-SemiBold.otf");
this->endGame = false;
this->startGameBool = false;
this->returnButtonClicked = false;
this->isUpdating = true;
this->isRendering = true;
this->showHowToPlay = false;
this->currentTutorialPage = 0;
this->cooldownTime = 0.2f; // Set cooldown to 200ms
this->canClick = true;
this->showCredits = false;
// Initialize credits text
this->creditsText.setFont(this->font);
this->creditsText.setCharacterSize(48);
this->creditsText.setFillColor(sf::Color::White);
this->creditsText.setPosition(
static_cast<float>(videoMode.width) / 8.f,
static_cast<float>(videoMode.height) / 2.f - 150.f
);
}
void MainMenu::initHowToPlayButtons() {
// Next button
this->nextButton = new Button(
sf::Vector2f(static_cast<float>(videoMode.width) - 200.f, static_cast<float>(videoMode.height) - 100.f),
sf::Vector2f(150.f, 50.f),
"Next",
this->font,
sf::Color(0, 0, 0, 150),
sf::Color(0, 0, 0, 200)
);
// Previous button
this->prevButton = new Button(
sf::Vector2f(50.f, static_cast<float>(videoMode.height) - 100.f),
sf::Vector2f(150.f, 50.f),
"Previous",
this->font,
sf::Color(0, 0, 0, 150),
sf::Color(0, 0, 0, 200)
);
// Back button
this->backButton = new Button(
sf::Vector2f(50.f, 50.f),
sf::Vector2f(150.f, 50.f),
"Back",
this->font,
sf::Color(0, 0, 0, 150),
sf::Color(0, 0, 0, 200)
);
}
void MainMenu::initCreditsButton() {
// Create return button for credits screen
this->creditsReturnButton = new Button(
sf::Vector2f(50.f, 50.f),
sf::Vector2f(150.f, 50.f),
"Back",
this->font,
sf::Color(0, 0, 0, 150),
sf::Color(0, 0, 0, 200)
);
setCreditsText("Hello, my name is Nikoloz Taturashvili and I am a student at Tbilisi State University.\nThis game was created for an assignment for the Data Structures(C++) course.\nThe game is a simple risk type kingdom game.\nThe game is nowhere near complete\nAnd I am not planning to add more features in the future. ;)\nI hope you enjoy the game! Thank you for playing!\nSpecial thanks to the creators of the SFML library for making this game possible");
}
void MainMenu::setCreditsText(const std::string& credits) {
this->creditsText.setString(credits);
}
void MainMenu::initBackground() {
if (!this->mainMenuTexture.loadFromFile("Textures/main_menu_background.jpg")) {
std::cout << "ERROR::GAME::INIT_MAIN_MENU::Failed to load main menu texture" << std::endl;
}
this->mainMenuSprite.setTexture(this->mainMenuTexture);
this->mainMenuSprite.setScale(
static_cast<float>(this->videoMode.width) / this->mainMenuSprite.getGlobalBounds().width,
static_cast<float>(this->videoMode.height) / this->mainMenuSprite.getGlobalBounds().height
);
this->buttonBackground.setSize(sf::Vector2f(static_cast<float>(videoMode.width) / 4.f, static_cast<float>(videoMode.height)));
this->buttonBackground.setFillColor(sf::Color(0, 0, 0, 150));
this->buttonBackground.setPosition(0, 0);
if (!this->titleTexture.loadFromFile("Textures/kingdom_builder_logo.png")) {
std::cout << "ERROR::GAME::INIT_MAIN_MENU::Failed to load main menu texture" << std::endl;
}
this->titleSprite.setTexture(this->titleTexture);
this->titleSprite.setScale(0.9f, 0.9f);
this->titleSprite.setPosition(
this->buttonBackground.getGlobalBounds().width / 2.f - this->titleSprite.getGlobalBounds().width / 2.f,
-this->titleSprite.getGlobalBounds().height / 4.f
);
}
void MainMenu::initButtons() {
this->startButton = new Button(
sf::Vector2f(0.f, this->buttonBackground.getLocalBounds().height / 2.f - 100.f),
sf::Vector2f(this->buttonBackground.getGlobalBounds().width, 50.f),
"Start",
this->font,
sf::Color(0, 0, 0, 0),
sf::Color(0, 0, 0, 150)
);
this->howToPlayButton = new Button(
sf::Vector2f(0.f, this->buttonBackground.getLocalBounds().height / 2.f - 50.f),
sf::Vector2f(this->buttonBackground.getGlobalBounds().width, 50.f),
"How to Play",
this->font,
sf::Color(0, 0, 0, 0),
sf::Color(0, 0, 0, 150)
);
this->creditsButton = new Button(
sf::Vector2f(0.f, this->buttonBackground.getLocalBounds().height / 2.f + 50.f),
sf::Vector2f(this->buttonBackground.getGlobalBounds().width, 50.f),
"Credits",
this->font,
sf::Color(0, 0, 0, 0),
sf::Color(0, 0, 0, 150)
);
this->exitButton = new Button(
sf::Vector2f(0.f, this->buttonBackground.getLocalBounds().height / 2.f + 100.f),
sf::Vector2f(this->buttonBackground.getGlobalBounds().width, 50.f),
"Exit",
this->font,
sf::Color(0, 0, 0, 0),
sf::Color(0, 0, 0, 150)
);
}
void MainMenu::startGame() {
this->startGameBool = true;
}
void MainMenu::deleteButtons() const {
delete this->startButton;
delete this->howToPlayButton;
delete this->creditsButton;
delete this->exitButton;
}
MainMenu::MainMenu() {
this->initVariables();
this->initBackground();
this->initButtons();
this->initHowToPlayButtons();
this->initCreditsButton();
}
MainMenu::~MainMenu() {
this->deleteButtons();
delete this->nextButton;
delete this->prevButton;
delete this->backButton;
delete this->creditsReturnButton;
}
bool MainMenu::getEndGame() const {
return this->endGame;
}
bool MainMenu::getGameStart() const {
return this->startGameBool;
}
void MainMenu::setGameStart(const bool start) {
this->startGameBool = start;
}
void MainMenu::setReturnButtonClicked(const bool clicked) {
this->returnButtonClicked = clicked;
}
void MainMenu::loadTutorialImages(const std::vector<std::string>& imagePaths) {
tutorialTextures.clear();
for (const auto& path : imagePaths) {
sf::Texture texture;
if (!texture.loadFromFile(path)) {
std::cout << "ERROR::GAME::LOAD_TUTORIAL::Failed to load tutorial image: " << path << std::endl;
continue;
}
tutorialTextures.push_back(texture);
}
if (!tutorialTextures.empty()) {
tutorialSprite.setTexture(tutorialTextures[0]);
// Scale to fit screen while maintaining aspect ratio
float scaleX = (static_cast<float>(videoMode.width) * 0.8f) / tutorialSprite.getGlobalBounds().width;
float scaleY = (static_cast<float>(videoMode.height) * 0.8f) / tutorialSprite.getGlobalBounds().height;
float scale = std::min(scaleX, scaleY);
tutorialSprite.setScale(scale, scale);
// Center the sprite
tutorialSprite.setPosition(
(videoMode.width - tutorialSprite.getGlobalBounds().width) / 2.f,
(videoMode.height - tutorialSprite.getGlobalBounds().height) / 2.f
);
}
}
void MainMenu::updateTutorialPage() {
if (currentTutorialPage < tutorialTextures.size()) {
tutorialSprite.setTexture(tutorialTextures[currentTutorialPage]);
// Maintain the same scaling and positioning
float scaleX = (static_cast<float>(videoMode.width) * 0.8f) / tutorialSprite.getGlobalBounds().width;
float scaleY = (static_cast<float>(videoMode.height) * 0.8f) / tutorialSprite.getGlobalBounds().height;
float scale = std::min(scaleX, scaleY);
tutorialSprite.setScale(scale, scale);
tutorialSprite.setPosition(
(videoMode.width - tutorialSprite.getGlobalBounds().width) / 2.f,
(videoMode.height - tutorialSprite.getGlobalBounds().height) / 2.f
);
}
}
void MainMenu::stopUpdate() {
this->isUpdating = false;
}
void MainMenu::stopRender() {
this->isRendering = false;
}
//Functions
void MainMenu::update() {
if (isUpdating) {
const sf::Vector2i mousePos = sf::Mouse::getPosition();
// Update click cooldown
if (!canClick) {
if (buttonCooldown.getElapsedTime().asSeconds() >= cooldownTime) {
canClick = true;
}
}
// If credits screen is shown
if (showCredits) {
this->creditsReturnButton->update(mousePos);
if (canClick) {
if (this->creditsReturnButton->isClicked(mousePos)) {
showCredits = false;
canClick = false;
buttonCooldown.restart();
}
}
return;
}
if (showHowToPlay) {
// Update tutorial navigation buttons
this->nextButton->update(mousePos);
this->prevButton->update(mousePos);
this->backButton->update(mousePos);
if (canClick) {
if (this->nextButton->isClicked(mousePos) && currentTutorialPage < tutorialTextures.size() - 1) {
currentTutorialPage++;
updateTutorialPage();
canClick = false;
buttonCooldown.restart();
}
if (this->prevButton->isClicked(mousePos) && currentTutorialPage > 0) {
currentTutorialPage--;
updateTutorialPage();
canClick = false;
buttonCooldown.restart();
}
if (this->backButton->isClicked(mousePos)) {
showHowToPlay = false;
currentTutorialPage = 0;
canClick = false;
buttonCooldown.restart();
}
}
}
else {
this->startButton->update(mousePos);
this->howToPlayButton->update(mousePos);
this->creditsButton->update(mousePos);
this->exitButton->update(mousePos);
if (canClick) {
if (this->startButton->isClicked(mousePos) && !startGameBool) {
this->startGame();
this->returnButtonClicked = true;
canClick = false;
buttonCooldown.restart();
}
if (this->howToPlayButton->isClicked(mousePos)) {
showHowToPlay = true;
currentTutorialPage = 0;
updateTutorialPage();
canClick = false;
buttonCooldown.restart();
}
if (this->creditsButton->isClicked(mousePos)) {
showCredits = true;
canClick = false;
buttonCooldown.restart();
}
if (this->exitButton->isClicked(mousePos)) {
this->endGame = true;
canClick = false;
buttonCooldown.restart();
}
}
}
}
}
void MainMenu::setCooldownTime(float seconds) {
this->cooldownTime = seconds;
}
void MainMenu::render(sf::RenderTarget& target) const {
if (isRendering) {
target.draw(this->mainMenuSprite);
if (showCredits) {
// Draw semi-transparent background
sf::RectangleShape background;
background.setSize(sf::Vector2f(static_cast<float>(videoMode.width), static_cast<float>(videoMode.height)));
background.setFillColor(sf::Color(0, 0, 0, 200));
target.draw(background);
// Draw credits text
target.draw(this->creditsText);
// Draw return button
this->creditsReturnButton->draw(target);
}
else if (showHowToPlay) {
// Existing how to play rendering
target.draw(tutorialSprite);
this->nextButton->draw(target);
this->prevButton->draw(target);
this->backButton->draw(target);
}
else if (!startGameBool && !returnButtonClicked) {
// Existing main menu rendering
target.draw(this->buttonBackground);
target.draw(this->titleSprite);
this->startButton->draw(target);
this->howToPlayButton->draw(target);
this->creditsButton->draw(target);
this->exitButton->draw(target);
}
}
}
void MainMenu::startRender() {
this->isRendering = true;
}
void MainMenu::startUpdate() {
this->isUpdating = true;
}