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
|
/** ---------------------------------------------------------------------------
* @file: window.cpp
*
* Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
* MIT License, see LICENSE file for more details.
* ----------------------------------------------------------------------------
*/
#include "window.h"
#include "../data/input.h"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <stdexcept>
using std::runtime_error;
namespace yage
{
namespace
{
void key_callback(GLFWwindow *window, int key, int scanCode, int action,
int mods)
{
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
} // namespace
Window::Window() = default;
Window::~Window()
{
glfwDestroyWindow(window_);
}
void Window::create(std::string window_name, int width, int height)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window_ =
glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
if (window_ == nullptr) {
throw runtime_error("GLFW Window creation failed");
}
// initialize the gl context
glfwMakeContextCurrent(window_);
// initialize glad
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
// set key callback
glfwSetKeyCallback(window_, key_callback);
// set resize callback
glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback);
// set vsync on
glfwSwapInterval(1);
// set alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void Window::swapBuffer()
{
// swap the window buffer
glfwSwapBuffers(window_);
}
void Window::clearBuffer()
{
// set the clear colour to black
glClearColor(0.18f, 0.18f, 0.18f, 1.f);
// set the clear depth
glClearDepth(1.f);
// clears buffer with clear colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::hide()
{
glfwHideWindow(window_);
}
void Window::show()
{
glfwShowWindow(window_);
}
bool Window::shouldClose()
{
return glfwWindowShouldClose(window_);
}
void Window::pollEvents() const
{
glfwPollEvents();
}
bool Window::keyPressed(key k)
{
if (window_ == nullptr) {
throw runtime_error("Window is not initialized");
}
if (glfwGetKey(window_, static_cast<int>(k)) == GLFW_PRESS) {
return true;
}
return false;
}
} // namespace yage
|