-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
220 lines (175 loc) · 4.99 KB
/
State.cpp
File metadata and controls
220 lines (175 loc) · 4.99 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
#include "stdafx.h"
#include "State.h"
//StateData class ====================================================================================================
//Constructors and Distructors:
//StateData::StateData()
//{
//
//}
//State class ====================================================================================================
//protected: Functions:
//Should use '\\' instead of '/' in the file path and put this file to the .exe location,
//to be able to play the game without Visual Studio.
void State::initKeybinds(const std::string& file_path)
{
std::ifstream in;
in.open(file_path);
if (in.fail())
{
std::cout << "ERROR::EditorState::initKeybinds::Failed to load: " << file_path << std::endl;
}
else if (in.is_open())
{
std::string action = "";
std::string key = "";
while (in >> action >> key)
{
this->keybinds[action] = this->supportedKeys->at(key);
}
}
in.close();
}
//Should use '../' before the file path to be able to play the game without Visual Studio.
void State::initFont(sf::Font& font, const std::string& file_path)
{
if (!font.loadFromFile(file_path))
{
std::cout << "ERROR::EditorState::initFont::Failed to load: " << file_path << std::endl;
}
}
//Should use '../' before the file path to be able to play the game without Visual Studio.
void State::initTexture(const int& index, const std::string& file_path)
{
if (!this->textures[index].loadFromFile(file_path))
{
std::cout << "ERROR::GameState::initTextures::Failed to load: " << file_path << std::endl;
}
}
void State::initKeyTime()
{
this->keyTimeMax = 0.3f; // Seconds
this->keyTimer.restart();
}
//Debug function.
//Initializes mouse position text.
void State::initMousePositionText()
{
//X
this->mousePositionTextX.setFont(this->systemFont);
this->mousePositionTextX.setCharacterSize(gui::calculateCharSize(3.3f, this->stateData->gfxSettings->resolution));
//Y
this->mousePositionTextY.setFont(this->systemFont);
this->mousePositionTextY.setCharacterSize(gui::calculateCharSize(3.3f, this->stateData->gfxSettings->resolution));
}
void State::initFpsCounter()
{
// FPS counter
this->fpsCounterText.setFont(this->systemFont);
this->fpsCounterText.setCharacterSize(gui::calculateCharSize(3.3f, this->stateData->gfxSettings->resolution));
this->fpsCounterText.setPosition
(
gui::percentIntoX(93.f, this->stateData->gfxSettings->resolution),
gui::percentIntoY(2.f, this->stateData->gfxSettings->resolution)
);
}
//Constructors and Destructor:
State::State(StateData* state_data)
{
this->stateData = state_data;
this->window = state_data->window;
this->supportedKeys = state_data->supportedKeys;
this->states = state_data->states;
this->quit = false;
this->paused = false;
this->gridSize = state_data->gridSize;
this->initFont(this->systemFont, "Fonts/PixellettersFull.ttf");
this->initMousePositionText();
this->initFpsCounter();
// Key timer
this->initKeyTime();
}
State::~State()
{
}
//Accessors:
const bool& State::getQuit() const
{
return this->quit;
}
const bool State::getKeyTime()
{
if (this->keyTimer.getElapsedTime().asSeconds() >= this->keyTimeMax)
{
this->keyTimer.restart();
return true;
}
return false;
}
const bool State::getKeyTime(const float key_time_max)
{
if (this->keyTimer.getElapsedTime().asSeconds() >= key_time_max)
{
this->keyTimer.restart();
return true;
}
return false;
}
//Functions:
void State::endState()
{
this->quit = true;
}
void State::pauseState()
{
this->paused = true;
}
void State::unpauseState()
{
this->paused = false;
}
//Debug function.
//Updating mouse position text.
void State::updateMousePositionText()
{
//X
this->mousePositionTextX.setPosition(this->mousePositionView.x + 20.f, this->mousePositionView.y - 24.f);
std::stringstream ssX;
ssX << "x: " << this->mousePositionView.x;
this->mousePositionTextX.setString(ssX.str());
//Y
this->mousePositionTextY.setPosition(this->mousePositionView.x + 70.f, this->mousePositionView.y - 24.f);
std::stringstream ssY;
ssY << "y: " << this->mousePositionView.y;
this->mousePositionTextY.setString(ssY.str());
}
void State::updateFpsCounter(const float& dt)
{
this->fpsCounterText.setString("FPS: " + std::to_string(static_cast<int>(1.f / dt)));
}
void State::updateMousePositions(sf::View* view)
{
this->mousePositionScreen = sf::Mouse::getPosition();
this->mousePositionWindow = sf::Mouse::getPosition(*this->window);
if (view)
{
this->window->setView(*view);
}
this->mousePositionView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window));
this->mousePositionGrid = sf::Vector2i
(
static_cast<int>(this->mousePositionView.x / this->gridSize),
static_cast<int>(this->mousePositionView.y / this->gridSize)
);
this->window->setView(this->window->getDefaultView());
}
//Debug function.
//Rendering mouse position text.
void State::renderMousePositionText(sf::RenderTarget* target)
{
target->draw(this->mousePositionTextX);
target->draw(this->mousePositionTextY);
}
void State::renderFpsCounter(sf::RenderTarget* target)
{
target->draw(this->fpsCounterText);
}