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. #include "../../nuklear.h"
  22. #include "nuklear_glfw_gl2.h"
  23. #define STB_IMAGE_IMPLEMENTATION
  24. #include "../../demo/common/filebrowser/stb_image.h"
  25. #define WINDOW_WIDTH 1200
  26. #define WINDOW_HEIGHT 800
  27. /* ===============================================================
  28. *
  29. * EXAMPLE
  30. *
  31. * ===============================================================*/
  32. /* This are some code examples to provide a small overview of what can be
  33. * done with this library. To try out an example uncomment the defines */
  34. /* #define INCLUDE_ALL */
  35. /* #define INCLUDE_STYLE */
  36. /* #define INCLUDE_CALCULATOR */
  37. /* #define INCLUDE_CANVAS */
  38. /* #define INCLUDE_FILE_BROWSER */
  39. #define INCLUDE_OVERVIEW
  40. /* #define INCLUDE_CONFIGURATOR */
  41. /* #define INCLUDE_NODE_EDITOR */
  42. #ifdef INCLUDE_ALL
  43. #define INCLUDE_STYLE
  44. #define INCLUDE_CALCULATOR
  45. #define INCLUDE_CANVAS
  46. #define INCLUDE_FILE_BROWSER
  47. #define INCLUDE_OVERVIEW
  48. #define INCLUDE_CONFIGURATOR
  49. #define INCLUDE_NODE_EDITOR
  50. #endif
  51. #ifdef INCLUDE_STYLE
  52. #include "../../demo/common/style.c"
  53. #endif
  54. #ifdef INCLUDE_CALCULATOR
  55. #include "../../demo/common/calculator.c"
  56. #endif
  57. #ifdef INCLUDE_CANVAS
  58. #include "../../demo/common/canvas.c"
  59. #endif
  60. #ifdef INCLUDE_FILE_BROWSER
  61. #include "../../demo/common/file_browser.c"
  62. #endif
  63. #ifdef INCLUDE_OVERVIEW
  64. #include "../../demo/common/overview.c"
  65. #endif
  66. #ifdef INCLUDE_CONFIGURATOR
  67. #include "../../demo/common/style_configurator.c"
  68. #endif
  69. #ifdef INCLUDE_NODE_EDITOR
  70. #include "../../demo/common/node_editor.c"
  71. #endif
  72. /* ===============================================================
  73. *
  74. * DEMO
  75. *
  76. * ===============================================================*/
  77. static void error_callback(int e, const char *d)
  78. {printf("Error %d: %s\n", e, d);}
  79. int main(void)
  80. {
  81. /* Platform */
  82. static GLFWwindow *win;
  83. int width = 0, height = 0;
  84. /* GUI */
  85. struct nk_context *ctx;
  86. struct nk_colorf bg;
  87. #ifdef INCLUDE_FILE_BROWSER
  88. struct file_browser browser;
  89. struct media media;
  90. #endif
  91. #ifdef INCLUDE_CONFIGURATOR
  92. static struct nk_color color_table[NK_COLOR_COUNT];
  93. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  94. #endif
  95. /* GLFW */
  96. glfwSetErrorCallback(error_callback);
  97. if (!glfwInit()) {
  98. fprintf(stdout, "[GFLW] failed to init!\n");
  99. exit(1);
  100. }
  101. win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
  102. glfwMakeContextCurrent(win);
  103. glfwGetWindowSize(win, &width, &height);
  104. /* GUI */
  105. ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
  106. /* Load Fonts: if none of these are loaded a default font will be used */
  107. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  108. {struct nk_font_atlas *atlas;
  109. nk_glfw3_font_stash_begin(&atlas);
  110. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  111. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  112. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  113. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  114. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  115. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  116. nk_glfw3_font_stash_end();
  117. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  118. /*nk_style_set_font(ctx, &droid->handle);*/}
  119. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  120. #ifdef INCLUDE_FILE_BROWSER
  121. /* icons */
  122. glEnable(GL_TEXTURE_2D);
  123. media.icons.home = icon_load("../../demo/common/filebrowser/icon/home.png");
  124. media.icons.directory = icon_load("../../demo/common/filebrowser/icon/directory.png");
  125. media.icons.computer = icon_load("../../demo/common/filebrowser/icon/computer.png");
  126. media.icons.desktop = icon_load("../../demo/common/filebrowser/icon/desktop.png");
  127. media.icons.default_file = icon_load("../../demo/common/filebrowser/icon/default.png");
  128. media.icons.text_file = icon_load("../../demo/common/filebrowser/icon/text.png");
  129. media.icons.music_file = icon_load("../../demo/common/filebrowser/icon/music.png");
  130. media.icons.font_file = icon_load("../../demo/common/filebrowser/icon/font.png");
  131. media.icons.img_file = icon_load("../../demo/common/filebrowser/icon/img.png");
  132. media.icons.movie_file = icon_load("../../demo/common/filebrowser/icon/movie.png");
  133. media_init(&media);
  134. file_browser_init(&browser, &media);
  135. #endif
  136. while (!glfwWindowShouldClose(win))
  137. {
  138. /* Input */
  139. glfwPollEvents();
  140. nk_glfw3_new_frame();
  141. /* GUI */
  142. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  143. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  144. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  145. {
  146. enum {EASY, HARD};
  147. static int op = EASY;
  148. static int property = 20;
  149. nk_layout_row_static(ctx, 30, 80, 1);
  150. if (nk_button_label(ctx, "button"))
  151. fprintf(stdout, "button pressed\n");
  152. nk_layout_row_dynamic(ctx, 30, 2);
  153. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  154. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  155. nk_layout_row_dynamic(ctx, 25, 1);
  156. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  157. nk_layout_row_dynamic(ctx, 20, 1);
  158. nk_label(ctx, "background:", NK_TEXT_LEFT);
  159. nk_layout_row_dynamic(ctx, 25, 1);
  160. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  161. nk_layout_row_dynamic(ctx, 120, 1);
  162. bg = nk_color_picker(ctx, bg, NK_RGBA);
  163. nk_layout_row_dynamic(ctx, 25, 1);
  164. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  165. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  166. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  167. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  168. nk_combo_end(ctx);
  169. }
  170. }
  171. nk_end(ctx);
  172. /* -------------- EXAMPLES ---------------- */
  173. #ifdef INCLUDE_CALCULATOR
  174. calculator(ctx);
  175. #endif
  176. #ifdef INCLUDE_CANVAS
  177. canvas(ctx);
  178. #endif
  179. #ifdef INCLUDE_FILE_BROWSER
  180. file_browser_run(&browser, ctx);
  181. #endif
  182. #ifdef INCLUDE_OVERVIEW
  183. overview(ctx);
  184. #endif
  185. #ifdef INCLUDE_CONFIGURATOR
  186. style_configurator(ctx, color_table);
  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. }