main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_NODE_EDITOR */
  41. /*#define INCLUDE_CANVAS */
  42. #ifdef INCLUDE_ALL
  43. #define INCLUDE_STYLE
  44. #define INCLUDE_CALCULATOR
  45. #define INCLUDE_OVERVIEW
  46. #define INCLUDE_NODE_EDITOR
  47. #define INCLUDE_CANVAS
  48. #endif
  49. #ifdef INCLUDE_STYLE
  50. #include "../common/style.c"
  51. #endif
  52. #ifdef INCLUDE_CALCULATOR
  53. #include "../common/calculator.c"
  54. #endif
  55. #ifdef INCLUDE_OVERVIEW
  56. #include "../common/overview.c"
  57. #endif
  58. #ifdef INCLUDE_NODE_EDITOR
  59. #include "../common/node_editor.c"
  60. #endif
  61. #ifdef INCLUDE_CANVAS
  62. #include "../common/canvas.c"
  63. #endif
  64. /* ===============================================================
  65. *
  66. * DEMO
  67. *
  68. * ===============================================================*/
  69. int
  70. main(void)
  71. {
  72. struct nk_xcb_context *xcb_ctx;
  73. struct nk_color background = nk_rgb(0, 0, 0);
  74. struct nk_cairo_context *cairo_ctx;
  75. struct nk_user_font *font;
  76. struct nk_context* ctx;
  77. int running = 1;
  78. int events;
  79. xcb_ctx = nk_xcb_init("Nuklear XCB/Cairo", 20, 20, 600, 800);
  80. cairo_ctx = nk_cairo_init(&background, NULL, 0, nk_xcb_create_cairo_surface(xcb_ctx));
  81. /*cairo_ctx = nk_cairo_init(&background, "../../extra_font/DroidSans.ttf", 0, nk_xcb_create_surface(xcb_ctx));*/
  82. font = nk_cairo_default_font(cairo_ctx);
  83. ctx = malloc(sizeof(struct nk_context));
  84. nk_init_default(ctx, font);
  85. #ifdef INCLUDE_STYLE
  86. set_style(ctx, THEME_BLACK);
  87. /*nk_style_push_float(ctx, &ctx->style.window.rounding, 20.0f);*/
  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. #endif
  93. while (running)
  94. {
  95. /* Events */
  96. events = nk_xcb_handle_event(xcb_ctx, ctx);
  97. if (events & NK_XCB_EVENT_STOP) {
  98. break;
  99. }
  100. if (events & NK_XCB_EVENT_PAINT) {
  101. nk_cairo_damage(cairo_ctx);
  102. }
  103. if (events & NK_XCB_EVENT_RESIZED) {
  104. nk_xcb_resize_cairo_surface(xcb_ctx, nk_cairo_surface(cairo_ctx));
  105. }
  106. /* GUI */
  107. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  108. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  109. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  110. {
  111. enum {EASY, HARD};
  112. static int op = EASY;
  113. static int property = 20;
  114. nk_layout_row_static(ctx, 30, 80, 1);
  115. if (nk_button_label(ctx, "button"))
  116. fprintf(stdout, "button pressed\n");
  117. nk_layout_row_dynamic(ctx, 30, 2);
  118. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  119. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  120. nk_layout_row_dynamic(ctx, 25, 1);
  121. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  122. }
  123. nk_end(ctx);
  124. if (nk_window_is_hidden(ctx, "Demo")) {
  125. break;
  126. }
  127. /* -------------- EXAMPLES ---------------- */
  128. #ifdef INCLUDE_CALCULATOR
  129. calculator(ctx);
  130. #endif
  131. #ifdef INCLUDE_OVERVIEW
  132. overview(ctx);
  133. #endif
  134. #ifdef INCLUDE_NODE_EDITOR
  135. node_editor(ctx);
  136. #endif
  137. #ifdef INCLUDE_CANVAS
  138. canvas(ctx);
  139. #endif
  140. /* ----------------------------------------- */
  141. /* Render */
  142. nk_cairo_render(cairo_ctx, ctx);
  143. nk_xcb_render(xcb_ctx);
  144. nk_clear(ctx);
  145. }
  146. nk_free(ctx);
  147. free(ctx);
  148. nk_cairo_free(cairo_ctx);
  149. nk_xcb_free(xcb_ctx);
  150. return EXIT_SUCCESS;
  151. }