-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
124 lines (99 loc) · 3.81 KB
/
main.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
#define GLFW_INCLUDE_NONE
#include "Renderer.hpp"
#include <GLFW/glfw3.h>
#include <iostream>
#include <array>
#include <algorithm>
#include "Snake.hpp"
#include "Quad.hpp"
#include "Apple.hpp"
#include <glbinding/glbinding.h>
GLFWwindow* initGl(const int width, const int height, const char* const title) {
if (!glfwInit())
return nullptr;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!window) {
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // V-sync
glbinding::initialize(glfwGetProcAddress);
return window;
}
void GLAPIENTRY MessageCallback(GLenum /*source*/, GLenum type, GLuint /*id*/, GLenum severity, GLsizei /*length*/, const GLchar* message, const void* /*userParam*/) {
std::cerr << std::hex << (type == GL_DEBUG_TYPE_ERROR ? "ERROR: " : "WARNING: ") << "type: 0x" << static_cast<GLuint>(type)
<< ", severity: 0x" << static_cast<GLuint>(severity) << ", " << message << std::endl;
}
void enableGlDebug() {
std::cout << glGetString(GL_VERSION) << std::endl;
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, nullptr);
}
// Declaring static variables in this cpp unit
uint8_t Snake::direction;
uint8_t Snake::previousDirection;
std::vector<GLfloat> Snake::horizontalPositions;
std::vector<GLfloat> Snake::verticalPositions;
std::vector<Quad> Snake::snake;
size_t Snake::length;
uint8_t Snake::score;
Quad Apple::apple;
GLfloat Apple::horizontalPosition;
GLfloat Apple::verticalPosition;
GLubyte Snake::r, Snake::g, Snake::b;
GLubyte Snake::rIncrement, Snake::gIncrement, Snake::bIncrement;
int main() {
GLFWwindow* window = initGl(720, 720, "CppSnake");
if (window == nullptr)
return EXIT_FAILURE;
//enableGlDebug(); Doesn't work with current version of glad; if you need to, download the debug version from https://glad.dav1d.de
{
constexpr GLsizei maxQuads = 2000;
constexpr GLsizei maxVertices = 4 * maxQuads;
constexpr GLsizei maxIndices = 6 * maxQuads;
DynamicVertexBuffer dynVertexBuffer(maxVertices * sizeof(Vertex));
VertexBufferLayout layout;
layout.add(Vertex::positionCount, static_cast<GLuint>(GL_FLOAT));
layout.add(Vertex::colorCount, static_cast<GLuint>(GL_UNSIGNED_BYTE));
VertexArray vertexArray;
vertexArray.addBuffer(dynVertexBuffer, layout);
// Indices necessary to draw all triangles making the quads
std::array<GLuint, maxIndices> indices = IndexBuffer::genIndices<maxIndices>();
IndexBuffer indexBuffer(indices);
Shader shader("./res/vertex.shader", "./res/fragment.shader");
shader.bind();
Renderer::setClearColor(static_cast<GLubyte>(22), 22, 22);
Snake::create();
Apple::create();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
Renderer::clear();
Snake::checkMove();
std::vector<Quad> data;
data.reserve(Snake::snake.size() + 1); // 1 is the number of apples
data.insert(data.end(), Snake::snake.begin(), Snake::snake.end());
data.insert(data.end(), Apple::apple);
dynVertexBuffer.setData(data);
if (Apple::checkCollision(Snake::snake.front())) {
Apple::create();
Snake::addQuad();
}
if (Snake::checkCollision())
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (Snake::score == 107)
Snake::RGB();
Renderer::draw(vertexArray, indexBuffer, shader);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
glfwSetKeyCallback(window, Snake::key_callback);
}
std::cout << "Punteggio: " << static_cast<short>(Snake::score) << std::endl;
} // Free all glObjects befor calling glfwTerminate to avoid them free already freed glResources
glfwTerminate();
}