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