main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* nuklear - v1.32.0 - public domain */
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <limits.h>
  8. #include <math.h>
  9. #include <sys/time.h>
  10. #include <unistd.h>
  11. #include <time.h>
  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_IMPLEMENTATION
  17. #include "../../nuklear.h"
  18. #define NK_XCB_CAIRO_IMPLEMENTATION
  19. #include "nuklear_xcb.h"
  20. static void die(const char *fmt, ...)
  21. {
  22. va_list ap;
  23. va_start(ap, fmt);
  24. vfprintf(stderr, fmt, ap);
  25. va_end(ap);
  26. fputs("\n", stderr);
  27. exit(EXIT_FAILURE);
  28. }
  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_OVERVIEW
  40. /*#define INCLUDE_CONFIGURATOR */
  41. /*#define INCLUDE_NODE_EDITOR */
  42. /*#define INCLUDE_CANVAS */
  43. #ifdef INCLUDE_ALL
  44. #define INCLUDE_STYLE
  45. #define INCLUDE_CALCULATOR
  46. #define INCLUDE_OVERVIEW
  47. #define INCLUDE_NODE_EDITOR
  48. #define INCLUDE_CONFIGURATOR
  49. #define INCLUDE_CANVAS
  50. #endif
  51. #ifdef INCLUDE_STYLE
  52. #include "../common/style.c"
  53. #endif
  54. #ifdef INCLUDE_CALCULATOR
  55. #include "../common/calculator.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_CONFIGURATOR
  61. #include "../../demo/common/style_configurator.c"
  62. #endif
  63. #ifdef INCLUDE_NODE_EDITOR
  64. #include "../common/node_editor.c"
  65. #endif
  66. #ifdef INCLUDE_CANVAS
  67. #include "../common/canvas.c"
  68. #endif
  69. /* ===============================================================
  70. *
  71. * DEMO
  72. *
  73. * ===============================================================*/
  74. int
  75. main(void)
  76. {
  77. struct nk_xcb_context *xcb_ctx;
  78. struct nk_color background = nk_rgb(0, 0, 0);
  79. struct nk_cairo_context *cairo_ctx;
  80. struct nk_user_font *font;
  81. struct nk_context* ctx;
  82. int running = 1;
  83. int events;
  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. xcb_ctx = nk_xcb_init("Nuklear XCB/Cairo", 20, 20, 600, 800);
  89. cairo_ctx = nk_cairo_init(&background, NULL, 0, nk_xcb_create_cairo_surface(xcb_ctx));
  90. /*cairo_ctx = nk_cairo_init(&background, "../../extra_font/DroidSans.ttf", 0, nk_xcb_create_surface(xcb_ctx));*/
  91. font = nk_cairo_default_font(cairo_ctx);
  92. ctx = malloc(sizeof(struct nk_context));
  93. nk_init_default(ctx, font);
  94. #ifdef INCLUDE_STYLE
  95. set_style(ctx, THEME_BLACK);
  96. /*nk_style_push_float(ctx, &ctx->style.window.rounding, 20.0f);*/
  97. /*set_style(ctx, THEME_WHITE);*/
  98. /*set_style(ctx, THEME_RED);*/
  99. /*set_style(ctx, THEME_BLUE);*/
  100. /*set_style(ctx, THEME_DARK);*/
  101. #endif
  102. while (running)
  103. {
  104. /* Events */
  105. events = nk_xcb_handle_event(xcb_ctx, ctx);
  106. if (events & NK_XCB_EVENT_STOP) {
  107. break;
  108. }
  109. if (events & NK_XCB_EVENT_PAINT) {
  110. nk_cairo_damage(cairo_ctx);
  111. }
  112. if (events & NK_XCB_EVENT_RESIZED) {
  113. nk_xcb_resize_cairo_surface(xcb_ctx, nk_cairo_surface(cairo_ctx));
  114. }
  115. /* GUI */
  116. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  117. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  118. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  119. {
  120. enum {EASY, HARD};
  121. static int op = EASY;
  122. static int property = 20;
  123. nk_layout_row_static(ctx, 30, 80, 1);
  124. if (nk_button_label(ctx, "button"))
  125. fprintf(stdout, "button pressed\n");
  126. nk_layout_row_dynamic(ctx, 30, 2);
  127. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  128. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  129. nk_layout_row_dynamic(ctx, 25, 1);
  130. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  131. }
  132. nk_end(ctx);
  133. if (nk_window_is_hidden(ctx, "Demo")) {
  134. break;
  135. }
  136. /* -------------- EXAMPLES ---------------- */
  137. #ifdef INCLUDE_CALCULATOR
  138. calculator(ctx);
  139. #endif
  140. #ifdef INCLUDE_OVERVIEW
  141. overview(ctx);
  142. #endif
  143. #ifdef INCLUDE_CONFIGURATOR
  144. style_configurator(ctx, color_table);
  145. #endif
  146. #ifdef INCLUDE_NODE_EDITOR
  147. node_editor(ctx);
  148. #endif
  149. #ifdef INCLUDE_CANVAS
  150. canvas(ctx);
  151. #endif
  152. /* ----------------------------------------- */
  153. /* Render */
  154. nk_cairo_render(cairo_ctx, ctx);
  155. nk_xcb_render(xcb_ctx);
  156. nk_clear(ctx);
  157. }
  158. nk_free(ctx);
  159. free(ctx);
  160. nk_cairo_free(cairo_ctx);
  161. nk_xcb_free(xcb_ctx);
  162. return EXIT_SUCCESS;
  163. }