main.c 12 KB

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