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_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. #ifdef INCLUDE_STYLE
  141. /* ease regression testing during Nuklear release process; not needed for anything else */
  142. #ifdef STYLE_WHITE
  143. set_style(ctx, THEME_WHITE);
  144. #elif defined(STYLE_RED)
  145. set_style(ctx, THEME_RED);
  146. #elif defined(STYLE_BLUE)
  147. set_style(ctx, THEME_BLUE);
  148. #elif defined(STYLE_DARK)
  149. set_style(ctx, THEME_DARK);
  150. #endif
  151. #endif
  152. while (running)
  153. {
  154. /* Input */
  155. XEvent evt;
  156. started = timestamp();
  157. nk_input_begin(ctx);
  158. while (XPending(xw.dpy)) {
  159. XNextEvent(xw.dpy, &evt);
  160. if (evt.type == ClientMessage) goto cleanup;
  161. if (XFilterEvent(&evt, xw.win)) continue;
  162. nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
  163. }
  164. nk_input_end(ctx);
  165. /* GUI */
  166. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  167. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  168. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  169. {
  170. enum {EASY, HARD};
  171. static int op = EASY;
  172. static int property = 20;
  173. nk_layout_row_static(ctx, 30, 80, 1);
  174. if (nk_button_label(ctx, "button"))
  175. fprintf(stdout, "button pressed\n");
  176. nk_layout_row_dynamic(ctx, 30, 2);
  177. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  178. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  179. nk_layout_row_dynamic(ctx, 25, 1);
  180. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  181. }
  182. nk_end(ctx);
  183. if (nk_window_is_hidden(ctx, "Demo")) break;
  184. /* -------------- EXAMPLES ---------------- */
  185. #ifdef INCLUDE_CALCULATOR
  186. calculator(ctx);
  187. #endif
  188. #ifdef INCLUDE_CANVAS
  189. canvas(ctx);
  190. #endif
  191. #ifdef INCLUDE_OVERVIEW
  192. overview(ctx);
  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. }