-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
57 lines (47 loc) · 1.63 KB
/
Window.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
#include "stdafx.h"
#include "Camera.h"
#include "Window.h"
Window::Window() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if(window == nullptr) spdlog::error("Failed to create GLFW window!");
glfwMakeContextCurrent(window);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
SPDLOG_CRITICAL("Failed to initialize GLAD");
}
#ifdef _DEBUG
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(glDebugCallback, nullptr);
GLuint ignoredIDs[] = { 0x20071 };
glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER, GL_DONT_CARE, 1, ignoredIDs, GL_FALSE);
glfwSetErrorCallback([](int errorCode, const char* desc) {
SPDLOG_CRITICAL("GLFW Error {}: {}", errorCode, desc);
});
#endif
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glViewport(0, 0, width, height);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
((Camera*)glfwGetWindowUserPointer(window))->setProjection(width, height);
});
}
void Window::swapBuffers() {
glfwSwapBuffers(window);
double curTime = glfwGetTime();
double dTime = curTime - prevTime;
if(dTime > 1) {
glfwSetWindowTitle(window, (title + " - " + std::to_string(framesPassed / dTime) + " FPS").c_str());
prevTime = curTime;
framesPassed = 0;
} else framesPassed++;
}
Window::~Window() {
glfwTerminate();
}