main.c 12 KB

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