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_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_NODE_EDITOR
  47. #endif
  48. #ifdef INCLUDE_STYLE
  49. #include "../../demo/common/style.c"
  50. #endif
  51. #ifdef INCLUDE_CALCULATOR
  52. #include "../../demo/common/calculator.c"
  53. #endif
  54. #ifdef INCLUDE_CANVAS
  55. #include "../../demo/common/canvas.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../../demo/common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_NODE_EDITOR
  61. #include "../../demo/common/node_editor.c"
  62. #endif
  63. /* ===============================================================
  64. *
  65. * DEMO
  66. *
  67. * ===============================================================*/
  68. struct XWindow {
  69. Display *dpy;
  70. Window win;
  71. XVisualInfo *vis;
  72. Colormap cmap;
  73. XSetWindowAttributes swa;
  74. XWindowAttributes attr;
  75. GLXFBConfig fbc;
  76. Atom wm_delete_window;
  77. int width, height;
  78. };
  79. static int gl_err = nk_false;
  80. static int gl_error_handler(Display *dpy, XErrorEvent *ev)
  81. {NK_UNUSED(dpy); NK_UNUSED(ev); gl_err = nk_true; return 0;}
  82. static void
  83. die(const char *fmt, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, fmt);
  87. vfprintf(stderr, fmt, ap);
  88. va_end(ap);
  89. fputs("\n", stderr);
  90. exit(EXIT_FAILURE);
  91. }
  92. static int
  93. has_extension(const char *string, const char *ext)
  94. {
  95. const char *start, *where, *term;
  96. where = strchr(ext, ' ');
  97. if (where || *ext == '\0')
  98. return nk_false;
  99. for (start = string;;) {
  100. where = strstr((const char*)start, ext);
  101. if (!where) break;
  102. term = where + strlen(ext);
  103. if (where == start || *(where - 1) == ' ') {
  104. if (*term == ' ' || *term == '\0')
  105. return nk_true;
  106. }
  107. start = term;
  108. }
  109. return nk_false;
  110. }
  111. int main(void)
  112. {
  113. /* Platform */
  114. int running = 1;
  115. struct XWindow win;
  116. GLXContext glContext;
  117. struct nk_context *ctx;
  118. struct nk_colorf bg;
  119. memset(&win, 0, sizeof(win));
  120. win.dpy = XOpenDisplay(NULL);
  121. if (!win.dpy) die("Failed to open X display\n");
  122. {
  123. /* check glx version */
  124. int glx_major, glx_minor;
  125. if (!glXQueryVersion(win.dpy, &glx_major, &glx_minor))
  126. die("[X11]: Error: Failed to query OpenGL version\n");
  127. if ((glx_major == 1 && glx_minor < 3) || (glx_major < 1))
  128. die("[X11]: Error: Invalid GLX version!\n");
  129. }
  130. {
  131. /* find and pick matching framebuffer visual */
  132. int fb_count;
  133. static GLint attr[] = {
  134. GLX_X_RENDERABLE, True,
  135. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  136. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  137. GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
  138. GLX_RED_SIZE, 8,
  139. GLX_GREEN_SIZE, 8,
  140. GLX_BLUE_SIZE, 8,
  141. GLX_ALPHA_SIZE, 8,
  142. GLX_DEPTH_SIZE, 24,
  143. GLX_STENCIL_SIZE, 8,
  144. GLX_DOUBLEBUFFER, True,
  145. None
  146. };
  147. GLXFBConfig *fbc;
  148. fbc = glXChooseFBConfig(win.dpy, DefaultScreen(win.dpy), attr, &fb_count);
  149. if (!fbc) die("[X11]: Error: failed to retrieve framebuffer configuration\n");
  150. {
  151. /* pick framebuffer with most samples per pixel */
  152. int i;
  153. int fb_best = -1, best_num_samples = -1;
  154. for (i = 0; i < fb_count; ++i) {
  155. XVisualInfo *vi = glXGetVisualFromFBConfig(win.dpy, fbc[i]);
  156. if (vi) {
  157. int sample_buffer, samples;
  158. glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLE_BUFFERS, &sample_buffer);
  159. glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples);
  160. if ((fb_best < 0) || (sample_buffer && samples > best_num_samples))
  161. fb_best = i, best_num_samples = samples;
  162. XFree(vi);
  163. }
  164. }
  165. win.fbc = fbc[fb_best];
  166. XFree(fbc);
  167. win.vis = glXGetVisualFromFBConfig(win.dpy, win.fbc);
  168. }
  169. }
  170. {
  171. /* create window */
  172. win.cmap = XCreateColormap(win.dpy, RootWindow(win.dpy, win.vis->screen), win.vis->visual, AllocNone);
  173. win.swa.colormap = win.cmap;
  174. win.swa.background_pixmap = None;
  175. win.swa.border_pixel = 0;
  176. win.swa.event_mask =
  177. ExposureMask | KeyPressMask | KeyReleaseMask |
  178. ButtonPress | ButtonReleaseMask| ButtonMotionMask |
  179. Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
  180. PointerMotionMask| StructureNotifyMask;
  181. win.win = XCreateWindow(win.dpy, RootWindow(win.dpy, win.vis->screen), 0, 0,
  182. WINDOW_WIDTH, WINDOW_HEIGHT, 0, win.vis->depth, InputOutput,
  183. win.vis->visual, CWBorderPixel|CWColormap|CWEventMask, &win.swa);
  184. if (!win.win) die("[X11]: Failed to create window\n");
  185. XFree(win.vis);
  186. XStoreName(win.dpy, win.win, "Demo");
  187. XMapWindow(win.dpy, win.win);
  188. win.wm_delete_window = XInternAtom(win.dpy, "WM_DELETE_WINDOW", False);
  189. XSetWMProtocols(win.dpy, win.win, &win.wm_delete_window, 1);
  190. }
  191. {
  192. /* create opengl context */
  193. typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
  194. int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler);
  195. const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy));
  196. glxCreateContext create_context = (glxCreateContext)
  197. glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
  198. gl_err = nk_false;
  199. if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) {
  200. fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n");
  201. fprintf(stdout, "[X11]: ... using old-style GLX context\n");
  202. glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True);
  203. } else {
  204. GLint attr[] = {
  205. GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
  206. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  207. None
  208. };
  209. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  210. XSync(win.dpy, False);
  211. if (gl_err || !glContext) {
  212. /* Could not create GL 3.0 context. Fallback to old 2.x context.
  213. * If a version below 3.0 is requested, implementations will
  214. * return the newest context version compatible with OpenGL
  215. * version less than version 3.0.*/
  216. attr[1] = 1; attr[3] = 0;
  217. gl_err = nk_false;
  218. fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n");
  219. fprintf(stdout, "[X11] ... using old-style GLX context!\n");
  220. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  221. }
  222. }
  223. XSync(win.dpy, False);
  224. XSetErrorHandler(old_handler);
  225. if (gl_err || !glContext)
  226. die("[X11]: Failed to create an OpenGL context\n");
  227. glXMakeCurrent(win.dpy, win.win, glContext);
  228. }
  229. ctx = nk_x11_init(win.dpy, win.win);
  230. /* Load Fonts: if none of these are loaded a default font will be used */
  231. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  232. {struct nk_font_atlas *atlas;
  233. nk_x11_font_stash_begin(&atlas);
  234. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  235. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  236. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  237. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  238. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  239. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  240. nk_x11_font_stash_end();
  241. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  242. /*nk_style_set_font(ctx, &droid->handle);*/}
  243. #ifdef INCLUDE_STYLE
  244. /* ease regression testing during Nuklear release process; not needed for anything else */
  245. #ifdef STYLE_WHITE
  246. set_style(ctx, THEME_WHITE);
  247. #elif defined(STYLE_RED)
  248. set_style(ctx, THEME_RED);
  249. #elif defined(STYLE_BLUE)
  250. set_style(ctx, THEME_BLUE);
  251. #elif defined(STYLE_DARK)
  252. set_style(ctx, THEME_DARK);
  253. #endif
  254. #endif
  255. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  256. while (running)
  257. {
  258. /* Input */
  259. XEvent evt;
  260. nk_input_begin(ctx);
  261. while (XPending(win.dpy)) {
  262. XNextEvent(win.dpy, &evt);
  263. if (evt.type == ClientMessage) goto cleanup;
  264. if (XFilterEvent(&evt, win.win)) continue;
  265. nk_x11_handle_event(&evt);
  266. }
  267. nk_input_end(ctx);
  268. /* GUI */
  269. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  270. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  271. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  272. {
  273. enum {EASY, HARD};
  274. static int op = EASY;
  275. static int property = 20;
  276. nk_layout_row_static(ctx, 30, 80, 1);
  277. if (nk_button_label(ctx, "button"))
  278. fprintf(stdout, "button pressed\n");
  279. nk_layout_row_dynamic(ctx, 30, 2);
  280. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  281. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  282. nk_layout_row_dynamic(ctx, 25, 1);
  283. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  284. nk_layout_row_dynamic(ctx, 20, 1);
  285. nk_label(ctx, "background:", NK_TEXT_LEFT);
  286. nk_layout_row_dynamic(ctx, 25, 1);
  287. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  288. nk_layout_row_dynamic(ctx, 120, 1);
  289. bg = nk_color_picker(ctx, bg, NK_RGBA);
  290. nk_layout_row_dynamic(ctx, 25, 1);
  291. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  292. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  293. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  294. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  295. nk_combo_end(ctx);
  296. }
  297. }
  298. nk_end(ctx);
  299. /* -------------- EXAMPLES ---------------- */
  300. #ifdef INCLUDE_CALCULATOR
  301. calculator(ctx);
  302. #endif
  303. #ifdef INCLUDE_CANVAS
  304. canvas(ctx);
  305. #endif
  306. #ifdef INCLUDE_OVERVIEW
  307. overview(ctx);
  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. }