main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, "Arial");
  139. ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win,
  140. #ifdef NK_XLIB_USE_XFT
  141. xw.vis, xw.cmap,
  142. #endif
  143. xw.width, xw.height);
  144. #ifdef INCLUDE_STYLE
  145. /* ease regression testing during Nuklear release process; not needed for anything else */
  146. #ifdef STYLE_WHITE
  147. set_style(ctx, THEME_WHITE);
  148. #elif defined(STYLE_RED)
  149. set_style(ctx, THEME_RED);
  150. #elif defined(STYLE_BLUE)
  151. set_style(ctx, THEME_BLUE);
  152. #elif defined(STYLE_DARK)
  153. set_style(ctx, THEME_DARK);
  154. #endif
  155. #endif
  156. while (running)
  157. {
  158. /* Input */
  159. XEvent evt;
  160. started = timestamp();
  161. nk_input_begin(ctx);
  162. while (XPending(xw.dpy)) {
  163. XNextEvent(xw.dpy, &evt);
  164. if (evt.type == ClientMessage) goto cleanup;
  165. if (XFilterEvent(&evt, xw.win)) continue;
  166. nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
  167. }
  168. nk_input_end(ctx);
  169. /* GUI */
  170. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  171. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  172. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  173. {
  174. enum {EASY, HARD};
  175. static int op = EASY;
  176. static int property = 20;
  177. nk_layout_row_static(ctx, 30, 80, 1);
  178. if (nk_button_label(ctx, "button"))
  179. fprintf(stdout, "button pressed\n");
  180. nk_layout_row_dynamic(ctx, 30, 2);
  181. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  182. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  183. nk_layout_row_dynamic(ctx, 25, 1);
  184. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  185. }
  186. nk_end(ctx);
  187. if (nk_window_is_hidden(ctx, "Demo")) break;
  188. /* -------------- EXAMPLES ---------------- */
  189. #ifdef INCLUDE_CALCULATOR
  190. calculator(ctx);
  191. #endif
  192. #ifdef INCLUDE_CANVAS
  193. canvas(ctx);
  194. #endif
  195. #ifdef INCLUDE_OVERVIEW
  196. overview(ctx);
  197. #endif
  198. #ifdef INCLUDE_NODE_EDITOR
  199. node_editor(ctx);
  200. #endif
  201. /* ----------------------------------------- */
  202. /* Draw */
  203. XClearWindow(xw.dpy, xw.win);
  204. nk_xlib_render(xw.win, nk_rgb(30,30,30));
  205. XFlush(xw.dpy);
  206. /* Timing */
  207. dt = timestamp() - started;
  208. if (dt < DTIME)
  209. sleep_for(DTIME - dt);
  210. }
  211. cleanup:
  212. nk_xfont_del(xw.dpy, xw.font);
  213. nk_xlib_shutdown();
  214. XUnmapWindow(xw.dpy, xw.win);
  215. XFreeColormap(xw.dpy, xw.cmap);
  216. XDestroyWindow(xw.dpy, xw.win);
  217. XCloseDisplay(xw.dpy);
  218. return 0;
  219. }