main.c 8.1 KB

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