main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <SDL2/SDL.h>
  12. #include <SDL2/SDL_opengl.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_SDL_GL2_IMPLEMENTATION
  22. #include "../../nuklear.h"
  23. #include "nuklear_sdl_gl2.h"
  24. #define WINDOW_WIDTH 1200
  25. #define WINDOW_HEIGHT 800
  26. /* ===============================================================
  27. *
  28. * EXAMPLE
  29. *
  30. * ===============================================================*/
  31. /* This are some code examples to provide a small overview of what can be
  32. * done with this library. To try out an example uncomment the defines */
  33. /*#define INCLUDE_ALL */
  34. /*#define INCLUDE_STYLE */
  35. /*#define INCLUDE_CALCULATOR */
  36. /*#define INCLUDE_CANVAS */
  37. #define INCLUDE_OVERVIEW
  38. /*#define INCLUDE_CONFIGURATOR */
  39. /*#define INCLUDE_NODE_EDITOR */
  40. #ifdef INCLUDE_ALL
  41. #define INCLUDE_STYLE
  42. #define INCLUDE_CALCULATOR
  43. #define INCLUDE_CANVAS
  44. #define INCLUDE_OVERVIEW
  45. #define INCLUDE_CONFIGURATOR
  46. #define INCLUDE_NODE_EDITOR
  47. #endif
  48. #ifdef INCLUDE_STYLE
  49. #include "../../demo/common/style.c"
  50. #endif
  51. #ifdef INCLUDE_CALCULATOR
  52. #include "../../demo/common/calculator.c"
  53. #endif
  54. #ifdef INCLUDE_CANVAS
  55. #include "../../demo/common/canvas.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../../demo/common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_CONFIGURATOR
  61. #include "../../demo/common/style_configurator.c"
  62. #endif
  63. #ifdef INCLUDE_NODE_EDITOR
  64. #include "../../demo/common/node_editor.c"
  65. #endif
  66. /* ===============================================================
  67. *
  68. * DEMO
  69. *
  70. * ===============================================================*/
  71. int
  72. main(int argc, char *argv[])
  73. {
  74. /* Platform */
  75. SDL_Window *win;
  76. SDL_GLContext glContext;
  77. int win_width, win_height;
  78. int running = 1;
  79. /* GUI */
  80. struct nk_context *ctx;
  81. struct nk_colorf bg;
  82. #ifdef INCLUDE_CONFIGURATOR
  83. static struct nk_color color_table[NK_COLOR_COUNT];
  84. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  85. #endif
  86. NK_UNUSED(argc);
  87. NK_UNUSED(argv);
  88. /* SDL setup */
  89. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  90. SDL_Init(SDL_INIT_VIDEO);
  91. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  92. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  93. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  94. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  95. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  96. win = SDL_CreateWindow("Demo",
  97. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  98. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  99. glContext = SDL_GL_CreateContext(win);
  100. SDL_GetWindowSize(win, &win_width, &win_height);
  101. /* GUI */
  102. ctx = nk_sdl_init(win);
  103. /* Load Fonts: if none of these are loaded a default font will be used */
  104. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  105. {struct nk_font_atlas *atlas;
  106. nk_sdl_font_stash_begin(&atlas);
  107. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  108. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
  109. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  110. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  111. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  112. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  113. nk_sdl_font_stash_end();
  114. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  115. /*nk_style_set_font(ctx, &roboto->handle)*/;}
  116. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  117. while (running)
  118. {
  119. /* Input */
  120. SDL_Event evt;
  121. nk_input_begin(ctx);
  122. while (SDL_PollEvent(&evt)) {
  123. if (evt.type == SDL_QUIT) goto cleanup;
  124. nk_sdl_handle_event(&evt);
  125. }
  126. nk_sdl_handle_grab(); /* optional grabbing behavior */
  127. nk_input_end(ctx);
  128. /* GUI */
  129. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  130. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  131. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  132. {
  133. enum {EASY, HARD};
  134. static int op = EASY;
  135. static int property = 20;
  136. nk_layout_row_static(ctx, 30, 80, 1);
  137. if (nk_button_label(ctx, "button"))
  138. fprintf(stdout, "button pressed\n");
  139. nk_layout_row_dynamic(ctx, 30, 2);
  140. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  141. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  142. nk_layout_row_dynamic(ctx, 25, 1);
  143. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  144. nk_layout_row_dynamic(ctx, 20, 1);
  145. nk_label(ctx, "background:", NK_TEXT_LEFT);
  146. nk_layout_row_dynamic(ctx, 25, 1);
  147. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  148. nk_layout_row_dynamic(ctx, 120, 1);
  149. bg = nk_color_picker(ctx, bg, NK_RGBA);
  150. nk_layout_row_dynamic(ctx, 25, 1);
  151. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  152. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  153. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  154. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  155. nk_combo_end(ctx);
  156. }
  157. }
  158. nk_end(ctx);
  159. /* -------------- EXAMPLES ---------------- */
  160. #ifdef INCLUDE_CALCULATOR
  161. calculator(ctx);
  162. #endif
  163. #ifdef INCLUDE_CANVAS
  164. canvas(ctx);
  165. #endif
  166. #ifdef INCLUDE_OVERVIEW
  167. overview(ctx);
  168. #endif
  169. #ifdef INCLUDE_CONFIGURATOR
  170. style_configurator(ctx, color_table);
  171. #endif
  172. #ifdef INCLUDE_NODE_EDITOR
  173. node_editor(ctx);
  174. #endif
  175. /* ----------------------------------------- */
  176. /* Draw */
  177. SDL_GetWindowSize(win, &win_width, &win_height);
  178. glViewport(0, 0, win_width, win_height);
  179. glClear(GL_COLOR_BUFFER_BIT);
  180. glClearColor(bg.r, bg.g, bg.b, bg.a);
  181. /* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
  182. * with blending, scissor, face culling, depth test and viewport and
  183. * defaults everything back into a default state.
  184. * Make sure to either a.) save and restore or b.) reset your own state after
  185. * rendering the UI. */
  186. nk_sdl_render(NK_ANTI_ALIASING_ON);
  187. SDL_GL_SwapWindow(win);
  188. }
  189. cleanup:
  190. nk_sdl_shutdown();
  191. SDL_GL_DeleteContext(glContext);
  192. SDL_DestroyWindow(win);
  193. SDL_Quit();
  194. return 0;
  195. }