-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.c
275 lines (222 loc) Β· 7.71 KB
/
gui.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//#include <GL/gl3w.h>
#include "gl.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include "shader.h"
#define TEXTURE_WIDTH 4
#define TEXTURE_HEIGHT 4
const GLubyte texture_data[] = {
255,0,0,255, 0,255,0,255, 255,255,0,255, 0,0,255,255,
255,0,255,255, 255,0,0,255, 0,255,0,255, 255,255,0,255,
0,0,255,255, 255,0,255,255, 255,0,0,255, 0,255,0,255,
255,255,0,255, 0,0,255,255, 255,0,255,255, 255,0,0,255
};
const GLfloat position_data[] = {
-0.5, -0.5,
-0.5, 0.5,
0.5, -0.5,
0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
};
const GLfloat texcoord_data[] = {
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
};
GLFWcursor *cursor_ew;
GLFWcursor *cursor_ns;
const char *vertex_shader_str =
"#version 410 core\n"
"in vec2 vertex_position;\n"
"in vec2 vertex_texcoord;\n"
"out vec2 texcoord;\n"
"void main() {\n"
" texcoord = vertex_texcoord;\n"
" gl_Position = vec4(vertex_position, 0.0, 1.0);\n"
"}\n";
const char *fragment_shader_str =
"#version 410 core\n"
"in vec2 texcoord;\n"
"uniform sampler2D tex_sampler;\n"
"out vec4 color;\n"
"void main() {\n"
"color = texture(tex_sampler, texcoord);\n"
"}\n";
GLuint program;
GLFWwindow *w;
GLFWwindow *w2;
volatile _Bool w2_show = 0;
void error_exit(const char *str)
{
fprintf(stderr, "%s\n", str);
exit(1);
}
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
printf("key %d %d %d %d\n", key, scancode, action, mods);
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos)
{
if (window == w && w2_show) return;
printf("cursor[%d] %f %f\n", window == w, xpos, ypos);
if (xpos > 150 && xpos < 300 && ypos > 150 && ypos < 300) {
glfwSetCursor(window, cursor_ew);
} else {
glfwSetCursor(window, NULL);
}
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
printf("scroll %f %f\n", xoffset, yoffset);
}
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
{
printf("mouse_button %d %d %d\n", button, action, mods);
if (w2_show == 1) return;
w2_show = 1;
w2 = glfwCreateWindow(200, 200, "π", NULL, NULL);
if (!w2) error_exit("Failed to create window.");
glfwShowWindow(w2);
// glfwMakeContextCurrent(w2);
glfwSetCursorPosCallback(w2, cursor_position_callback);
}
void char_mods_callback(GLFWwindow *window, unsigned int codepoint, int mods)
{
printf("char_mods %d %d\n", codepoint, mods);
}
void char_callback(GLFWwindow *window, unsigned int codepoint)
{
printf("char %d\n", codepoint);
}
void drop_callback(GLFWwindow *window, int path_count, const char* paths[])
{
for (int i = 0; i < path_count; i++) {
printf("drop[%d] %s\n", i, paths[i]);
}
}
void cursor_enter_callback(GLFWwindow *window, int entered)
{
printf("cursor_enter %d\n", entered);
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
printf("framebuffer_size %d %d\n", width, height);
// glViewport(0, 0, width, height);
}
void window_pos_callback(GLFWwindow *window, int xpos, int ypos)
{
printf("window_pos %d %d\n", xpos, ypos);
}
void window_focus_callback(GLFWwindow* window, int focused)
{
printf("focus[%d] %d\n", window == w, focused);
if (window == w && focused) {
if (w2_show) glfwFocusWindow(w2);
glfwRequestWindowAttention(w2);
}
}
int main(int argc, char *argv[])
{
glfwSetErrorCallback(error_callback);
if (!glfwInit())
error_exit("Failed to initialize glfw.");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// glfwWindowHint(GLFW_DECORATED, GL_FALSE);
// glfwWindowHint(GLFW_FLOATING, GL_TRUE);
w = glfwCreateWindow(600, 600, "πππππππππππ«ππππ₯ππ₯₯", NULL, NULL);
if (!w) error_exit("Failed to create window.");
glfwWindowHint(GLFW_FLOATING, GL_TRUE);
//glfwMakeContextCurrent(w2);
glfwMakeContextCurrent(w);
cursor_ew = glfwCreateStandardCursor(GLFW_RESIZE_EW_CURSOR);
cursor_ns = glfwCreateStandardCursor(GLFW_RESIZE_NS_CURSOR);
// if (gl3wInit())
// error_exit("Failed to initialize OpenGL.\n");
// if (!gl3wIsSupported(4, 1))
// error_exit("OpenGL 4.1 not supported.\n");
// printf("GL_VERSION=%s\n", glGetString(GL_VERSION));
if(create_program((GLchar *)vertex_shader_str, (GLchar *)fragment_shader_str, (GLchar *)0, &program))
error_exit("Failder to create program.\n");
glUseProgram(program);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint position;
glGenBuffers(1, &position);
glBindBuffer(GL_ARRAY_BUFFER, position);
glBufferData(GL_ARRAY_BUFFER, sizeof(position_data), position_data, GL_STATIC_DRAW);
GLuint pos_loc = glGetAttribLocation(program, "vertex_position");
glEnableVertexAttribArray(pos_loc);
glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLuint texcoord;
glGenBuffers(1, &texcoord);
glBindBuffer(GL_ARRAY_BUFFER, texcoord);
glBufferData(GL_ARRAY_BUFFER, sizeof(texcoord_data), texcoord_data, GL_STATIC_DRAW);
GLuint tex_loc = glGetAttribLocation(program, "vertex_texcoord");
glEnableVertexAttribArray(tex_loc);
glVertexAttribPointer(tex_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
if (glfwRawMouseMotionSupported()) {
printf("glfwRawMouseMotionSupported()");
glfwSetInputMode(w, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
}
glfwSetKeyCallback(w, key_callback);
glfwSetCursorPosCallback(w, cursor_position_callback);
glfwSetScrollCallback(w, scroll_callback);
glfwSetMouseButtonCallback(w, mouse_button_callback);
glfwSetCharModsCallback(w, char_mods_callback);
glfwSetCharCallback(w, char_callback);
glfwSetDropCallback(w, drop_callback);
glfwSetCursorEnterCallback(w, cursor_enter_callback);
glfwSetFramebufferSizeCallback(w, framebuffer_size_callback);
glfwSetWindowPosCallback(w, window_pos_callback);
glfwSetWindowFocusCallback(w, window_focus_callback);
// glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
// glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_CAPTURED);
// glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glfwSetInputMode(w, GLFW_LOCK_KEY_MODS, 1);
while (!glfwWindowShouldClose(w)) {
if (w2_show) {
while (!glfwWindowShouldClose(w2)) {
glfwPollEvents();
}
w2_show = 0;
//glfwFocusWindow(w);
glfwDestroyWindow(w2);
//glfwHideWindow(w2);
// glfwMakeContextCurrent(w);
} else {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(w);
glfwPollEvents();
}
}
printf("terminate\n");
glfwTerminate();
return 0;
}