1 /* nuklear - 1.40.8 - 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 <limits.h>
10 #include <time.h>
11 
12 #define NK_INCLUDE_FIXED_TYPES
13 #define NK_INCLUDE_STANDARD_IO
14 #define NK_INCLUDE_STANDARD_VARARGS
15 #define NK_INCLUDE_DEFAULT_ALLOCATOR
16 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
17 #define NK_INCLUDE_FONT_BAKING
18 #define NK_INCLUDE_DEFAULT_FONT
19 #define NK_IMPLEMENTATION
20 #define NK_SDL_GLES2_IMPLEMENTATION
21 #include "../../nuklear.h"
22 #include "nuklear_sdl_gles2.h"
23 
24 #define WINDOW_WIDTH 800
25 #define WINDOW_HEIGHT 600
26 
27 #define MAX_VERTEX_MEMORY 512 * 1024
28 #define MAX_ELEMENT_MEMORY 128 * 1024
29 
30 #define UNUSED(a) (void)a
31 #define MIN(a,b) ((a) < (b) ? (a) : (b))
32 #define MAX(a,b) ((a) < (b) ? (b) : (a))
33 #define LEN(a) (sizeof(a)/sizeof(a)[0])
34 
35 /* ===============================================================
36  *
37  *                          EXAMPLE
38  *
39  * ===============================================================*/
40 /* This are some code examples to provide a small overview of what can be
41  * done with this library. To try out an example uncomment the defines */
42 /*#define INCLUDE_ALL */
43 /*#define INCLUDE_STYLE */
44 /*#define INCLUDE_CALCULATOR */
45 /*#define INCLUDE_CANVAS */
46 /*#define INCLUDE_OVERVIEW */
47 /*#define INCLUDE_NODE_EDITOR */
48 
49 #ifdef INCLUDE_ALL
50   #define INCLUDE_STYLE
51   #define INCLUDE_CALCULATOR
52   #define INCLUDE_CANVAS
53   #define INCLUDE_OVERVIEW
54   #define INCLUDE_NODE_EDITOR
55 #endif
56 
57 #ifdef INCLUDE_STYLE
58   #include "../style.c"
59 #endif
60 #ifdef INCLUDE_CALCULATOR
61   #include "../calculator.c"
62 #endif
63 #ifdef INCLUDE_CANVAS
64   #include "../canvas.c"
65 #endif
66 #ifdef INCLUDE_OVERVIEW
67   #include "../overview.c"
68 #endif
69 #ifdef INCLUDE_NODE_EDITOR
70   #include "../node_editor.c"
71 #endif
72 
73 /* ===============================================================
74  *
75  *                          DEMO
76  *
77  * ===============================================================*/
78 
79 
80 /* Platform */
81 SDL_Window *win;
82 int running = nk_true;
83 
84 static void
MainLoop(void * loopArg)85 MainLoop(void* loopArg){
86     struct nk_context *ctx = (struct nk_context *)loopArg;
87 
88     /* Input */
89     SDL_Event evt;
90     nk_input_begin(ctx);
91     while (SDL_PollEvent(&evt)) {
92         if (evt.type == SDL_QUIT) running = nk_false;
93         nk_sdl_handle_event(&evt);
94     }
95     nk_input_end(ctx);
96 
97 
98     /* GUI */
99     if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
100         NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
101         NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
102     {
103         nk_menubar_begin(ctx);
104         nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
105         nk_layout_row_push(ctx, 45);
106         if (nk_menu_begin_label(ctx, "FILE", NK_TEXT_LEFT, nk_vec2(120, 200))) {
107             nk_layout_row_dynamic(ctx, 30, 1);
108             nk_menu_item_label(ctx, "OPEN", NK_TEXT_LEFT);
109             nk_menu_item_label(ctx, "CLOSE", NK_TEXT_LEFT);
110             nk_menu_end(ctx);
111         }
112         nk_layout_row_push(ctx, 45);
113         if (nk_menu_begin_label(ctx, "EDIT", NK_TEXT_LEFT, nk_vec2(120, 200))) {
114             nk_layout_row_dynamic(ctx, 30, 1);
115             nk_menu_item_label(ctx, "COPY", NK_TEXT_LEFT);
116             nk_menu_item_label(ctx, "CUT", NK_TEXT_LEFT);
117             nk_menu_item_label(ctx, "PASTE", NK_TEXT_LEFT);
118             nk_menu_end(ctx);
119         }
120         nk_layout_row_end(ctx);
121         nk_menubar_end(ctx);
122 
123         {
124             enum {EASY, HARD};
125             static int op = EASY;
126             static int property = 20;
127             nk_layout_row_static(ctx, 30, 80, 1);
128             if (nk_button_label(ctx, "button"))
129                 fprintf(stdout, "button pressed\n");
130              nk_layout_row_dynamic(ctx, 30, 2);
131              if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
132              if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
133              nk_layout_row_dynamic(ctx, 25, 1);
134              nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
135         }
136     }
137     nk_end(ctx);
138 
139     /* -------------- EXAMPLES ---------------- */
140     #ifdef INCLUDE_CALCULATOR
141       calculator(ctx);
142     #endif
143     #ifdef INCLUDE_CANVAS
144       canvas(ctx);
145     #endif
146     #ifdef INCLUDE_OVERVIEW
147       overview(ctx);
148     #endif
149     #ifdef INCLUDE_NODE_EDITOR
150       node_editor(ctx);
151     #endif
152     /* ----------------------------------------- */
153 
154     /* Draw */
155     {float bg[4];
156     int win_width, win_height;
157     nk_color_fv(bg, nk_rgb(28,48,62));
158     SDL_GetWindowSize(win, &win_width, &win_height);
159     glViewport(0, 0, win_width, win_height);
160     glClear(GL_COLOR_BUFFER_BIT);
161     glClearColor(bg[0], bg[1], bg[2], bg[3]);
162     /* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
163      * with blending, scissor, face culling, depth test and viewport and
164      * defaults everything back into a default state.
165      * Make sure to either a.) save and restore or b.) reset your own state after
166      * rendering the UI. */
167     nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
168     SDL_GL_SwapWindow(win);}
169 }
170 
main(int argc,char * argv[])171 int main(int argc, char* argv[])
172 {
173     /* GUI */
174     struct nk_context *ctx;
175     SDL_GLContext glContext;
176 
177     NK_UNUSED(argc);
178     NK_UNUSED(argv);
179 
180     /* SDL setup */
181     SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
182     /*SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); // - do NOT init SDL on GL ES 2 */
183     SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
184     SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
185     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
186     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
187     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
188     win = SDL_CreateWindow("Demo",
189         SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
190         WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
191     glContext = SDL_GL_CreateContext(win);
192 
193     /* OpenGL setup */
194     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
195 
196     ctx = nk_sdl_init(win);
197     /* Load Fonts: if none of these are loaded a default font will be used  */
198     /* Load Cursor: if you uncomment cursor loading please hide the cursor */
199     {struct nk_font_atlas *atlas;
200     nk_sdl_font_stash_begin(&atlas);
201     /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
202     /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
203     /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
204     /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
205     /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
206     /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
207     nk_sdl_font_stash_end();
208     /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
209     /*nk_style_set_font(ctx, &roboto->handle)*/;}
210 
211     /* style.c */
212     /*set_style(ctx, THEME_WHITE);*/
213     /*set_style(ctx, THEME_RED);*/
214     /*set_style(ctx, THEME_BLUE);*/
215     /*set_style(ctx, THEME_DARK);*/
216 
217 #if defined(__EMSCRIPTEN__)
218     #include <emscripten.h>
219     emscripten_set_main_loop_arg(MainLoop, (void*)ctx, 0, nk_true);
220 #else
221     while (running) MainLoop((void*)ctx);
222 #endif
223 
224     nk_sdl_shutdown();
225     SDL_GL_DeleteContext(glContext);
226     SDL_DestroyWindow(win);
227     SDL_Quit();
228     return 0;
229 }
230 
231