main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <math.h>
  10. #include <limits.h>
  11. #include <time.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_GL2_IMPLEMENTATION
  23. #include "../../nuklear.h"
  24. #include "nuklear_sdl_gl2.h"
  25. #define WINDOW_WIDTH 1200
  26. #define WINDOW_HEIGHT 800
  27. #define UNUSED(a) (void)a
  28. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  29. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  30. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  31. /* ===============================================================
  32. *
  33. * EXAMPLE
  34. *
  35. * ===============================================================*/
  36. /* This are some code examples to provide a small overview of what can be
  37. * done with this library. To try out an example uncomment the include
  38. * and the corresponding function. */
  39. /*#include "../style.c"*/
  40. /*#include "../calculator.c"*/
  41. /*#include "../overview.c"*/
  42. /*#include "../node_editor.c"*/
  43. /* ===============================================================
  44. *
  45. * DEMO
  46. *
  47. * ===============================================================*/
  48. int
  49. main(int argc, char* argv[])
  50. {
  51. /* Platform */
  52. SDL_Window *win;
  53. SDL_GLContext glContext;
  54. struct nk_color background;
  55. int win_width, win_height;
  56. int running = 1;
  57. /* GUI */
  58. struct nk_context *ctx;
  59. /* SDL setup */
  60. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  61. SDL_Init(SDL_INIT_VIDEO);
  62. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  63. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  64. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  65. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  66. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  67. win = SDL_CreateWindow("Demo",
  68. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  69. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  70. glContext = SDL_GL_CreateContext(win);
  71. SDL_GetWindowSize(win, &win_width, &win_height);
  72. /* GUI */
  73. ctx = nk_sdl_init(win);
  74. /* Load Fonts: if none of these are loaded a default font will be used */
  75. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  76. {struct nk_font_atlas *atlas;
  77. nk_sdl_font_stash_begin(&atlas);
  78. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  79. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
  80. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  81. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  82. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  83. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  84. nk_sdl_font_stash_end();
  85. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  86. /*nk_style_set_font(ctx, &roboto->handle)*/;}
  87. /* style.c */
  88. /*set_style(ctx, THEME_WHITE);*/
  89. /*set_style(ctx, THEME_RED);*/
  90. /*set_style(ctx, THEME_BLUE);*/
  91. /*set_style(ctx, THEME_DARK);*/
  92. background = nk_rgb(28,48,62);
  93. while (running)
  94. {
  95. /* Input */
  96. SDL_Event evt;
  97. nk_input_begin(ctx);
  98. while (SDL_PollEvent(&evt)) {
  99. if (evt.type == SDL_QUIT) goto cleanup;
  100. nk_sdl_handle_event(&evt);
  101. }
  102. nk_input_end(ctx);
  103. /* GUI */
  104. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 210, 250),
  105. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  106. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  107. {
  108. enum {EASY, HARD};
  109. static int op = EASY;
  110. static int property = 20;
  111. nk_layout_row_static(ctx, 30, 80, 1);
  112. if (nk_button_label(ctx, "button"))
  113. fprintf(stdout, "button pressed\n");
  114. nk_layout_row_dynamic(ctx, 30, 2);
  115. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  116. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  117. nk_layout_row_dynamic(ctx, 25, 1);
  118. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  119. nk_layout_row_dynamic(ctx, 20, 1);
  120. nk_label(ctx, "background:", NK_TEXT_LEFT);
  121. nk_layout_row_dynamic(ctx, 25, 1);
  122. if (nk_combo_begin_color(ctx, background, nk_vec2(nk_widget_width(ctx),400))) {
  123. nk_layout_row_dynamic(ctx, 120, 1);
  124. background = nk_color_picker(ctx, background, NK_RGBA);
  125. nk_layout_row_dynamic(ctx, 25, 1);
  126. background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
  127. background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
  128. background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
  129. background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
  130. nk_combo_end(ctx);
  131. }
  132. }
  133. nk_end(ctx);
  134. /* -------------- EXAMPLES ---------------- */
  135. /*calculator(ctx);*/
  136. /*overview(ctx);*/
  137. /*node_editor(ctx);*/
  138. /* ----------------------------------------- */
  139. /* Draw */
  140. {float bg[4];
  141. nk_color_fv(bg, background);
  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);
  152. SDL_GL_SwapWindow(win);}
  153. }
  154. cleanup:
  155. nk_sdl_shutdown();
  156. SDL_GL_DeleteContext(glContext);
  157. SDL_DestroyWindow(win);
  158. SDL_Quit();
  159. return 0;
  160. }