• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // Window properties test
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 //    claim that you wrote the original software. If you use this software
15 //    in a product, an acknowledgment in the product documentation would
16 //    be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 //    be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 //    distribution.
23 //
24 //========================================================================
25 
26 #define GLAD_GL_IMPLEMENTATION
27 #include <glad/gl.h>
28 #define GLFW_INCLUDE_NONE
29 #include <GLFW/glfw3.h>
30 
31 #include <stdarg.h>
32 
33 #define NK_IMPLEMENTATION
34 #define NK_INCLUDE_FIXED_TYPES
35 #define NK_INCLUDE_FONT_BAKING
36 #define NK_INCLUDE_DEFAULT_FONT
37 #define NK_INCLUDE_DEFAULT_ALLOCATOR
38 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
39 #define NK_INCLUDE_STANDARD_VARARGS
40 #define NK_BUTTON_TRIGGER_ON_RELEASE
41 #include <nuklear.h>
42 
43 #define NK_GLFW_GL2_IMPLEMENTATION
44 #include <nuklear_glfw_gl2.h>
45 
46 #include <stdbool.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <limits.h>
50 
main(int argc,char ** argv)51 int main(int argc, char** argv)
52 {
53     int windowed_x, windowed_y, windowed_width, windowed_height;
54     int last_xpos = INT_MIN, last_ypos = INT_MIN;
55     int last_width = INT_MIN, last_height = INT_MIN;
56     int limit_aspect_ratio = false, aspect_numer = 1, aspect_denom = 1;
57     int limit_min_size = false, min_width = 400, min_height = 400;
58     int limit_max_size = false, max_width = 400, max_height = 400;
59     char width_buffer[12] = "", height_buffer[12] = "";
60     char xpos_buffer[12] = "", ypos_buffer[12] = "";
61     char numer_buffer[12] = "", denom_buffer[12] = "";
62     char min_width_buffer[12] = "", min_height_buffer[12] = "";
63     char max_width_buffer[12] = "", max_height_buffer[12] = "";
64     int may_close = true;
65     char window_title[64] = "";
66 
67     if (!glfwInit())
68         exit(EXIT_FAILURE);
69 
70     glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
71     glfwWindowHint(GLFW_WIN32_KEYBOARD_MENU, GLFW_TRUE);
72     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
73     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
74 
75     GLFWwindow* window = glfwCreateWindow(600, 660, "Window Features", NULL, NULL);
76     if (!window)
77     {
78         glfwTerminate();
79         exit(EXIT_FAILURE);
80     }
81     glfwSetInputMode(window, GLFW_UNLIMITED_MOUSE_BUTTONS, GLFW_TRUE);
82 
83     glfwMakeContextCurrent(window);
84     gladLoadGL(glfwGetProcAddress);
85     glfwSwapInterval(0);
86 
87     bool position_supported = true;
88 
89     glfwGetError(NULL);
90     glfwGetWindowPos(window, &last_xpos, &last_ypos);
91     sprintf(xpos_buffer, "%i", last_xpos);
92     sprintf(ypos_buffer, "%i", last_ypos);
93     if (glfwGetError(NULL) == GLFW_FEATURE_UNAVAILABLE)
94         position_supported = false;
95 
96     glfwGetWindowSize(window, &last_width, &last_height);
97     sprintf(width_buffer, "%i", last_width);
98     sprintf(height_buffer, "%i", last_height);
99 
100     sprintf(numer_buffer, "%i", aspect_numer);
101     sprintf(denom_buffer, "%i", aspect_denom);
102 
103     sprintf(min_width_buffer, "%i", min_width);
104     sprintf(min_height_buffer, "%i", min_height);
105     sprintf(max_width_buffer, "%i", max_width);
106     sprintf(max_height_buffer, "%i", max_height);
107 
108     struct nk_context* nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
109 
110     struct nk_font_atlas* atlas;
111     nk_glfw3_font_stash_begin(&atlas);
112     nk_glfw3_font_stash_end();
113 
114     strncpy(window_title, glfwGetWindowTitle(window), sizeof(window_title));
115 
116     while (!(may_close && glfwWindowShouldClose(window)))
117     {
118         int width, height;
119 
120         glfwGetWindowSize(window, &width, &height);
121 
122         struct nk_rect area = nk_rect(0.f, 0.f, (float) width, (float) height);
123         nk_window_set_bounds(nk, "main", area);
124 
125         nk_glfw3_new_frame();
126         if (nk_begin(nk, "main", area, 0))
127         {
128             nk_layout_row_dynamic(nk, 30, 4);
129 
130             if (glfwGetWindowMonitor(window))
131             {
132                 if (nk_button_label(nk, "Make Windowed"))
133                 {
134                     glfwSetWindowMonitor(window, NULL,
135                                          windowed_x, windowed_y,
136                                          windowed_width, windowed_height, 0);
137                 }
138             }
139             else
140             {
141                 if (nk_button_label(nk, "Make Fullscreen"))
142                 {
143                     GLFWmonitor* monitor = glfwGetPrimaryMonitor();
144                     const GLFWvidmode* mode = glfwGetVideoMode(monitor);
145                     glfwGetWindowPos(window, &windowed_x, &windowed_y);
146                     glfwGetWindowSize(window, &windowed_width, &windowed_height);
147                     glfwSetWindowMonitor(window, monitor,
148                                          0, 0, mode->width, mode->height,
149                                          mode->refreshRate);
150                 }
151             }
152 
153             if (nk_button_label(nk, "Maximize"))
154                 glfwMaximizeWindow(window);
155             if (nk_button_label(nk, "Iconify"))
156                 glfwIconifyWindow(window);
157             if (nk_button_label(nk, "Restore"))
158                 glfwRestoreWindow(window);
159 
160             nk_layout_row_dynamic(nk, 30, 2);
161 
162             if (nk_button_label(nk, "Hide (for 3s)"))
163             {
164                 glfwHideWindow(window);
165 
166                 const double time = glfwGetTime() + 3.0;
167                 while (glfwGetTime() < time)
168                     glfwWaitEventsTimeout(1.0);
169 
170                 glfwShowWindow(window);
171             }
172             if (nk_button_label(nk, "Request Attention (after 3s)"))
173             {
174                 glfwIconifyWindow(window);
175 
176                 const double time = glfwGetTime() + 3.0;
177                 while (glfwGetTime() < time)
178                     glfwWaitEventsTimeout(1.0);
179 
180                 glfwRequestWindowAttention(window);
181             }
182 
183             nk_layout_row_dynamic(nk, 30, 1);
184 
185             if (glfwGetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH))
186             {
187                 nk_label(nk, "Press H to disable mouse passthrough", NK_TEXT_CENTERED);
188 
189                 if (glfwGetKey(window, GLFW_KEY_H))
190                     glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, false);
191             }
192 
193             nk_label(nk, "Press Enter in a text field to set value", NK_TEXT_CENTERED);
194 
195             nk_flags events;
196             const nk_flags flags = NK_EDIT_FIELD |
197                                    NK_EDIT_SIG_ENTER |
198                                    NK_EDIT_GOTO_END_ON_ACTIVATE;
199 
200             nk_layout_row_begin(nk, NK_DYNAMIC, 30, 2);
201             nk_layout_row_push(nk, 1.f / 3.f);
202             nk_label(nk, "Title", NK_TEXT_LEFT);
203             nk_layout_row_push(nk, 2.f / 3.f);
204             events = nk_edit_string_zero_terminated(nk, flags, window_title,
205                                                     sizeof(window_title), NULL);
206             if (events & NK_EDIT_COMMITED)
207                 glfwSetWindowTitle(window, window_title);
208             nk_layout_row_end(nk);
209 
210             if (position_supported)
211             {
212                 int xpos, ypos;
213                 glfwGetWindowPos(window, &xpos, &ypos);
214 
215                 nk_layout_row_dynamic(nk, 30, 3);
216                 nk_label(nk, "Position", NK_TEXT_LEFT);
217 
218                 events = nk_edit_string_zero_terminated(nk, flags, xpos_buffer,
219                                                         sizeof(xpos_buffer),
220                                                         nk_filter_decimal);
221                 if (events & NK_EDIT_COMMITED)
222                 {
223                     xpos = atoi(xpos_buffer);
224                     glfwSetWindowPos(window, xpos, ypos);
225                 }
226                 else if (xpos != last_xpos || (events & NK_EDIT_DEACTIVATED))
227                     sprintf(xpos_buffer, "%i", xpos);
228 
229                 events = nk_edit_string_zero_terminated(nk, flags, ypos_buffer,
230                                                         sizeof(ypos_buffer),
231                                                         nk_filter_decimal);
232                 if (events & NK_EDIT_COMMITED)
233                 {
234                     ypos = atoi(ypos_buffer);
235                     glfwSetWindowPos(window, xpos, ypos);
236                 }
237                 else if (ypos != last_ypos || (events & NK_EDIT_DEACTIVATED))
238                     sprintf(ypos_buffer, "%i", ypos);
239 
240                 last_xpos = xpos;
241                 last_ypos = ypos;
242             }
243             else
244                 nk_label(nk, "Platform does not support window position", NK_TEXT_LEFT);
245 
246             nk_layout_row_dynamic(nk, 30, 3);
247             nk_label(nk, "Size", NK_TEXT_LEFT);
248 
249             events = nk_edit_string_zero_terminated(nk, flags, width_buffer,
250                                                     sizeof(width_buffer),
251                                                     nk_filter_decimal);
252             if (events & NK_EDIT_COMMITED)
253             {
254                 width = atoi(width_buffer);
255                 glfwSetWindowSize(window, width, height);
256             }
257             else if (width != last_width || (events & NK_EDIT_DEACTIVATED))
258                 sprintf(width_buffer, "%i", width);
259 
260             events = nk_edit_string_zero_terminated(nk, flags, height_buffer,
261                                                     sizeof(height_buffer),
262                                                     nk_filter_decimal);
263             if (events & NK_EDIT_COMMITED)
264             {
265                 height = atoi(height_buffer);
266                 glfwSetWindowSize(window, width, height);
267             }
268             else if (height != last_height || (events & NK_EDIT_DEACTIVATED))
269                 sprintf(height_buffer, "%i", height);
270 
271             last_width = width;
272             last_height = height;
273 
274             bool update_ratio_limit = false;
275             if (nk_checkbox_label(nk, "Aspect Ratio", &limit_aspect_ratio))
276                 update_ratio_limit = true;
277 
278             events = nk_edit_string_zero_terminated(nk, flags, numer_buffer,
279                                                     sizeof(numer_buffer),
280                                                     nk_filter_decimal);
281             if (events & NK_EDIT_COMMITED)
282             {
283                 aspect_numer = abs(atoi(numer_buffer));
284                 update_ratio_limit = true;
285             }
286             else if (events & NK_EDIT_DEACTIVATED)
287                 sprintf(numer_buffer, "%i", aspect_numer);
288 
289             events = nk_edit_string_zero_terminated(nk, flags, denom_buffer,
290                                                     sizeof(denom_buffer),
291                                                     nk_filter_decimal);
292             if (events & NK_EDIT_COMMITED)
293             {
294                 aspect_denom = abs(atoi(denom_buffer));
295                 update_ratio_limit = true;
296             }
297             else if (events & NK_EDIT_DEACTIVATED)
298                 sprintf(denom_buffer, "%i", aspect_denom);
299 
300             if (update_ratio_limit)
301             {
302                 if (limit_aspect_ratio)
303                     glfwSetWindowAspectRatio(window, aspect_numer, aspect_denom);
304                 else
305                     glfwSetWindowAspectRatio(window, GLFW_DONT_CARE, GLFW_DONT_CARE);
306             }
307 
308             bool update_size_limit = false;
309 
310             if (nk_checkbox_label(nk, "Minimum Size", &limit_min_size))
311                 update_size_limit = true;
312 
313             events = nk_edit_string_zero_terminated(nk, flags, min_width_buffer,
314                                                     sizeof(min_width_buffer),
315                                                     nk_filter_decimal);
316             if (events & NK_EDIT_COMMITED)
317             {
318                 min_width = abs(atoi(min_width_buffer));
319                 update_size_limit = true;
320             }
321             else if (events & NK_EDIT_DEACTIVATED)
322                 sprintf(min_width_buffer, "%i", min_width);
323 
324             events = nk_edit_string_zero_terminated(nk, flags, min_height_buffer,
325                                                     sizeof(min_height_buffer),
326                                                     nk_filter_decimal);
327             if (events & NK_EDIT_COMMITED)
328             {
329                 min_height = abs(atoi(min_height_buffer));
330                 update_size_limit = true;
331             }
332             else if (events & NK_EDIT_DEACTIVATED)
333                 sprintf(min_height_buffer, "%i", min_height);
334 
335             if (nk_checkbox_label(nk, "Maximum Size", &limit_max_size))
336                 update_size_limit = true;
337 
338             events = nk_edit_string_zero_terminated(nk, flags, max_width_buffer,
339                                                     sizeof(max_width_buffer),
340                                                     nk_filter_decimal);
341             if (events & NK_EDIT_COMMITED)
342             {
343                 max_width = abs(atoi(max_width_buffer));
344                 update_size_limit = true;
345             }
346             else if (events & NK_EDIT_DEACTIVATED)
347                 sprintf(max_width_buffer, "%i", max_width);
348 
349             events = nk_edit_string_zero_terminated(nk, flags, max_height_buffer,
350                                                     sizeof(max_height_buffer),
351                                                     nk_filter_decimal);
352             if (events & NK_EDIT_COMMITED)
353             {
354                 max_height = abs(atoi(max_height_buffer));
355                 update_size_limit = true;
356             }
357             else if (events & NK_EDIT_DEACTIVATED)
358                 sprintf(max_height_buffer, "%i", max_height);
359 
360             if (update_size_limit)
361             {
362                 glfwSetWindowSizeLimits(window,
363                                         limit_min_size ? min_width : GLFW_DONT_CARE,
364                                         limit_min_size ? min_height : GLFW_DONT_CARE,
365                                         limit_max_size ? max_width : GLFW_DONT_CARE,
366                                         limit_max_size ? max_height : GLFW_DONT_CARE);
367             }
368 
369             int fb_width, fb_height;
370             glfwGetFramebufferSize(window, &fb_width, &fb_height);
371             nk_label(nk, "Framebuffer Size", NK_TEXT_LEFT);
372             nk_labelf(nk, NK_TEXT_LEFT, "%i", fb_width);
373             nk_labelf(nk, NK_TEXT_LEFT, "%i", fb_height);
374 
375             float xscale, yscale;
376             glfwGetWindowContentScale(window, &xscale, &yscale);
377             nk_label(nk, "Content Scale", NK_TEXT_LEFT);
378             nk_labelf(nk, NK_TEXT_LEFT, "%f", xscale);
379             nk_labelf(nk, NK_TEXT_LEFT, "%f", yscale);
380 
381             nk_layout_row_begin(nk, NK_DYNAMIC, 30, 5);
382             int frame_left, frame_top, frame_right, frame_bottom;
383             glfwGetWindowFrameSize(window, &frame_left, &frame_top, &frame_right, &frame_bottom);
384             nk_layout_row_push(nk, 1.f / 3.f);
385             nk_label(nk, "Frame Size:", NK_TEXT_LEFT);
386             nk_layout_row_push(nk, 1.f / 6.f);
387             nk_labelf(nk, NK_TEXT_LEFT, "%i", frame_left);
388             nk_layout_row_push(nk, 1.f / 6.f);
389             nk_labelf(nk, NK_TEXT_LEFT, "%i", frame_top);
390             nk_layout_row_push(nk, 1.f / 6.f);
391             nk_labelf(nk, NK_TEXT_LEFT, "%i", frame_right);
392             nk_layout_row_push(nk, 1.f / 6.f);
393             nk_labelf(nk, NK_TEXT_LEFT, "%i", frame_bottom);
394             nk_layout_row_end(nk);
395 
396             nk_layout_row_begin(nk, NK_DYNAMIC, 30, 2);
397             float opacity = glfwGetWindowOpacity(window);
398             nk_layout_row_push(nk, 1.f / 3.f);
399             nk_labelf(nk, NK_TEXT_LEFT, "Opacity: %0.3f", opacity);
400             nk_layout_row_push(nk, 2.f / 3.f);
401             if (nk_slider_float(nk, 0.f, &opacity, 1.f, 0.001f))
402                 glfwSetWindowOpacity(window, opacity);
403             nk_layout_row_end(nk);
404 
405             nk_layout_row_begin(nk, NK_DYNAMIC, 30, 2);
406             int should_close = glfwWindowShouldClose(window);
407             nk_layout_row_push(nk, 1.f / 3.f);
408             if (nk_checkbox_label(nk, "Should Close", &should_close))
409                 glfwSetWindowShouldClose(window, should_close);
410             nk_layout_row_push(nk, 2.f / 3.f);
411             nk_checkbox_label(nk, "May Close", &may_close);
412             nk_layout_row_end(nk);
413 
414             nk_layout_row_dynamic(nk, 30, 1);
415             nk_label(nk, "Attributes", NK_TEXT_CENTERED);
416 
417             nk_layout_row_dynamic(nk, 30, width > 200 ? width / 200 : 1);
418 
419             int decorated = glfwGetWindowAttrib(window, GLFW_DECORATED);
420             if (nk_checkbox_label(nk, "Decorated", &decorated))
421                 glfwSetWindowAttrib(window, GLFW_DECORATED, decorated);
422 
423             int resizable = glfwGetWindowAttrib(window, GLFW_RESIZABLE);
424             if (nk_checkbox_label(nk, "Resizable", &resizable))
425                 glfwSetWindowAttrib(window, GLFW_RESIZABLE, resizable);
426 
427             int floating = glfwGetWindowAttrib(window, GLFW_FLOATING);
428             if (nk_checkbox_label(nk, "Floating", &floating))
429                 glfwSetWindowAttrib(window, GLFW_FLOATING, floating);
430 
431             int passthrough = glfwGetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH);
432             if (nk_checkbox_label(nk, "Mouse Passthrough", &passthrough))
433                 glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, passthrough);
434 
435             int auto_iconify = glfwGetWindowAttrib(window, GLFW_AUTO_ICONIFY);
436             if (nk_checkbox_label(nk, "Auto Iconify", &auto_iconify))
437                 glfwSetWindowAttrib(window, GLFW_AUTO_ICONIFY, auto_iconify);
438 
439             nk_value_bool(nk, "Focused", glfwGetWindowAttrib(window, GLFW_FOCUSED));
440             nk_value_bool(nk, "Hovered", glfwGetWindowAttrib(window, GLFW_HOVERED));
441             nk_value_bool(nk, "Visible", glfwGetWindowAttrib(window, GLFW_VISIBLE));
442             nk_value_bool(nk, "Iconified", glfwGetWindowAttrib(window, GLFW_ICONIFIED));
443             nk_value_bool(nk, "Maximized", glfwGetWindowAttrib(window, GLFW_MAXIMIZED));
444         }
445         nk_end(nk);
446 
447         glClear(GL_COLOR_BUFFER_BIT);
448         nk_glfw3_render(NK_ANTI_ALIASING_ON);
449         glfwSwapBuffers(window);
450 
451         glfwWaitEvents();
452     }
453 
454     nk_glfw3_shutdown();
455     glfwTerminate();
456     exit(EXIT_SUCCESS);
457 }
458 
459