main.c 8.0 KB

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