1 //========================================================================
2 // Gamma correction test program
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 // This program is used to test the gamma correction functionality for
27 // both full screen and windowed mode windows
28 //
29 //========================================================================
30
31 #define GLAD_GL_IMPLEMENTATION
32 #include <glad/gl.h>
33 #define GLFW_INCLUDE_NONE
34 #include <GLFW/glfw3.h>
35
36 #define NK_IMPLEMENTATION
37 #define NK_INCLUDE_FIXED_TYPES
38 #define NK_INCLUDE_FONT_BAKING
39 #define NK_INCLUDE_DEFAULT_FONT
40 #define NK_INCLUDE_DEFAULT_ALLOCATOR
41 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
42 #define NK_INCLUDE_STANDARD_VARARGS
43 #define NK_BUTTON_TRIGGER_ON_RELEASE
44 #include <nuklear.h>
45
46 #define NK_GLFW_GL2_IMPLEMENTATION
47 #include <nuklear_glfw_gl2.h>
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
error_callback(int error,const char * description)53 static void error_callback(int error, const char* description)
54 {
55 fprintf(stderr, "Error: %s\n", description);
56 }
57
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)58 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
59 {
60 if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
61 glfwSetWindowShouldClose(window, GLFW_TRUE);
62 }
63
chart_ramp_array(struct nk_context * nk,struct nk_color color,int count,unsigned short int * values)64 static void chart_ramp_array(struct nk_context* nk,
65 struct nk_color color,
66 int count, unsigned short int* values)
67 {
68 if (nk_chart_begin_colored(nk, NK_CHART_LINES,
69 color, nk_rgb(255, 255, 255),
70 count, 0, 65535))
71 {
72 int i;
73 for (i = 0; i < count; i++)
74 {
75 char buffer[1024];
76 if (nk_chart_push(nk, values[i]))
77 {
78 snprintf(buffer, sizeof(buffer), "#%u: %u (%0.5f) ",
79 i, values[i], values[i] / 65535.f);
80 nk_tooltip(nk, buffer);
81 }
82 }
83
84 nk_chart_end(nk);
85 }
86 }
87
main(int argc,char ** argv)88 int main(int argc, char** argv)
89 {
90 GLFWmonitor* monitor = NULL;
91 GLFWwindow* window;
92 GLFWgammaramp orig_ramp;
93 struct nk_context* nk;
94 struct nk_font_atlas* atlas;
95 float gamma_value = 1.f;
96
97 glfwSetErrorCallback(error_callback);
98
99 if (!glfwInit())
100 exit(EXIT_FAILURE);
101
102 monitor = glfwGetPrimaryMonitor();
103
104 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
105 glfwWindowHint(GLFW_WIN32_KEYBOARD_MENU, GLFW_TRUE);
106
107 window = glfwCreateWindow(800, 400, "Gamma Test", NULL, NULL);
108 if (!window)
109 {
110 glfwTerminate();
111 exit(EXIT_FAILURE);
112 }
113
114 {
115 const GLFWgammaramp* ramp = glfwGetGammaRamp(monitor);
116 if (!ramp)
117 {
118 glfwTerminate();
119 exit(EXIT_FAILURE);
120 }
121
122 const size_t array_size = ramp->size * sizeof(short);
123 orig_ramp.size = ramp->size;
124 orig_ramp.red = malloc(array_size);
125 orig_ramp.green = malloc(array_size);
126 orig_ramp.blue = malloc(array_size);
127 memcpy(orig_ramp.red, ramp->red, array_size);
128 memcpy(orig_ramp.green, ramp->green, array_size);
129 memcpy(orig_ramp.blue, ramp->blue, array_size);
130 }
131
132 glfwMakeContextCurrent(window);
133 gladLoadGL(glfwGetProcAddress);
134 glfwSwapInterval(1);
135
136 nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
137 nk_glfw3_font_stash_begin(&atlas);
138 nk_glfw3_font_stash_end();
139
140 glfwSetKeyCallback(window, key_callback);
141
142 while (!glfwWindowShouldClose(window))
143 {
144 int width, height;
145 struct nk_rect area;
146
147 glfwGetWindowSize(window, &width, &height);
148 area = nk_rect(0.f, 0.f, (float) width, (float) height);
149 nk_window_set_bounds(nk, "", area);
150
151 glClear(GL_COLOR_BUFFER_BIT);
152 nk_glfw3_new_frame();
153 if (nk_begin(nk, "", area, 0))
154 {
155 const GLFWgammaramp* ramp;
156
157 nk_layout_row_dynamic(nk, 30, 3);
158 if (nk_slider_float(nk, 0.1f, &gamma_value, 5.f, 0.1f))
159 glfwSetGamma(monitor, gamma_value);
160 nk_labelf(nk, NK_TEXT_LEFT, "%0.1f", gamma_value);
161 if (nk_button_label(nk, "Revert"))
162 glfwSetGammaRamp(monitor, &orig_ramp);
163
164 ramp = glfwGetGammaRamp(monitor);
165
166 nk_layout_row_dynamic(nk, height - 60.f, 3);
167 chart_ramp_array(nk, nk_rgb(255, 0, 0), ramp->size, ramp->red);
168 chart_ramp_array(nk, nk_rgb(0, 255, 0), ramp->size, ramp->green);
169 chart_ramp_array(nk, nk_rgb(0, 0, 255), ramp->size, ramp->blue);
170 }
171
172 nk_end(nk);
173 nk_glfw3_render(NK_ANTI_ALIASING_ON);
174
175 glfwSwapBuffers(window);
176 glfwWaitEventsTimeout(1.0);
177 }
178
179 free(orig_ramp.red);
180 free(orig_ramp.green);
181 free(orig_ramp.blue);
182
183 nk_glfw3_shutdown();
184 glfwTerminate();
185 exit(EXIT_SUCCESS);
186 }
187
188