-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
146 lines (121 loc) · 4.01 KB
/
Window.cpp
File metadata and controls
146 lines (121 loc) · 4.01 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
145
#pragma once
#include "Window.h"
Window::Window(int width, int height, const char* title, bool fullscreen, bool vsync)
: fullscreen(fullscreen), vsync(vsync) {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW\n";
std::exit(-1);
}
if (fullscreen) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, title, monitor, nullptr);
}
else {
window = glfwCreateWindow(width, height, title, nullptr, nullptr);
}
if (!window) {
std::cerr << "Failed to create GLFW window\n";
glfwTerminate();
std::exit(-1);
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetWindowUserPointer(window, this);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetKeyCallback(window, key_callback);
glfwSetScrollCallback(window, scroll_callback);
}
Window::~Window() {
glfwDestroyWindow(window);
glfwTerminate();
}
GLFWwindow* Window::getWindow() const {
return window;
}
void Window::set_fullscreen(bool fullscreen) {
this->fullscreen = fullscreen;
if (fullscreen) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
else {
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowMonitor(window, nullptr, 150, 150, mode->width / 2, mode->height / 2, GLFW_DONT_CARE);
}
}
void Window::set_vsync(bool vsync)
{
this->vsync = vsync;
if (vsync) {
glfwSwapInterval(1);
return;
}
glfwSwapInterval(0);
}
bool Window::isVSynced() const
{
return vsync;
}
bool Window::isFullscreen() const {
return fullscreen;
}
// Static key callback function
void Window::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
// glfwGetWindowUserPointer() vytáhne z okna custom pointer, který je k nìmu pøiøazen
// v našem pøípadì jsem k tomu pøidadil instanci tøídy Window v jejím konstruktoru
// static_cast<Window*> je type_casting syntax (pøevod datového typu) - tím my c++ øekneme, že ten pointer je na tøídu Window
Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (instance) {
instance->handle_key_event(key, action);
}
}
/*
Funkce pro handlování inputu v oknì.
*/
void Window::handle_key_event(int key, int action) {
switch (action) {
case GLFW_PRESS:
this->handle_key_press(key, action);
default:
break;
}
}
void Window::handle_key_press(int key, int action) {
switch (key) {
case GLFW_KEY_ESCAPE: {
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
}
case GLFW_KEY_F: {
fullscreen = !fullscreen;
set_fullscreen(fullscreen);
std::cout << "Fullscreen " << (fullscreen ? "enabled" : "disabled") << std::endl;
break;
}
case GLFW_KEY_V: {
vsync = !vsync;
set_vsync(vsync);
std::cout << "VSync " << (vsync ? "enabled" : "disabled") << std::endl;
break;
}
}
}
void Window::scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
// Pro vysvìtlení viz Window::handle_key_event
Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (instance) {
instance->handle_scroll_event(xoffset, yoffset);
}
}
void Window::handle_scroll_event(double xoffset, double yoffset) {
GLclampf redf = static_cast<GLclampf>(yoffset / 10);
GLclampf greenf = static_cast<GLclampf>(yoffset / 10);
GLclampf bluef = static_cast<GLclampf>(yoffset / 10);
glClearColor(redf, greenf, bluef, 1);
glClear(GL_COLOR_BUFFER_BIT);
}
void Window::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}