-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMenuState.cpp
224 lines (184 loc) · 5.59 KB
/
MainMenuState.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
#include "stdafx.h"
#include "MainMenuState.h"
//private: Functions:
void MainMenuState::initVariables()
{
}
//Should use '../' before the file path to be able to play the game without Visual Studio.
void MainMenuState::initBackground(const std::string file_path)
{
sf::VideoMode vm = this->stateData->gfxSettings->resolution;
//Background initialization
this->background.setSize
(
sf::Vector2f
(
static_cast<float>(vm.width),
static_cast<float>(vm.height)
)
);
if (!this->backgroundTexture.loadFromFile(file_path))
{
std::cout << "ERROR::SettingsState::initBackground::Failed to load: " << file_path << std::endl;
this->background.setFillColor(sf::Color(30, 60, 60, 200));
}
else
{
this->background.setTexture(&this->backgroundTexture);
}
//Container initialization
// this->container.setSize(sf::Vector2f(gui::percentIntoX(22.f, vm), gui::percentIntoY(56.f, vm)));
// this->container.setFillColor(sf::Color(0, 40, 80, 80));
// this->container.setPosition(sf::Vector2f(gui::percentIntoX(8.f, vm), gui::percentIntoY(12.f, vm)));
this->container.setPosition(0.f, gui::percentIntoY(70.f, vm));
this->container.setFillColor(sf::Color(0, 40, 80, 80));
this->container.setSize
(
sf::Vector2f
(
static_cast<float>(vm.width),
static_cast<float>(vm.height) / 9.f
)
);
}
void MainMenuState::initGui()
{
const sf::VideoMode& vm = this->stateData->gfxSettings->resolution;
//Background
this->initBackground("Resources/Images/Backgrounds/background_1280x720.png");
//Buttons
this->buttons["GAME_STATE"] = new gui::Button
(
gui::percentIntoX(12.f, vm), gui::percentIntoY(73.f, vm),
gui::percentIntoX(12.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Start", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
this->buttons["SETTINGS_STATE"] = new gui::Button
(
gui::percentIntoX(32.f, vm), gui::percentIntoY(73.f, vm),
gui::percentIntoX(12.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Settings", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
this->buttons["EDITOR_STATE"] = new gui::Button
(
gui::percentIntoX(52.f, vm), gui::percentIntoY(73.f, vm),
gui::percentIntoX(12.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Editor", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
this->buttons["EXIT_STATE"] = new gui::Button
(
gui::percentIntoX(72.f, vm), gui::percentIntoY(73.f, vm),
gui::percentIntoX(12.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Exit", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
}
//Clears the GUI elements and reinitializes the GUI.
//@return void
void MainMenuState::resetGui()
{
auto it = this->buttons.begin();
for (it = this->buttons.begin(); it != this->buttons.end(); ++it)
{
delete it->second;
}
this->buttons.clear();
this->initGui();
}
//Constructors and Destructor:
MainMenuState::MainMenuState(StateData* state_data)
: State(state_data)
{
this->initVariables();
// std::thread t2(&MainMenuState::initFont, this, std::ref(this->font), "Fonts/aAtmospheric.ttf");
// std::thread t3(&MainMenuState::initFont, this, std::ref(this->systemFont), "Fonts/Dosis-Light.ttf");
// std::thread t4(&MainMenuState::initKeybinds, this, "Config/mainmenustate_keybinds.ini");
this->initFont(this->font ,"Fonts/slkscr.ttf");
// this->initFont(this->systemFont ,"Fonts/Dosis-Light.ttf");
this->initKeybinds("Config/mainmenustate_keybinds.ini");
// t2.join();
// t3.join();
// t4.join();
this->initGui();
this->resetGui();
}
MainMenuState::~MainMenuState()
{
//Deleting buttons
auto it = this->buttons.begin();
for (it = this->buttons.begin(); it != this->buttons.end(); ++it)
{
delete it->second;
}
}
//Functions:
void MainMenuState::updateInput(const float& dt)
{
}
//Updates all the buttons in the state and handles theis functionality.
void MainMenuState::updateButtons(const float& dt)
{
for (auto& it : this->buttons)
{
it.second->update(this->mousePositionWindow, false, true);
it.second->updateButtonColor(dt);
}
//New game
if (this->buttons["GAME_STATE"]->isPressed() && this->getKeyTime())
{
//Game
this->states->push(new GameState(this->stateData));
}
//Settings
if (this->buttons["SETTINGS_STATE"]->isPressed() && this->getKeyTime())
{
this->states->push(new SettingsState(this->stateData));
}
//Editor
if (this->buttons["EDITOR_STATE"]->isPressed() && this->getKeyTime())
{
//Editor
this->states->push(new EditorState(this->stateData));
}
//Quit the game
if (this->buttons["EXIT_STATE"]->isPressed() && this->getKeyTime())
{
this->endState();
}
}
void MainMenuState::update(const float& dt)
{
this->updateMousePositions();
this->updateInput(dt);
this->updateButtons(dt);
//Debug:
this->updateFpsCounter(dt);
// this->updateMousePositionText();
}
void MainMenuState::renderButtons(sf::RenderTarget& target)
{
for (auto& it : this->buttons)
{
it.second->render(target);
}
}
void MainMenuState::render(sf::RenderTarget* target)
{
if (!target)
{
target = this->window;
}
target->draw(this->background);
target->draw(this->container);
this->renderButtons(*target);
//Debug:
this->renderFpsCounter(target);
// this->renderMousePositionText(target);
}