main.c 12 KB

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