main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_GL4_IMPLEMENTATION
  22. #include "../../nuklear.h"
  23. #include "nuklear_glfw_gl4.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. static GLFWwindow *win;
  79. int width = 0, height = 0;
  80. struct nk_context *ctx;
  81. struct nk_colorf bg;
  82. struct nk_image img;
  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, 4);
  94. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
  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(win, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  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(&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();
  121. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  122. /*nk_style_set_font(ctx, &droid->handle);*/}
  123. /* Create bindless texture.
  124. * The index returned is not the opengl resource id.
  125. * IF you need the GL resource id use: nk_glfw3_get_tex_ogl_id() */
  126. {int tex_index = 0;
  127. enum {tex_width = 256, tex_height = 256};
  128. char pixels[tex_width * tex_height * 4];
  129. memset(pixels, 128, sizeof(pixels));
  130. tex_index = nk_glfw3_create_texture(pixels, tex_width, tex_height);
  131. img = nk_image_id(tex_index);}
  132. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  133. while (!glfwWindowShouldClose(win))
  134. {
  135. /* Input */
  136. glfwPollEvents();
  137. nk_glfw3_new_frame();
  138. /* GUI */
  139. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  140. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  141. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  142. {
  143. enum {EASY, HARD};
  144. static int op = EASY;
  145. static int property = 20;
  146. nk_layout_row_static(ctx, 30, 80, 1);
  147. if (nk_button_label(ctx, "button"))
  148. fprintf(stdout, "button pressed\n");
  149. nk_layout_row_dynamic(ctx, 30, 2);
  150. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  151. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  152. nk_layout_row_dynamic(ctx, 25, 1);
  153. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  154. nk_layout_row_dynamic(ctx, 20, 1);
  155. nk_label(ctx, "background:", NK_TEXT_LEFT);
  156. nk_layout_row_dynamic(ctx, 25, 1);
  157. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  158. nk_layout_row_dynamic(ctx, 120, 1);
  159. bg = nk_color_picker(ctx, bg, NK_RGBA);
  160. nk_layout_row_dynamic(ctx, 25, 1);
  161. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  162. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  163. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  164. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  165. nk_combo_end(ctx);
  166. }
  167. }
  168. nk_end(ctx);
  169. /* Bindless Texture */
  170. if (nk_begin(ctx, "Texture", nk_rect(250, 150, 230, 250),
  171. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  172. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  173. {
  174. struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
  175. struct nk_rect total_space = nk_window_get_content_region(ctx);
  176. nk_draw_image(canvas, total_space, &img, nk_white);
  177. }
  178. nk_end(ctx);
  179. /* -------------- EXAMPLES ---------------- */
  180. #ifdef INCLUDE_CALCULATOR
  181. calculator(ctx);
  182. #endif
  183. #ifdef INCLUDE_CANVAS
  184. canvas(ctx);
  185. #endif
  186. #ifdef INCLUDE_OVERVIEW
  187. overview(ctx);
  188. #endif
  189. #ifdef INCLUDE_CONFIGURATOR
  190. style_configurator(ctx, color_table);
  191. #endif
  192. #ifdef INCLUDE_NODE_EDITOR
  193. node_editor(ctx);
  194. #endif
  195. /* ----------------------------------------- */
  196. /* Draw */
  197. glfwGetWindowSize(win, &width, &height);
  198. glViewport(0, 0, width, height);
  199. glClear(GL_COLOR_BUFFER_BIT);
  200. glClearColor(bg.r, bg.g, bg.b, bg.a);
  201. /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
  202. * with blending, scissor, face culling, depth test and viewport and
  203. * defaults everything back into a default state.
  204. * Make sure to either a.) save and restore or b.) reset your own state after
  205. * rendering the UI. */
  206. nk_glfw3_render(NK_ANTI_ALIASING_ON);
  207. glfwSwapBuffers(win);
  208. }
  209. nk_glfw3_shutdown();
  210. glfwTerminate();
  211. return 0;
  212. }