main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* nuklear - v1.17 - 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. 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. }
  169. {
  170. /* create opengl context */
  171. typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
  172. int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler);
  173. const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy));
  174. glxCreateContext create_context = (glxCreateContext)
  175. glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
  176. gl_err = FALSE;
  177. if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) {
  178. fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n");
  179. fprintf(stdout, "[X11]: ... using old-style GLX context\n");
  180. glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True);
  181. } else {
  182. GLint attr[] = {
  183. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  184. GLX_CONTEXT_MINOR_VERSION_ARB, 0,
  185. None
  186. };
  187. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  188. XSync(win.dpy, False);
  189. if (gl_err || !glContext) {
  190. /* Could not create GL 3.0 context. Fallback to old 2.x context.
  191. * If a version below 3.0 is requested, implementations will
  192. * return the newest context version compatible with OpenGL
  193. * version less than version 3.0.*/
  194. attr[1] = 1; attr[3] = 0;
  195. gl_err = FALSE;
  196. fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n");
  197. fprintf(stdout, "[X11] ... using old-style GLX context!\n");
  198. glContext = create_context(win.dpy, win.fbc, 0, True, attr);
  199. }
  200. }
  201. XSync(win.dpy, False);
  202. XSetErrorHandler(old_handler);
  203. if (gl_err || !glContext)
  204. die("[X11]: Failed to create an OpenGL context\n");
  205. glXMakeCurrent(win.dpy, win.win, glContext);
  206. }
  207. ctx = nk_x11_init(win.dpy, win.win);
  208. /* Load Fonts: if none of these are loaded a default font will be used */
  209. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  210. {struct nk_font_atlas *atlas;
  211. nk_x11_font_stash_begin(&atlas);
  212. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  213. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  214. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  215. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  216. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  217. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  218. nk_x11_font_stash_end();
  219. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  220. /*nk_style_set_font(ctx, &droid->handle);*/}
  221. /* style.c */
  222. /*set_style(ctx, THEME_WHITE);*/
  223. /*set_style(ctx, THEME_RED);*/
  224. /*set_style(ctx, THEME_BLUE);*/
  225. /*set_style(ctx, THEME_DARK);*/
  226. background = nk_rgb(28,48,62);
  227. while (running)
  228. {
  229. /* Input */
  230. XEvent evt;
  231. nk_input_begin(ctx);
  232. while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)){
  233. if (XFilterEvent(&evt, win.win)) continue;
  234. nk_x11_handle_event(&evt);
  235. }
  236. nk_input_end(ctx);
  237. /* GUI */
  238. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  239. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  240. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  241. {
  242. enum {EASY, HARD};
  243. static int op = EASY;
  244. static int property = 20;
  245. nk_layout_row_static(ctx, 30, 80, 1);
  246. if (nk_button_label(ctx, "button"))
  247. fprintf(stdout, "button pressed\n");
  248. nk_layout_row_dynamic(ctx, 30, 2);
  249. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  250. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  251. nk_layout_row_dynamic(ctx, 25, 1);
  252. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  253. nk_layout_row_dynamic(ctx, 20, 1);
  254. nk_label(ctx, "background:", NK_TEXT_LEFT);
  255. nk_layout_row_dynamic(ctx, 25, 1);
  256. if (nk_combo_begin_color(ctx, background, nk_vec2(nk_widget_width(ctx),400))) {
  257. nk_layout_row_dynamic(ctx, 120, 1);
  258. background = nk_color_picker(ctx, background, NK_RGBA);
  259. nk_layout_row_dynamic(ctx, 25, 1);
  260. background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
  261. background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
  262. background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
  263. background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
  264. nk_combo_end(ctx);
  265. }
  266. }
  267. nk_end(ctx);
  268. if (nk_window_is_closed(ctx, "Demo")) break;
  269. /* -------------- EXAMPLES ---------------- */
  270. /*calculator(ctx);*/
  271. /*overview(ctx);*/
  272. /*node_editor(ctx);*/
  273. /* ----------------------------------------- */
  274. /* Draw */
  275. {float bg[4];
  276. nk_color_fv(bg, background);
  277. XGetWindowAttributes(win.dpy, win.win, &win.attr);
  278. glViewport(0, 0, win.width, win.height);
  279. glClear(GL_COLOR_BUFFER_BIT);
  280. glClearColor(bg[0], bg[1], bg[2], bg[3]);
  281. /* IMPORTANT: `nk_x11_render` modifies some global OpenGL state
  282. * with blending, scissor, face culling, depth test and viewport and
  283. * defaults everything back into a default state.
  284. * Make sure to either a.) save and restore or b.) reset your own state after
  285. * rendering the UI. */
  286. nk_x11_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  287. glXSwapBuffers(win.dpy, win.win);}
  288. }
  289. nk_x11_shutdown();
  290. glXMakeCurrent(win.dpy, 0, 0);
  291. glXDestroyContext(win.dpy, glContext);
  292. XUnmapWindow(win.dpy, win.win);
  293. XFreeColormap(win.dpy, win.cmap);
  294. XDestroyWindow(win.dpy, win.win);
  295. XCloseDisplay(win.dpy);
  296. return 0;
  297. }