diff --git a/demo/common/overview.c b/demo/common/overview.c index 45458543b..491e424e2 100644 --- a/demo/common/overview.c +++ b/demo/common/overview.c @@ -18,7 +18,7 @@ overview(struct nk_context *ctx) #ifdef INCLUDE_STYLE /* styles */ - static const char* themes[] = {"Black", "White", "Red", "Blue", "Dark", "Dracula", + static const char* themes[] = {"Black", "White", "Red", "Blue", "Dark", "Dracula", "Catppucin Latte", "Catppucin Frappe", "Catppucin Macchiato", "Catppucin Mocha"}; static int current_theme = 0; #endif @@ -820,7 +820,7 @@ overview(struct nk_context *ctx) if (nk_popup_begin(ctx, NK_POPUP_STATIC, "Error", 0, s)) { nk_layout_row_dynamic(ctx, 25, 1); - nk_label(ctx, "A terrible error as occurred", NK_TEXT_LEFT); + nk_label(ctx, "A terrible error has occurred", NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 2); if (nk_button_label(ctx, "OK")) { popup_active = 0; diff --git a/demo/sdl_opengl2/main.c b/demo/sdl_opengl2/main.c index ebd84eb53..1e2901b40 100644 --- a/demo/sdl_opengl2/main.c +++ b/demo/sdl_opengl2/main.c @@ -12,6 +12,11 @@ #include #include +long SDL_GetTicks64() +{ + return SDL_GetTicks(); +} + #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_STANDARD_IO #define NK_INCLUDE_STANDARD_VARARGS @@ -79,13 +84,18 @@ int main(int argc, char *argv[]) { /* Platform */ + struct nk_buffer cmds; + struct nk_draw_null_texture tex_null; + GLuint font_tex; + struct nk_font_atlas atlas; + Uint64 time_of_last_frame; SDL_Window *win; SDL_GLContext glContext; int win_width, win_height; int running = 1; /* GUI */ - struct nk_context *ctx; + struct nk_context ctx; struct nk_colorf bg; #ifdef INCLUDE_CONFIGURATOR @@ -111,36 +121,35 @@ main(int argc, char *argv[]) SDL_GetWindowSize(win, &win_width, &win_height); /* GUI */ - ctx = nk_sdl_init(win); + nk_sdl_init(&ctx, &cmds, &time_of_last_frame); /* Load Fonts: if none of these are loaded a default font will be used */ /* Load Cursor: if you uncomment cursor loading please hide the cursor */ - {struct nk_font_atlas *atlas; - nk_sdl_font_stash_begin(&atlas); - /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ - /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ - /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ - /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ - /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ - /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ - nk_sdl_font_stash_end(); - /*nk_style_load_all_cursors(ctx, atlas->cursors);*/ - /*nk_style_set_font(ctx, &roboto->handle)*/;} + {nk_sdl_font_stash_begin(&atlas); + /*struct nk_font *droid = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ + /*struct nk_font *roboto = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ + /*struct nk_font *future = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ + /*struct nk_font *clean = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ + /*struct nk_font *tiny = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ + /*struct nk_font *cousine = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ + nk_sdl_font_stash_end(&ctx, &atlas, &tex_null, &font_tex); + /*nk_style_load_all_cursors(&ctx, atlas.cursors);*/ + /*nk_style_set_font(&ctx, &roboto->handle)*/;} bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f; while (running) { /* Input */ SDL_Event evt; - nk_input_begin(ctx); + nk_input_begin(&ctx); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) goto cleanup; - nk_sdl_handle_event(&evt); + nk_sdl_handle_event(&ctx, &evt); } - nk_sdl_handle_grab(); /* optional grabbing behavior */ - nk_input_end(ctx); + nk_sdl_handle_grab(&ctx, win); /* optional grabbing behavior */ + nk_input_end(&ctx); /* GUI */ - if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), + if (nk_begin(&ctx, "Demo", nk_rect(50, 50, 230, 250), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { @@ -148,46 +157,46 @@ main(int argc, char *argv[]) static int op = EASY; static int property = 20; - nk_layout_row_static(ctx, 30, 80, 1); - if (nk_button_label(ctx, "button")) + nk_layout_row_static(&ctx, 30, 80, 1); + if (nk_button_label(&ctx, "button")) fprintf(stdout, "button pressed\n"); - nk_layout_row_dynamic(ctx, 30, 2); - if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; - if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; - nk_layout_row_dynamic(ctx, 25, 1); - nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); - - nk_layout_row_dynamic(ctx, 20, 1); - nk_label(ctx, "background:", NK_TEXT_LEFT); - nk_layout_row_dynamic(ctx, 25, 1); - if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { - nk_layout_row_dynamic(ctx, 120, 1); - bg = nk_color_picker(ctx, bg, NK_RGBA); - nk_layout_row_dynamic(ctx, 25, 1); - bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); - bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); - bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); - bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); - nk_combo_end(ctx); + nk_layout_row_dynamic(&ctx, 30, 2); + if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY; + if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD; + nk_layout_row_dynamic(&ctx, 25, 1); + nk_property_int(&ctx, "Compression:", 0, &property, 100, 10, 1); + + nk_layout_row_dynamic(&ctx, 20, 1); + nk_label(&ctx, "background:", NK_TEXT_LEFT); + nk_layout_row_dynamic(&ctx, 25, 1); + if (nk_combo_begin_color(&ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(&ctx),400))) { + nk_layout_row_dynamic(&ctx, 120, 1); + bg = nk_color_picker(&ctx, bg, NK_RGBA); + nk_layout_row_dynamic(&ctx, 25, 1); + bg.r = nk_propertyf(&ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); + bg.g = nk_propertyf(&ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); + bg.b = nk_propertyf(&ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); + bg.a = nk_propertyf(&ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); + nk_combo_end(&ctx); } } - nk_end(ctx); + nk_end(&ctx); /* -------------- EXAMPLES ---------------- */ #ifdef INCLUDE_CALCULATOR - calculator(ctx); + calculator(&ctx); #endif #ifdef INCLUDE_CANVAS - canvas(ctx); + canvas(&ctx); #endif #ifdef INCLUDE_OVERVIEW - overview(ctx); + overview(&ctx); #endif #ifdef INCLUDE_CONFIGURATOR - style_configurator(ctx, color_table); + style_configurator(&ctx, color_table); #endif #ifdef INCLUDE_NODE_EDITOR - node_editor(ctx); + node_editor(&ctx); #endif /* ----------------------------------------- */ @@ -201,15 +210,14 @@ main(int argc, char *argv[]) * defaults everything back into a default state. * Make sure to either a.) save and restore or b.) reset your own state after * rendering the UI. */ - nk_sdl_render(NK_ANTI_ALIASING_ON); + nk_sdl_render(&ctx, &cmds, &tex_null, win, NK_ANTI_ALIASING_ON, &time_of_last_frame); SDL_GL_SwapWindow(win); } cleanup: - nk_sdl_shutdown(); + nk_sdl_shutdown(&ctx, &atlas, &cmds, font_tex); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; } - diff --git a/demo/sdl_opengl2/nuklear_sdl_gl2.h b/demo/sdl_opengl2/nuklear_sdl_gl2.h index be6b6db40..38da8bf64 100644 --- a/demo/sdl_opengl2/nuklear_sdl_gl2.h +++ b/demo/sdl_opengl2/nuklear_sdl_gl2.h @@ -14,13 +14,18 @@ #define NK_SDL_GL2_H_ #include -NK_API struct nk_context* nk_sdl_init(SDL_Window *win); -NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); -NK_API void nk_sdl_font_stash_end(void); -NK_API int nk_sdl_handle_event(SDL_Event *evt); -NK_API void nk_sdl_render(enum nk_anti_aliasing); -NK_API void nk_sdl_shutdown(void); -NK_API void nk_sdl_handle_grab(void); + +NK_API void nk_sdl_init(struct nk_context *ctx, struct nk_buffer *cmds, Uint64 *time_of_last_frame); +NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas *atlas); +NK_API void nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, + struct nk_draw_null_texture *tex_null, GLuint *font_tex); +NK_API int nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt); +NK_API void nk_sdl_render(struct nk_context *ctx, struct nk_buffer *cmds, + struct nk_draw_null_texture *tex_null, SDL_Window *win, + enum nk_anti_aliasing AA, Uint64 *time_of_last_frame); +NK_API void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, + struct nk_buffer *cmds, GLuint font_tex); +NK_API void nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win); #endif /* @@ -34,32 +39,17 @@ NK_API void nk_sdl_handle_grab(void); #include #include -struct nk_sdl_device { - struct nk_buffer cmds; - struct nk_draw_null_texture tex_null; - GLuint font_tex; -}; - struct nk_sdl_vertex { float position[2]; float uv[2]; nk_byte col[4]; }; -static struct nk_sdl { - SDL_Window *win; - struct nk_sdl_device ogl; - struct nk_context ctx; - struct nk_font_atlas atlas; - Uint64 time_of_last_frame; -} sdl; - NK_INTERN void -nk_sdl_device_upload_atlas(const void *image, int width, int height) +nk_sdl_device_upload_atlas(GLuint *font_tex, const void *image, int width, int height) { - struct nk_sdl_device *dev = &sdl.ogl; - glGenTextures(1, &dev->font_tex); - glBindTexture(GL_TEXTURE_2D, dev->font_tex); + glGenTextures(1, font_tex); + glBindTexture(GL_TEXTURE_2D, *font_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, @@ -67,20 +57,26 @@ nk_sdl_device_upload_atlas(const void *image, int width, int height) } NK_API void -nk_sdl_render(enum nk_anti_aliasing AA) +nk_sdl_render(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_draw_null_texture *tex_null, SDL_Window *win, enum nk_anti_aliasing AA, Uint64 *time_of_last_frame) { /* setup global state */ - struct nk_sdl_device *dev = &sdl.ogl; int width, height; int display_width, display_height; struct nk_vec2 scale; + Uint64 now; + + NK_ASSERT(ctx); + NK_ASSERT(cmds); + NK_ASSERT(tex_null); + NK_ASSERT(win); + NK_ASSERT(time_of_last_frame); - Uint64 now = SDL_GetTicks64(); - sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000; - sdl.time_of_last_frame = now; + now = SDL_GetTicks64(); + ctx->delta_time_seconds = (float)(now - *time_of_last_frame) / 1000; + *time_of_last_frame = now; - SDL_GetWindowSize(sdl.win, &width, &height); - SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); + SDL_GetWindowSize(win, &width, &height); + SDL_GL_GetDrawableSize(win, &display_width, &display_height); scale.x = (float)display_width/(float)width; scale.y = (float)display_height/(float)height; @@ -128,7 +124,7 @@ nk_sdl_render(enum nk_anti_aliasing AA) config.vertex_layout = vertex_layout; config.vertex_size = sizeof(struct nk_sdl_vertex); config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex); - config.tex_null = dev->tex_null; + config.tex_null = *tex_null; config.circle_segment_count = 22; config.curve_segment_count = 22; config.arc_segment_count = 22; @@ -139,7 +135,7 @@ nk_sdl_render(enum nk_anti_aliasing AA) /* convert shapes into vertexes */ nk_buffer_init_default(&vbuf); nk_buffer_init_default(&ebuf); - nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config); + nk_convert(ctx, cmds, &vbuf, &ebuf, &config); /* setup vertex buffer pointer */ {const void *vertices = nk_buffer_memory_const(&vbuf); @@ -149,7 +145,7 @@ nk_sdl_render(enum nk_anti_aliasing AA) /* iterate over and execute each draw command */ offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf); - nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) + nk_draw_foreach(cmd, ctx, cmds) { if (!cmd->elem_count) continue; glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); @@ -161,8 +157,8 @@ nk_sdl_render(enum nk_anti_aliasing AA) glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); offset += cmd->elem_count; } - nk_clear(&sdl.ctx); - nk_buffer_clear(&dev->cmds); + nk_clear(ctx); + nk_buffer_clear(cmds); nk_buffer_free(&vbuf); nk_buffer_free(&ebuf); } @@ -211,48 +207,59 @@ nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len) free(str); } -NK_API struct nk_context* -nk_sdl_init(SDL_Window *win) +NK_API void +nk_sdl_init(struct nk_context *ctx, struct nk_buffer *cmds, Uint64 *time_of_last_frame) { - sdl.win = win; - nk_init_default(&sdl.ctx, 0); - sdl.ctx.clip.copy = nk_sdl_clipboard_copy; - sdl.ctx.clip.paste = nk_sdl_clipboard_paste; - sdl.ctx.clip.userdata = nk_handle_ptr(0); - nk_buffer_init_default(&sdl.ogl.cmds); - sdl.time_of_last_frame = SDL_GetTicks64(); - return &sdl.ctx; + NK_ASSERT(ctx); + NK_ASSERT(cmds); + NK_ASSERT(time_of_last_frame); + + nk_init_default(ctx, 0); + ctx->clip.copy = nk_sdl_clipboard_copy; + ctx->clip.paste = nk_sdl_clipboard_paste; + ctx->clip.userdata = nk_handle_ptr(0); + nk_buffer_init_default(cmds); + *time_of_last_frame = SDL_GetTicks64(); } NK_API void -nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) +nk_sdl_font_stash_begin(struct nk_font_atlas *atlas) { - nk_font_atlas_init_default(&sdl.atlas); - nk_font_atlas_begin(&sdl.atlas); - *atlas = &sdl.atlas; + NK_ASSERT(atlas); + + nk_font_atlas_init_default(atlas); + nk_font_atlas_begin(atlas); } NK_API void -nk_sdl_font_stash_end(void) +nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_draw_null_texture *tex_null, GLuint *font_tex) { const void *image; int w, h; - image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); - nk_sdl_device_upload_atlas(image, w, h); - nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null); - if (sdl.atlas.default_font) - nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); + + NK_ASSERT(ctx); + NK_ASSERT(atlas); + NK_ASSERT(tex_null); + NK_ASSERT(font_tex); + + image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); + nk_sdl_device_upload_atlas(font_tex, image, w, h); + nk_font_atlas_end(atlas, nk_handle_id((int)*font_tex), tex_null); + if (atlas->default_font) + nk_style_set_font(ctx, &atlas->default_font->handle); } NK_API void -nk_sdl_handle_grab(void) +nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win) { - struct nk_context *ctx = &sdl.ctx; + NK_ASSERT(ctx); + NK_ASSERT(win); + if (ctx->input.mouse.grab) { SDL_SetRelativeMouseMode(SDL_TRUE); } else if (ctx->input.mouse.ungrab) { /* better support for older SDL by setting mode first; causes an extra mouse motion event */ SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + SDL_WarpMouseInWindow(win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); } else if (ctx->input.mouse.grabbed) { ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; @@ -260,10 +267,10 @@ nk_sdl_handle_grab(void) } NK_API int -nk_sdl_handle_event(SDL_Event *evt) +nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt) { - struct nk_context *ctx = &sdl.ctx; - int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); + NK_ASSERT(ctx); + NK_ASSERT(evt); switch(evt->type) { @@ -271,6 +278,7 @@ nk_sdl_handle_event(SDL_Event *evt) case SDL_KEYDOWN: { int down = evt->type == SDL_KEYDOWN; + int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); switch(evt->key.keysym.sym) { case SDLK_RSHIFT: /* RSHIFT & LSHIFT share same routine */ @@ -356,14 +364,18 @@ nk_sdl_handle_event(SDL_Event *evt) } NK_API -void nk_sdl_shutdown(void) +void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_buffer *cmds, GLuint font_tex) { - struct nk_sdl_device *dev = &sdl.ogl; - nk_font_atlas_clear(&sdl.atlas); - nk_free(&sdl.ctx); - glDeleteTextures(1, &dev->font_tex); - nk_buffer_free(&dev->cmds); - memset(&sdl, 0, sizeof(sdl)); + NK_ASSERT(ctx); + NK_ASSERT(atlas); + NK_ASSERT(cmds); + + nk_font_atlas_clear(atlas); + nk_free(ctx); + glDeleteTextures(1, &font_tex); + nk_buffer_free(cmds); + memset(atlas, 0, sizeof(*atlas)); + memset(cmds, 0, sizeof(*cmds)); } #endif diff --git a/demo/sdl_opengl3/main.c b/demo/sdl_opengl3/main.c index 499a07ec6..512df44e1 100644 --- a/demo/sdl_opengl3/main.c +++ b/demo/sdl_opengl3/main.c @@ -13,6 +13,11 @@ #include #include +long SDL_GetTicks64() +{ + return SDL_GetTicks(); +} + #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_STANDARD_IO #define NK_INCLUDE_STANDARD_VARARGS @@ -82,13 +87,16 @@ int main(int argc, char *argv[]) { /* Platform */ + struct nk_sdl_device dev; + struct nk_font_atlas atlas; + Uint64 time_of_last_frame; SDL_Window *win; SDL_GLContext glContext; int win_width, win_height; int running = 1; /* GUI */ - struct nk_context *ctx; + struct nk_context ctx; struct nk_colorf bg; #ifdef INCLUDE_CONFIGURATOR @@ -121,20 +129,19 @@ int main(int argc, char *argv[]) exit(1); } - ctx = nk_sdl_init(win); + nk_sdl_init(&ctx, &dev, &time_of_last_frame); /* Load Fonts: if none of these are loaded a default font will be used */ /* Load Cursor: if you uncomment cursor loading please hide the cursor */ - {struct nk_font_atlas *atlas; - nk_sdl_font_stash_begin(&atlas); - /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ - /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ - /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ - /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ - /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ - /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ - nk_sdl_font_stash_end(); - /*nk_style_load_all_cursors(ctx, atlas->cursors);*/ - /*nk_style_set_font(ctx, &roboto->handle);*/} + {nk_sdl_font_stash_begin(&atlas); + /*struct nk_font *droid = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ + /*struct nk_font *roboto = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ + /*struct nk_font *future = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ + /*struct nk_font *clean = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ + /*struct nk_font *tiny = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ + /*struct nk_font *cousine = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ + nk_sdl_font_stash_end(&ctx, &atlas, &dev.tex_null, &dev.font_tex); + /*nk_style_load_all_cursors(&ctx, atlas.cursors);*/ + /*nk_style_set_font(&ctx, &roboto->handle);*/} /* style.c */ #ifdef INCLUDE_STYLE @@ -155,16 +162,16 @@ int main(int argc, char *argv[]) { /* Input */ SDL_Event evt; - nk_input_begin(ctx); + nk_input_begin(&ctx); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) goto cleanup; - nk_sdl_handle_event(&evt); + nk_sdl_handle_event(&ctx, &evt); } - nk_sdl_handle_grab(); /* optional grabbing behavior */ - nk_input_end(ctx); + nk_sdl_handle_grab(&ctx, win); /* optional grabbing behavior */ + nk_input_end(&ctx); /* GUI */ - if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), + if (nk_begin(&ctx, "Demo", nk_rect(50, 50, 230, 250), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { @@ -172,46 +179,46 @@ int main(int argc, char *argv[]) static int op = EASY; static int property = 20; - nk_layout_row_static(ctx, 30, 80, 1); - if (nk_button_label(ctx, "button")) + nk_layout_row_static(&ctx, 30, 80, 1); + if (nk_button_label(&ctx, "button")) printf("button pressed!\n"); - nk_layout_row_dynamic(ctx, 30, 2); - if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; - if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; - nk_layout_row_dynamic(ctx, 22, 1); - nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); - - nk_layout_row_dynamic(ctx, 20, 1); - nk_label(ctx, "background:", NK_TEXT_LEFT); - nk_layout_row_dynamic(ctx, 25, 1); - if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { - nk_layout_row_dynamic(ctx, 120, 1); - bg = nk_color_picker(ctx, bg, NK_RGBA); - nk_layout_row_dynamic(ctx, 25, 1); - bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); - bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); - bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); - bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); - nk_combo_end(ctx); + nk_layout_row_dynamic(&ctx, 30, 2); + if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY; + if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD; + nk_layout_row_dynamic(&ctx, 22, 1); + nk_property_int(&ctx, "Compression:", 0, &property, 100, 10, 1); + + nk_layout_row_dynamic(&ctx, 20, 1); + nk_label(&ctx, "background:", NK_TEXT_LEFT); + nk_layout_row_dynamic(&ctx, 25, 1); + if (nk_combo_begin_color(&ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(&ctx),400))) { + nk_layout_row_dynamic(&ctx, 120, 1); + bg = nk_color_picker(&ctx, bg, NK_RGBA); + nk_layout_row_dynamic(&ctx, 25, 1); + bg.r = nk_propertyf(&ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); + bg.g = nk_propertyf(&ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); + bg.b = nk_propertyf(&ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); + bg.a = nk_propertyf(&ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); + nk_combo_end(&ctx); } } - nk_end(ctx); + nk_end(&ctx); /* -------------- EXAMPLES ---------------- */ #ifdef INCLUDE_CALCULATOR - calculator(ctx); + calculator(&ctx); #endif #ifdef INCLUDE_CANVAS - canvas(ctx); + canvas(&ctx); #endif #ifdef INCLUDE_OVERVIEW - overview(ctx); + overview(&ctx); #endif #ifdef INCLUDE_CONFIGURATOR - style_configurator(ctx, color_table); + style_configurator(&ctx, color_table); #endif #ifdef INCLUDE_NODE_EDITOR - node_editor(ctx); + node_editor(&ctx); #endif /* ----------------------------------------- */ @@ -225,15 +232,14 @@ int main(int argc, char *argv[]) * defaults everything back into a default state. * Make sure to either a.) save and restore or b.) reset your own state after * rendering the UI. */ - nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY); + nk_sdl_render(&ctx, &dev, win, NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY, &time_of_last_frame); SDL_GL_SwapWindow(win); } cleanup: - nk_sdl_shutdown(); + nk_sdl_shutdown(&ctx, &atlas, &dev); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; } - diff --git a/demo/sdl_opengl3/nuklear_sdl_gl3.h b/demo/sdl_opengl3/nuklear_sdl_gl3.h index 79a7752b3..2754cda69 100644 --- a/demo/sdl_opengl3/nuklear_sdl_gl3.h +++ b/demo/sdl_opengl3/nuklear_sdl_gl3.h @@ -16,15 +16,30 @@ #include #include -NK_API struct nk_context* nk_sdl_init(SDL_Window *win); -NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); -NK_API void nk_sdl_font_stash_end(void); -NK_API int nk_sdl_handle_event(SDL_Event *evt); -NK_API void nk_sdl_render(enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer); -NK_API void nk_sdl_shutdown(void); -NK_API void nk_sdl_device_destroy(void); -NK_API void nk_sdl_device_create(void); -NK_API void nk_sdl_handle_grab(void); +struct nk_sdl_device { + struct nk_buffer cmds; + struct nk_draw_null_texture tex_null; + GLuint vbo, vao, ebo; + GLuint prog; + GLuint vert_shdr; + GLuint frag_shdr; + GLint attrib_pos; + GLint attrib_uv; + GLint attrib_col; + GLint uniform_tex; + GLint uniform_proj; + GLuint font_tex; +}; + +NK_API void nk_sdl_init(struct nk_context *ctx, struct nk_sdl_device *dev, Uint64 *time_of_last_frame); +NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas *atlas); +NK_API void nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_draw_null_texture *tex_null, GLuint *font_tex); +NK_API int nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt); +NK_API void nk_sdl_render(struct nk_context *ctx, struct nk_sdl_device *dev, SDL_Window *win, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer, Uint64 *time_of_last_frame); +NK_API void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_sdl_device *dev); +NK_API void nk_sdl_device_destroy(struct nk_sdl_device *dev); +NK_API void nk_sdl_device_create(struct nk_sdl_device *dev); +NK_API void nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win); #endif @@ -41,34 +56,19 @@ NK_API void nk_sdl_handle_grab(void); #include #include -struct nk_sdl_device { - struct nk_buffer cmds; - struct nk_draw_null_texture tex_null; - GLuint vbo, vao, ebo; - GLuint prog; - GLuint vert_shdr; - GLuint frag_shdr; - GLint attrib_pos; - GLint attrib_uv; - GLint attrib_col; - GLint uniform_tex; - GLint uniform_proj; - GLuint font_tex; -}; - struct nk_sdl_vertex { float position[2]; float uv[2]; nk_byte col[4]; }; -static struct nk_sdl { +struct nk_sdl { SDL_Window *win; struct nk_sdl_device ogl; struct nk_context ctx; struct nk_font_atlas atlas; Uint64 time_of_last_frame; -} sdl; +}; #ifdef __APPLE__ #define NK_SHADER_VERSION "#version 150\n" @@ -76,7 +76,7 @@ static struct nk_sdl { #define NK_SHADER_VERSION "#version 300 es\n" #endif NK_API void -nk_sdl_device_create(void) +nk_sdl_device_create(struct nk_sdl_device *dev) { GLint status; static const GLchar *vertex_shader = @@ -103,7 +103,6 @@ nk_sdl_device_create(void) " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" "}\n"; - struct nk_sdl_device *dev = &sdl.ogl; nk_buffer_init_default(&dev->cmds); dev->prog = glCreateProgram(); dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); @@ -159,11 +158,10 @@ nk_sdl_device_create(void) } NK_INTERN void -nk_sdl_device_upload_atlas(const void *image, int width, int height) +nk_sdl_device_upload_atlas(GLuint *font_tex, const void *image, int width, int height) { - struct nk_sdl_device *dev = &sdl.ogl; - glGenTextures(1, &dev->font_tex); - glBindTexture(GL_TEXTURE_2D, dev->font_tex); + glGenTextures(1, font_tex); + glBindTexture(GL_TEXTURE_2D, *font_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, @@ -171,9 +169,8 @@ nk_sdl_device_upload_atlas(const void *image, int width, int height) } NK_API void -nk_sdl_device_destroy(void) +nk_sdl_device_destroy(struct nk_sdl_device *dev) { - struct nk_sdl_device *dev = &sdl.ogl; glDetachShader(dev->prog, dev->vert_shdr); glDetachShader(dev->prog, dev->frag_shdr); glDeleteShader(dev->vert_shdr); @@ -186,9 +183,8 @@ nk_sdl_device_destroy(void) } NK_API void -nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer) +nk_sdl_render(struct nk_context *ctx, struct nk_sdl_device *dev, SDL_Window *win, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer, Uint64 *time_of_last_frame) { - struct nk_sdl_device *dev = &sdl.ogl; int width, height; int display_width, display_height; struct nk_vec2 scale; @@ -200,11 +196,11 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b }; Uint64 now = SDL_GetTicks64(); - sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000; - sdl.time_of_last_frame = now; + ctx->delta_time_seconds = (float)(now - *time_of_last_frame) / 1000; + *time_of_last_frame = now; - SDL_GetWindowSize(sdl.win, &width, &height); - SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); + SDL_GetWindowSize(win, &width, &height); + SDL_GL_GetDrawableSize(win, &display_width, &display_height); ortho[0][0] /= (GLfloat)width; ortho[1][1] /= (GLfloat)height; @@ -267,13 +263,13 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b /* setup buffers to load vertices and elements */ nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer); nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer); - nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config); + nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config); } glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); /* iterate over and execute each draw command */ - nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) { + nk_draw_foreach(cmd, ctx, &dev->cmds) { if (!cmd->elem_count) continue; glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); glScissor((GLint)(cmd->clip_rect.x * scale.x), @@ -283,7 +279,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); offset += cmd->elem_count; } - nk_clear(&sdl.ctx); + nk_clear(ctx); nk_buffer_clear(&dev->cmds); } @@ -320,49 +316,58 @@ nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len) free(str); } -NK_API struct nk_context* -nk_sdl_init(SDL_Window *win) +NK_API void +nk_sdl_init(struct nk_context *ctx, struct nk_sdl_device *dev, Uint64 *time_of_last_frame) { - sdl.win = win; - nk_init_default(&sdl.ctx, 0); - sdl.ctx.clip.copy = nk_sdl_clipboard_copy; - sdl.ctx.clip.paste = nk_sdl_clipboard_paste; - sdl.ctx.clip.userdata = nk_handle_ptr(0); - nk_sdl_device_create(); - sdl.time_of_last_frame = SDL_GetTicks64(); - return &sdl.ctx; + NK_ASSERT(ctx); + NK_ASSERT(time_of_last_frame); + + nk_init_default(ctx, 0); + ctx->clip.copy = nk_sdl_clipboard_copy; + ctx->clip.paste = nk_sdl_clipboard_paste; + ctx->clip.userdata = nk_handle_ptr(0); + nk_sdl_device_create(dev); + *time_of_last_frame = SDL_GetTicks64(); } NK_API void -nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) +nk_sdl_font_stash_begin(struct nk_font_atlas *atlas) { - nk_font_atlas_init_default(&sdl.atlas); - nk_font_atlas_begin(&sdl.atlas); - *atlas = &sdl.atlas; + NK_ASSERT(atlas); + + nk_font_atlas_init_default(atlas); + nk_font_atlas_begin(atlas); } NK_API void -nk_sdl_font_stash_end(void) +nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_draw_null_texture *tex_null, GLuint *font_tex) { const void *image; int w, h; - image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); - nk_sdl_device_upload_atlas(image, w, h); - nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null); - if (sdl.atlas.default_font) - nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); + NK_ASSERT(ctx); + NK_ASSERT(atlas); + NK_ASSERT(tex_null); + NK_ASSERT(font_tex); + + image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); + nk_sdl_device_upload_atlas(font_tex, image, w, h); + nk_font_atlas_end(atlas, nk_handle_id((int)*font_tex), tex_null); + if (atlas->default_font) + nk_style_set_font(ctx, &atlas->default_font->handle); } NK_API void -nk_sdl_handle_grab(void) +nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win) { - struct nk_context *ctx = &sdl.ctx; + NK_ASSERT(ctx); + NK_ASSERT(win); + if (ctx->input.mouse.grab) { SDL_SetRelativeMouseMode(SDL_TRUE); } else if (ctx->input.mouse.ungrab) { /* better support for older SDL by setting mode first; causes an extra mouse motion event */ SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + SDL_WarpMouseInWindow(win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); } else if (ctx->input.mouse.grabbed) { ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; @@ -370,10 +375,10 @@ nk_sdl_handle_grab(void) } NK_API int -nk_sdl_handle_event(SDL_Event *evt) +nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt) { - struct nk_context *ctx = &sdl.ctx; - int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); + NK_ASSERT(ctx); + NK_ASSERT(evt); switch(evt->type) { @@ -381,6 +386,7 @@ nk_sdl_handle_event(SDL_Event *evt) case SDL_KEYDOWN: { int down = evt->type == SDL_KEYDOWN; + int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); switch(evt->key.keysym.sym) { case SDLK_RSHIFT: /* RSHIFT & LSHIFT share same routine */ @@ -466,12 +472,17 @@ nk_sdl_handle_event(SDL_Event *evt) } NK_API -void nk_sdl_shutdown(void) +void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_sdl_device *dev) { - nk_font_atlas_clear(&sdl.atlas); - nk_free(&sdl.ctx); - nk_sdl_device_destroy(); - memset(&sdl, 0, sizeof(sdl)); + NK_ASSERT(ctx); + NK_ASSERT(atlas); + NK_ASSERT(dev); + + nk_font_atlas_clear(atlas); + nk_free(ctx); + nk_sdl_device_destroy(dev); + memset(atlas, 0, sizeof(*atlas)); + memset(dev, 0, sizeof(*dev)); } #endif diff --git a/demo/sdl_opengles2/main.c b/demo/sdl_opengles2/main.c index b9517481f..9b29a81fe 100644 --- a/demo/sdl_opengles2/main.c +++ b/demo/sdl_opengles2/main.c @@ -83,6 +83,9 @@ /* Platform */ +struct nk_sdl_device dev; +struct nk_font_atlas atlas; +Uint64 time_of_last_frame; SDL_Window *win; int running = nk_true; @@ -99,9 +102,9 @@ MainLoop(void* loopArg){ nk_input_begin(ctx); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) running = nk_false; - nk_sdl_handle_event(&evt); + nk_sdl_handle_event(ctx, &evt); } - nk_sdl_handle_grab(); /* optional grabbing behavior */ + nk_sdl_handle_grab(ctx, win); /* optional grabbing behavior */ nk_input_end(ctx); @@ -177,14 +180,14 @@ MainLoop(void* loopArg){ * defaults everything back into a default state. * Make sure to either a.) save and restore or b.) reset your own state after * rendering the UI. */ - nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY); + nk_sdl_render(ctx, &dev, win, NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY, &time_of_last_frame); SDL_GL_SwapWindow(win);} } int main(int argc, char* argv[]) { /* GUI */ - struct nk_context *ctx; + struct nk_context ctx; SDL_GLContext glContext; NK_UNUSED(argc); @@ -206,32 +209,30 @@ int main(int argc, char* argv[]) /* OpenGL setup */ glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); - ctx = nk_sdl_init(win); + nk_sdl_init(&ctx, &dev, &time_of_last_frame); /* Load Fonts: if none of these are loaded a default font will be used */ /* Load Cursor: if you uncomment cursor loading please hide the cursor */ - {struct nk_font_atlas *atlas; - nk_sdl_font_stash_begin(&atlas); - /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ - /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ - /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ - /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ - /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ - /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ - nk_sdl_font_stash_end(); - /*nk_style_load_all_cursors(ctx, atlas->cursors);*/ - /*nk_style_set_font(ctx, &roboto->handle)*/;} + {nk_sdl_font_stash_begin(&atlas); + /*struct nk_font *droid = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ + /*struct nk_font *roboto = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ + /*struct nk_font *future = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ + /*struct nk_font *clean = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ + /*struct nk_font *tiny = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ + /*struct nk_font *cousine = nk_font_atlas_add_from_file(&atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ + nk_sdl_font_stash_end(&ctx, &atlas, &dev.tex_null, &dev.font_tex); + /*nk_style_load_all_cursors(&ctx, atlas.cursors);*/ + /*nk_style_set_font(&ctx, &roboto->handle);*/} #if defined(__EMSCRIPTEN__) #include - emscripten_set_main_loop_arg(MainLoop, (void*)ctx, 0, nk_true); + emscripten_set_main_loop_arg(MainLoop, (void*)&ctx, 0, nk_true); #else - while (running) MainLoop((void*)ctx); + while (running) MainLoop((void*)&ctx); #endif - nk_sdl_shutdown(); + nk_sdl_shutdown(&ctx, &atlas, &dev); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; } - diff --git a/demo/sdl_opengles2/nuklear_sdl_gles2.h b/demo/sdl_opengles2/nuklear_sdl_gles2.h index 029800390..bddf1e467 100644 --- a/demo/sdl_opengles2/nuklear_sdl_gles2.h +++ b/demo/sdl_opengles2/nuklear_sdl_gles2.h @@ -18,30 +18,10 @@ #include #include - -NK_API struct nk_context* nk_sdl_init(SDL_Window *win); -NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); -NK_API void nk_sdl_font_stash_end(void); -NK_API int nk_sdl_handle_event(SDL_Event *evt); -NK_API void nk_sdl_render(enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer); -NK_API void nk_sdl_shutdown(void); -NK_API void nk_sdl_device_destroy(void); -NK_API void nk_sdl_device_create(void); -NK_API void nk_sdl_handle_grab(void); - -#endif - -/* - * ============================================================== - * - * IMPLEMENTATION - * - * =============================================================== - */ -#ifdef NK_SDL_GLES2_IMPLEMENTATION -#include -#include -#include +long SDL_GetTicks64() +{ + return SDL_GetTicks(); +} struct nk_sdl_device { struct nk_buffer cmds; @@ -60,26 +40,50 @@ struct nk_sdl_device { size_t vp, vt, vc; }; +NK_API void nk_sdl_init(struct nk_context *ctx, struct nk_sdl_device *dev, Uint64 *time_of_last_frame); +NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas *atlas); +NK_API void nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_draw_null_texture *tex_null, GLuint *font_tex); +NK_API int nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt); +NK_API void nk_sdl_render(struct nk_context *ctx, struct nk_sdl_device *dev, SDL_Window *win, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer, Uint64 *time_of_last_frame); +NK_API void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_sdl_device *dev); +NK_API void nk_sdl_device_destroy(struct nk_sdl_device *dev); +NK_API void nk_sdl_device_create(struct nk_sdl_device *dev); +NK_API void nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win); + +#endif + +/* + * ============================================================== + * + * IMPLEMENTATION + * + * =============================================================== + */ +#ifdef NK_SDL_GLES2_IMPLEMENTATION +#include +#include +#include + struct nk_sdl_vertex { GLfloat position[2]; GLfloat uv[2]; nk_byte col[4]; }; -static struct nk_sdl { +struct nk_sdl { SDL_Window *win; struct nk_sdl_device ogl; struct nk_context ctx; struct nk_font_atlas atlas; Uint64 time_of_last_frame; -} sdl; +}; #define NK_SHADER_VERSION "#version 100\n" NK_API void -nk_sdl_device_create(void) +nk_sdl_device_create(struct nk_sdl_device *dev) { GLint status; static const GLchar *vertex_shader = @@ -105,8 +109,6 @@ nk_sdl_device_create(void) " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" "}\n"; - struct nk_sdl_device *dev = &sdl.ogl; - nk_buffer_init_default(&dev->cmds); dev->prog = glCreateProgram(); dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); @@ -147,11 +149,10 @@ nk_sdl_device_create(void) } NK_INTERN void -nk_sdl_device_upload_atlas(const void *image, int width, int height) +nk_sdl_device_upload_atlas(GLuint *font_tex, const void *image, int width, int height) { - struct nk_sdl_device *dev = &sdl.ogl; - glGenTextures(1, &dev->font_tex); - glBindTexture(GL_TEXTURE_2D, dev->font_tex); + glGenTextures(1, font_tex); + glBindTexture(GL_TEXTURE_2D, *font_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, @@ -159,9 +160,8 @@ nk_sdl_device_upload_atlas(const void *image, int width, int height) } NK_API void -nk_sdl_device_destroy(void) +nk_sdl_device_destroy(struct nk_sdl_device *dev) { - struct nk_sdl_device *dev = &sdl.ogl; glDetachShader(dev->prog, dev->vert_shdr); glDetachShader(dev->prog, dev->frag_shdr); glDeleteShader(dev->vert_shdr); @@ -174,9 +174,8 @@ nk_sdl_device_destroy(void) } NK_API void -nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer) +nk_sdl_render(struct nk_context *ctx, struct nk_sdl_device *dev, SDL_Window *win, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer, Uint64 *time_of_last_frame) { - struct nk_sdl_device *dev = &sdl.ogl; int width, height; int display_width, display_height; struct nk_vec2 scale; @@ -188,11 +187,11 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b }; Uint64 now = SDL_GetTicks64(); - sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000; - sdl.time_of_last_frame = now; + ctx->delta_time_seconds = (float)(now - *time_of_last_frame) / 1000; + *time_of_last_frame = now; - SDL_GetWindowSize(sdl.win, &width, &height); - SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); + SDL_GetWindowSize(win, &width, &height); + SDL_GL_GetDrawableSize(win, &display_width, &display_height); ortho[0][0] /= (GLfloat)width; ortho[1][1] /= (GLfloat)height; @@ -265,7 +264,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b {struct nk_buffer vbuf, ebuf; nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer); nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer); - nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config);} + nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config);} } glBufferSubData(GL_ARRAY_BUFFER, 0, (size_t)max_vertex_buffer, vertices); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, (size_t)max_element_buffer, elements); @@ -273,7 +272,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b free(elements); /* iterate over and execute each draw command */ - nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) { + nk_draw_foreach(cmd, ctx, &dev->cmds) { if (!cmd->elem_count) continue; glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); glScissor((GLint)(cmd->clip_rect.x * scale.x), @@ -283,7 +282,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); offset += cmd->elem_count; } - nk_clear(&sdl.ctx); + nk_clear(ctx); nk_buffer_clear(&dev->cmds); } @@ -320,49 +319,47 @@ nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len) free(str); } -NK_API struct nk_context* -nk_sdl_init(SDL_Window *win) +NK_API void +nk_sdl_init(struct nk_context *ctx, struct nk_sdl_device *dev, Uint64 *time_of_last_frame) { - sdl.win = win; - nk_init_default(&sdl.ctx, 0); - sdl.ctx.clip.copy = nk_sdl_clipboard_copy; - sdl.ctx.clip.paste = nk_sdl_clipboard_paste; - sdl.ctx.clip.userdata = nk_handle_ptr(0); - nk_sdl_device_create(); - sdl.time_of_last_frame = SDL_GetTicks64(); - return &sdl.ctx; + nk_init_default(ctx, 0); + ctx->clip.copy = nk_sdl_clipboard_copy; + ctx->clip.paste = nk_sdl_clipboard_paste; + ctx->clip.userdata = nk_handle_ptr(0); + nk_sdl_device_create(dev); + *time_of_last_frame = SDL_GetTicks(); } NK_API void -nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) +nk_sdl_font_stash_begin(struct nk_font_atlas *atlas) { - nk_font_atlas_init_default(&sdl.atlas); - nk_font_atlas_begin(&sdl.atlas); - *atlas = &sdl.atlas; + NK_ASSERT(atlas); + + nk_font_atlas_init_default(atlas); + nk_font_atlas_begin(atlas); } NK_API void -nk_sdl_font_stash_end(void) +nk_sdl_font_stash_end(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_draw_null_texture *tex_null, GLuint *font_tex) { const void *image; int w, h; - image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); - nk_sdl_device_upload_atlas(image, w, h); - nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null); - if (sdl.atlas.default_font) - nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); + image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); + nk_sdl_device_upload_atlas(font_tex, image, w, h); + nk_font_atlas_end(atlas, nk_handle_id((int)*font_tex), tex_null); + if (atlas->default_font) + nk_style_set_font(ctx, &atlas->default_font->handle); } NK_API void -nk_sdl_handle_grab(void) +nk_sdl_handle_grab(struct nk_context *ctx, SDL_Window *win) { - struct nk_context *ctx = &sdl.ctx; if (ctx->input.mouse.grab) { SDL_SetRelativeMouseMode(SDL_TRUE); } else if (ctx->input.mouse.ungrab) { /* better support for older SDL by setting mode first; causes an extra mouse motion event */ SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + SDL_WarpMouseInWindow(win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); } else if (ctx->input.mouse.grabbed) { ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; @@ -370,17 +367,15 @@ nk_sdl_handle_grab(void) } NK_API int -nk_sdl_handle_event(SDL_Event *evt) +nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt) { - struct nk_context *ctx = &sdl.ctx; - int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); - switch(evt->type) { case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ case SDL_KEYDOWN: { int down = evt->type == SDL_KEYDOWN; + int ctrl_down = SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL); switch(evt->key.keysym.sym) { case SDLK_RSHIFT: /* RSHIFT & LSHIFT share same routine */ @@ -466,12 +461,17 @@ nk_sdl_handle_event(SDL_Event *evt) } NK_API -void nk_sdl_shutdown(void) +void nk_sdl_shutdown(struct nk_context *ctx, struct nk_font_atlas *atlas, struct nk_sdl_device *dev) { - nk_font_atlas_clear(&sdl.atlas); - nk_free(&sdl.ctx); - nk_sdl_device_destroy(); - memset(&sdl, 0, sizeof(sdl)); + NK_ASSERT(ctx); + NK_ASSERT(atlas); + NK_ASSERT(dev); + + nk_font_atlas_clear(atlas); + nk_free(ctx); + nk_sdl_device_destroy(dev); + memset(atlas, 0, sizeof(*atlas)); + memset(dev, 0, sizeof(*dev)); } #endif