main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #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_SDL_RENDERER_IMPLEMENTATION
  21. #include "../../nuklear.h"
  22. #include "nuklear_sdl_renderer.h"
  23. #define WINDOW_WIDTH 1200
  24. #define WINDOW_HEIGHT 800
  25. /* ===============================================================
  26. *
  27. * EXAMPLE
  28. *
  29. * ===============================================================*/
  30. /* This are some code examples to provide a small overview of what can be
  31. * done with this library. To try out an example uncomment the defines */
  32. /*#define INCLUDE_ALL */
  33. /*#define INCLUDE_STYLE */
  34. /*#define INCLUDE_CALCULATOR */
  35. /*#define INCLUDE_CANVAS */
  36. #define INCLUDE_OVERVIEW
  37. /*#define INCLUDE_CONFIGURATOR */
  38. /*#define INCLUDE_NODE_EDITOR */
  39. #ifdef INCLUDE_ALL
  40. #define INCLUDE_STYLE
  41. #define INCLUDE_CALCULATOR
  42. #define INCLUDE_CANVAS
  43. #define INCLUDE_OVERVIEW
  44. #define INCLUDE_CONFIGURATOR
  45. #define INCLUDE_NODE_EDITOR
  46. #endif
  47. #ifdef INCLUDE_STYLE
  48. #include "../../demo/common/style.c"
  49. #endif
  50. #ifdef INCLUDE_CALCULATOR
  51. #include "../../demo/common/calculator.c"
  52. #endif
  53. #ifdef INCLUDE_CANVAS
  54. #include "../../demo/common/canvas.c"
  55. #endif
  56. #ifdef INCLUDE_OVERVIEW
  57. #include "../../demo/common/overview.c"
  58. #endif
  59. #ifdef INCLUDE_CONFIGURATOR
  60. #include "../../demo/common/style_configurator.c"
  61. #endif
  62. #ifdef INCLUDE_NODE_EDITOR
  63. #include "../../demo/common/node_editor.c"
  64. #endif
  65. /* ===============================================================
  66. *
  67. * DEMO
  68. *
  69. * ===============================================================*/
  70. int
  71. main(void)
  72. {
  73. /* Platform */
  74. SDL_Window *win;
  75. SDL_Renderer *renderer;
  76. int running = 1;
  77. int flags = 0;
  78. float font_scale = 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. /* SDL setup */
  87. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  88. SDL_Init(SDL_INIT_VIDEO);
  89. win = SDL_CreateWindow("Demo",
  90. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  91. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  92. if (win == NULL) {
  93. SDL_Log("Error SDL_CreateWindow %s", SDL_GetError());
  94. exit(-1);
  95. }
  96. flags |= SDL_RENDERER_ACCELERATED;
  97. flags |= SDL_RENDERER_PRESENTVSYNC;
  98. #if 0
  99. SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
  100. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  101. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
  102. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  103. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  104. #endif
  105. renderer = SDL_CreateRenderer(win, -1, flags);
  106. if (renderer == NULL) {
  107. SDL_Log("Error SDL_CreateRenderer %s", SDL_GetError());
  108. exit(-1);
  109. }
  110. /* scale the renderer output for High-DPI displays */
  111. {
  112. int render_w, render_h;
  113. int window_w, window_h;
  114. float scale_x, scale_y;
  115. SDL_GetRendererOutputSize(renderer, &render_w, &render_h);
  116. SDL_GetWindowSize(win, &window_w, &window_h);
  117. scale_x = (float)(render_w) / (float)(window_w);
  118. scale_y = (float)(render_h) / (float)(window_h);
  119. SDL_RenderSetScale(renderer, scale_x, scale_y);
  120. font_scale = scale_y;
  121. }
  122. /* GUI */
  123. ctx = nk_sdl_init(win, renderer);
  124. /* Load Fonts: if none of these are loaded a default font will be used */
  125. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  126. {
  127. struct nk_font_atlas *atlas;
  128. struct nk_font_config config = nk_font_config(0);
  129. struct nk_font *font;
  130. /* set up the font atlas and add desired font; note that font sizes are
  131. * multiplied by font_scale to produce better results at higher DPIs */
  132. nk_sdl_font_stash_begin(&atlas);
  133. font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config);
  134. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14 * font_scale, &config);*/
  135. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16 * font_scale, &config);*/
  136. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13 * font_scale, &config);*/
  137. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12 * font_scale, &config);*/
  138. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10 * font_scale, &config);*/
  139. /*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13 * font_scale, &config);*/
  140. nk_sdl_font_stash_end();
  141. /* this hack makes the font appear to be scaled down to the desired
  142. * size and is only necessary when font_scale > 1 */
  143. font->handle.height /= font_scale;
  144. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  145. nk_style_set_font(ctx, &font->handle);
  146. }
  147. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  148. while (running)
  149. {
  150. /* Input */
  151. SDL_Event evt;
  152. nk_input_begin(ctx);
  153. while (SDL_PollEvent(&evt)) {
  154. if (evt.type == SDL_QUIT) goto cleanup;
  155. nk_sdl_handle_event(&evt);
  156. }
  157. nk_sdl_handle_grab(); /* optional grabbing behavior */
  158. nk_input_end(ctx);
  159. /* GUI */
  160. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  161. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  162. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  163. {
  164. enum {EASY, HARD};
  165. static int op = EASY;
  166. static int property = 20;
  167. nk_layout_row_static(ctx, 30, 80, 1);
  168. if (nk_button_label(ctx, "button"))
  169. fprintf(stdout, "button pressed\n");
  170. nk_layout_row_dynamic(ctx, 30, 2);
  171. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  172. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  173. nk_layout_row_dynamic(ctx, 25, 1);
  174. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  175. nk_layout_row_dynamic(ctx, 20, 1);
  176. nk_label(ctx, "background:", NK_TEXT_LEFT);
  177. nk_layout_row_dynamic(ctx, 25, 1);
  178. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  179. nk_layout_row_dynamic(ctx, 120, 1);
  180. bg = nk_color_picker(ctx, bg, NK_RGBA);
  181. nk_layout_row_dynamic(ctx, 25, 1);
  182. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  183. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  184. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  185. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  186. nk_combo_end(ctx);
  187. }
  188. }
  189. nk_end(ctx);
  190. /* -------------- EXAMPLES ---------------- */
  191. #ifdef INCLUDE_CALCULATOR
  192. calculator(ctx);
  193. #endif
  194. #ifdef INCLUDE_CANVAS
  195. canvas(ctx);
  196. #endif
  197. #ifdef INCLUDE_OVERVIEW
  198. overview(ctx);
  199. #endif
  200. #ifdef INCLUDE_CONFIGURATOR
  201. style_configurator(ctx, color_table);
  202. #endif
  203. #ifdef INCLUDE_NODE_EDITOR
  204. node_editor(ctx);
  205. #endif
  206. /* ----------------------------------------- */
  207. SDL_SetRenderDrawColor(renderer, bg.r * 255, bg.g * 255, bg.b * 255, bg.a * 255);
  208. SDL_RenderClear(renderer);
  209. nk_sdl_render(NK_ANTI_ALIASING_ON);
  210. SDL_RenderPresent(renderer);
  211. }
  212. cleanup:
  213. nk_sdl_shutdown();
  214. SDL_DestroyRenderer(renderer);
  215. SDL_DestroyWindow(win);
  216. SDL_Quit();
  217. return 0;
  218. }