gamma.c 5.5 KB

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