opacity.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //========================================================================
  2. // Window opacity 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. #include <glad/gl.h>
  26. #define GLFW_INCLUDE_NONE
  27. #include <GLFW/glfw3.h>
  28. #define NK_IMPLEMENTATION
  29. #define NK_INCLUDE_FIXED_TYPES
  30. #define NK_INCLUDE_FONT_BAKING
  31. #define NK_INCLUDE_DEFAULT_FONT
  32. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  33. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  34. #define NK_INCLUDE_STANDARD_VARARGS
  35. #include <nuklear.h>
  36. #define NK_GLFW_GL2_IMPLEMENTATION
  37. #include <nuklear_glfw_gl2.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. static void error_callback(int error, const char* description)
  41. {
  42. fprintf(stderr, "Error: %s\n", description);
  43. }
  44. int main(int argc, char** argv)
  45. {
  46. GLFWwindow* window;
  47. struct nk_context* nk;
  48. struct nk_font_atlas* atlas;
  49. glfwSetErrorCallback(error_callback);
  50. if (!glfwInit())
  51. exit(EXIT_FAILURE);
  52. glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
  53. window = glfwCreateWindow(400, 400, "Opacity", NULL, NULL);
  54. if (!window)
  55. {
  56. glfwTerminate();
  57. exit(EXIT_FAILURE);
  58. }
  59. glfwMakeContextCurrent(window);
  60. gladLoadGL(glfwGetProcAddress);
  61. glfwSwapInterval(1);
  62. nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
  63. nk_glfw3_font_stash_begin(&atlas);
  64. nk_glfw3_font_stash_end();
  65. while (!glfwWindowShouldClose(window))
  66. {
  67. int width, height;
  68. struct nk_rect area;
  69. glfwGetWindowSize(window, &width, &height);
  70. area = nk_rect(0.f, 0.f, (float) width, (float) height);
  71. glClear(GL_COLOR_BUFFER_BIT);
  72. nk_glfw3_new_frame();
  73. if (nk_begin(nk, "", area, 0))
  74. {
  75. float opacity = glfwGetWindowOpacity(window);
  76. nk_layout_row_dynamic(nk, 30, 2);
  77. if (nk_slider_float(nk, 0.f, &opacity, 1.f, 0.001f))
  78. glfwSetWindowOpacity(window, opacity);
  79. nk_labelf(nk, NK_TEXT_LEFT, "%0.3f", opacity);
  80. }
  81. nk_end(nk);
  82. nk_glfw3_render(NK_ANTI_ALIASING_ON);
  83. glfwSwapBuffers(window);
  84. glfwWaitEventsTimeout(1.0);
  85. }
  86. nk_glfw3_shutdown();
  87. glfwTerminate();
  88. exit(EXIT_SUCCESS);
  89. }