main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* nuklear - v1.32.0 - public domain */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <time.h>
  10. #include <limits.h>
  11. #define NK_INCLUDE_FIXED_TYPES
  12. #define NK_INCLUDE_STANDARD_IO
  13. #define NK_INCLUDE_STANDARD_VARARGS
  14. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  15. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  16. #define NK_INCLUDE_FONT_BAKING
  17. #define NK_INCLUDE_DEFAULT_FONT
  18. #define NK_IMPLEMENTATION
  19. #define NK_XLIB_GL3_IMPLEMENTATION
  20. #define NK_XLIB_LOAD_OPENGL_EXTENSIONS
  21. #include "../../nuklear.h"
  22. #include "nuklear_xlib_gl3.h"
  23. #define WINDOW_WIDTH 1200
  24. #define WINDOW_HEIGHT 800
  25. #define MAX_VERTEX_BUFFER 512 * 1024
  26. #define MAX_ELEMENT_BUFFER 128 * 1024
  27. /* ===============================================================
  28. *
  29. * EXAMPLE
  30. *
  31. * ===============================================================*/
  32. /* This are some code examples to provide a small overview of what can be
  33. * done with this library. To try out an example uncomment the defines */
  34. /*#define INCLUDE_ALL */
  35. /*#define INCLUDE_STYLE */
  36. /*#define INCLUDE_CALCULATOR */
  37. /*#define INCLUDE_CANVAS */
  38. #define INCLUDE_OVERVIEW
  39. /*#define INCLUDE_CONFIGURATOR */
  40. /*#define INCLUDE_NODE_EDITOR */
  41. #ifdef INCLUDE_ALL
  42. #define INCLUDE_STYLE
  43. #define INCLUDE_CALCULATOR
  44. #define INCLUDE_CANVAS
  45. #define INCLUDE_OVERVIEW
  46. #define INCLUDE_CONFIGURATOR
  47. #define INCLUDE_NODE_EDITOR
  48. #endif
  49. #ifdef INCLUDE_STYLE
  50. #include "../../demo/common/style.c"
  51. #endif
  52. #ifdef INCLUDE_CALCULATOR
  53. #include "../../demo/common/calculator.c"
  54. #endif
  55. #ifdef INCLUDE_CANVAS
  56. #include "../../demo/common/canvas.c"
  57. #endif
  58. #ifdef INCLUDE_OVERVIEW
  59. #include "../../demo/common/overview.c"
  60. #endif
  61. #ifdef INCLUDE_CONFIGURATOR
  62. #include "../../demo/common/style_configurator.c"
  63. #endif
  64. #ifdef INCLUDE_NODE_EDITOR
  65. #include "../../demo/common/node_editor.c"
  66. #endif
  67. /* ===============================================================
  68. *
  69. * DEMO
  70. *
  71. * ===============================================================*/
  72. struct XWindow {
  73. Display *dpy;
  74. Window win;
  75. XVisualInfo *vis;
  76. Colormap cmap;
  77. XSetWindowAttributes swa;
  78. XWindowAttributes attr;
  79. GLXFBConfig fbc;
  80. Atom wm_delete_window;
  81. int width, height;
  82. };
  83. static int gl_err = nk_false;
  84. static int gl_error_handler(Display *dpy, XErrorEvent *ev)
  85. {NK_UNUSED(dpy); NK_UNUSED(ev); gl_err = nk_true;return 0;}
  86. static void
  87. die(const char *fmt, ...)
  88. {
  89. va_list ap;
  90. va_start(ap, fmt);
  91. vfprintf(stderr, fmt, ap);
  92. va_end(ap);
  93. fputs("\n", stderr);
  94. exit(EXIT_FAILURE);
  95. }
  96. static int
  97. has_extension(const char *string, const char *ext)
  98. {
  99. const char *start, *where, *term;
  100. where = strchr(ext, ' ');
  101. if (where || *ext == '\0')
  102. return nk_false;
  103. for (start = string;;) {
  104. where = strstr((const char*)start, ext);
  105. if (!where) break;
  106. term = where + strlen(ext);
  107. if (where == start || *(where - 1) == ' ') {
  108. if (*term == ' ' || *term == '\0')
  109. return nk_true;
  110. }
  111. start = term;
  112. }
  113. return nk_false;
  114. }
  115. int main(void)
  116. {
  117. /* Platform */
  118. int running = 1;
  119. struct XWindow win;
  120. GLXContext glContext;
  121. struct nk_context *ctx;
  122. struct nk_colorf bg;
  123. #ifdef INCLUDE_CONFIGURATOR
  124. static struct nk_color color_table[NK_COLOR_COUNT];
  125. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  126. #endif
  127. memset(&win, 0, sizeof(win));
  128. win.dpy = XOpenDisplay(NULL);
  129. if (!win.dpy) die("Failed to open X display\n");
  130. {
  131. /* check glx version */
  132. int glx_major, glx_minor;
  133. if (!glXQueryVersion(win.dpy, &glx_major, &glx_minor))
  134. die("[X11]: Error: Failed to query OpenGL version\n");
  135. if ((glx_major == 1 && glx_minor < 3) || (glx_major < 1))
  136. die("[X11]: Error: Invalid GLX version!\n");
  137. }
  138. {
  139. /* find and pick matching framebuffer visual */
  140. int fb_count;
  141. static GLint attr[] = {
  142. GLX_X_RENDERABLE, True,
  143. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  144. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  145. GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
  146. GLX_RED_SIZE, 8,
  147. GLX_GREEN_SIZE, 8,
  148. GLX_BLUE_SIZE, 8,
  149. GLX_ALPHA_SIZE, 8,
  150. GLX_DEPTH_SIZE, 24,
  151. GLX_STENCIL_SIZE, 8,
  152. GLX_DOUBLEBUFFER, True,
  153. None
  154. };
  155. GLXFBConfig *fbc;
  156. fbc = glXChooseFBConfig(win.dpy, DefaultScreen(win.dpy), attr, &fb_count);
  157. if (!fbc) die("[X11]: Error: failed to retrieve framebuffer configuration\n");
  158. {
  159. /* pick framebuffer with most samples per pixel */
  160. int i;
  161. int fb_best = -1, best_num_samples = -1;
  162. for (i = 0; i < fb_count; ++i) {
  163. XVisualInfo *vi = glXGetVisualFromFBConfig(win.dpy, fbc[i]);
  164. if (vi) {
  165. int sample_buffer, samples;
  166. glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLE_BUFFERS, &sample_buffer);
  167. glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples);
  168. if ((fb_best < 0) || (sample_buffer && samples > best_num_samples))
  169. fb_best = i, best_num_samples = samples;
  170. XFree(vi);
  171. }
  172. }
  173. win.fbc = fbc[fb_best];
  174. XFree(fbc);
  175. win.vis = glXGetVisualFromFBConfig(win.dpy, win.fbc);
  176. }
  177. }
  178. {
  179. /* create window */
  180. win.cmap = XCreateColormap(win.dpy, RootWindow(win.dpy, win.vis->screen), win.vis->visual, AllocNone);
  181. win.swa.colormap = win.cmap;
  182. win.swa.background_pixmap = None;
  183. win.swa.border_pixel = 0;
  184. win.swa.event_mask =
  185. ExposureMask | KeyPressMask | KeyReleaseMask |
  186. ButtonPress | ButtonReleaseMask| ButtonMotionMask |
  187. Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
  188. PointerMotionMask| StructureNotifyMask;
  189. win.win = XCreateWindow(win.dpy, RootWindow(win.dpy, win.vis->screen), 0, 0,
  190. WINDOW_WIDTH, WINDOW_HEIGHT, 0, win.vis->depth, InputOutput,
  191. win.vis->visual, CWBorderPixel|CWColormap|CWEventMask, &win.swa);
  192. if (!win.win) die("[X11]: Failed to create window\n");
  193. XFree(win.vis);
  194. XStoreName(win.dpy, win.win, "Demo");
  195. XMapWindow(win.dpy, win.win);
  196. win.wm_delete_window = XInternAtom(win.dpy, "WM_DELETE_WINDOW", False);
  197. XSetWMProtocols(win.dpy, win.win, &win.wm_delete_window, 1);
  198. }
  199. {
  200. /* create opengl context */
  201. typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
  202. int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler);
  203. const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy));
  204. glxCreateContext create_context = (glxCreateContext)
  205. glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
  206. gl_err = nk_false;
  207. if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) {
  208. fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n");
  209. fprintf(stdout, "[X11]: ... using old-style GLX context\n");
  210. glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True);
  211. } else {
  212. GLint attr[] = {
  213. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  214. GLX_CONTEXT_MINOR_VERSION_ARB, 0,
  215. None
  216. };
  217. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  218. XSync(win.dpy, False);
  219. if (gl_err || !glContext) {
  220. /* Could not create GL 3.0 context. Fallback to old 2.x context.
  221. * If a version below 3.0 is requested, implementations will
  222. * return the newest context version compatible with OpenGL
  223. * version less than version 3.0.*/
  224. attr[1] = 1; attr[3] = 0;
  225. gl_err = nk_false;
  226. fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n");
  227. fprintf(stdout, "[X11] ... using old-style GLX context!\n");
  228. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  229. }
  230. }
  231. XSync(win.dpy, False);
  232. XSetErrorHandler(old_handler);
  233. if (gl_err || !glContext)
  234. die("[X11]: Failed to create an OpenGL context\n");
  235. glXMakeCurrent(win.dpy, win.win, glContext);
  236. }
  237. ctx = nk_x11_init(win.dpy, win.win);
  238. /* Load Fonts: if none of these are loaded a default font will be used */
  239. {struct nk_font_atlas *atlas;
  240. nk_x11_font_stash_begin(&atlas);
  241. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  242. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  243. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  244. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  245. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  246. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  247. nk_x11_font_stash_end();
  248. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  249. /*nk_style_set_font(ctx, &droid->handle);*/}
  250. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  251. while (running)
  252. {
  253. /* Input */
  254. XEvent evt;
  255. nk_input_begin(ctx);
  256. while (XPending(win.dpy)) {
  257. XNextEvent(win.dpy, &evt);
  258. if (evt.type == ClientMessage) goto cleanup;
  259. if (XFilterEvent(&evt, win.win)) continue;
  260. nk_x11_handle_event(&evt);
  261. }
  262. nk_input_end(ctx);
  263. /* GUI */
  264. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  265. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  266. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  267. {
  268. enum {EASY, HARD};
  269. static int op = EASY;
  270. static int property = 20;
  271. nk_layout_row_static(ctx, 30, 80, 1);
  272. if (nk_button_label(ctx, "button"))
  273. fprintf(stdout, "button pressed\n");
  274. nk_layout_row_dynamic(ctx, 30, 2);
  275. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  276. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  277. nk_layout_row_dynamic(ctx, 25, 1);
  278. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  279. nk_layout_row_dynamic(ctx, 20, 1);
  280. nk_label(ctx, "background:", NK_TEXT_LEFT);
  281. nk_layout_row_dynamic(ctx, 25, 1);
  282. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  283. nk_layout_row_dynamic(ctx, 120, 1);
  284. bg = nk_color_picker(ctx, bg, NK_RGBA);
  285. nk_layout_row_dynamic(ctx, 25, 1);
  286. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  287. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  288. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  289. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  290. nk_combo_end(ctx);
  291. }
  292. }
  293. nk_end(ctx);
  294. /* -------------- EXAMPLES ---------------- */
  295. #ifdef INCLUDE_CALCULATOR
  296. calculator(ctx);
  297. #endif
  298. #ifdef INCLUDE_CANVAS
  299. canvas(ctx);
  300. #endif
  301. #ifdef INCLUDE_OVERVIEW
  302. overview(ctx);
  303. #endif
  304. #ifdef INCLUDE_CONFIGURATOR
  305. style_configurator(ctx, color_table);
  306. #endif
  307. #ifdef INCLUDE_NODE_EDITOR
  308. node_editor(ctx);
  309. #endif
  310. /* ----------------------------------------- */
  311. /* Draw */
  312. XGetWindowAttributes(win.dpy, win.win, &win.attr);
  313. glViewport(0, 0, win.width, win.height);
  314. glClear(GL_COLOR_BUFFER_BIT);
  315. glClearColor(bg.r, bg.g, bg.b, bg.a);
  316. /* IMPORTANT: `nk_x11_render` modifies some global OpenGL state
  317. * with blending, scissor, face culling, depth test and viewport and
  318. * defaults everything back into a default state.
  319. * Make sure to either a.) save and restore or b.) reset your own state after
  320. * rendering the UI. */
  321. nk_x11_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  322. glXSwapBuffers(win.dpy, win.win);
  323. }
  324. cleanup:
  325. nk_x11_shutdown();
  326. glXMakeCurrent(win.dpy, 0, 0);
  327. glXDestroyContext(win.dpy, glContext);
  328. XUnmapWindow(win.dpy, win.win);
  329. XFreeColormap(win.dpy, win.cmap);
  330. XDestroyWindow(win.dpy, win.win);
  331. XCloseDisplay(win.dpy);
  332. return 0;
  333. }