opacity.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/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. glfwWindowHint(GLFW_WIN32_KEYBOARD_MENU, GLFW_TRUE);
  54. window = glfwCreateWindow(400, 400, "Opacity", NULL, NULL);
  55. if (!window)
  56. {
  57. glfwTerminate();
  58. exit(EXIT_FAILURE);
  59. }
  60. glfwMakeContextCurrent(window);
  61. gladLoadGL(glfwGetProcAddress);
  62. glfwSwapInterval(1);
  63. nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
  64. nk_glfw3_font_stash_begin(&atlas);
  65. nk_glfw3_font_stash_end();
  66. while (!glfwWindowShouldClose(window))
  67. {
  68. int width, height;
  69. struct nk_rect area;
  70. glfwGetWindowSize(window, &width, &height);
  71. area = nk_rect(0.f, 0.f, (float) width, (float) height);
  72. glClear(GL_COLOR_BUFFER_BIT);
  73. nk_glfw3_new_frame();
  74. if (nk_begin(nk, "", area, 0))
  75. {
  76. float opacity = glfwGetWindowOpacity(window);
  77. nk_layout_row_dynamic(nk, 30, 2);
  78. if (nk_slider_float(nk, 0.f, &opacity, 1.f, 0.001f))
  79. glfwSetWindowOpacity(window, opacity);
  80. nk_labelf(nk, NK_TEXT_LEFT, "%0.3f", opacity);
  81. }
  82. nk_end(nk);
  83. nk_glfw3_render(NK_ANTI_ALIASING_ON);
  84. glfwSwapBuffers(window);
  85. glfwWaitEventsTimeout(1.0);
  86. }
  87. nk_glfw3_shutdown();
  88. glfwTerminate();
  89. exit(EXIT_SUCCESS);
  90. }