main.c 12 KB

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