1 /* nuklear - v1.05 - 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/glew.h>
14 #include <GLFW/glfw3.h>
15 
16 #define NK_INCLUDE_FIXED_TYPES
17 #define NK_INCLUDE_STANDARD_IO
18 #define NK_INCLUDE_DEFAULT_ALLOCATOR
19 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
20 #define NK_INCLUDE_FONT_BAKING
21 #define NK_INCLUDE_DEFAULT_FONT
22 #define NK_IMPLEMENTATION
23 #include "../nuklear.h"
24 
25 #define STB_IMAGE_IMPLEMENTATION
26 #include "stb_image.h"
27 
28 /* macros */
29 #define WINDOW_WIDTH 1200
30 #define WINDOW_HEIGHT 800
31 
32 #define MAX_VERTEX_MEMORY 512 * 1024
33 #define MAX_ELEMENT_MEMORY 128 * 1024
34 
35 #define UNUSED(a) (void)a
36 #define MIN(a,b) ((a) < (b) ? (a) : (b))
37 #define MAX(a,b) ((a) < (b) ? (b) : (a))
38 #define LEN(a) (sizeof(a)/sizeof(a)[0])
39 
40 #ifdef __APPLE__
41   #define NK_SHADER_VERSION "#version 150\n"
42 #else
43   #define NK_SHADER_VERSION "#version 300 es\n"
44 #endif
45 
46 struct media {
47     GLint skin;
48     struct nk_image menu;
49     struct nk_image check;
50     struct nk_image check_cursor;
51     struct nk_image option;
52     struct nk_image option_cursor;
53     struct nk_image header;
54     struct nk_image window;
55     struct nk_image scrollbar_inc_button;
56     struct nk_image scrollbar_inc_button_hover;
57     struct nk_image scrollbar_dec_button;
58     struct nk_image scrollbar_dec_button_hover;
59     struct nk_image button;
60     struct nk_image button_hover;
61     struct nk_image button_active;
62     struct nk_image tab_minimize;
63     struct nk_image tab_maximize;
64     struct nk_image slider;
65     struct nk_image slider_hover;
66     struct nk_image slider_active;
67 };
68 
69 
70 /* ===============================================================
71  *
72  *                          DEVICE
73  *
74  * ===============================================================*/
75 struct nk_glfw_vertex {
76     float position[2];
77     float uv[2];
78     nk_byte col[4];
79 };
80 
81 struct device {
82     struct nk_buffer cmds;
83     struct nk_draw_null_texture null;
84     GLuint vbo, vao, ebo;
85     GLuint prog;
86     GLuint vert_shdr;
87     GLuint frag_shdr;
88     GLint attrib_pos;
89     GLint attrib_uv;
90     GLint attrib_col;
91     GLint uniform_tex;
92     GLint uniform_proj;
93     GLuint font_tex;
94 };
95 
96 static void
die(const char * fmt,...)97 die(const char *fmt, ...)
98 {
99     va_list ap;
100     va_start(ap, fmt);
101     vfprintf(stderr, fmt, ap);
102     va_end(ap);
103     fputs("\n", stderr);
104     exit(EXIT_FAILURE);
105 }
106 
107 static GLuint
image_load(const char * filename)108 image_load(const char *filename)
109 {
110     int x,y,n;
111     GLuint tex;
112     unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
113     if (!data) die("failed to load image: %s", filename);
114 
115     glGenTextures(1, &tex);
116     glBindTexture(GL_TEXTURE_2D, tex);
117     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
118     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
119     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
120     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
121     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
122     glGenerateMipmap(GL_TEXTURE_2D);
123     stbi_image_free(data);
124     return tex;
125 }
126 
127 static void
device_init(struct device * dev)128 device_init(struct device *dev)
129 {
130     GLint status;
131     static const GLchar *vertex_shader =
132         NK_SHADER_VERSION
133         "uniform mat4 ProjMtx;\n"
134         "in vec2 Position;\n"
135         "in vec2 TexCoord;\n"
136         "in vec4 Color;\n"
137         "out vec2 Frag_UV;\n"
138         "out vec4 Frag_Color;\n"
139         "void main() {\n"
140         "   Frag_UV = TexCoord;\n"
141         "   Frag_Color = Color;\n"
142         "   gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
143         "}\n";
144     static const GLchar *fragment_shader =
145         NK_SHADER_VERSION
146         "precision mediump float;\n"
147         "uniform sampler2D Texture;\n"
148         "in vec2 Frag_UV;\n"
149         "in vec4 Frag_Color;\n"
150         "out vec4 Out_Color;\n"
151         "void main(){\n"
152         "   Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
153         "}\n";
154 
155     nk_buffer_init_default(&dev->cmds);
156     dev->prog = glCreateProgram();
157     dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
158     dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
159     glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
160     glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0);
161     glCompileShader(dev->vert_shdr);
162     glCompileShader(dev->frag_shdr);
163     glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status);
164     assert(status == GL_TRUE);
165     glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status);
166     assert(status == GL_TRUE);
167     glAttachShader(dev->prog, dev->vert_shdr);
168     glAttachShader(dev->prog, dev->frag_shdr);
169     glLinkProgram(dev->prog);
170     glGetProgramiv(dev->prog, GL_LINK_STATUS, &status);
171     assert(status == GL_TRUE);
172 
173     dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture");
174     dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx");
175     dev->attrib_pos = glGetAttribLocation(dev->prog, "Position");
176     dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord");
177     dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
178 
179     {
180         /* buffer setup */
181         GLsizei vs = sizeof(struct nk_glfw_vertex);
182         size_t vp = offsetof(struct nk_glfw_vertex, position);
183         size_t vt = offsetof(struct nk_glfw_vertex, uv);
184         size_t vc = offsetof(struct nk_glfw_vertex, col);
185 
186         glGenBuffers(1, &dev->vbo);
187         glGenBuffers(1, &dev->ebo);
188         glGenVertexArrays(1, &dev->vao);
189 
190         glBindVertexArray(dev->vao);
191         glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
192         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
193 
194         glEnableVertexAttribArray((GLuint)dev->attrib_pos);
195         glEnableVertexAttribArray((GLuint)dev->attrib_uv);
196         glEnableVertexAttribArray((GLuint)dev->attrib_col);
197 
198         glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp);
199         glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt);
200         glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc);
201     }
202 
203     glBindTexture(GL_TEXTURE_2D, 0);
204     glBindBuffer(GL_ARRAY_BUFFER, 0);
205     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
206     glBindVertexArray(0);
207 }
208 
209 static void
device_upload_atlas(struct device * dev,const void * image,int width,int height)210 device_upload_atlas(struct device *dev, const void *image, int width, int height)
211 {
212     glGenTextures(1, &dev->font_tex);
213     glBindTexture(GL_TEXTURE_2D, dev->font_tex);
214     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
215     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
216     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
217                 GL_RGBA, GL_UNSIGNED_BYTE, image);
218 }
219 
220 static void
device_shutdown(struct device * dev)221 device_shutdown(struct device *dev)
222 {
223     glDetachShader(dev->prog, dev->vert_shdr);
224     glDetachShader(dev->prog, dev->frag_shdr);
225     glDeleteShader(dev->vert_shdr);
226     glDeleteShader(dev->frag_shdr);
227     glDeleteProgram(dev->prog);
228     glDeleteTextures(1, &dev->font_tex);
229     glDeleteBuffers(1, &dev->vbo);
230     glDeleteBuffers(1, &dev->ebo);
231     nk_buffer_free(&dev->cmds);
232 }
233 
234 static void
device_draw(struct device * dev,struct nk_context * ctx,int width,int height,struct nk_vec2 scale,enum nk_anti_aliasing AA)235 device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
236     struct nk_vec2 scale, enum nk_anti_aliasing AA)
237 {
238     GLfloat ortho[4][4] = {
239         {2.0f, 0.0f, 0.0f, 0.0f},
240         {0.0f,-2.0f, 0.0f, 0.0f},
241         {0.0f, 0.0f,-1.0f, 0.0f},
242         {-1.0f,1.0f, 0.0f, 1.0f},
243     };
244     ortho[0][0] /= (GLfloat)width;
245     ortho[1][1] /= (GLfloat)height;
246 
247     /* setup global state */
248     glEnable(GL_BLEND);
249     glBlendEquation(GL_FUNC_ADD);
250     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
251     glDisable(GL_CULL_FACE);
252     glDisable(GL_DEPTH_TEST);
253     glEnable(GL_SCISSOR_TEST);
254     glActiveTexture(GL_TEXTURE0);
255 
256     /* setup program */
257     glUseProgram(dev->prog);
258     glUniform1i(dev->uniform_tex, 0);
259     glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]);
260     {
261         /* convert from command queue into draw list and draw to screen */
262         const struct nk_draw_command *cmd;
263         void *vertices, *elements;
264         const nk_draw_index *offset = NULL;
265 
266         /* allocate vertex and element buffer */
267         glBindVertexArray(dev->vao);
268         glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
269         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
270 
271         glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_MEMORY, NULL, GL_STREAM_DRAW);
272         glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW);
273 
274         /* load draw vertices & elements directly into vertex + element buffer */
275         vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
276         elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
277         {
278             /* fill convert configuration */
279             struct nk_convert_config config;
280             static const struct nk_draw_vertex_layout_element vertex_layout[] = {
281                 {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, position)},
282                 {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, uv)},
283                 {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_glfw_vertex, col)},
284                 {NK_VERTEX_LAYOUT_END}
285             };
286             NK_MEMSET(&config, 0, sizeof(config));
287             config.vertex_layout = vertex_layout;
288             config.vertex_size = sizeof(struct nk_glfw_vertex);
289             config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
290             config.null = dev->null;
291             config.circle_segment_count = 22;
292             config.curve_segment_count = 22;
293             config.arc_segment_count = 22;
294             config.global_alpha = 1.0f;
295             config.shape_AA = AA;
296             config.line_AA = AA;
297 
298             /* setup buffers to load vertices and elements */
299             {struct nk_buffer vbuf, ebuf;
300             nk_buffer_init_fixed(&vbuf, vertices, MAX_VERTEX_MEMORY);
301             nk_buffer_init_fixed(&ebuf, elements, MAX_ELEMENT_MEMORY);
302             nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config);}
303         }
304         glUnmapBuffer(GL_ARRAY_BUFFER);
305         glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
306 
307         /* iterate over and execute each draw command */
308         nk_draw_foreach(cmd, ctx, &dev->cmds)
309         {
310             if (!cmd->elem_count) continue;
311             glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
312             glScissor(
313                 (GLint)(cmd->clip_rect.x * scale.x),
314                 (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y),
315                 (GLint)(cmd->clip_rect.w * scale.x),
316                 (GLint)(cmd->clip_rect.h * scale.y));
317             glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
318             offset += cmd->elem_count;
319         }
320         nk_clear(ctx);
321     }
322 
323     /* default OpenGL state */
324     glUseProgram(0);
325     glBindBuffer(GL_ARRAY_BUFFER, 0);
326     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
327     glBindVertexArray(0);
328     glDisable(GL_BLEND);
329     glDisable(GL_SCISSOR_TEST);
330 }
331 
332 /* glfw callbacks (I don't know if there is a easier way to access text and scroll )*/
error_callback(int e,const char * d)333 static void error_callback(int e, const char *d){printf("Error %d: %s\n", e, d);}
text_input(GLFWwindow * win,unsigned int codepoint)334 static void text_input(GLFWwindow *win, unsigned int codepoint)
335 {nk_input_unicode((struct nk_context*)glfwGetWindowUserPointer(win), codepoint);}
scroll_input(GLFWwindow * win,double _,double yoff)336 static void scroll_input(GLFWwindow *win, double _, double yoff)
337 {UNUSED(_);nk_input_scroll((struct nk_context*)glfwGetWindowUserPointer(win), nk_vec2(0, (float)yoff));}
338 
main(int argc,char * argv[])339 int main(int argc, char *argv[])
340 {
341     /* Platform */
342     static GLFWwindow *win;
343     int width = 0, height = 0;
344     int display_width=0, display_height=0;
345 
346     /* GUI */
347     struct device device;
348     struct nk_font_atlas atlas;
349     struct media media;
350     struct nk_context ctx;
351     struct nk_font *font;
352 
353     /* GLFW */
354     glfwSetErrorCallback(error_callback);
355     if (!glfwInit()) {
356         fprintf(stdout, "[GFLW] failed to init!\n");
357         exit(1);
358     }
359     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
360     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
361     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
362 #ifdef __APPLE__
363     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
364 #endif
365     win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
366     glfwMakeContextCurrent(win);
367     glfwSetWindowUserPointer(win, &ctx);
368     glfwSetCharCallback(win, text_input);
369     glfwSetScrollCallback(win, scroll_input);
370     glfwGetWindowSize(win, &width, &height);
371     glfwGetFramebufferSize(win, &display_width, &display_height);
372 
373     /* OpenGL */
374     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
375     glewExperimental = 1;
376     if (glewInit() != GLEW_OK) {
377         fprintf(stderr, "Failed to setup GLEW\n");
378         exit(1);
379     }
380 
381     /* GUI */
382     {device_init(&device);
383     {const void *image; int w, h;
384     const char *font_path = (argc > 1) ? argv[1]: 0;
385     nk_font_atlas_init_default(&atlas);
386     nk_font_atlas_begin(&atlas);
387     if (font_path) font = nk_font_atlas_add_from_file(&atlas, font_path, 13.0f, NULL);
388     else font = nk_font_atlas_add_default(&atlas, 13.0f, NULL);
389     image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
390     device_upload_atlas(&device, image, w, h);
391     nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);}
392     nk_init_default(&ctx, &font->handle);}
393 
394     {   /* skin */
395         glEnable(GL_TEXTURE_2D);
396         media.skin = image_load("../skins/gwen.png");
397         media.check = nk_subimage_id(media.skin, 512,512, nk_rect(464,32,15,15));
398         media.check_cursor = nk_subimage_id(media.skin, 512,512, nk_rect(450,34,11,11));
399         media.option = nk_subimage_id(media.skin, 512,512, nk_rect(464,64,15,15));
400         media.option_cursor = nk_subimage_id(media.skin, 512,512, nk_rect(451,67,9,9));
401         media.header = nk_subimage_id(media.skin, 512,512, nk_rect(128,0,127,24));
402         media.window = nk_subimage_id(media.skin, 512,512, nk_rect(128,23,127,104));
403         media.scrollbar_inc_button = nk_subimage_id(media.skin, 512,512, nk_rect(464,256,15,15));
404         media.scrollbar_inc_button_hover = nk_subimage_id(media.skin, 512,512, nk_rect(464,320,15,15));
405         media.scrollbar_dec_button = nk_subimage_id(media.skin, 512,512, nk_rect(464,224,15,15));
406         media.scrollbar_dec_button_hover = nk_subimage_id(media.skin, 512,512, nk_rect(464,288,15,15));
407         media.button = nk_subimage_id(media.skin, 512,512, nk_rect(384,336,127,31));
408         media.button_hover = nk_subimage_id(media.skin, 512,512, nk_rect(384,368,127,31));
409         media.button_active = nk_subimage_id(media.skin, 512,512, nk_rect(384,400,127,31));
410         media.tab_minimize = nk_subimage_id(media.skin, 512,512, nk_rect(451, 99, 9, 9));
411         media.tab_maximize = nk_subimage_id(media.skin, 512,512, nk_rect(467,99,9,9));
412         media.slider = nk_subimage_id(media.skin, 512,512, nk_rect(418,33,11,14));
413         media.slider_hover = nk_subimage_id(media.skin, 512,512, nk_rect(418,49,11,14));
414         media.slider_active = nk_subimage_id(media.skin, 512,512, nk_rect(418,64,11,14));
415 
416         /* window */
417         ctx.style.window.background = nk_rgb(204,204,204);
418         ctx.style.window.fixed_background = nk_style_item_image(media.window);
419         ctx.style.window.border_color = nk_rgb(67,67,67);
420         ctx.style.window.combo_border_color = nk_rgb(67,67,67);
421         ctx.style.window.contextual_border_color = nk_rgb(67,67,67);
422         ctx.style.window.menu_border_color = nk_rgb(67,67,67);
423         ctx.style.window.group_border_color = nk_rgb(67,67,67);
424         ctx.style.window.tooltip_border_color = nk_rgb(67,67,67);
425         ctx.style.window.scrollbar_size = nk_vec2(16,16);
426         ctx.style.window.border_color = nk_rgba(0,0,0,0);
427         ctx.style.window.padding = nk_vec2(8,4);
428         ctx.style.window.border = 3;
429 
430         /* window header */
431         ctx.style.window.header.normal = nk_style_item_image(media.header);
432         ctx.style.window.header.hover = nk_style_item_image(media.header);
433         ctx.style.window.header.active = nk_style_item_image(media.header);
434         ctx.style.window.header.label_normal = nk_rgb(95,95,95);
435         ctx.style.window.header.label_hover = nk_rgb(95,95,95);
436         ctx.style.window.header.label_active = nk_rgb(95,95,95);
437 
438         /* scrollbar */
439         ctx.style.scrollv.normal          = nk_style_item_color(nk_rgb(184,184,184));
440         ctx.style.scrollv.hover           = nk_style_item_color(nk_rgb(184,184,184));
441         ctx.style.scrollv.active          = nk_style_item_color(nk_rgb(184,184,184));
442         ctx.style.scrollv.cursor_normal   = nk_style_item_color(nk_rgb(220,220,220));
443         ctx.style.scrollv.cursor_hover    = nk_style_item_color(nk_rgb(235,235,235));
444         ctx.style.scrollv.cursor_active   = nk_style_item_color(nk_rgb(99,202,255));
445         ctx.style.scrollv.dec_symbol      = NK_SYMBOL_NONE;
446         ctx.style.scrollv.inc_symbol      = NK_SYMBOL_NONE;
447         ctx.style.scrollv.show_buttons    = nk_true;
448         ctx.style.scrollv.border_color    = nk_rgb(81,81,81);
449         ctx.style.scrollv.cursor_border_color = nk_rgb(81,81,81);
450         ctx.style.scrollv.border          = 1;
451         ctx.style.scrollv.rounding        = 0;
452         ctx.style.scrollv.border_cursor   = 1;
453         ctx.style.scrollv.rounding_cursor = 2;
454 
455         /* scrollbar buttons */
456         ctx.style.scrollv.inc_button.normal          = nk_style_item_image(media.scrollbar_inc_button);
457         ctx.style.scrollv.inc_button.hover           = nk_style_item_image(media.scrollbar_inc_button_hover);
458         ctx.style.scrollv.inc_button.active          = nk_style_item_image(media.scrollbar_inc_button_hover);
459         ctx.style.scrollv.inc_button.border_color    = nk_rgba(0,0,0,0);
460         ctx.style.scrollv.inc_button.text_background = nk_rgba(0,0,0,0);
461         ctx.style.scrollv.inc_button.text_normal     = nk_rgba(0,0,0,0);
462         ctx.style.scrollv.inc_button.text_hover      = nk_rgba(0,0,0,0);
463         ctx.style.scrollv.inc_button.text_active     = nk_rgba(0,0,0,0);
464         ctx.style.scrollv.inc_button.border          = 0.0f;
465 
466         ctx.style.scrollv.dec_button.normal          = nk_style_item_image(media.scrollbar_dec_button);
467         ctx.style.scrollv.dec_button.hover           = nk_style_item_image(media.scrollbar_dec_button_hover);
468         ctx.style.scrollv.dec_button.active          = nk_style_item_image(media.scrollbar_dec_button_hover);
469         ctx.style.scrollv.dec_button.border_color    = nk_rgba(0,0,0,0);
470         ctx.style.scrollv.dec_button.text_background = nk_rgba(0,0,0,0);
471         ctx.style.scrollv.dec_button.text_normal     = nk_rgba(0,0,0,0);
472         ctx.style.scrollv.dec_button.text_hover      = nk_rgba(0,0,0,0);
473         ctx.style.scrollv.dec_button.text_active     = nk_rgba(0,0,0,0);
474         ctx.style.scrollv.dec_button.border          = 0.0f;
475 
476         /* checkbox toggle */
477         {struct nk_style_toggle *toggle;
478         toggle = &ctx.style.checkbox;
479         toggle->normal          = nk_style_item_image(media.check);
480         toggle->hover           = nk_style_item_image(media.check);
481         toggle->active          = nk_style_item_image(media.check);
482         toggle->cursor_normal   = nk_style_item_image(media.check_cursor);
483         toggle->cursor_hover    = nk_style_item_image(media.check_cursor);
484         toggle->text_normal     = nk_rgb(95,95,95);
485         toggle->text_hover      = nk_rgb(95,95,95);
486         toggle->text_active     = nk_rgb(95,95,95);}
487 
488         /* option toggle */
489         {struct nk_style_toggle *toggle;
490         toggle = &ctx.style.option;
491         toggle->normal          = nk_style_item_image(media.option);
492         toggle->hover           = nk_style_item_image(media.option);
493         toggle->active          = nk_style_item_image(media.option);
494         toggle->cursor_normal   = nk_style_item_image(media.option_cursor);
495         toggle->cursor_hover    = nk_style_item_image(media.option_cursor);
496         toggle->text_normal     = nk_rgb(95,95,95);
497         toggle->text_hover      = nk_rgb(95,95,95);
498         toggle->text_active     = nk_rgb(95,95,95);}
499 
500         /* default button */
501         ctx.style.button.normal = nk_style_item_image(media.button);
502         ctx.style.button.hover = nk_style_item_image(media.button_hover);
503         ctx.style.button.active = nk_style_item_image(media.button_active);
504         ctx.style.button.border_color = nk_rgba(0,0,0,0);
505         ctx.style.button.text_background = nk_rgba(0,0,0,0);
506         ctx.style.button.text_normal = nk_rgb(95,95,95);
507         ctx.style.button.text_hover = nk_rgb(95,95,95);
508         ctx.style.button.text_active = nk_rgb(95,95,95);
509 
510         /* default text */
511         ctx.style.text.color = nk_rgb(95,95,95);
512 
513         /* contextual button */
514         ctx.style.contextual_button.normal = nk_style_item_color(nk_rgb(206,206,206));
515         ctx.style.contextual_button.hover = nk_style_item_color(nk_rgb(229,229,229));
516         ctx.style.contextual_button.active = nk_style_item_color(nk_rgb(99,202,255));
517         ctx.style.contextual_button.border_color = nk_rgba(0,0,0,0);
518         ctx.style.contextual_button.text_background = nk_rgba(0,0,0,0);
519         ctx.style.contextual_button.text_normal = nk_rgb(95,95,95);
520         ctx.style.contextual_button.text_hover = nk_rgb(95,95,95);
521         ctx.style.contextual_button.text_active = nk_rgb(95,95,95);
522 
523         /* menu button */
524         ctx.style.menu_button.normal = nk_style_item_color(nk_rgb(206,206,206));
525         ctx.style.menu_button.hover = nk_style_item_color(nk_rgb(229,229,229));
526         ctx.style.menu_button.active = nk_style_item_color(nk_rgb(99,202,255));
527         ctx.style.menu_button.border_color = nk_rgba(0,0,0,0);
528         ctx.style.menu_button.text_background = nk_rgba(0,0,0,0);
529         ctx.style.menu_button.text_normal = nk_rgb(95,95,95);
530         ctx.style.menu_button.text_hover = nk_rgb(95,95,95);
531         ctx.style.menu_button.text_active = nk_rgb(95,95,95);
532 
533         /* tree */
534         ctx.style.tab.text = nk_rgb(95,95,95);
535         ctx.style.tab.tab_minimize_button.normal = nk_style_item_image(media.tab_minimize);
536         ctx.style.tab.tab_minimize_button.hover = nk_style_item_image(media.tab_minimize);
537         ctx.style.tab.tab_minimize_button.active = nk_style_item_image(media.tab_minimize);
538         ctx.style.tab.tab_minimize_button.text_background = nk_rgba(0,0,0,0);
539         ctx.style.tab.tab_minimize_button.text_normal = nk_rgba(0,0,0,0);
540         ctx.style.tab.tab_minimize_button.text_hover = nk_rgba(0,0,0,0);
541         ctx.style.tab.tab_minimize_button.text_active = nk_rgba(0,0,0,0);
542 
543         ctx.style.tab.tab_maximize_button.normal = nk_style_item_image(media.tab_maximize);
544         ctx.style.tab.tab_maximize_button.hover = nk_style_item_image(media.tab_maximize);
545         ctx.style.tab.tab_maximize_button.active = nk_style_item_image(media.tab_maximize);
546         ctx.style.tab.tab_maximize_button.text_background = nk_rgba(0,0,0,0);
547         ctx.style.tab.tab_maximize_button.text_normal = nk_rgba(0,0,0,0);
548         ctx.style.tab.tab_maximize_button.text_hover = nk_rgba(0,0,0,0);
549         ctx.style.tab.tab_maximize_button.text_active = nk_rgba(0,0,0,0);
550 
551         ctx.style.tab.node_minimize_button.normal = nk_style_item_image(media.tab_minimize);
552         ctx.style.tab.node_minimize_button.hover = nk_style_item_image(media.tab_minimize);
553         ctx.style.tab.node_minimize_button.active = nk_style_item_image(media.tab_minimize);
554         ctx.style.tab.node_minimize_button.text_background = nk_rgba(0,0,0,0);
555         ctx.style.tab.node_minimize_button.text_normal = nk_rgba(0,0,0,0);
556         ctx.style.tab.node_minimize_button.text_hover = nk_rgba(0,0,0,0);
557         ctx.style.tab.node_minimize_button.text_active = nk_rgba(0,0,0,0);
558 
559         ctx.style.tab.node_maximize_button.normal = nk_style_item_image(media.tab_maximize);
560         ctx.style.tab.node_maximize_button.hover = nk_style_item_image(media.tab_maximize);
561         ctx.style.tab.node_maximize_button.active = nk_style_item_image(media.tab_maximize);
562         ctx.style.tab.node_maximize_button.text_background = nk_rgba(0,0,0,0);
563         ctx.style.tab.node_maximize_button.text_normal = nk_rgba(0,0,0,0);
564         ctx.style.tab.node_maximize_button.text_hover = nk_rgba(0,0,0,0);
565         ctx.style.tab.node_maximize_button.text_active = nk_rgba(0,0,0,0);
566 
567         /* selectable */
568         ctx.style.selectable.normal = nk_style_item_color(nk_rgb(206,206,206));
569         ctx.style.selectable.hover = nk_style_item_color(nk_rgb(206,206,206));
570         ctx.style.selectable.pressed = nk_style_item_color(nk_rgb(206,206,206));
571         ctx.style.selectable.normal_active = nk_style_item_color(nk_rgb(185,205,248));
572         ctx.style.selectable.hover_active = nk_style_item_color(nk_rgb(185,205,248));
573         ctx.style.selectable.pressed_active = nk_style_item_color(nk_rgb(185,205,248));
574         ctx.style.selectable.text_normal = nk_rgb(95,95,95);
575         ctx.style.selectable.text_hover = nk_rgb(95,95,95);
576         ctx.style.selectable.text_pressed = nk_rgb(95,95,95);
577         ctx.style.selectable.text_normal_active = nk_rgb(95,95,95);
578         ctx.style.selectable.text_hover_active = nk_rgb(95,95,95);
579         ctx.style.selectable.text_pressed_active = nk_rgb(95,95,95);
580 
581         /* slider */
582         ctx.style.slider.normal          = nk_style_item_hide();
583         ctx.style.slider.hover           = nk_style_item_hide();
584         ctx.style.slider.active          = nk_style_item_hide();
585         ctx.style.slider.bar_normal      = nk_rgb(156,156,156);
586         ctx.style.slider.bar_hover       = nk_rgb(156,156,156);
587         ctx.style.slider.bar_active      = nk_rgb(156,156,156);
588         ctx.style.slider.bar_filled      = nk_rgb(156,156,156);
589         ctx.style.slider.cursor_normal   = nk_style_item_image(media.slider);
590         ctx.style.slider.cursor_hover    = nk_style_item_image(media.slider_hover);
591         ctx.style.slider.cursor_active   = nk_style_item_image(media.slider_active);
592         ctx.style.slider.cursor_size     = nk_vec2(16.5f,21);
593         ctx.style.slider.bar_height      = 1;
594 
595         /* progressbar */
596         ctx.style.progress.normal = nk_style_item_color(nk_rgb(231,231,231));
597         ctx.style.progress.hover = nk_style_item_color(nk_rgb(231,231,231));
598         ctx.style.progress.active = nk_style_item_color(nk_rgb(231,231,231));
599         ctx.style.progress.cursor_normal = nk_style_item_color(nk_rgb(63,242,93));
600         ctx.style.progress.cursor_hover = nk_style_item_color(nk_rgb(63,242,93));
601         ctx.style.progress.cursor_active = nk_style_item_color(nk_rgb(63,242,93));
602         ctx.style.progress.border_color = nk_rgb(114,116,115);
603         ctx.style.progress.padding = nk_vec2(0,0);
604         ctx.style.progress.border = 2;
605         ctx.style.progress.rounding = 1;
606 
607         /* combo */
608         ctx.style.combo.normal = nk_style_item_color(nk_rgb(216,216,216));
609         ctx.style.combo.hover = nk_style_item_color(nk_rgb(216,216,216));
610         ctx.style.combo.active = nk_style_item_color(nk_rgb(216,216,216));
611         ctx.style.combo.border_color = nk_rgb(95,95,95);
612         ctx.style.combo.label_normal = nk_rgb(95,95,95);
613         ctx.style.combo.label_hover = nk_rgb(95,95,95);
614         ctx.style.combo.label_active = nk_rgb(95,95,95);
615         ctx.style.combo.border = 1;
616         ctx.style.combo.rounding = 1;
617 
618         /* combo button */
619         ctx.style.combo.button.normal = nk_style_item_color(nk_rgb(216,216,216));
620         ctx.style.combo.button.hover = nk_style_item_color(nk_rgb(216,216,216));
621         ctx.style.combo.button.active = nk_style_item_color(nk_rgb(216,216,216));
622         ctx.style.combo.button.text_background = nk_rgb(216,216,216);
623         ctx.style.combo.button.text_normal = nk_rgb(95,95,95);
624         ctx.style.combo.button.text_hover = nk_rgb(95,95,95);
625         ctx.style.combo.button.text_active = nk_rgb(95,95,95);
626 
627         /* property */
628         ctx.style.property.normal = nk_style_item_color(nk_rgb(216,216,216));
629         ctx.style.property.hover = nk_style_item_color(nk_rgb(216,216,216));
630         ctx.style.property.active = nk_style_item_color(nk_rgb(216,216,216));
631         ctx.style.property.border_color = nk_rgb(81,81,81);
632         ctx.style.property.label_normal = nk_rgb(95,95,95);
633         ctx.style.property.label_hover = nk_rgb(95,95,95);
634         ctx.style.property.label_active = nk_rgb(95,95,95);
635         ctx.style.property.sym_left = NK_SYMBOL_TRIANGLE_LEFT;
636         ctx.style.property.sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
637         ctx.style.property.rounding = 10;
638         ctx.style.property.border = 1;
639 
640         /* edit */
641         ctx.style.edit.normal = nk_style_item_color(nk_rgb(240,240,240));
642         ctx.style.edit.hover = nk_style_item_color(nk_rgb(240,240,240));
643         ctx.style.edit.active = nk_style_item_color(nk_rgb(240,240,240));
644         ctx.style.edit.border_color = nk_rgb(62,62,62);
645         ctx.style.edit.cursor_normal = nk_rgb(99,202,255);
646         ctx.style.edit.cursor_hover = nk_rgb(99,202,255);
647         ctx.style.edit.cursor_text_normal = nk_rgb(95,95,95);
648         ctx.style.edit.cursor_text_hover = nk_rgb(95,95,95);
649         ctx.style.edit.text_normal = nk_rgb(95,95,95);
650         ctx.style.edit.text_hover = nk_rgb(95,95,95);
651         ctx.style.edit.text_active = nk_rgb(95,95,95);
652         ctx.style.edit.selected_normal = nk_rgb(99,202,255);
653         ctx.style.edit.selected_hover = nk_rgb(99,202,255);
654         ctx.style.edit.selected_text_normal = nk_rgb(95,95,95);
655         ctx.style.edit.selected_text_hover = nk_rgb(95,95,95);
656         ctx.style.edit.border = 1;
657         ctx.style.edit.rounding = 2;
658 
659         /* property buttons */
660         ctx.style.property.dec_button.normal = nk_style_item_color(nk_rgb(216,216,216));
661         ctx.style.property.dec_button.hover = nk_style_item_color(nk_rgb(216,216,216));
662         ctx.style.property.dec_button.active = nk_style_item_color(nk_rgb(216,216,216));
663         ctx.style.property.dec_button.text_background = nk_rgba(0,0,0,0);
664         ctx.style.property.dec_button.text_normal = nk_rgb(95,95,95);
665         ctx.style.property.dec_button.text_hover = nk_rgb(95,95,95);
666         ctx.style.property.dec_button.text_active = nk_rgb(95,95,95);
667         ctx.style.property.inc_button = ctx.style.property.dec_button;
668 
669         /* property edit */
670         ctx.style.property.edit.normal = nk_style_item_color(nk_rgb(216,216,216));
671         ctx.style.property.edit.hover = nk_style_item_color(nk_rgb(216,216,216));
672         ctx.style.property.edit.active = nk_style_item_color(nk_rgb(216,216,216));
673         ctx.style.property.edit.border_color = nk_rgba(0,0,0,0);
674         ctx.style.property.edit.cursor_normal = nk_rgb(95,95,95);
675         ctx.style.property.edit.cursor_hover = nk_rgb(95,95,95);
676         ctx.style.property.edit.cursor_text_normal = nk_rgb(216,216,216);
677         ctx.style.property.edit.cursor_text_hover = nk_rgb(216,216,216);
678         ctx.style.property.edit.text_normal = nk_rgb(95,95,95);
679         ctx.style.property.edit.text_hover = nk_rgb(95,95,95);
680         ctx.style.property.edit.text_active = nk_rgb(95,95,95);
681         ctx.style.property.edit.selected_normal = nk_rgb(95,95,95);
682         ctx.style.property.edit.selected_hover = nk_rgb(95,95,95);
683         ctx.style.property.edit.selected_text_normal = nk_rgb(216,216,216);
684         ctx.style.property.edit.selected_text_hover = nk_rgb(216,216,216);
685 
686         /* chart */
687         ctx.style.chart.background = nk_style_item_color(nk_rgb(216,216,216));
688         ctx.style.chart.border_color = nk_rgb(81,81,81);
689         ctx.style.chart.color = nk_rgb(95,95,95);
690         ctx.style.chart.selected_color = nk_rgb(255,0,0);
691         ctx.style.chart.border = 1;
692     }
693 
694     while (!glfwWindowShouldClose(win))
695     {
696         /* High DPI displays */
697         struct nk_vec2 scale;
698         glfwGetWindowSize(win, &width, &height);
699         glfwGetFramebufferSize(win, &display_width, &display_height);
700         scale.x = (float)display_width/(float)width;
701         scale.y = (float)display_height/(float)height;
702 
703         /* Input */
704         {double x, y;
705         nk_input_begin(&ctx);
706         glfwPollEvents();
707         nk_input_key(&ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
708         nk_input_key(&ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
709         nk_input_key(&ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
710         nk_input_key(&ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
711         nk_input_key(&ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
712         nk_input_key(&ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
713         nk_input_key(&ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
714         nk_input_key(&ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
715         if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
716             glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
717             nk_input_key(&ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
718             nk_input_key(&ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS);
719             nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
720             nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
721             nk_input_key(&ctx, NK_KEY_SHIFT, 1);
722         } else {
723             nk_input_key(&ctx, NK_KEY_COPY, 0);
724             nk_input_key(&ctx, NK_KEY_PASTE, 0);
725             nk_input_key(&ctx, NK_KEY_CUT, 0);
726             nk_input_key(&ctx, NK_KEY_SHIFT, 0);
727         }
728         glfwGetCursorPos(win, &x, &y);
729         nk_input_motion(&ctx, (int)x, (int)y);
730         nk_input_button(&ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
731         nk_input_button(&ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
732         nk_input_button(&ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
733         nk_input_end(&ctx);}
734 
735         /* GUI */
736         {struct nk_panel layout, tab;
737         if (nk_begin(&ctx, "Demo", nk_rect(50, 50, 300, 400),
738             NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_TITLE))
739         {
740             int i;
741             float id;
742             static int slider = 10;
743             static int field_len;
744             static nk_size prog_value = 60;
745             static int current_weapon = 0;
746             static char field_buffer[64];
747             static float pos;
748             static const char *weapons[] = {"Fist","Pistol","Shotgun","Plasma","BFG"};
749             const float step = (2*3.141592654f) / 32;
750 
751             nk_layout_row_static(&ctx, 30, 120, 1);
752             if (nk_button_label(&ctx, "button"))
753                 fprintf(stdout, "button pressed\n");
754 
755             nk_layout_row_dynamic(&ctx, 20, 1);
756             nk_label(&ctx, "Label", NK_TEXT_LEFT);
757             nk_layout_row_dynamic(&ctx, 30, 2);
758             nk_check_label(&ctx, "inactive", 0);
759             nk_check_label(&ctx, "active", 1);
760             nk_option_label(&ctx, "active", 1);
761             nk_option_label(&ctx, "inactive", 0);
762 
763             nk_layout_row_dynamic(&ctx, 30, 1);
764             nk_slider_int(&ctx, 0, &slider, 16, 1);
765             nk_layout_row_dynamic(&ctx, 20, 1);
766             nk_progress(&ctx, &prog_value, 100, NK_MODIFIABLE);
767 
768             nk_layout_row_dynamic(&ctx, 25, 1);
769             nk_edit_string(&ctx, NK_EDIT_FIELD, field_buffer, &field_len, 64, nk_filter_default);
770             nk_property_float(&ctx, "#X:", -1024.0f, &pos, 1024.0f, 1, 1);
771             current_weapon = nk_combo(&ctx, weapons, LEN(weapons), current_weapon, 25, nk_vec2(nk_widget_width(&ctx),200));
772 
773             nk_layout_row_dynamic(&ctx, 100, 1);
774             if (nk_chart_begin_colored(&ctx, NK_CHART_LINES, nk_rgb(255,0,0), nk_rgb(150,0,0), 32, 0.0f, 1.0f)) {
775                 nk_chart_add_slot_colored(&ctx, NK_CHART_LINES, nk_rgb(0,0,255), nk_rgb(0,0,150),32, -1.0f, 1.0f);
776                 nk_chart_add_slot_colored(&ctx, NK_CHART_LINES, nk_rgb(0,255,0), nk_rgb(0,150,0), 32, -1.0f, 1.0f);
777                 for (id = 0, i = 0; i < 32; ++i) {
778                     nk_chart_push_slot(&ctx, (float)fabs(sin(id)), 0);
779                     nk_chart_push_slot(&ctx, (float)cos(id), 1);
780                     nk_chart_push_slot(&ctx, (float)sin(id), 2);
781                     id += step;
782                 }
783             }
784             nk_chart_end(&ctx);
785 
786             nk_layout_row_dynamic(&ctx, 250, 1);
787             if (nk_group_begin(&ctx, "Standard", NK_WINDOW_BORDER|NK_WINDOW_BORDER))
788             {
789                 if (nk_tree_push(&ctx, NK_TREE_NODE, "Window", NK_MAXIMIZED)) {
790                     static int selected[8];
791                     if (nk_tree_push(&ctx, NK_TREE_NODE, "Next", NK_MAXIMIZED)) {
792                         nk_layout_row_dynamic(&ctx, 20, 1);
793                         for (i = 0; i < 4; ++i)
794                             nk_selectable_label(&ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_LEFT, &selected[i]);
795                         nk_tree_pop(&ctx);
796                     }
797                     if (nk_tree_push(&ctx, NK_TREE_NODE, "Previous", NK_MAXIMIZED)) {
798                         nk_layout_row_dynamic(&ctx, 20, 1);
799                         for (i = 4; i < 8; ++i)
800                             nk_selectable_label(&ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_LEFT, &selected[i]);
801                         nk_tree_pop(&ctx);
802                     }
803                     nk_tree_pop(&ctx);
804                 }
805                 nk_group_end(&ctx);
806             }
807         }
808         nk_end(&ctx);}
809 
810         /* Draw */
811         glViewport(0, 0, display_width, display_height);
812         glClear(GL_COLOR_BUFFER_BIT);
813         glClearColor(0.5882, 0.6666, 0.6666, 1.0f);
814         device_draw(&device, &ctx, width, height, scale, NK_ANTI_ALIASING_ON);
815         glfwSwapBuffers(win);
816     }
817     glDeleteTextures(1,(const GLuint*)&media.skin);
818     nk_font_atlas_clear(&atlas);
819     nk_free(&ctx);
820     device_shutdown(&device);
821     glfwTerminate();
822     return 0;
823 }
824 
825