1 //========================================================================
2 // Gamma correction test program
3 // Copyright (c) Camilla Berglund <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 #include <glad/glad.h>
32 #include <GLFW/glfw3.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "getopt.h"
38
39 #define STEP_SIZE 0.1f
40
41 static GLfloat gamma_value = 1.0f;
42
usage(void)43 static void usage(void)
44 {
45 printf("Usage: gamma [-h] [-f]\n");
46 printf("Options:\n");
47 printf(" -f create full screen window\n");
48 printf(" -h show this help\n");
49 }
50
set_gamma(GLFWwindow * window,float value)51 static void set_gamma(GLFWwindow* window, float value)
52 {
53 GLFWmonitor* monitor = glfwGetWindowMonitor(window);
54 if (!monitor)
55 monitor = glfwGetPrimaryMonitor();
56
57 gamma_value = value;
58 printf("Gamma: %f\n", gamma_value);
59 glfwSetGamma(monitor, gamma_value);
60 }
61
error_callback(int error,const char * description)62 static void error_callback(int error, const char* description)
63 {
64 fprintf(stderr, "Error: %s\n", description);
65 }
66
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)67 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
68 {
69 if (action != GLFW_PRESS)
70 return;
71
72 switch (key)
73 {
74 case GLFW_KEY_ESCAPE:
75 {
76 glfwSetWindowShouldClose(window, GLFW_TRUE);
77 break;
78 }
79
80 case GLFW_KEY_KP_ADD:
81 case GLFW_KEY_UP:
82 case GLFW_KEY_Q:
83 {
84 set_gamma(window, gamma_value + STEP_SIZE);
85 break;
86 }
87
88 case GLFW_KEY_KP_SUBTRACT:
89 case GLFW_KEY_DOWN:
90 case GLFW_KEY_W:
91 {
92 if (gamma_value - STEP_SIZE > 0.f)
93 set_gamma(window, gamma_value - STEP_SIZE);
94
95 break;
96 }
97 }
98 }
99
framebuffer_size_callback(GLFWwindow * window,int width,int height)100 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
101 {
102 glViewport(0, 0, width, height);
103 }
104
main(int argc,char ** argv)105 int main(int argc, char** argv)
106 {
107 int width, height, ch;
108 GLFWmonitor* monitor = NULL;
109 GLFWwindow* window;
110
111 glfwSetErrorCallback(error_callback);
112
113 if (!glfwInit())
114 exit(EXIT_FAILURE);
115
116 while ((ch = getopt(argc, argv, "fh")) != -1)
117 {
118 switch (ch)
119 {
120 case 'h':
121 usage();
122 exit(EXIT_SUCCESS);
123
124 case 'f':
125 monitor = glfwGetPrimaryMonitor();
126 break;
127
128 default:
129 usage();
130 exit(EXIT_FAILURE);
131 }
132 }
133
134 if (monitor)
135 {
136 const GLFWvidmode* mode = glfwGetVideoMode(monitor);
137
138 glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
139 glfwWindowHint(GLFW_RED_BITS, mode->redBits);
140 glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
141 glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
142
143 width = mode->width;
144 height = mode->height;
145 }
146 else
147 {
148 width = 200;
149 height = 200;
150 }
151
152 window = glfwCreateWindow(width, height, "Gamma Test", monitor, NULL);
153 if (!window)
154 {
155 glfwTerminate();
156 exit(EXIT_FAILURE);
157 }
158
159 set_gamma(window, 1.f);
160
161 glfwMakeContextCurrent(window);
162 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
163 glfwSwapInterval(1);
164
165 glfwSetKeyCallback(window, key_callback);
166 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
167
168 glMatrixMode(GL_PROJECTION);
169 glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
170 glMatrixMode(GL_MODELVIEW);
171
172 glClearColor(0.5f, 0.5f, 0.5f, 0);
173
174 while (!glfwWindowShouldClose(window))
175 {
176 glClear(GL_COLOR_BUFFER_BIT);
177
178 glColor3f(0.8f, 0.2f, 0.4f);
179 glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
180
181 glfwSwapBuffers(window);
182 glfwWaitEvents();
183 }
184
185 glfwTerminate();
186 exit(EXIT_SUCCESS);
187 }
188
189