main.c 6.7 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. #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. /* GUI */
  73. struct nk_context *ctx;
  74. struct nk_colorf bg;
  75. /* SDL setup */
  76. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  77. SDL_Init(SDL_INIT_VIDEO);
  78. win = SDL_CreateWindow("Demo",
  79. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  80. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  81. if (win == NULL) {
  82. SDL_Log("Error SDL_CreateWindow %s", SDL_GetError());
  83. exit(-1);
  84. }
  85. flags |= SDL_RENDERER_ACCELERATED;
  86. flags |= SDL_RENDERER_PRESENTVSYNC;
  87. #if 0
  88. SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
  89. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  90. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
  91. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  92. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  93. #endif
  94. renderer = SDL_CreateRenderer(win, -1, flags);
  95. if (renderer == NULL) {
  96. SDL_Log("Error SDL_CreateRenderer %s", SDL_GetError());
  97. exit(-1);
  98. }
  99. /* GUI */
  100. ctx = nk_sdl_init(win, renderer);
  101. /* Load Fonts: if none of these are loaded a default font will be used */
  102. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  103. {struct nk_font_atlas *atlas;
  104. nk_sdl_font_stash_begin(&atlas);
  105. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  106. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
  107. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  108. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  109. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  110. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  111. nk_sdl_font_stash_end();
  112. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  113. /*nk_style_set_font(ctx, &roboto->handle)*/;}
  114. #ifdef INCLUDE_STYLE
  115. /*set_style(ctx, THEME_WHITE);*/
  116. /*set_style(ctx, THEME_RED);*/
  117. /*set_style(ctx, THEME_BLUE);*/
  118. /*set_style(ctx, THEME_DARK);*/
  119. #endif
  120. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  121. while (running)
  122. {
  123. /* Input */
  124. SDL_Event evt;
  125. nk_input_begin(ctx);
  126. while (SDL_PollEvent(&evt)) {
  127. if (evt.type == SDL_QUIT) goto cleanup;
  128. nk_sdl_handle_event(&evt);
  129. }
  130. nk_input_end(ctx);
  131. /* GUI */
  132. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  133. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  134. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  135. {
  136. enum {EASY, HARD};
  137. static int op = EASY;
  138. static int property = 20;
  139. nk_layout_row_static(ctx, 30, 80, 1);
  140. if (nk_button_label(ctx, "button"))
  141. fprintf(stdout, "button pressed\n");
  142. nk_layout_row_dynamic(ctx, 30, 2);
  143. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  144. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  145. nk_layout_row_dynamic(ctx, 25, 1);
  146. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  147. nk_layout_row_dynamic(ctx, 20, 1);
  148. nk_label(ctx, "background:", NK_TEXT_LEFT);
  149. nk_layout_row_dynamic(ctx, 25, 1);
  150. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  151. nk_layout_row_dynamic(ctx, 120, 1);
  152. bg = nk_color_picker(ctx, bg, NK_RGBA);
  153. nk_layout_row_dynamic(ctx, 25, 1);
  154. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  155. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  156. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  157. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  158. nk_combo_end(ctx);
  159. }
  160. }
  161. nk_end(ctx);
  162. /* -------------- EXAMPLES ---------------- */
  163. #ifdef INCLUDE_CALCULATOR
  164. calculator(ctx);
  165. #endif
  166. #ifdef INCLUDE_CANVAS
  167. canvas(ctx);
  168. #endif
  169. #ifdef INCLUDE_OVERVIEW
  170. overview(ctx);
  171. #endif
  172. #ifdef INCLUDE_NODE_EDITOR
  173. node_editor(ctx);
  174. #endif
  175. /* ----------------------------------------- */
  176. SDL_SetRenderDrawColor(renderer, bg.r * 255, bg.g * 255, bg.b * 255, bg.a * 255);
  177. SDL_RenderClear(renderer);
  178. nk_sdl_render(NK_ANTI_ALIASING_ON);
  179. SDL_RenderPresent(renderer);
  180. }
  181. cleanup:
  182. nk_sdl_shutdown();
  183. SDL_DestroyRenderer(renderer);
  184. SDL_DestroyWindow(win);
  185. SDL_Quit();
  186. return 0;
  187. }