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