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