opacity.c 3.1 KB

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