-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGLFWProject.cpp
executable file
·103 lines (95 loc) · 2.5 KB
/
GLFWProject.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
#include "GLFWProject.h"
#include <glm/glm.hpp>
GLFWwindow *GLFWProject::Init(const char *title, int width, int height)
{
if (!glfwInit())
return NULL;
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
GLFWwindow *window;
if (width * height == 0)
{
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, title, monitor, NULL);
}
else
{
window = glfwCreateWindow(width, height, title, NULL, NULL);
}
if (!window)
{
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
glfwTerminate();
return NULL;
}
return window;
}
bool GLFWProject::WASDStrafe(
Camera *camera,
GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_W)
{
if (action == GLFW_PRESS) camera->MoveForward = true;
else if (action == GLFW_RELEASE) camera->MoveForward = false;
else return false;
}
else if (key == GLFW_KEY_S)
{
if (action == GLFW_PRESS) camera->MoveBackward = true;
else if (action == GLFW_RELEASE) camera->MoveBackward = false;
else return false;
}
else if (key == GLFW_KEY_A)
{
if (action == GLFW_PRESS) camera->StrafeLeft = true;
else if (action == GLFW_RELEASE) camera->StrafeLeft = false;
else return false;
}
else if (key == GLFW_KEY_D)
{
if (action == GLFW_PRESS) camera->StrafeRight = true;
else if (action == GLFW_RELEASE) camera->StrafeRight = false;
else return false;
}
else return false;
return true;
}
bool GLFWProject::MouseTurn(
Camera *camera, double *xcursor, double *ycursor,
GLFWwindow *window, double xpos, double ypos)
{
if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
camera->Turn((float)(xpos - *xcursor), (float)(ypos - *ycursor));
else
return false;
return true;
}
bool GLFWProject::ClickDisablesCursor(
double *xcursor, double *ycursor,
GLFWwindow *window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetCursorPos(window, xcursor, ycursor);
}
else
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
else return false;
return true;
}