-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
171 lines (138 loc) · 5.51 KB
/
menu.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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "constants.h"
using namespace std;
const int MENU_ITEM_COUNT = 3;
const string MENU_TEXTS[MENU_ITEM_COUNT] = { START_GAME_TEXT, RESUME_GAME_TEXT, EXIT_GAME_TEXT };
const sf::Vector2f MENU_POSITIONS[MENU_ITEM_COUNT] = { sf::Vector2f(100, 200), sf::Vector2f(100, 300), sf::Vector2f(100, 400) };
const int MENU_TEXT_SIZE = 50;
sf::Font font;
sf::Text createMenuItemText(const sf::RenderWindow& window, const string& text, float verticalOffset) {
if (!font.loadFromFile("assets/font.ttf")) {
cerr << "Error loading font" << endl;
}
sf::Text menuItemText;
menuItemText.setFont(font);
menuItemText.setString(text);
menuItemText.setCharacterSize(MENU_TEXT_SIZE);
menuItemText.setFillColor(TEXT_COLOR_BROWN);
// Calculate center position
sf::FloatRect textRect = menuItemText.getLocalBounds();
menuItemText.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
menuItemText.setPosition(sf::Vector2f(window.getSize().x / 2.0f, verticalOffset));
return menuItemText;
}
void showMenu(sf::RenderWindow& window, sf::Text menuTexts[MENU_ITEM_COUNT]) {
// Create a rectangle that covers the entire window
sf::RectangleShape background(sf::Vector2f(window.getSize().x, window.getSize().y));
background.setFillColor(TERMINAL_COLOR);
// Draw the background
window.draw(background);
for (int i = 0; i < MENU_ITEM_COUNT; ++i) {
window.draw(menuTexts[i]);
}
}
bool isMouseOverText(const sf::RenderWindow& window, const sf::Text& text) {
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
return text.getGlobalBounds().contains(mousePos);
}
void updateCursorForMenu(sf::RenderWindow& window, const sf::Text menuTexts[MENU_ITEM_COUNT], bool menuActive) {
static sf::Cursor handCursor, arrowCursor;
static bool cursorsLoaded = false;
if (!cursorsLoaded) {
if (!handCursor.loadFromSystem(sf::Cursor::Hand) || !arrowCursor.loadFromSystem(sf::Cursor::Arrow)) {
cerr << "Error loading cursors" << endl;
return;
}
cursorsLoaded = true;
}
if (menuActive) {
bool isMouseOverAnyText = false;
for (int i = 0; i < MENU_ITEM_COUNT; ++i) {
if (isMouseOverText(window, menuTexts[i])) {
isMouseOverAnyText = true;
break;
}
}
if (isMouseOverAnyText) {
window.setMouseCursor(handCursor);
}
else {
window.setMouseCursor(arrowCursor);
}
}
else {
window.setMouseCursor(arrowCursor);
}
}
void handleMenuInput(sf::RenderWindow& window, const sf::Event& event, sf::Text menuTexts[MENU_ITEM_COUNT], bool& startGame, bool& exitGame, bool& resumeGameSelected) {
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
if (isMouseOverText(window, menuTexts[0])) {
startGame = true;
}
else if (isMouseOverText(window, menuTexts[1])) {
resumeGameSelected = true;
}
else if (isMouseOverText(window, menuTexts[2])) {
exitGame = true;
}
}
}
void showAndHandleMenu(sf::RenderWindow& window, bool& startGame, bool& exitGame, bool& resumeGameSelected) {
sf::Text menuTexts[MENU_ITEM_COUNT];
bool menuActive = true; // Flag to indicate if the menu is active
float verticalSpacing = 100.0f; // Vertical spacing between menu items
float startY = window.getSize().y / 2.0f - verticalSpacing * (MENU_ITEM_COUNT / 2.0f);
for (int i = 0; i < MENU_ITEM_COUNT; ++i) {
menuTexts[i] = createMenuItemText(window, MENU_TEXTS[i], startY + i * verticalSpacing);
}
// Cursor objects for different cursor types
sf::Cursor handCursor, arrowCursor;
if (!handCursor.loadFromSystem(sf::Cursor::Hand) || !arrowCursor.loadFromSystem(sf::Cursor::Arrow)) {
cerr << "Error loading cursors" << endl;
return;
}
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
handleMenuInput(window, event, menuTexts, startGame, exitGame, resumeGameSelected);
if (startGame || resumeGameSelected || exitGame) {
menuActive = false; // Menu is no longer active
break; // Break out of the event loop
}
// Check if mouse is over any text
bool isMouseOverAnyText = false;
for (int i = 0; i < MENU_ITEM_COUNT; ++i) {
if (isMouseOverText(window, menuTexts[i])) {
isMouseOverAnyText = true;
break;
}
}
// Change cursor based on mouse position
if (isMouseOverAnyText) {
window.setMouseCursor(handCursor);
} else {
window.setMouseCursor(arrowCursor);
}
}
updateCursorForMenu(window, menuTexts, menuActive);
if (menuActive) {
window.clear();
showMenu(window, menuTexts);
sf::Font font;
if (!font.loadFromFile("assets/font.ttf")) {
cerr << "Error loading from file " << endl;
}
sf::Text title;
title.setFont(font);
title.setString(GAME_TITLE);
title.setCharacterSize(140);
title.setStyle(sf::Text::Bold);
title.setFillColor(TEXT_COLOR_BROWN);
title.setPosition(sf::Vector2f(window.getSize().x / 4.0f, 20));
window.draw(title);
window.display();
}
}