-
Notifications
You must be signed in to change notification settings - Fork 0
/
hud.cpp
267 lines (204 loc) · 8.89 KB
/
hud.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
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
#include "hud.h"
hud::hud(glm::mat4 model, int windowWidth, int windowHeight) {
//model = glm::translate(model, glm::vec3(-0.99f, 0.99f, 0.0));
this->windowHeight = windowHeight;
this->windowWidth = windowWidth;
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(0.006f * 9, 0.006f * 16, 1.0f));
this->model = model;
//genVAO();
}
void hud::render(Shader *shader) {
if (VAO != 0) {
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_2D, Resource_Manager::getTexture("font"));
shader->setMat4("model", model);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, hudVerts.size());
}
if (menuVAO != 0) {
glBindVertexArray(menuVAO);
glBindTexture(GL_TEXTURE_2D, Resource_Manager::getTexture("textures"));
shader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, menuVerts.size());
}
}
void hud::cleanUp() {
if (VAO != 0 || VBO != 0) {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
VAO = 0;
VBO = 0;
}
//glDeleteBuffers(1, &EBO);
}
void hud::cleanUpMenu() {
if (menuVAO != 0 || menuVBO != 0) {
glDeleteVertexArrays(1, &menuVAO);
glDeleteBuffers(1, &menuVBO);
menuVAO = 0;
menuVBO = 0;
}
}
void hud::drawTextures() {
int amountOfTextures = (sizeOfTextureImage * sizeOfTextureImage) / (sizeOfTexture * sizeOfTexture);
float posX = -18.0f;
float posY = -5.0f;
float gap = 1.0f;
for (int i = 0; i < amountOfTextures; i++) {
int texturePos = i;
int imagesPerRow = sizeOfTextureImage / sizeOfTexture;
int horiz = texturePos % imagesPerRow;
int verti = floor(texturePos / imagesPerRow);
float horizInImage = (float)horiz / imagesPerRow;
float vertiInImage = (float)verti / imagesPerRow;
float increment = (float)sizeOfUI / sizeOfTextureImage;
Vertex2D v1 = { glm::vec2((-0.5f * scaleTexture) + posX + (i * gap), (-0.5f * scaleTexture) + posY), glm::vec2(horizInImage, vertiInImage) }; //Top left
Vertex2D v2 = { glm::vec2((0.5f * scaleTexture) + posX + (i * gap), (0.5f * scaleTexture) + posY), glm::vec2(horizInImage + increment, vertiInImage - increment) }; //Bottom right
Vertex2D v3 = { glm::vec2((0.5f * scaleTexture) + posX + (i * gap), (-0.5f * scaleTexture) + posY), glm::vec2(horizInImage + increment, vertiInImage) }; //Top right
Vertex2D v4 = { glm::vec2((-0.5f * scaleTexture) + posX + (i * gap), (0.5f * scaleTexture) + posY), glm::vec2(horizInImage, vertiInImage - increment) }; //Bottom left
menuVerts.push_back(v1);
menuVerts.push_back(v2);
menuVerts.push_back(v3);
menuVerts.push_back(v1);
menuVerts.push_back(v4);
menuVerts.push_back(v2);
std::cout << menuVerts[i*6].pos.x << " " << menuVerts[i*6].pos.y << "\n";
}
//std::cout << menuVerts[0].pos.x << " " << menuVerts[0].pos.y << "\n";
cleanUpMenu();
genMenuVAO();
}
int hud::checkCollision(double xpos, double ypos) {
float hudPosX = -((windowWidth / 2.0f) - xpos); //* 0.006f * 9;
float hudPosY = -((windowHeight / 2.0f)- ypos);// * 0.006f * 16;
hudPosX = hudPosX * (0.006f * 9.0f);
hudPosY = hudPosY * (0.006f * 16.0f);
std::cout << hudPosX << " " << hudPosY << "\n";
return 0;
}
void hud::drawCrosshair() {
std::vector<Vertex2D> tempVerts;
int crossHairPos = 128;
int imagesPerRow = sizeOfFontImage / sizeOfUI;
int horiz = crossHairPos % imagesPerRow;
int verti = floor(crossHairPos / imagesPerRow);
float horizInImage = (float)horiz / imagesPerRow;
float vertiInImage = (float)verti / imagesPerRow;
float increment = (float)sizeOfUI / sizeOfFontImage;
Vertex2D v1 = { glm::vec2(-0.5f * crossHairSize, -0.5f * crossHairSize), glm::vec2(horizInImage, vertiInImage) }; //Top left
Vertex2D v2 = { glm::vec2(0.5f * crossHairSize, 0.5f * crossHairSize), glm::vec2(horizInImage + increment, vertiInImage - increment) }; //Bottom right
Vertex2D v3 = { glm::vec2(0.5f * crossHairSize, -0.5f * crossHairSize), glm::vec2(horizInImage + increment, vertiInImage) }; //Top right
Vertex2D v4 = { glm::vec2(-0.5f * crossHairSize, 0.5f * crossHairSize), glm::vec2(horizInImage, vertiInImage - increment) }; //Bottom left
tempVerts.push_back(v1);
tempVerts.push_back(v2);
tempVerts.push_back(v3);
tempVerts.push_back(v1);
tempVerts.push_back(v4);
tempVerts.push_back(v2);
//hudParts.push_back(tempVerts);
hudParts["crossHair"] = tempVerts;
//cleanUp();
//genVAO();
compileHud();
}
void hud::drawString(std::string id, std::string string, float posx, float posy) {
std::vector<char> chars;
std::vector<int> charToInt;
std::vector<Vertex2D> tempVerts;
for (int i = 0; i < string.length(); i++) {
//std::vector<Vertex2D> tempVerts;
//std::cout << i << "\n";
chars.push_back(string.at(i));
if ((int)chars[i] >= 97) {
charToInt.push_back((int)chars[i] - 97); //Lower case
}
else if ((int)chars[i] >= 65) {
charToInt.push_back((int)chars[i] - 40); //Upper case
}
else if ((int)chars[i] >= 48) { //Numbers
charToInt.push_back((int)chars[i] + 3);
}
else { //Space
charToInt.push_back((int)chars[i] + 30);
}
Vertex2D v1 = { glm::vec2(((posx + (i * 1.0f)) * scale) * 8, (posy * scale) * 16), glm::vec2((charToInt[i] % (int)(sizeOfFontImage / sizeOfFontLetterX)) * (0.5f / sizeOfFontLetterX), (1 - ((2.0f / sizeOfFontLetterY) * (floor(charToInt[i] / sizeOfFontLetterY))))) };
Vertex2D v2 = { glm::vec2((((posx + 1.0f) + (i * 1.0f)) * scale) * 8, ((posy + 1.0f) * scale) * 16), glm::vec2(((charToInt[i] % (int)(sizeOfFontImage / sizeOfFontLetterX)) * (0.5f / sizeOfFontLetterX)) + (0.5f / sizeOfFontLetterX), (1 - ((2.0f / sizeOfFontLetterY) * (floor(charToInt[i] / sizeOfFontLetterY)))) - (sizeOfFontLetterY / sizeOfFontImage)) };
Vertex2D v3 = { glm::vec2((((posx + 1.0f) + (i * 1.0f)) * scale) * 8, (posy * scale) * 16), glm::vec2(((charToInt[i] % (int)(sizeOfFontImage / sizeOfFontLetterX)) * (0.5f / sizeOfFontLetterX)) + (0.5f / sizeOfFontLetterX), (1 - ((2.0f / sizeOfFontLetterY) * (floor(charToInt[i] / sizeOfFontLetterY))))) };
Vertex2D v4 = { glm::vec2(((posx + (i * 1.0f)) * scale) * 8, ((posy + 1.0f) * scale) * 16), glm::vec2((charToInt[i] % (int)(sizeOfFontImage / sizeOfFontLetterX)) * (0.5f / sizeOfFontLetterX), (1 - ((2.0f / sizeOfFontLetterY) * (floor(charToInt[i] / sizeOfFontLetterY)))) - (sizeOfFontLetterY / sizeOfFontImage)) };
/*Vertex v1 = { glm::vec2(0.0f, 0.0f), glm::vec2(0.0f, 0.0f) };
Vertex v2 = { glm::vec2(1.0f, 1.0f), glm::vec2(1.0f, 1.0f) };
Vertex v3 = { glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 0.0f) };
Vertex v4 = { glm::vec2(0.0f, 1.0f), glm::vec2(0.0f, 1.0f) };*/
tempVerts.push_back(v1);
tempVerts.push_back(v2);
tempVerts.push_back(v3);
tempVerts.push_back(v1);
tempVerts.push_back(v4);
tempVerts.push_back(v2);
}
//hudParts.push_back(tempVerts);
hudParts[id] = tempVerts;
//cleanUp();
//genVAO();
compileHud();
}
void hud::genVAO() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex2D) * hudVerts.size(), &hudVerts[0], GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicesPlane), indicesPlane, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
}
void hud::genMenuVAO() {
glGenVertexArrays(1, &menuVAO);
glGenBuffers(1, &menuVBO);
glBindVertexArray(menuVAO);
glBindBuffer(GL_ARRAY_BUFFER, menuVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex2D) * menuVerts.size(), &menuVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
}
void hud::compileHud() {
hudVerts.clear();
cleanUp();
for (auto it : hudParts) {
hudVerts.insert(hudVerts.end(), it.second.begin(), it.second.end());
}
if (hudVerts.size() > 0) {
genVAO();
}
}
void hud::removeHudPart(std::string id) {
//hudVerts.clear();
//cleanUp();
hudParts.erase(id);
//hudParts.erase(hudParts.begin() + p);
/*std::vector<std::vector<Vertex2D>> tempHudParts;
std::vector<Vertex2D> tempVerts;
for (int i = 0; i < hudParts.size(); i++) {
if (i == p) {
//Do nothing
}
else {
tempVerts.insert(tempVerts.end(), hudParts[i].begin(), hudParts[i].end());
tempHudParts.push_back(tempVerts);
tempVerts.clear();
hudVerts.insert(hudVerts.end(), hudParts[i].begin(), hudParts[i].end());
}
}
hudParts.clear();
hudParts = tempHudParts;*/
//cleanUp();
//genVAO();
compileHud();
}