1b877906bSopenharmony_ci//========================================================================
2b877906bSopenharmony_ci// Gamma correction test program
3b877906bSopenharmony_ci// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4b877906bSopenharmony_ci//
5b877906bSopenharmony_ci// This software is provided 'as-is', without any express or implied
6b877906bSopenharmony_ci// warranty. In no event will the authors be held liable for any damages
7b877906bSopenharmony_ci// arising from the use of this software.
8b877906bSopenharmony_ci//
9b877906bSopenharmony_ci// Permission is granted to anyone to use this software for any purpose,
10b877906bSopenharmony_ci// including commercial applications, and to alter it and redistribute it
11b877906bSopenharmony_ci// freely, subject to the following restrictions:
12b877906bSopenharmony_ci//
13b877906bSopenharmony_ci// 1. The origin of this software must not be misrepresented; you must not
14b877906bSopenharmony_ci//    claim that you wrote the original software. If you use this software
15b877906bSopenharmony_ci//    in a product, an acknowledgment in the product documentation would
16b877906bSopenharmony_ci//    be appreciated but is not required.
17b877906bSopenharmony_ci//
18b877906bSopenharmony_ci// 2. Altered source versions must be plainly marked as such, and must not
19b877906bSopenharmony_ci//    be misrepresented as being the original software.
20b877906bSopenharmony_ci//
21b877906bSopenharmony_ci// 3. This notice may not be removed or altered from any source
22b877906bSopenharmony_ci//    distribution.
23b877906bSopenharmony_ci//
24b877906bSopenharmony_ci//========================================================================
25b877906bSopenharmony_ci//
26b877906bSopenharmony_ci// This program is used to test the gamma correction functionality for
27b877906bSopenharmony_ci// both full screen and windowed mode windows
28b877906bSopenharmony_ci//
29b877906bSopenharmony_ci//========================================================================
30b877906bSopenharmony_ci
31b877906bSopenharmony_ci#define GLAD_GL_IMPLEMENTATION
32b877906bSopenharmony_ci#include <glad/gl.h>
33b877906bSopenharmony_ci#define GLFW_INCLUDE_NONE
34b877906bSopenharmony_ci#include <GLFW/glfw3.h>
35b877906bSopenharmony_ci
36b877906bSopenharmony_ci#define NK_IMPLEMENTATION
37b877906bSopenharmony_ci#define NK_INCLUDE_FIXED_TYPES
38b877906bSopenharmony_ci#define NK_INCLUDE_FONT_BAKING
39b877906bSopenharmony_ci#define NK_INCLUDE_DEFAULT_FONT
40b877906bSopenharmony_ci#define NK_INCLUDE_DEFAULT_ALLOCATOR
41b877906bSopenharmony_ci#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
42b877906bSopenharmony_ci#define NK_INCLUDE_STANDARD_VARARGS
43b877906bSopenharmony_ci#define NK_BUTTON_TRIGGER_ON_RELEASE
44b877906bSopenharmony_ci#include <nuklear.h>
45b877906bSopenharmony_ci
46b877906bSopenharmony_ci#define NK_GLFW_GL2_IMPLEMENTATION
47b877906bSopenharmony_ci#include <nuklear_glfw_gl2.h>
48b877906bSopenharmony_ci
49b877906bSopenharmony_ci#include <stdio.h>
50b877906bSopenharmony_ci#include <stdlib.h>
51b877906bSopenharmony_ci#include <string.h>
52b877906bSopenharmony_ci
53b877906bSopenharmony_cistatic void error_callback(int error, const char* description)
54b877906bSopenharmony_ci{
55b877906bSopenharmony_ci    fprintf(stderr, "Error: %s\n", description);
56b877906bSopenharmony_ci}
57b877906bSopenharmony_ci
58b877906bSopenharmony_cistatic void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
59b877906bSopenharmony_ci{
60b877906bSopenharmony_ci    if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
61b877906bSopenharmony_ci        glfwSetWindowShouldClose(window, GLFW_TRUE);
62b877906bSopenharmony_ci}
63b877906bSopenharmony_ci
64b877906bSopenharmony_cistatic void chart_ramp_array(struct nk_context* nk,
65b877906bSopenharmony_ci                             struct nk_color color,
66b877906bSopenharmony_ci                             int count, unsigned short int* values)
67b877906bSopenharmony_ci{
68b877906bSopenharmony_ci    if (nk_chart_begin_colored(nk, NK_CHART_LINES,
69b877906bSopenharmony_ci                               color, nk_rgb(255, 255, 255),
70b877906bSopenharmony_ci                               count, 0, 65535))
71b877906bSopenharmony_ci    {
72b877906bSopenharmony_ci        int i;
73b877906bSopenharmony_ci        for (i = 0;  i < count;  i++)
74b877906bSopenharmony_ci        {
75b877906bSopenharmony_ci            char buffer[1024];
76b877906bSopenharmony_ci            if (nk_chart_push(nk, values[i]))
77b877906bSopenharmony_ci            {
78b877906bSopenharmony_ci                snprintf(buffer, sizeof(buffer), "#%u: %u (%0.5f) ",
79b877906bSopenharmony_ci                         i, values[i], values[i] / 65535.f);
80b877906bSopenharmony_ci                nk_tooltip(nk, buffer);
81b877906bSopenharmony_ci            }
82b877906bSopenharmony_ci        }
83b877906bSopenharmony_ci
84b877906bSopenharmony_ci        nk_chart_end(nk);
85b877906bSopenharmony_ci    }
86b877906bSopenharmony_ci}
87b877906bSopenharmony_ci
88b877906bSopenharmony_ciint main(int argc, char** argv)
89b877906bSopenharmony_ci{
90b877906bSopenharmony_ci    GLFWmonitor* monitor = NULL;
91b877906bSopenharmony_ci    GLFWwindow* window;
92b877906bSopenharmony_ci    GLFWgammaramp orig_ramp;
93b877906bSopenharmony_ci    struct nk_context* nk;
94b877906bSopenharmony_ci    struct nk_font_atlas* atlas;
95b877906bSopenharmony_ci    float gamma_value = 1.f;
96b877906bSopenharmony_ci
97b877906bSopenharmony_ci    glfwSetErrorCallback(error_callback);
98b877906bSopenharmony_ci
99b877906bSopenharmony_ci    if (!glfwInit())
100b877906bSopenharmony_ci        exit(EXIT_FAILURE);
101b877906bSopenharmony_ci
102b877906bSopenharmony_ci    monitor = glfwGetPrimaryMonitor();
103b877906bSopenharmony_ci
104b877906bSopenharmony_ci    glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
105b877906bSopenharmony_ci    glfwWindowHint(GLFW_WIN32_KEYBOARD_MENU, GLFW_TRUE);
106b877906bSopenharmony_ci
107b877906bSopenharmony_ci    window = glfwCreateWindow(800, 400, "Gamma Test", NULL, NULL);
108b877906bSopenharmony_ci    if (!window)
109b877906bSopenharmony_ci    {
110b877906bSopenharmony_ci        glfwTerminate();
111b877906bSopenharmony_ci        exit(EXIT_FAILURE);
112b877906bSopenharmony_ci    }
113b877906bSopenharmony_ci
114b877906bSopenharmony_ci    {
115b877906bSopenharmony_ci        const GLFWgammaramp* ramp = glfwGetGammaRamp(monitor);
116b877906bSopenharmony_ci        if (!ramp)
117b877906bSopenharmony_ci        {
118b877906bSopenharmony_ci            glfwTerminate();
119b877906bSopenharmony_ci            exit(EXIT_FAILURE);
120b877906bSopenharmony_ci        }
121b877906bSopenharmony_ci
122b877906bSopenharmony_ci        const size_t array_size = ramp->size * sizeof(short);
123b877906bSopenharmony_ci        orig_ramp.size = ramp->size;
124b877906bSopenharmony_ci        orig_ramp.red = malloc(array_size);
125b877906bSopenharmony_ci        orig_ramp.green = malloc(array_size);
126b877906bSopenharmony_ci        orig_ramp.blue = malloc(array_size);
127b877906bSopenharmony_ci        memcpy(orig_ramp.red, ramp->red, array_size);
128b877906bSopenharmony_ci        memcpy(orig_ramp.green, ramp->green, array_size);
129b877906bSopenharmony_ci        memcpy(orig_ramp.blue, ramp->blue, array_size);
130b877906bSopenharmony_ci    }
131b877906bSopenharmony_ci
132b877906bSopenharmony_ci    glfwMakeContextCurrent(window);
133b877906bSopenharmony_ci    gladLoadGL(glfwGetProcAddress);
134b877906bSopenharmony_ci    glfwSwapInterval(1);
135b877906bSopenharmony_ci
136b877906bSopenharmony_ci    nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
137b877906bSopenharmony_ci    nk_glfw3_font_stash_begin(&atlas);
138b877906bSopenharmony_ci    nk_glfw3_font_stash_end();
139b877906bSopenharmony_ci
140b877906bSopenharmony_ci    glfwSetKeyCallback(window, key_callback);
141b877906bSopenharmony_ci
142b877906bSopenharmony_ci    while (!glfwWindowShouldClose(window))
143b877906bSopenharmony_ci    {
144b877906bSopenharmony_ci        int width, height;
145b877906bSopenharmony_ci        struct nk_rect area;
146b877906bSopenharmony_ci
147b877906bSopenharmony_ci        glfwGetWindowSize(window, &width, &height);
148b877906bSopenharmony_ci        area = nk_rect(0.f, 0.f, (float) width, (float) height);
149b877906bSopenharmony_ci        nk_window_set_bounds(nk, "", area);
150b877906bSopenharmony_ci
151b877906bSopenharmony_ci        glClear(GL_COLOR_BUFFER_BIT);
152b877906bSopenharmony_ci        nk_glfw3_new_frame();
153b877906bSopenharmony_ci        if (nk_begin(nk, "", area, 0))
154b877906bSopenharmony_ci        {
155b877906bSopenharmony_ci            const GLFWgammaramp* ramp;
156b877906bSopenharmony_ci
157b877906bSopenharmony_ci            nk_layout_row_dynamic(nk, 30, 3);
158b877906bSopenharmony_ci            if (nk_slider_float(nk, 0.1f, &gamma_value, 5.f, 0.1f))
159b877906bSopenharmony_ci                glfwSetGamma(monitor, gamma_value);
160b877906bSopenharmony_ci            nk_labelf(nk, NK_TEXT_LEFT, "%0.1f", gamma_value);
161b877906bSopenharmony_ci            if (nk_button_label(nk, "Revert"))
162b877906bSopenharmony_ci                glfwSetGammaRamp(monitor, &orig_ramp);
163b877906bSopenharmony_ci
164b877906bSopenharmony_ci            ramp = glfwGetGammaRamp(monitor);
165b877906bSopenharmony_ci
166b877906bSopenharmony_ci            nk_layout_row_dynamic(nk, height - 60.f, 3);
167b877906bSopenharmony_ci            chart_ramp_array(nk, nk_rgb(255, 0, 0), ramp->size, ramp->red);
168b877906bSopenharmony_ci            chart_ramp_array(nk, nk_rgb(0, 255, 0), ramp->size, ramp->green);
169b877906bSopenharmony_ci            chart_ramp_array(nk, nk_rgb(0, 0, 255), ramp->size, ramp->blue);
170b877906bSopenharmony_ci        }
171b877906bSopenharmony_ci
172b877906bSopenharmony_ci        nk_end(nk);
173b877906bSopenharmony_ci        nk_glfw3_render(NK_ANTI_ALIASING_ON);
174b877906bSopenharmony_ci
175b877906bSopenharmony_ci        glfwSwapBuffers(window);
176b877906bSopenharmony_ci        glfwWaitEventsTimeout(1.0);
177b877906bSopenharmony_ci    }
178b877906bSopenharmony_ci
179b877906bSopenharmony_ci    free(orig_ramp.red);
180b877906bSopenharmony_ci    free(orig_ramp.green);
181b877906bSopenharmony_ci    free(orig_ramp.blue);
182b877906bSopenharmony_ci
183b877906bSopenharmony_ci    nk_glfw3_shutdown();
184b877906bSopenharmony_ci    glfwTerminate();
185b877906bSopenharmony_ci    exit(EXIT_SUCCESS);
186b877906bSopenharmony_ci}
187b877906bSopenharmony_ci
188