-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
155 lines (127 loc) · 4.84 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
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
#include "stdafx.h"
#include "App.h"
#include <iostream>
using namespace std;
int W_WIDTH = 1920;
int W_HEIGHT = 1080;
double lastX = (double)W_WIDTH / 2.0f;
double lastY = (double)W_HEIGHT / 2.0f;
bool firstMouse = true;
void mouse_callback(GLFWwindow* window, double x, double y);
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 = new Camera(glm::vec3(0.0f, 0.0f, 3.0f));
App *app = new App(camera, W_WIDTH, W_HEIGHT);
int main(int argc, char *argv[]) {
GLFWwindow *window;
if (!glfwInit()) { return 1; }
glfwSetErrorCallback([](int error, const char *description) {
std::cerr << "Error: " << description << '\n';
});
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
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);
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);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback);
glewInit();
alutInit (&argc, argv);
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
glViewport(0, 0, W_WIDTH, W_HEIGHT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Enables the Stencil Buffer
glEnable(GL_STENCIL_TEST);
// Sets rules for outcomes of stecil tests
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))
{
// input
// -----
processInput(window);
app->run();
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
delete app;
alutExit();
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);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera->processKeyboard(Camera_Movement::FORWARD, 0.1);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
camera->processKeyboard(Camera_Movement::BACKWARD, 0.1);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera->processKeyboard(Camera_Movement::LEFT, 0.1);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
camera->processKeyboard(Camera_Movement::RIGHT, 0.1);
}
}
// 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);
}
// 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;
}
double offsetX = x - lastX;
double offsetY = lastY - y; // reversed since y-coordinates go from bottom to top
lastX = x;
lastY = y;
// camera->processMouseMovement(offsetX, offsetY);
}
// 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, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS) {
app->processInput(key);
}
}