-
Notifications
You must be signed in to change notification settings - Fork 2
/
display_glfw.cpp
146 lines (120 loc) · 3.77 KB
/
display_glfw.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "display_glfw.hpp"
#include "version.h"
#ifdef USE_GLEW
#include <GL/glew.h>
#endif
#include <stdexcept>
#include <GLFW/glfw3.h>
static const float vertices[][4] = {{-1.f,-1.f,+0.f,+1.f},
{+1.f,-1.f,+1.f,+1.f},
{-1.f,+1.f,+0.f,+0.f},
{+1.f,+1.f,+1.f,+0.f}};
Display::Display(int width, int height, bool fullscreen)
{
if (glfwInit() != GL_TRUE) {
throw std::runtime_error("glfwInit failed!");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* vidmode = glfwGetVideoMode(monitor);
if (fullscreen) {
width = vidmode->width;
height = vidmode->height;
}
if (width < 0 || height < 0) {
width = vidmode->width / 3 * 2;
height = vidmode->height / 3 * 2;
}
window = glfwCreateWindow(width, height, RELEASE_NAME, fullscreen ? monitor : nullptr, nullptr);
if (!window) {
throw std::runtime_error("glfwCreateWindow failed!");
}
glfwSetWindowUserPointer(window, this);
if (fullscreen) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
glfwMakeContextCurrent(window);
#ifdef USE_GLEW
glewInit();
#endif
glfwSetFramebufferSizeCallback(window, [] (GLFWwindow*, int width, int height) {
glViewport(0, 0, width, height);
});
glfwSetKeyCallback(window, [] (GLFWwindow* window, int key, int, int action, int) {
Display& d = *static_cast<Display*>(glfwGetWindowUserPointer(window));
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, 1);
}
if (key == GLFW_KEY_DELETE && action == GLFW_PRESS && d.bindCanvas && d.releaseCanvas) {
d.releaseCanvas();
d.cleanupTexture();
int width, height;
glfwGetFramebufferSize(window, &width, &height);
d.createTexture(width, height);
d.bindCanvas();
}
});
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, sizeof(vertices[0]), &(vertices[0][0]));
glTexCoordPointer(2, GL_FLOAT, sizeof(vertices[0]), &(vertices[0][2]));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
createTexture(width, height);
}
Display::~Display()
{
cleanupTexture();
glfwDestroyWindow(window);
}
void Display::operator()()
{
if (bindCanvas && releaseCanvas) {
bindCanvas();
}
do {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, canvas.width, canvas.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImageBuf);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertices) / sizeof(vertices[0]));
glfwSwapBuffers(window);
glfwPollEvents();
} while (!glfwWindowShouldClose(window));
if (bindCanvas && releaseCanvas) {
releaseCanvas();
}
}
void Display::createTexture(int width, int height)
{
canvas.width = width;
canvas.height = height;
canvas.data = nullptr;
#ifdef USE_GLEW
if (GL_ARB_buffer_storage) {
glGenBuffers(1, &buf);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buf);
GLuint size = width * height * sizeof(uint32_t);
GLuint flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
glBufferStorage(GL_PIXEL_UNPACK_BUFFER, size, nullptr, flags | GL_CLIENT_STORAGE_BIT);
canvas.data = (uint32_t*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, size, flags);
texImageBuf = nullptr;
} else
#endif
texImageBuf = canvas.data = new uint32_t[width * height];
}
void Display::cleanupTexture()
{
#ifdef USE_GLEW
if (GL_ARB_buffer_storage) {
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glDeleteBuffers(1, &buf);
}
#endif
if (texImageBuf) {
delete[] texImageBuf;
}
}