main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #define NK_XLIB_IMPLEMENTATION
  18. #include "../../nuklear.h"
  19. #include "nuklear_xlib.h"
  20. #define DTIME 20
  21. #define WINDOW_WIDTH 800
  22. #define WINDOW_HEIGHT 600
  23. typedef struct XWindow XWindow;
  24. struct XWindow {
  25. Display *dpy;
  26. Window root;
  27. Visual *vis;
  28. Colormap cmap;
  29. XWindowAttributes attr;
  30. XSetWindowAttributes swa;
  31. Window win;
  32. int screen;
  33. XFont *font;
  34. unsigned int width;
  35. unsigned int height;
  36. Atom wm_delete_window;
  37. };
  38. static void
  39. die(const char *fmt, ...)
  40. {
  41. va_list ap;
  42. va_start(ap, fmt);
  43. vfprintf(stderr, fmt, ap);
  44. va_end(ap);
  45. fputs("\n", stderr);
  46. exit(EXIT_FAILURE);
  47. }
  48. static long
  49. timestamp(void)
  50. {
  51. struct timeval tv;
  52. if (gettimeofday(&tv, NULL) < 0) return 0;
  53. return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
  54. }
  55. static void
  56. sleep_for(long t)
  57. {
  58. struct timespec req;
  59. const time_t sec = (int)(t/1000);
  60. const long ms = t - (sec * 1000);
  61. req.tv_sec = sec;
  62. req.tv_nsec = ms * 1000000L;
  63. while(-1 == nanosleep(&req, &req));
  64. }
  65. /* ===============================================================
  66. *
  67. * EXAMPLE
  68. *
  69. * ===============================================================*/
  70. /* This are some code examples to provide a small overview of what can be
  71. * done with this library. To try out an example uncomment the defines */
  72. /*#define INCLUDE_ALL */
  73. /*#define INCLUDE_STYLE */
  74. /*#define INCLUDE_CALCULATOR */
  75. /*#define INCLUDE_CANVAS */
  76. #define INCLUDE_OVERVIEW
  77. /*#define INCLUDE_CONFIGURATOR */
  78. /*#define INCLUDE_NODE_EDITOR */
  79. #ifdef INCLUDE_ALL
  80. #define INCLUDE_STYLE
  81. #define INCLUDE_CALCULATOR
  82. #define INCLUDE_CANVAS
  83. #define INCLUDE_OVERVIEW
  84. #define INCLUDE_CONFIGURATOR
  85. #define INCLUDE_NODE_EDITOR
  86. #endif
  87. #ifdef INCLUDE_STYLE
  88. #include "../../demo/common/style.c"
  89. #endif
  90. #ifdef INCLUDE_CALCULATOR
  91. #include "../../demo/common/calculator.c"
  92. #endif
  93. #ifdef INCLUDE_CANVAS
  94. #include "../../demo/common/canvas.c"
  95. #endif
  96. #ifdef INCLUDE_OVERVIEW
  97. #include "../../demo/common/overview.c"
  98. #endif
  99. #ifdef INCLUDE_CONFIGURATOR
  100. #include "../../demo/common/style_configurator.c"
  101. #endif
  102. #ifdef INCLUDE_NODE_EDITOR
  103. #include "../../demo/common/node_editor.c"
  104. #endif
  105. /* ===============================================================
  106. *
  107. * DEMO
  108. *
  109. * ===============================================================*/
  110. int
  111. main(void)
  112. {
  113. long dt;
  114. long started;
  115. int running = 1;
  116. XWindow xw;
  117. struct nk_context *ctx;
  118. #ifdef INCLUDE_CONFIGURATOR
  119. static struct nk_color color_table[NK_COLOR_COUNT];
  120. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  121. #endif
  122. /* X11 */
  123. memset(&xw, 0, sizeof xw);
  124. xw.dpy = XOpenDisplay(NULL);
  125. if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
  126. xw.root = DefaultRootWindow(xw.dpy);
  127. xw.screen = XDefaultScreen(xw.dpy);
  128. xw.vis = XDefaultVisual(xw.dpy, xw.screen);
  129. xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
  130. xw.swa.colormap = xw.cmap;
  131. xw.swa.event_mask =
  132. ExposureMask | KeyPressMask | KeyReleaseMask |
  133. ButtonPress | ButtonReleaseMask| ButtonMotionMask |
  134. Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
  135. PointerMotionMask | KeymapStateMask;
  136. xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
  137. XDefaultDepth(xw.dpy, xw.screen), InputOutput,
  138. xw.vis, CWEventMask | CWColormap, &xw.swa);
  139. XStoreName(xw.dpy, xw.win, "X11");
  140. XMapWindow(xw.dpy, xw.win);
  141. xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  142. XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
  143. XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
  144. xw.width = (unsigned int)xw.attr.width;
  145. xw.height = (unsigned int)xw.attr.height;
  146. /* GUI */
  147. xw.font = nk_xfont_create(xw.dpy, "fixed");
  148. ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
  149. while (running)
  150. {
  151. /* Input */
  152. XEvent evt;
  153. started = timestamp();
  154. nk_input_begin(ctx);
  155. while (XPending(xw.dpy)) {
  156. XNextEvent(xw.dpy, &evt);
  157. if (evt.type == ClientMessage) goto cleanup;
  158. if (XFilterEvent(&evt, xw.win)) continue;
  159. nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
  160. }
  161. nk_input_end(ctx);
  162. /* GUI */
  163. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  164. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  165. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  166. {
  167. enum {EASY, HARD};
  168. static int op = EASY;
  169. static int property = 20;
  170. nk_layout_row_static(ctx, 30, 80, 1);
  171. if (nk_button_label(ctx, "button"))
  172. fprintf(stdout, "button pressed\n");
  173. nk_layout_row_dynamic(ctx, 30, 2);
  174. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  175. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  176. nk_layout_row_dynamic(ctx, 25, 1);
  177. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  178. }
  179. nk_end(ctx);
  180. if (nk_window_is_hidden(ctx, "Demo")) break;
  181. /* -------------- EXAMPLES ---------------- */
  182. #ifdef INCLUDE_CALCULATOR
  183. calculator(ctx);
  184. #endif
  185. #ifdef INCLUDE_CANVAS
  186. canvas(ctx);
  187. #endif
  188. #ifdef INCLUDE_OVERVIEW
  189. overview(ctx);
  190. #endif
  191. #ifdef INCLUDE_CONFIGURATOR
  192. style_configurator(ctx, color_table);
  193. #endif
  194. #ifdef INCLUDE_NODE_EDITOR
  195. node_editor(ctx);
  196. #endif
  197. /* ----------------------------------------- */
  198. /* Draw */
  199. XClearWindow(xw.dpy, xw.win);
  200. nk_xlib_render(xw.win, nk_rgb(30,30,30));
  201. XFlush(xw.dpy);
  202. /* Timing */
  203. dt = timestamp() - started;
  204. if (dt < DTIME)
  205. sleep_for(DTIME - dt);
  206. }
  207. cleanup:
  208. nk_xfont_del(xw.dpy, xw.font);
  209. nk_xlib_shutdown();
  210. XUnmapWindow(xw.dpy, xw.win);
  211. XFreeColormap(xw.dpy, xw.cmap);
  212. XDestroyWindow(xw.dpy, xw.win);
  213. XCloseDisplay(xw.dpy);
  214. return 0;
  215. }