-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
113 lines (95 loc) · 3.25 KB
/
Copy pathWindow.cpp
File metadata and controls
113 lines (95 loc) · 3.25 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
#include "Window.h"
#include <iostream>
#include "InputHandler.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
Window::Window(int windowWidth, int windowHeight, std::string windowName)
: m_windowHeight(windowHeight), m_windowWidth(windowWidth)
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_window = glfwCreateWindow(windowWidth, windowHeight, windowName.c_str(), NULL, NULL);
if (m_window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
}
glfwMakeContextCurrent(m_window);
glfwSetFramebufferSizeCallback(m_window, framebuffer_size_callback);
glfwSetKeyCallback(m_window, key_callback);
glfwSetCursorPosCallback(m_window, cursor_position_callback);
glfwSetScrollCallback(m_window, scroll_callback);
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
}
glEnable(GL_DEPTH_TEST);
}
Window::~Window()
{
glfwTerminate();
}
void Window::update()
{
glfwSwapBuffers(m_window);
glfwPollEvents();
}
bool Window::isWindowShouldClose()
{
if (glfwWindowShouldClose(m_window))
return true;
else
return false;
}
void Window::windowShouldClose()
{
glfwSetWindowShouldClose(m_window, true);
}
GLFWwindow* Window::getWindow()
{
return m_window;
}
unsigned int Window::getWindowHeight()
{
return m_windowHeight;
}
unsigned int Window::getWindowWidth()
{
return m_windowWidth;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
InputHandler* inputHandler = static_cast<InputHandler*>(glfwGetWindowUserPointer(window));
inputHandler->key_callback(window, key, scancode, action, mods);
}
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
InputHandler* inputHandler = static_cast<InputHandler*>(glfwGetWindowUserPointer(window));
inputHandler->cursor_position_callback(window, xpos, ypos);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
InputHandler* inputHandler = static_cast<InputHandler*>(glfwGetWindowUserPointer(window));
inputHandler->scroll_callback(window, xoffset, yoffset);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
InputHandler* inputHandler = static_cast<InputHandler*>(glfwGetWindowUserPointer(window));
inputHandler->mouse_button_callback(window, button, action, mods);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
InputHandler* inputHandler = static_cast<InputHandler*>(glfwGetWindowUserPointer(window));
inputHandler->framebuffer_size_callback(window, width, height);
}