-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
119 lines (95 loc) · 2.67 KB
/
window.cpp
File metadata and controls
119 lines (95 loc) · 2.67 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
#include "framework.h"
Window::Window(Application *parent)
{
app = parent;
width = Application::WIDTH;
height = Application::HEIGHT;
lastX = width / 2;
lastY = height / 2;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);
wndptr = glfwCreateWindow(width, height, "Shoal of fish", NULL, NULL);
if (wndptr == NULL)
{
std::cout << "Failed to create a window\n";
glfwTerminate();
}
glfwMakeContextCurrent(wndptr);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initalize GLAD" << std::endl;
}
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
//glfwSetInputMode(wndptr, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(wndptr, this);
glfwSetFramebufferSizeCallback(wndptr,
[](GLFWwindow* window, int width, int height)
{
Window& wnd = *(Window*)glfwGetWindowUserPointer(window);
wnd.width = width;
wnd.height = height;
glViewport(0, 0, width, height);
});
glfwSetCursorPosCallback(wndptr,
[](GLFWwindow *window, double xposIn, double yposIn)
{
Window& wnd = *(Window*)glfwGetWindowUserPointer(window);
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (wnd.firstMouse)
{
wnd.lastX = xpos;
wnd.lastY = ypos;
wnd.firstMouse = false;
}
float xoffset = xpos - wnd.lastX;
float yoffset = wnd.lastY - ypos; // reversed since y-coordinates go from bottom to top
wnd.lastX = xpos;
wnd.lastY = ypos;
wnd.app->processMouseMovement(xoffset, yoffset);
});
glfwSetScrollCallback(wndptr,
[](GLFWwindow* window, double xoffset, double yoffset)
{
Window& wnd = *(Window*)glfwGetWindowUserPointer(window);
wnd.app->updateCameraZoom((float)yoffset);
});
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui_ImplGlfw_InitForOpenGL(wndptr, true);
ImGui_ImplOpenGL3_Init();
glEnable(GL_MULTISAMPLE);
}
void Window::processInput()
{
if (glfwGetKey(wndptr, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(wndptr, true);
constexpr int len = 11;
int cameraPosKeys[len] = {
GLFW_KEY_W,
GLFW_KEY_S,
GLFW_KEY_A,
GLFW_KEY_D,
GLFW_KEY_SPACE,
GLFW_KEY_LEFT_SHIFT,
GLFW_KEY_Q,
GLFW_KEY_E,
GLFW_KEY_1,
GLFW_KEY_3,
GLFW_KEY_F
};
for (int i = 0; i < len; i++)
{
if (glfwGetKey(wndptr, cameraPosKeys[i]) == GLFW_PRESS)
app->processKeyboard(cameraPosKeys[i]);
}
}