-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
150 lines (123 loc) · 4.78 KB
/
main.cpp
File metadata and controls
150 lines (123 loc) · 4.78 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
#include "stdafx.h"
#include "App.h"
#include <iostream>
using namespace std;
int W_WIDTH = 1920;
int W_HEIGHT = 1080;
double lastX = static_cast<double>(W_WIDTH) / 2.0f;
double lastY = static_cast<double>(W_HEIGHT) / 2.0f;
bool firstMouse = true;
void mouse_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void scroll_callback(GLFWwindow* window, double offsetX, double offsetY);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void processInput(GLFWwindow *window);
auto camera = make_shared<Camera>(glm::vec3(0.0f, 0.0f, 3.0f));
auto app = make_shared<App>(camera, W_WIDTH, W_HEIGHT);
int main(int argc, char *argv[]) {
if (!glfwInit()) { return 1; }
glfwSetErrorCallback([](int error, const char *description) {
std::cerr << "Error: " << description << '\n';
});
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
//glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow *window = glfwCreateWindow(W_WIDTH, W_HEIGHT, "Snake 3", nullptr, nullptr);
if (!window) {
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback);
glewInit();
glfwGetFramebufferSize(window, &W_WIDTH, &W_HEIGHT);
app->resize(W_WIDTH, W_HEIGHT);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
glViewport(0, 0, W_WIDTH, W_HEIGHT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL error: " << err << std::endl;
}
app->Init();
while (!glfwWindowShouldClose(window))
{
app->run();
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
app.reset();
camera.reset();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
// glfw: whenever the window size changed (by OS or user resize), this callback function executes
// ----------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, const int width, const 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);
W_WIDTH = width;
W_HEIGHT = height;
if (app) {
app->resize(width, height);
}
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double x, double y)
{
// if (firstMouse)
// {
// lastX = x;
// lastY = y;
// firstMouse = false;
// }
//
// const double offsetX = x - lastX;
// const double offsetY = lastY - y; // reversed since y-coordinates go from bottom to top
//
// lastX = x;
// lastY = y;
//
// camera->processMouseMovement(offsetX, offsetY);
app->mousePositionCallback(window, x, y);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
app->mouseButtonCallback(window, button, action, mods);
}
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double offsetX, double offsetY)
{
// camera->processMouseScroll(offsetY);
}
// glfw: keyboard callback is called
// ----------------------------------------------------------------------
void key_callback(GLFWwindow* window, const int key, const int scancode, const int action, const int mods)
{
if (action == GLFW_PRESS) {
app->setKeyState(key, true);
} else if (action == GLFW_RELEASE) {
app->setKeyState(key, false);
}
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
app->processInput(window, key, scancode, action, mods);
}
app->cameraProcessKeyboard(window);
}