main.c 7.7 KB

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