main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <math.h>
  10. #include <limits.h>
  11. #include <time.h>
  12. #include <SFML/Window.hpp>
  13. #define NK_INCLUDE_FIXED_TYPES
  14. #define NK_INCLUDE_STANDARD_IO
  15. #define NK_INCLUDE_STANDARD_VARARGS
  16. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  17. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  18. #define NK_INCLUDE_FONT_BAKING
  19. #define NK_INCLUDE_DEFAULT_FONT
  20. #define NK_IMPLEMENTATION
  21. #define NK_SFML_GL3_IMPLEMENTATION
  22. #include "../../nuklear.h"
  23. #include "nuklear_sfml_gl3.h"
  24. #define WINDOW_WIDTH 1200
  25. #define WINDOW_HEIGHT 800
  26. #define MAX_VERTEX_BUFFER 512 * 1024
  27. #define MAX_ELEMENT_BUFFER 128 * 1024
  28. #define UNUSED(a) (void)a
  29. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  30. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  31. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  32. /* ===============================================================
  33. *
  34. * EXAMPLE
  35. *
  36. * ===============================================================*/
  37. /* This are some code examples to provide a small overview of what can be
  38. * done with this library. To try out an example uncomment the include
  39. * and the corresponding function. */
  40. /*#include "../style.c"*/
  41. /*#include "../calculator.c"*/
  42. /*#include "../overview.c"*/
  43. /*#include "../node_editor.c"*/
  44. /* ===============================================================
  45. *
  46. * DEMO
  47. *
  48. * ===============================================================*/
  49. int main(void)
  50. {
  51. /* Platform */
  52. sf::ContextSettings settings(24, 8, 4, 3, 3);
  53. sf::Window window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Demo", sf::Style::Default, settings);
  54. window.setVerticalSyncEnabled(true);
  55. window.setActive(true);
  56. /* Load OpenGL extensions */
  57. if(!gladLoadGL())
  58. {
  59. printf("Failed to load OpenGL extensions!\n");
  60. return -1;
  61. }
  62. glViewport(0, 0, window.getSize().x, window.getSize().y);
  63. struct nk_context *ctx;
  64. struct nk_color background;
  65. /* GUI */
  66. ctx = nk_sfml_init(&window);
  67. /* Load Fonts: if none of these are loaded a default font will be used */
  68. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  69. {
  70. struct nk_font_atlas *atlas;
  71. nk_sfml_font_stash_begin(&atlas);
  72. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  73. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  74. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  75. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  76. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  77. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  78. nk_sfml_font_stash_end();
  79. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  80. /*nk_style_set_font(ctx, &droid->handle);*/
  81. }
  82. /* style.c */
  83. /*set_style(ctx, THEME_WHITE);*/
  84. /*set_style(ctx, THEME_RED);*/
  85. /*set_style(ctx, THEME_BLUE);*/
  86. /*set_style(ctx, THEME_DARK);*/
  87. background = nk_rgb(28,48,62);
  88. while (window.isOpen())
  89. {
  90. /* Input */
  91. sf::Event event;
  92. nk_input_begin(ctx);
  93. while(window.pollEvent(event))
  94. {
  95. if(event.type == sf::Event::Closed)
  96. window.close();
  97. else if(event.type == sf::Event::Resized)
  98. glViewport(0, 0, event.size.width, event.size.height);
  99. nk_sfml_handle_event(&event);
  100. }
  101. nk_input_end(ctx);
  102. /* GUI */
  103. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  104. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  105. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  106. {
  107. enum {EASY, HARD};
  108. static int op = EASY;
  109. static int property = 20;
  110. nk_layout_row_static(ctx, 30, 80, 1);
  111. if (nk_button_label(ctx, "button"))
  112. fprintf(stdout, "button pressed\n");
  113. nk_layout_row_dynamic(ctx, 30, 2);
  114. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  115. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  116. nk_layout_row_dynamic(ctx, 25, 1);
  117. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  118. nk_layout_row_dynamic(ctx, 20, 1);
  119. nk_label(ctx, "background:", NK_TEXT_LEFT);
  120. nk_layout_row_dynamic(ctx, 25, 1);
  121. if (nk_combo_begin_color(ctx, background, nk_vec2(nk_widget_width(ctx),400))) {
  122. nk_layout_row_dynamic(ctx, 120, 1);
  123. background = nk_color_picker(ctx, background, NK_RGBA);
  124. nk_layout_row_dynamic(ctx, 25, 1);
  125. background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
  126. background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
  127. background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
  128. background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
  129. nk_combo_end(ctx);
  130. }
  131. }
  132. nk_end(ctx);
  133. /* -------------- EXAMPLES ---------------- */
  134. /*calculator(ctx);*/
  135. /*overview(ctx);*/
  136. /*node_editor(ctx);*/
  137. /* ----------------------------------------- */
  138. /* Draw */
  139. {
  140. window.setActive(true);
  141. float bg[4];
  142. nk_color_fv(bg, background);
  143. glClear(GL_COLOR_BUFFER_BIT);
  144. glClearColor(bg[0], bg[1], bg[2], bg[3]);
  145. /* IMPORTANT: `nk_sfml_render` modifies some global OpenGL state
  146. * with blending, scissor, face culling and depth test and defaults everything
  147. * back into a default state. Make sure to either save and restore or
  148. * reset your own state after drawing rendering the UI. */
  149. nk_sfml_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  150. window.display();
  151. }
  152. }
  153. nk_sfml_shutdown();
  154. return 0;
  155. }