main.c 12 KB

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