main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* nuklear - v1.32.0 - public domain */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <limits.h>
  10. #include <time.h>
  11. #include <GLFW/glfw3.h>
  12. #define NK_INCLUDE_FIXED_TYPES
  13. #define NK_INCLUDE_STANDARD_IO
  14. #define NK_INCLUDE_STANDARD_VARARGS
  15. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  16. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17. #define NK_INCLUDE_FONT_BAKING
  18. #define NK_INCLUDE_DEFAULT_FONT
  19. #define NK_IMPLEMENTATION
  20. #define NK_GLFW_GL2_IMPLEMENTATION
  21. #define NK_KEYSTATE_BASED_INPUT
  22. #include "../../nuklear.h"
  23. #include "nuklear_glfw_gl2.h"
  24. #define STB_IMAGE_IMPLEMENTATION
  25. #include "../../demo/common/filebrowser/stb_image.h"
  26. #define WINDOW_WIDTH 1200
  27. #define WINDOW_HEIGHT 800
  28. /* ===============================================================
  29. *
  30. * EXAMPLE
  31. *
  32. * ===============================================================*/
  33. /* This are some code examples to provide a small overview of what can be
  34. * done with this library. To try out an example uncomment the defines */
  35. /* #define INCLUDE_ALL */
  36. /* #define INCLUDE_STYLE */
  37. /* #define INCLUDE_CALCULATOR */
  38. #define INCLUDE_CANVAS
  39. /* #define INCLUDE_FILE_BROWSER */
  40. /* #define INCLUDE_OVERVIEW */
  41. /* #define INCLUDE_NODE_EDITOR */
  42. #ifdef INCLUDE_ALL
  43. #define INCLUDE_STYLE
  44. #define INCLUDE_CALCULATOR
  45. #define INCLUDE_CANVAS
  46. #define INCLUDE_OVERVIEW
  47. #define INCLUDE_NODE_EDITOR
  48. #endif
  49. #ifdef INCLUDE_STYLE
  50. #include "../../demo/common/style.c"
  51. #endif
  52. #ifdef INCLUDE_CALCULATOR
  53. #include "../../demo/common/calculator.c"
  54. #endif
  55. #ifdef INCLUDE_CANVAS
  56. #include "../../demo/common/canvas.c"
  57. #endif
  58. #ifdef INCLUDE_FILE_BROWSER
  59. #include "../../demo/common/file_browser.c"
  60. #endif
  61. #ifdef INCLUDE_OVERVIEW
  62. #include "../../demo/common/overview.c"
  63. #endif
  64. #ifdef INCLUDE_NODE_EDITOR
  65. #include "../../demo/common/node_editor.c"
  66. #endif
  67. /* ===============================================================
  68. *
  69. * DEMO
  70. *
  71. * ===============================================================*/
  72. static void error_callback(int e, const char *d)
  73. {printf("Error %d: %s\n", e, d);}
  74. int main(void)
  75. {
  76. /* Platform */
  77. static GLFWwindow *win;
  78. int width = 0, height = 0;
  79. /* GUI */
  80. struct nk_context *ctx;
  81. struct nk_colorf bg;
  82. #ifdef INCLUDE_FILE_BROWSER
  83. struct file_browser browser;
  84. struct media media;
  85. #endif
  86. /* GLFW */
  87. glfwSetErrorCallback(error_callback);
  88. if (!glfwInit()) {
  89. fprintf(stdout, "[GFLW] failed to init!\n");
  90. exit(1);
  91. }
  92. win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
  93. glfwMakeContextCurrent(win);
  94. glfwGetWindowSize(win, &width, &height);
  95. /* GUI */
  96. ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
  97. /* Load Fonts: if none of these are loaded a default font will be used */
  98. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  99. {struct nk_font_atlas *atlas;
  100. nk_glfw3_font_stash_begin(&atlas);
  101. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  102. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  103. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  104. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  105. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  106. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  107. nk_glfw3_font_stash_end();
  108. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  109. /*nk_style_set_font(ctx, &droid->handle);*/}
  110. #ifdef INCLUDE_STYLE
  111. /* ease regression testing during Nuklear release process; not needed for anything else */
  112. #ifdef STYLE_WHITE
  113. set_style(ctx, THEME_WHITE);
  114. #elif defined(STYLE_RED)
  115. set_style(ctx, THEME_RED);
  116. #elif defined(STYLE_BLUE)
  117. set_style(ctx, THEME_BLUE);
  118. #elif defined(STYLE_DARK)
  119. set_style(ctx, THEME_DARK);
  120. #endif
  121. #endif
  122. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  123. #ifdef INCLUDE_FILE_BROWSER
  124. /* icons */
  125. glEnable(GL_TEXTURE_2D);
  126. media.icons.home = icon_load("../../demo/common/filebrowser/icon/home.png");
  127. media.icons.directory = icon_load("../../demo/common/filebrowser/icon/directory.png");
  128. media.icons.computer = icon_load("../../demo/common/filebrowser/icon/computer.png");
  129. media.icons.desktop = icon_load("../../demo/common/filebrowser/icon/desktop.png");
  130. media.icons.default_file = icon_load("../../demo/common/filebrowser/icon/default.png");
  131. media.icons.text_file = icon_load("../../demo/common/filebrowser/icon/text.png");
  132. media.icons.music_file = icon_load("../../demo/common/filebrowser/icon/music.png");
  133. media.icons.font_file = icon_load("../../demo/common/filebrowser/icon/font.png");
  134. media.icons.img_file = icon_load("../../demo/common/filebrowser/icon/img.png");
  135. media.icons.movie_file = icon_load("../../demo/common/filebrowser/icon/movie.png");
  136. media_init(&media);
  137. file_browser_init(&browser, &media);
  138. #endif
  139. while (!glfwWindowShouldClose(win))
  140. {
  141. /* Input */
  142. glfwPollEvents();
  143. nk_glfw3_new_frame();
  144. /* GUI */
  145. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  146. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  147. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  148. {
  149. enum {EASY, HARD};
  150. static int op = EASY;
  151. static int property = 20;
  152. nk_layout_row_static(ctx, 30, 80, 1);
  153. if (nk_button_label(ctx, "button"))
  154. fprintf(stdout, "button pressed\n");
  155. nk_layout_row_dynamic(ctx, 30, 2);
  156. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  157. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  158. nk_layout_row_dynamic(ctx, 25, 1);
  159. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  160. nk_layout_row_dynamic(ctx, 20, 1);
  161. nk_label(ctx, "background:", NK_TEXT_LEFT);
  162. nk_layout_row_dynamic(ctx, 25, 1);
  163. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  164. nk_layout_row_dynamic(ctx, 120, 1);
  165. bg = nk_color_picker(ctx, bg, NK_RGBA);
  166. nk_layout_row_dynamic(ctx, 25, 1);
  167. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  168. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  169. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  170. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  171. nk_combo_end(ctx);
  172. }
  173. }
  174. nk_end(ctx);
  175. /* -------------- EXAMPLES ---------------- */
  176. #ifdef INCLUDE_CALCULATOR
  177. calculator(ctx);
  178. #endif
  179. #ifdef INCLUDE_CANVAS
  180. canvas(ctx);
  181. #endif
  182. #ifdef INCLUDE_FILE_BROWSER
  183. file_browser_run(&browser, ctx);
  184. #endif
  185. #ifdef INCLUDE_OVERVIEW
  186. overview(ctx);
  187. #endif
  188. #ifdef INCLUDE_NODE_EDITOR
  189. node_editor(ctx);
  190. #endif
  191. /* ----------------------------------------- */
  192. /* Draw */
  193. glfwGetWindowSize(win, &width, &height);
  194. glViewport(0, 0, width, height);
  195. glClear(GL_COLOR_BUFFER_BIT);
  196. glClearColor(bg.r, bg.g, bg.b, bg.a);
  197. /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
  198. * with blending, scissor, face culling and depth test and defaults everything
  199. * back into a default state. Make sure to either save and restore or
  200. * reset your own state after drawing rendering the UI. */
  201. nk_glfw3_render(NK_ANTI_ALIASING_ON);
  202. glfwSwapBuffers(win);
  203. }
  204. #ifdef INCLUDE_FILE_BROWSER
  205. glDeleteTextures(1,(const GLuint*)&media.icons.home.handle.id);
  206. glDeleteTextures(1,(const GLuint*)&media.icons.directory.handle.id);
  207. glDeleteTextures(1,(const GLuint*)&media.icons.computer.handle.id);
  208. glDeleteTextures(1,(const GLuint*)&media.icons.desktop.handle.id);
  209. glDeleteTextures(1,(const GLuint*)&media.icons.default_file.handle.id);
  210. glDeleteTextures(1,(const GLuint*)&media.icons.text_file.handle.id);
  211. glDeleteTextures(1,(const GLuint*)&media.icons.music_file.handle.id);
  212. glDeleteTextures(1,(const GLuint*)&media.icons.font_file.handle.id);
  213. glDeleteTextures(1,(const GLuint*)&media.icons.img_file.handle.id);
  214. glDeleteTextures(1,(const GLuint*)&media.icons.movie_file.handle.id);
  215. file_browser_free(&browser);
  216. #endif
  217. nk_glfw3_shutdown();
  218. glfwTerminate();
  219. return 0;
  220. }