1 /* nuklear - 1.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 <limits.h>
11 #include <time.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_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_GLFW_GL3_IMPLEMENTATION
25 #include "../../nuklear.h"
26 #include "nuklear_glfw_gl3.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  * ===============================================================*/
error_callback(int e,const char * d)72 static void error_callback(int e, const char *d)
73 {printf("Error %d: %s\n", e, d);}
74 
main(void)75 int main(void)
76 {
77     /* Platform */
78     static GLFWwindow *win;
79     int width = 0, height = 0;
80     struct nk_context *ctx;
81     struct nk_colorf bg;
82 
83     /* GLFW */
84     glfwSetErrorCallback(error_callback);
85     if (!glfwInit()) {
86         fprintf(stdout, "[GFLW] failed to init!\n");
87         exit(1);
88     }
89     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
90     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
91     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
92 #ifdef __APPLE__
93     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
94 #endif
95     win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
96     glfwMakeContextCurrent(win);
97     glfwGetWindowSize(win, &width, &height);
98 
99     /* OpenGL */
100     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
101     glewExperimental = 1;
102     if (glewInit() != GLEW_OK) {
103         fprintf(stderr, "Failed to setup GLEW\n");
104         exit(1);
105     }
106 
107     ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
108     /* Load Fonts: if none of these are loaded a default font will be used  */
109     /* Load Cursor: if you uncomment cursor loading please hide the cursor */
110     {struct nk_font_atlas *atlas;
111     nk_glfw3_font_stash_begin(&atlas);
112     /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
113     /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
114     /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
115     /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
116     /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
117     /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
118     nk_glfw3_font_stash_end();
119     /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
120     /*nk_style_set_font(ctx, &droid->handle);*/}
121 
122     #ifdef INCLUDE_STYLE
123     /*set_style(ctx, THEME_WHITE);*/
124     /*set_style(ctx, THEME_RED);*/
125     /*set_style(ctx, THEME_BLUE);*/
126     /*set_style(ctx, THEME_DARK);*/
127     #endif
128 
129     bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
130     while (!glfwWindowShouldClose(win))
131     {
132         /* Input */
133         glfwPollEvents();
134         nk_glfw3_new_frame();
135 
136         /* GUI */
137         if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
138             NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
139             NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
140         {
141             enum {EASY, HARD};
142             static int op = EASY;
143             static int property = 20;
144             nk_layout_row_static(ctx, 30, 80, 1);
145             if (nk_button_label(ctx, "button"))
146                 fprintf(stdout, "button pressed\n");
147 
148             nk_layout_row_dynamic(ctx, 30, 2);
149             if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
150             if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
151 
152             nk_layout_row_dynamic(ctx, 25, 1);
153             nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
154 
155             nk_layout_row_dynamic(ctx, 20, 1);
156             nk_label(ctx, "background:", NK_TEXT_LEFT);
157             nk_layout_row_dynamic(ctx, 25, 1);
158             if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
159                 nk_layout_row_dynamic(ctx, 120, 1);
160                 bg = nk_color_picker(ctx, bg, NK_RGBA);
161                 nk_layout_row_dynamic(ctx, 25, 1);
162                 bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
163                 bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
164                 bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
165                 bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
166                 nk_combo_end(ctx);
167             }
168         }
169         nk_end(ctx);
170 
171         /* -------------- EXAMPLES ---------------- */
172         #ifdef INCLUDE_CALCULATOR
173           calculator(ctx);
174         #endif
175         #ifdef INCLUDE_OVERVIEW
176           overview(ctx);
177         #endif
178         #ifdef INCLUDE_NODE_EDITOR
179           node_editor(ctx);
180         #endif
181         /* ----------------------------------------- */
182 
183         /* Draw */
184         glfwGetWindowSize(win, &width, &height);
185         glViewport(0, 0, width, height);
186         glClear(GL_COLOR_BUFFER_BIT);
187         glClearColor(bg.r, bg.g, bg.b, bg.a);
188         /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
189          * with blending, scissor, face culling, depth test and viewport and
190          * defaults everything back into a default state.
191          * Make sure to either a.) save and restore or b.) reset your own state after
192          * rendering the UI. */
193         nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
194         glfwSwapBuffers(win);
195     }
196     nk_glfw3_shutdown();
197     glfwTerminate();
198     return 0;
199 }
200 
201