main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_NODE_EDITOR */
  78. #ifdef INCLUDE_ALL
  79. #define INCLUDE_STYLE
  80. #define INCLUDE_CALCULATOR
  81. #define INCLUDE_CANVAS
  82. #define INCLUDE_OVERVIEW
  83. #define INCLUDE_NODE_EDITOR
  84. #endif
  85. #ifdef INCLUDE_STYLE
  86. #include "../../demo/common/style.c"
  87. #endif
  88. #ifdef INCLUDE_CALCULATOR
  89. #include "../../demo/common/calculator.c"
  90. #endif
  91. #ifdef INCLUDE_CANVAS
  92. #include "../../demo/common/canvas.c"
  93. #endif
  94. #ifdef INCLUDE_OVERVIEW
  95. #include "../../demo/common/overview.c"
  96. #endif
  97. #ifdef INCLUDE_NODE_EDITOR
  98. #include "../../demo/common/node_editor.c"
  99. #endif
  100. /* ===============================================================
  101. *
  102. * DEMO
  103. *
  104. * ===============================================================*/
  105. int
  106. main(void)
  107. {
  108. long dt;
  109. long started;
  110. int running = 1;
  111. XWindow xw;
  112. struct nk_context *ctx;
  113. /* X11 */
  114. memset(&xw, 0, sizeof xw);
  115. xw.dpy = XOpenDisplay(NULL);
  116. if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
  117. xw.root = DefaultRootWindow(xw.dpy);
  118. xw.screen = XDefaultScreen(xw.dpy);
  119. xw.vis = XDefaultVisual(xw.dpy, xw.screen);
  120. xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
  121. xw.swa.colormap = xw.cmap;
  122. xw.swa.event_mask =
  123. ExposureMask | KeyPressMask | KeyReleaseMask |
  124. ButtonPress | ButtonReleaseMask| ButtonMotionMask |
  125. Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
  126. PointerMotionMask | KeymapStateMask;
  127. xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
  128. XDefaultDepth(xw.dpy, xw.screen), InputOutput,
  129. xw.vis, CWEventMask | CWColormap, &xw.swa);
  130. XStoreName(xw.dpy, xw.win, "X11");
  131. XMapWindow(xw.dpy, xw.win);
  132. xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  133. XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
  134. XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
  135. xw.width = (unsigned int)xw.attr.width;
  136. xw.height = (unsigned int)xw.attr.height;
  137. /* GUI */
  138. xw.font = nk_xfont_create(xw.dpy, "fixed");
  139. ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
  140. while (running)
  141. {
  142. /* Input */
  143. XEvent evt;
  144. started = timestamp();
  145. nk_input_begin(ctx);
  146. while (XPending(xw.dpy)) {
  147. XNextEvent(xw.dpy, &evt);
  148. if (evt.type == ClientMessage) goto cleanup;
  149. if (XFilterEvent(&evt, xw.win)) continue;
  150. nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
  151. }
  152. nk_input_end(ctx);
  153. /* GUI */
  154. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  155. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  156. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  157. {
  158. enum {EASY, HARD};
  159. static int op = EASY;
  160. static int property = 20;
  161. nk_layout_row_static(ctx, 30, 80, 1);
  162. if (nk_button_label(ctx, "button"))
  163. fprintf(stdout, "button pressed\n");
  164. nk_layout_row_dynamic(ctx, 30, 2);
  165. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  166. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  167. nk_layout_row_dynamic(ctx, 25, 1);
  168. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  169. }
  170. nk_end(ctx);
  171. if (nk_window_is_hidden(ctx, "Demo")) break;
  172. /* -------------- EXAMPLES ---------------- */
  173. #ifdef INCLUDE_CALCULATOR
  174. calculator(ctx);
  175. #endif
  176. #ifdef INCLUDE_CANVAS
  177. canvas(ctx);
  178. #endif
  179. #ifdef INCLUDE_OVERVIEW
  180. overview(ctx);
  181. #endif
  182. #ifdef INCLUDE_NODE_EDITOR
  183. node_editor(ctx);
  184. #endif
  185. /* ----------------------------------------- */
  186. /* Draw */
  187. XClearWindow(xw.dpy, xw.win);
  188. nk_xlib_render(xw.win, nk_rgb(30,30,30));
  189. XFlush(xw.dpy);
  190. /* Timing */
  191. dt = timestamp() - started;
  192. if (dt < DTIME)
  193. sleep_for(DTIME - dt);
  194. }
  195. cleanup:
  196. nk_xfont_del(xw.dpy, xw.font);
  197. nk_xlib_shutdown();
  198. XUnmapWindow(xw.dpy, xw.win);
  199. XFreeColormap(xw.dpy, xw.cmap);
  200. XDestroyWindow(xw.dpy, xw.win);
  201. XCloseDisplay(xw.dpy);
  202. return 0;
  203. }