main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* nuklear - v1.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 <SFML/Window.hpp>
  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_SFML_GL3_IMPLEMENTATION
  21. #include "../../nuklear.h"
  22. #include "nuklear_sfml_gl3.h"
  23. #define WINDOW_WIDTH 1200
  24. #define WINDOW_HEIGHT 800
  25. #define MAX_VERTEX_BUFFER 512 * 1024
  26. #define MAX_ELEMENT_BUFFER 128 * 1024
  27. /* ===============================================================
  28. *
  29. * EXAMPLE
  30. *
  31. * ===============================================================*/
  32. /* This are some code examples to provide a small overview of what can be
  33. * done with this library. To try out an example uncomment the defines */
  34. /*#define INCLUDE_ALL */
  35. /*#define INCLUDE_STYLE */
  36. /*#define INCLUDE_CALCULATOR */
  37. /*#define INCLUDE_CANVAS */
  38. /*#define INCLUDE_OVERVIEW */
  39. /*#define INCLUDE_NODE_EDITOR */
  40. #ifdef INCLUDE_ALL
  41. #define INCLUDE_STYLE
  42. #define INCLUDE_CALCULATOR
  43. #define INCLUDE_CANVAS
  44. #define INCLUDE_OVERVIEW
  45. #define INCLUDE_NODE_EDITOR
  46. #endif
  47. #ifdef INCLUDE_STYLE
  48. #include "../../demo/common/style.c"
  49. #endif
  50. #ifdef INCLUDE_CALCULATOR
  51. #include "../../demo/common/calculator.c"
  52. #endif
  53. #ifdef INCLUDE_CANVAS
  54. #include "../../demo/common/canvas.c"
  55. #endif
  56. #ifdef INCLUDE_OVERVIEW
  57. #include "../../demo/common/overview.c"
  58. #endif
  59. #ifdef INCLUDE_NODE_EDITOR
  60. #include "../../demo/common/node_editor.c"
  61. #endif
  62. /* ===============================================================
  63. *
  64. * DEMO
  65. *
  66. * ===============================================================*/
  67. int main(void)
  68. {
  69. /* Platform */
  70. sf::ContextSettings settings(24, 8, 4, 3, 3);
  71. sf::Window win(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Demo", sf::Style::Default, settings);
  72. win.setVerticalSyncEnabled(true);
  73. win.setActive(true);
  74. if(!gladLoadGL()) { /* Load OpenGL extensions */
  75. printf("Failed to load OpenGL extensions!\n");
  76. return -1;
  77. }
  78. glViewport(0, 0, win.getSize().x, win.getSize().y);
  79. /* GUI */
  80. struct nk_context *ctx;
  81. ctx = nk_sfml_init(&win);
  82. /* Load Fonts: if none of these are loaded a default font will be used */
  83. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  84. struct nk_font_atlas *atlas;
  85. nk_sfml_font_stash_begin(&atlas);
  86. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  87. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  88. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  89. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  90. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  91. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  92. nk_sfml_font_stash_end();
  93. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  94. /*nk_style_set_font(ctx, &droid->handle);*/
  95. /* style.c */
  96. #ifdef INCLUDE_STYLE
  97. /* ease regression testing during Nuklear release process; not needed for anything else */
  98. #ifdef STYLE_WHITE
  99. set_style(ctx, THEME_WHITE);
  100. #elif defined(STYLE_RED)
  101. set_style(ctx, THEME_RED);
  102. #elif defined(STYLE_BLUE)
  103. set_style(ctx, THEME_BLUE);
  104. #elif defined(STYLE_DARK)
  105. set_style(ctx, THEME_DARK);
  106. #endif
  107. #endif
  108. struct nk_colorf bg;
  109. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  110. while (win.isOpen())
  111. {
  112. /* Input */
  113. sf::Event evt;
  114. nk_input_begin(ctx);
  115. while(win.pollEvent(evt)) {
  116. if(evt.type == sf::Event::Closed)
  117. win.close();
  118. else if(evt.type == sf::Event::Resized)
  119. glViewport(0, 0, evt.size.width, evt.size.height);
  120. nk_sfml_handle_event(&evt);
  121. }
  122. nk_input_end(ctx);
  123. /* GUI */
  124. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  125. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  126. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  127. {
  128. enum {EASY, HARD};
  129. static int op = EASY;
  130. static int property = 20;
  131. nk_layout_row_static(ctx, 30, 80, 1);
  132. if (nk_button_label(ctx, "button"))
  133. fprintf(stdout, "button pressed\n");
  134. nk_layout_row_dynamic(ctx, 30, 2);
  135. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  136. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  137. nk_layout_row_dynamic(ctx, 25, 1);
  138. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  139. nk_layout_row_dynamic(ctx, 20, 1);
  140. nk_label(ctx, "background:", NK_TEXT_LEFT);
  141. nk_layout_row_dynamic(ctx, 25, 1);
  142. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  143. nk_layout_row_dynamic(ctx, 120, 1);
  144. bg = nk_color_picker(ctx, bg, NK_RGBA);
  145. nk_layout_row_dynamic(ctx, 25, 1);
  146. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  147. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  148. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  149. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  150. nk_combo_end(ctx);
  151. }
  152. }
  153. nk_end(ctx);
  154. /* -------------- EXAMPLES ---------------- */
  155. #ifdef INCLUDE_CALCULATOR
  156. calculator(ctx);
  157. #endif
  158. #ifdef INCLUDE_CANVAS
  159. canvas(ctx);
  160. #endif
  161. #ifdef INCLUDE_OVERVIEW
  162. overview(ctx);
  163. #endif
  164. #ifdef INCLUDE_NODE_EDITOR
  165. node_editor(ctx);
  166. #endif
  167. /* ----------------------------------------- */
  168. /* Draw */
  169. win.setActive(true);
  170. glClear(GL_COLOR_BUFFER_BIT);
  171. glClearColor(bg.r, bg.g, bg.b, bg.a);
  172. /* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
  173. * with blending, scissor, face culling and depth test and defaults everything
  174. * back into a default state. Make sure to either save and restore or
  175. * reset your own state after drawing rendering the UI. */
  176. nk_sfml_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  177. win.display();
  178. }
  179. nk_sfml_shutdown();
  180. return 0;
  181. }