main.c 13 KB

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