main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* nuklear - 1.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 <GL/glew.h>
  12. #include <GLFW/glfw3.h>
  13. #define NK_INCLUDE_FIXED_TYPES
  14. #define NK_INCLUDE_STANDARD_IO
  15. #define NK_INCLUDE_STANDARD_VARARGS
  16. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  17. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  18. #define NK_INCLUDE_FONT_BAKING
  19. #define NK_INCLUDE_DEFAULT_FONT
  20. #define NK_IMPLEMENTATION
  21. #define NK_GLFW_GL3_IMPLEMENTATION
  22. #include "../../nuklear.h"
  23. #include "nuklear_glfw_gl3.h"
  24. #define WINDOW_WIDTH 1200
  25. #define WINDOW_HEIGHT 800
  26. #define MAX_VERTEX_BUFFER 512 * 1024
  27. #define MAX_ELEMENT_BUFFER 128 * 1024
  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_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_OVERVIEW
  47. #define INCLUDE_CONFIGURATOR
  48. #define INCLUDE_NODE_EDITOR
  49. #endif
  50. #ifdef INCLUDE_STYLE
  51. #include "../../demo/common/style.c"
  52. #endif
  53. #ifdef INCLUDE_CALCULATOR
  54. #include "../../demo/common/calculator.c"
  55. #endif
  56. #ifdef INCLUDE_CANVAS
  57. #include "../../demo/common/canvas.c"
  58. #endif
  59. #ifdef INCLUDE_OVERVIEW
  60. #include "../../demo/common/overview.c"
  61. #endif
  62. #ifdef INCLUDE_CONFIGURATOR
  63. #include "../../demo/common/style_configurator.c"
  64. #endif
  65. #ifdef INCLUDE_NODE_EDITOR
  66. #include "../../demo/common/node_editor.c"
  67. #endif
  68. /* ===============================================================
  69. *
  70. * DEMO
  71. *
  72. * ===============================================================*/
  73. static void error_callback(int e, const char *d)
  74. {printf("Error %d: %s\n", e, d);}
  75. int main(void)
  76. {
  77. /* Platform */
  78. struct nk_glfw glfw = {0};
  79. static GLFWwindow *win;
  80. int width = 0, height = 0;
  81. struct nk_context *ctx;
  82. struct nk_colorf bg;
  83. #ifdef INCLUDE_CONFIGURATOR
  84. static struct nk_color color_table[NK_COLOR_COUNT];
  85. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  86. #endif
  87. /* GLFW */
  88. glfwSetErrorCallback(error_callback);
  89. if (!glfwInit()) {
  90. fprintf(stdout, "[GFLW] failed to init!\n");
  91. exit(1);
  92. }
  93. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  94. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  95. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  96. #ifdef __APPLE__
  97. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  98. #endif
  99. win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
  100. glfwMakeContextCurrent(win);
  101. glfwGetWindowSize(win, &width, &height);
  102. /* OpenGL */
  103. glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
  104. glewExperimental = 1;
  105. if (glewInit() != GLEW_OK) {
  106. fprintf(stderr, "Failed to setup GLEW\n");
  107. exit(1);
  108. }
  109. ctx = nk_glfw3_init(&glfw, win, NK_GLFW3_INSTALL_CALLBACKS);
  110. /* Load Fonts: if none of these are loaded a default font will be used */
  111. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  112. {struct nk_font_atlas *atlas;
  113. nk_glfw3_font_stash_begin(&glfw, &atlas);
  114. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  115. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  116. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  117. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  118. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  119. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  120. nk_glfw3_font_stash_end(&glfw);
  121. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  122. /*nk_style_set_font(ctx, &droid->handle);*/}
  123. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  124. while (!glfwWindowShouldClose(win))
  125. {
  126. /* Input */
  127. glfwPollEvents();
  128. nk_glfw3_new_frame(&glfw);
  129. /* GUI */
  130. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  131. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  132. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  133. {
  134. enum {EASY, HARD};
  135. static int op = EASY;
  136. static int property = 20;
  137. nk_layout_row_static(ctx, 30, 80, 1);
  138. if (nk_button_label(ctx, "button"))
  139. fprintf(stdout, "button pressed\n");
  140. nk_layout_row_dynamic(ctx, 30, 2);
  141. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  142. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  143. nk_layout_row_dynamic(ctx, 25, 1);
  144. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  145. nk_layout_row_dynamic(ctx, 20, 1);
  146. nk_label(ctx, "background:", NK_TEXT_LEFT);
  147. nk_layout_row_dynamic(ctx, 25, 1);
  148. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  149. nk_layout_row_dynamic(ctx, 120, 1);
  150. bg = nk_color_picker(ctx, bg, NK_RGBA);
  151. nk_layout_row_dynamic(ctx, 25, 1);
  152. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  153. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  154. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  155. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  156. nk_combo_end(ctx);
  157. }
  158. }
  159. nk_end(ctx);
  160. /* -------------- EXAMPLES ---------------- */
  161. #ifdef INCLUDE_CALCULATOR
  162. calculator(ctx);
  163. #endif
  164. #ifdef INCLUDE_CANVAS
  165. canvas(ctx);
  166. #endif
  167. #ifdef INCLUDE_OVERVIEW
  168. overview(ctx);
  169. #endif
  170. #ifdef INCLUDE_CONFIGURATOR
  171. style_configurator(ctx, color_table);
  172. #endif
  173. #ifdef INCLUDE_NODE_EDITOR
  174. node_editor(ctx);
  175. #endif
  176. /* ----------------------------------------- */
  177. /* Draw */
  178. glfwGetWindowSize(win, &width, &height);
  179. glViewport(0, 0, width, height);
  180. glClear(GL_COLOR_BUFFER_BIT);
  181. glClearColor(bg.r, bg.g, bg.b, bg.a);
  182. /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
  183. * with blending, scissor, face culling, depth test and viewport and
  184. * defaults everything back into a default state.
  185. * Make sure to either a.) save and restore or b.) reset your own state after
  186. * rendering the UI. */
  187. nk_glfw3_render(&glfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  188. glfwSwapBuffers(win);
  189. }
  190. nk_glfw3_shutdown(&glfw);
  191. glfwTerminate();
  192. return 0;
  193. }