-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyWindow.cpp
More file actions
144 lines (116 loc) · 3.19 KB
/
MyWindow.cpp
File metadata and controls
144 lines (116 loc) · 3.19 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
#include "MyWindow.h"
MyWindow::MyWindow()
{
width = 800;
height = 600;
for (size_t i = 0; i < 1024; i++) {
keys[i] = 0;
}
mouseFirstMoved = true;
xChange = 0.0f;
yChange = 0.0f;
}
MyWindow::MyWindow(GLint windowWidth, GLint windowHeight)
{
width = windowWidth;
height = windowHeight;
for (size_t i = 0; i < 1024; i++) {
keys[i] = 0;
}
mouseFirstMoved = true;
xChange = 0.0f;
yChange = 0.0f;
}
int MyWindow::Initialise()
{
//initialize glfw
if (!glfwInit()) {
std::cout << "glfw init failed" << std::endl;
glfwTerminate();
return 1;
}
//setup glfw windows properties
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
mainWindow = glfwCreateWindow(width, height, "test", NULL, NULL);
if (!mainWindow) {
std::cout << "window failed" << std::endl;
glfwTerminate();
return 1;
}
//get buffer size information
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
//set context for glew to use
glfwMakeContextCurrent(mainWindow);
//handle key + mouse input
createCallbacks();
//glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//allow modern extension features
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
// 3D depth
glEnable(GL_DEPTH_TEST);
//setup viewport size
glViewport(0, 0, bufferWidth, bufferHeight);
//
glfwSetWindowUserPointer(mainWindow, this);
}
GLfloat MyWindow::getXChange()
{
GLfloat theChange = xChange;
xChange = 0.0f;
return theChange;
}
GLfloat MyWindow::getYChange()
{
GLfloat theChange = yChange;
yChange = 0.0f;
return theChange;
}
MyWindow::~MyWindow()
{
glfwDestroyWindow(mainWindow);
glfwTerminate();
}
void MyWindow::createCallbacks() //
{
glfwSetKeyCallback(mainWindow, handleKeys);
glfwSetCursorPosCallback(mainWindow, handleMouse);
}
void MyWindow::handleKeys(GLFWwindow * window, int key, int code, int action,int mode)
{
MyWindow* theWindow = static_cast<MyWindow*>(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS) {
theWindow->keys[key] = true;
//std::cout << "Pressed " <<key<< std::endl;
}
else if (action == GLFW_RELEASE) {
theWindow->keys[key] = false;
//std::cout << "Released " << key << std::endl;
}
}
}
void MyWindow::handleMouse(GLFWwindow * window, double xPos, double yPos)
{
MyWindow* theWindow = static_cast<MyWindow*>(glfwGetWindowUserPointer(window));//
if (theWindow->mouseFirstMoved) {
theWindow->lastX = xPos;
theWindow->lastY = yPos;
theWindow->mouseFirstMoved = false;
}
theWindow->xChange = xPos - theWindow->lastX;
theWindow->yChange = theWindow->lastY - yPos;
theWindow->lastX = xPos;
theWindow->lastY = yPos;
//std::cout << "x:" << theWindow->xChange << ", y:" << theWindow->yChange << std::endl;
}