main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* nuklear - 1.40.8 - 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. #define NK_INCLUDE_FIXED_TYPES
  12. #define NK_INCLUDE_STANDARD_IO
  13. #define NK_INCLUDE_STANDARD_VARARGS
  14. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  15. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  16. #define NK_INCLUDE_FONT_BAKING
  17. #define NK_INCLUDE_DEFAULT_FONT
  18. #define NK_IMPLEMENTATION
  19. #define NK_SDL_GLES2_IMPLEMENTATION
  20. #include "../../nuklear.h"
  21. #include "nuklear_sdl_gles2.h"
  22. #define WINDOW_WIDTH 800
  23. #define WINDOW_HEIGHT 600
  24. #define MAX_VERTEX_MEMORY 512 * 1024
  25. #define MAX_ELEMENT_MEMORY 128 * 1024
  26. #define UNUSED(a) (void)a
  27. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  28. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  29. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  30. /* ===============================================================
  31. *
  32. * EXAMPLE
  33. *
  34. * ===============================================================*/
  35. /* This are some code examples to provide a small overview of what can be
  36. * done with this library. To try out an example uncomment the defines */
  37. /*#define INCLUDE_ALL */
  38. /*#define INCLUDE_STYLE */
  39. /*#define INCLUDE_CALCULATOR */
  40. /*#define INCLUDE_CANVAS */
  41. /*#define INCLUDE_OVERVIEW */
  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_NODE_EDITOR
  49. #endif
  50. #ifdef INCLUDE_STYLE
  51. #include "../../demo/common/style.c"
  52. #endif
  53. #ifdef INCLUDE_CALCULATOR
  54. #include "../../demo/common/calculator.c"
  55. #endif
  56. #ifdef INCLUDE_CANVAS
  57. #include "../../demo/common/canvas.c"
  58. #endif
  59. #ifdef INCLUDE_OVERVIEW
  60. #include "../../demo/common/overview.c"
  61. #endif
  62. #ifdef INCLUDE_NODE_EDITOR
  63. #include "../../demo/common/node_editor.c"
  64. #endif
  65. /* ===============================================================
  66. *
  67. * DEMO
  68. *
  69. * ===============================================================*/
  70. /* Platform */
  71. SDL_Window *win;
  72. int running = nk_true;
  73. static void
  74. MainLoop(void* loopArg){
  75. struct nk_context *ctx = (struct nk_context *)loopArg;
  76. /* Input */
  77. SDL_Event evt;
  78. nk_input_begin(ctx);
  79. while (SDL_PollEvent(&evt)) {
  80. if (evt.type == SDL_QUIT) running = nk_false;
  81. nk_sdl_handle_event(&evt);
  82. }
  83. nk_sdl_handle_grab(); /* optional grabbing behavior */
  84. nk_input_end(ctx);
  85. /* GUI */
  86. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  87. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  88. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  89. {
  90. nk_menubar_begin(ctx);
  91. nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  92. nk_layout_row_push(ctx, 45);
  93. if (nk_menu_begin_label(ctx, "FILE", NK_TEXT_LEFT, nk_vec2(120, 200))) {
  94. nk_layout_row_dynamic(ctx, 30, 1);
  95. nk_menu_item_label(ctx, "OPEN", NK_TEXT_LEFT);
  96. nk_menu_item_label(ctx, "CLOSE", NK_TEXT_LEFT);
  97. nk_menu_end(ctx);
  98. }
  99. nk_layout_row_push(ctx, 45);
  100. if (nk_menu_begin_label(ctx, "EDIT", NK_TEXT_LEFT, nk_vec2(120, 200))) {
  101. nk_layout_row_dynamic(ctx, 30, 1);
  102. nk_menu_item_label(ctx, "COPY", NK_TEXT_LEFT);
  103. nk_menu_item_label(ctx, "CUT", NK_TEXT_LEFT);
  104. nk_menu_item_label(ctx, "PASTE", NK_TEXT_LEFT);
  105. nk_menu_end(ctx);
  106. }
  107. nk_layout_row_end(ctx);
  108. nk_menubar_end(ctx);
  109. {
  110. enum {EASY, HARD};
  111. static int op = EASY;
  112. static int property = 20;
  113. nk_layout_row_static(ctx, 30, 80, 1);
  114. if (nk_button_label(ctx, "button"))
  115. fprintf(stdout, "button pressed\n");
  116. nk_layout_row_dynamic(ctx, 30, 2);
  117. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  118. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  119. nk_layout_row_dynamic(ctx, 25, 1);
  120. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  121. }
  122. }
  123. nk_end(ctx);
  124. /* -------------- EXAMPLES ---------------- */
  125. #ifdef INCLUDE_CALCULATOR
  126. calculator(ctx);
  127. #endif
  128. #ifdef INCLUDE_CANVAS
  129. canvas(ctx);
  130. #endif
  131. #ifdef INCLUDE_OVERVIEW
  132. overview(ctx);
  133. #endif
  134. #ifdef INCLUDE_NODE_EDITOR
  135. node_editor(ctx);
  136. #endif
  137. /* ----------------------------------------- */
  138. /* Draw */
  139. {float bg[4];
  140. int win_width, win_height;
  141. nk_color_fv(bg, nk_rgb(28,48,62));
  142. SDL_GetWindowSize(win, &win_width, &win_height);
  143. glViewport(0, 0, win_width, win_height);
  144. glClear(GL_COLOR_BUFFER_BIT);
  145. glClearColor(bg[0], bg[1], bg[2], bg[3]);
  146. /* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
  147. * with blending, scissor, face culling, depth test and viewport and
  148. * defaults everything back into a default state.
  149. * Make sure to either a.) save and restore or b.) reset your own state after
  150. * rendering the UI. */
  151. nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
  152. SDL_GL_SwapWindow(win);}
  153. }
  154. int main(int argc, char* argv[])
  155. {
  156. /* GUI */
  157. struct nk_context *ctx;
  158. SDL_GLContext glContext;
  159. NK_UNUSED(argc);
  160. NK_UNUSED(argv);
  161. /* SDL setup */
  162. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  163. /*SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); // - do NOT init SDL on GL ES 2 */
  164. SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  165. SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  166. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  167. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  168. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  169. win = SDL_CreateWindow("Demo",
  170. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  171. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  172. glContext = SDL_GL_CreateContext(win);
  173. /* OpenGL setup */
  174. glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
  175. ctx = nk_sdl_init(win);
  176. /* Load Fonts: if none of these are loaded a default font will be used */
  177. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  178. {struct nk_font_atlas *atlas;
  179. nk_sdl_font_stash_begin(&atlas);
  180. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  181. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
  182. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  183. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  184. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  185. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  186. nk_sdl_font_stash_end();
  187. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  188. /*nk_style_set_font(ctx, &roboto->handle)*/;}
  189. #if defined(__EMSCRIPTEN__)
  190. #include <emscripten.h>
  191. emscripten_set_main_loop_arg(MainLoop, (void*)ctx, 0, nk_true);
  192. #else
  193. while (running) MainLoop((void*)ctx);
  194. #endif
  195. nk_sdl_shutdown();
  196. SDL_GL_DeleteContext(glContext);
  197. SDL_DestroyWindow(win);
  198. SDL_Quit();
  199. return 0;
  200. }