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