-
Notifications
You must be signed in to change notification settings - Fork 3
/
screenshot.cpp
167 lines (122 loc) · 3.52 KB
/
screenshot.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "gl.h"
#include "glfw_utilities.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h> // https://github.com/nothings/stb
void flipVertically(int width, int height, char *data)
{
char rgb[3];
for (int y = 0; y < height / 2; ++y)
{
for (int x = 0; x < width; ++x)
{
int top = (x + y * width) * 3;
int bottom = (x + (height - y - 1) * width) * 3;
memcpy(rgb, data + top, sizeof(rgb));
memcpy(data + top, data + bottom, sizeof(rgb));
memcpy(data + bottom, rgb, sizeof(rgb));
}
}
}
int saveScreenshot(const char *filename)
{
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
int x = viewport[0];
int y = viewport[1];
int width = viewport[2];
int height = viewport[3];
char *data = (char*) malloc((size_t) (width * height * 3)); // 3 components (R, G, B)
if (!data)
return 0;
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
flipVertically(width, height, data);
int saved = stbi_write_png(filename, width, height, 3, data, 0);
free(data);
return saved;
}
const char* createScreenshotBasename()
{
static char basename[30];
time_t t = time(NULL);
strftime(basename, 30, "%Y%m%d_%H%M%S.png", localtime(&t));
return basename;
}
int captureScreenshot()
{
char filename[50];
strcpy(filename, "screenshots/");
strcat(filename, createScreenshotBasename());
int saved = saveScreenshot(filename);
if (saved)
printf("Successfully Saved Image: %s\n", filename);
else
fprintf(stderr, "Failed Saving Image: %s\n", filename);
return saved;
}
int main(int argc, char *argv[])
{
// flipVertically in saveScreenshot
// or
// stbi_flip_vertically_on_write(1);
if (!glfwInit())
{
fprintf(stderr, "Failed initializing GLFW\n");
return EXIT_FAILURE;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow *window = glfwCreateWindow(640, 480, "Screenshot - GLCollection", NULL, NULL);
if (!window)
{
fprintf(stderr, "Failed creating window\n");
glfwTerminate();
return EXIT_FAILURE;
}
glcCenterWindow(window, glcGetBestMonitor(window));
glfwShowWindow(window);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
fprintf(stderr, "Failed loading OpenGL functions and extensions\n");
glfwTerminate();
return EXIT_FAILURE;
}
glEnable(GL_SCISSOR_TEST);
bool repeated = false;
while (!glfwWindowShouldClose(window))
{
int down = glfwGetKey(window, GLFW_KEY_F5);
if (down && !repeated)
{
captureScreenshot();
repeated = true;
}
else if (!down)
repeated = false;
int viewportWidth, viewportHeight;
glfwGetFramebufferSize(window, &viewportWidth, &viewportHeight);
glViewport(0, 0, viewportWidth, viewportHeight);
glScissor(0, viewportHeight / 2, viewportWidth / 2, viewportHeight / 2);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glScissor(viewportWidth / 2, viewportHeight / 2, viewportWidth / 2, viewportHeight / 2);
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glScissor(0, 0, viewportWidth / 2, viewportHeight / 2);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glScissor(viewportWidth / 2, 0, viewportWidth / 2, viewportHeight / 2);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}