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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
| #include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "Shaders.h"
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <ft2build.h>
#include <freetype.h>
#include <text_renderer.h>
#include <map>
#include "Character.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void kickInput(GLFWwindow* window, float& dt, int& timer);
void checkForX(float(&offsets)[3], bool(&flags)[3], float(&heights)[6], float& yoffset, int& score, bool& game_state);
void calculateHeight(float(&offsets)[3], float(&heights)[6], bool(&flags)[3]);
void RenderText(Shader& shader, std::string text, float x, float y, float scale, glm::vec3 color);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
GLuint VBO[4], VAO[4], EBO[4];
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glGenVertexArrays(4, VAO);
glGenBuffers(4, VBO);
glGenBuffers(4, EBO);
TextRenderer* Text;
Text = new TextRenderer(800, 600);
Text->Load("fonts/ocraext.TTF", 24);
// configure VAO/VBO for texture quads
// -----------------------------------
glBindVertexArray(VAO[3]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[3]);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
std::cout << glGetError() << std::endl;
Shader ourShader("Game/shaders/3.3.shader.vs", "Game/shaders/3.3.shader.fs"); // you can name your shader files however you like
Shader pipe1("Game/shaders/pipe1.vs", "Game/shaders/pipe1.fs"); // you can name your shader files however you like
Shader pipe2("Game/shaders/pipe2.vs", "Game/shaders/pipe2.fs"); // you can name your shader files however you like
float pipes[] = {
// positions // colors // texture coords
1.0f, 2.5f, 0.0f, 0.0f, 1.0f, 0.0f, // top right
1.0f, 0.25f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
0.5f, 0.25f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.5f, 2.5f, 0.0f, 0.0f, 1.0f, 0.0f, // top left
};
float pipes2[] = {
// positions // colors // texture coords
1.0f, -2.5f, 0.0f, 0.0f, 1.0f, 0.0f, // top right
1.0f, -0.25f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
0.5f, -0.25f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.5f, -2.5f, 0.0f, 0.0f, 1.0f, 0.0f, // top left
};
unsigned int pipesindices[] = {
0, 1, 3,
1, 2, 3
};
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3,
1, 2, 3
};
glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char* data = stbi_load("Game/container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
std::cout << glGetError() << std::endl;
glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(pipes), pipes, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(pipesindices), pipesindices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
std::cout << glGetError() << std::endl;
glBindVertexArray(VAO[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(pipes2), pipes2, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(pipesindices), pipesindices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
std::cout << glGetError() << std::endl;
float offsets[3];
float yoffset = -0.0f;
float boostFall = 0.0025f;
float velocityUp = 0.5f;
float dt = 0;
int timer = 0;
float whitespace = .5f;
float heights[6];
bool flags[3];
int score = 0;
flags[0] = false;
offsets[0] = 1.0f;
bool game_state = true;
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
calculateHeight(offsets, heights, flags);
checkForX(offsets, flags, heights, yoffset, score, game_state);
kickInput(window, dt, timer);
if (yoffset <= -10 || game_state == false) {
//break;
}
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
dt++;
timer++;
yoffset -= boostFall * dt;
//offsets[0] -= 0.001f;
offsets[0] -= 0.005f;
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::scale(trans, glm::vec3(0.1, 0.1, 0.1));
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
glBindVertexArray(VAO[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
ourShader.setFloat("yOffset", yoffset);
ourShader.use();
//std::cout << glGetError() << std::endl;
glm::mat4 trans2 = glm::mat4(1.0f);
trans2 = glm::scale(trans2, glm::vec3(1, 1, 1));
trans2 = glm::translate(trans2, glm::vec3(.0f, heights[0], .0f));
unsigned int transformLoc2 = glGetUniformLocation(pipe1.ID, "transform");
glUniformMatrix4fv(transformLoc2, 1, GL_FALSE, glm::value_ptr(trans2));
glBindVertexArray(VAO[1]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
pipe1.setFloat("xOffset", offsets[0]);
pipe1.use();
//std::cout << glGetError() << std::endl;
glm::mat4 trans3 = glm::mat4(1.0f);
trans3 = glm::scale(trans3, glm::vec3(1, 1, 1));
trans3 = glm::translate(trans3, glm::vec3(.0f, heights[1], .0f));
unsigned int transformLoc3 = glGetUniformLocation(pipe2.ID, "transform");
glUniformMatrix4fv(transformLoc3, 1, GL_FALSE, glm::value_ptr(trans3));
glBindVertexArray(VAO[2]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
pipe2.setFloat("xOffset", offsets[0]);
pipe2.use();
//std::cout << glGetError() << std::endl;
std::string text = "Score: " + std::to_string(score);
Text->RenderText("Lives:", 5.0f, 5.0f, 1.0f);
//RenderText(shader, text, 25.0f, 25.0f, 1.0f, glm::vec3(0.5, 0.8f, 0.2f));
//RenderText(shader, "(C) LearnOpenGL.com", 540.0f, 570.0f, 0.5f, glm::vec3(0.3, 0.7f, 0.9f));
//std::cout << glGetError() << std::endl;
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(4, VAO);
glDeleteBuffers(4, VBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void calculateHeight(float(&offsets)[3], float(&heights)[6], bool(&flags)[3])
{
float rndm1 = (rand() % 700 + 1); // 0.6
int rndm2 = rand() % 2 + 1;
if (rndm2 == 1) {
rndm1 = (rndm1 / 1000);
}
else {
rndm1 = -(rndm1 / 1000);
}
if (offsets[0] > 0.9f) { // first
heights[0] = rndm1;
heights[1] = rndm1;
}
if (offsets[1] > 0.9f) { // second
heights[2] = rndm1;
heights[3] = rndm1;
}
if (offsets[2] > 0.9f) { // third
heights[4] = rndm1;
heights[5] = rndm1;
}
}
void checkForX(float(&offsets)[3], bool(&flags)[3], float(&heights)[6], float& yoffset, int& score, bool& game_state)
{
if (offsets[0] <= -2.0f) {
offsets[0] = 1.0f;
flags[0] = false;
}
if (offsets[1] <= -1.0f) {
offsets[1] = -0.5f;
}
if (offsets[2] <= -1.0f) {
offsets[2] = -0.5f;
}
//std::cout << offsets[0] << std::endl;
if (offsets[0] <= -0.4f && offsets[0] >= -1.05f) {
//std::cout << heights[0]+0.25 << " " << yoffset / 10 << " " << heights[1] - 0.25 << std::endl;
if (yoffset / 10 > heights[0] + 0.21 || yoffset / 10 < heights[1] - 0.21) {
game_state = false;
}
}
if (offsets[0] <= -1.1f && flags[0] == false) {
score++;
std::cout << score << std::endl;
flags[0] = true;
}
//if (offsets[1] <= -0.4f && offsets[0] >= -1.05f) {
// if (yoffset / 10 > heights[2] + 0.25 || yoffset / 10 < heights[3] - 0.25) {
// std::cout << "hitted";
// }
//}
//if (offsets[2] <= -0.4f && offsets[0] >= -1.05f) {
// if (yoffset / 10 > heights[4] + 0.25 || yoffset / 10 < heights[5] - 0.25) {
// std::cout << "hitted";
// }
//}
}
void kickInput(GLFWwindow* window, float& dt, int& timer)
{
if (timer >= 30) {
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
dt = -30;
timer = 0;
}
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
} |