-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
126 lines (103 loc) · 3.99 KB
/
application.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
#include "application.hpp"
#include <iostream>
namespace cg
{
Application::Application(std::string title, int window_width, int window_height)
{
// GLFW init
glfwSetErrorCallback(error_callback);
if(!glfwInit())
{
std::cerr << "GLFW failed to initialize\n";
throw std::runtime_error{"GLFW failed to initialize."};
}
// Window settings
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Set Opengl constext version 3.3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Set OpenGL core profile
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); // Make window visible on creation
glfwWindowHint(GLFW_SAMPLES, 16); // Anti-aliasing
// Window init
constexpr auto window_deleter{[] (GLFWwindow* window) { glfwDestroyWindow(window); glfwTerminate(); }};
window = WindowPointer{glfwCreateWindow(window_width, window_height, title.c_str(), nullptr, nullptr), window_deleter};
if(!window)
{
std::cerr << "GLFW window creation failed\n";
throw std::runtime_error{"GLFW window creation failed."};
}
glfwSetFramebufferSizeCallback(window.get(), framebuffer_callback);
glfwMakeContextCurrent(window.get());
glfwSwapInterval(1); // Turn VSYNC off
std::cout << "Application: GLFW initialized successfully\n";
// GLEW init
glewExperimental = GL_TRUE;
GLenum status{glewInit()};
if(status != GLEW_OK)
{
std::cerr << "GLEW failed to initialize with message: " << reinterpret_cast<const char*>(glewGetErrorString(status)) << '\n';
throw std::runtime_error{"GLEW failed to initialize."};
}
std::cout << "Application: GLEW initialized successfully\n";
glfwSetWindowUserPointer(window.get(), nullptr);
}
GLFWwindow* Application::get_window() const
{
return window.get();
}
void Application::error_callback(int errorcode, const char* description)
{
std::cerr << "GLFW failed with error code " << errorcode << " and description: " << description;
throw std::runtime_error{"GLFW encountered an error."};
}
void Application::framebuffer_callback(GLFWwindow* /*window*/, int width, int height)
{
glViewport(0, 0, width, height);
}
void Application::set_input(InputManager* input)
{
if(window)
{
glfwSetWindowUserPointer(window.get(), input);
auto key_callback{[] (GLFWwindow* window, int keycode, int /*scancode*/, int action, int /*mods*/) {
auto input_ptr = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
if(input_ptr)
{
if(action == GLFW_PRESS)
input_ptr->key_pressed(keycode);
else if(action == GLFW_RELEASE)
input_ptr->key_released(keycode);
}
}};
glfwSetKeyCallback(window.get(), key_callback);
auto button_callback{[] (GLFWwindow* window, int buttoncode, int action, int /*modifier*/) {
auto input_ptr = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
if(input_ptr)
{
if(action == GLFW_PRESS)
input_ptr->key_pressed(buttoncode);
else if(action == GLFW_RELEASE)
input_ptr->key_released(buttoncode);
}
}};
glfwSetMouseButtonCallback(window.get(), button_callback);
auto cursor_callback{[] (GLFWwindow* window, double x, double y) {
auto input_ptr = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
if(input_ptr)
input_ptr->cursor_moved(glm::vec2{static_cast<float>(x), static_cast<float>(y)});
}};
glfwSetCursorPosCallback(window.get(), cursor_callback);
auto scroll_callback{[] (GLFWwindow* window, double x, double y) {
auto input_ptr = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
if(input_ptr)
input_ptr->mouse_scrolled(glm::ivec2{static_cast<int>(x), static_cast<int>(y)});
}};
glfwSetScrollCallback(window.get(), scroll_callback);
auto focus_callback{[] (GLFWwindow* window, int /*focused*/) {
auto input_ptr = static_cast<InputManager*>(glfwGetWindowUserPointer(window));
if(input_ptr)
input_ptr->ignore_cursor_once();
}};
glfwSetWindowFocusCallback(window.get(), focus_callback);
}
}
}