diff options
| author | Andre Weissflog <floooh@gmail.com> | 2024-03-02 13:07:18 +0100 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2024-03-02 13:07:18 +0100 |
| commit | 9dddcebe1435330f463ab1d038dc4057bf8dbfa6 (patch) | |
| tree | d6b7106d07113ca35f5b8c02ee48984dd515a131 | |
| parent | 5b0a5fdefaa622dfd67060f419d3986b26adbf7c (diff) | |
| parent | b5ef2c7d98bdf5aeb6f7e5a484c8a709085aa859 (diff) | |
Merge branch 'Dvad-font_texture'
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | util/sokol_imgui.h | 99 |
2 files changed, 71 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ea137776..81751e26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,13 @@ https://floooh.github.io/sokol-html5/texcube-sapp.html +- sokol_imgui.h: PR https://github.com/floooh/sokol/pull/994 has been merged, this adds two + font management helper functions which drastically reduce boilerplate code + when injecting a custom font into Dear ImGui via sokol_imgui.h. + + See the PR for details, and the updated sample https://floooh.github.io/sokol-html5/imgui-highdpi-sapp.html + via this PR: https://github.com/floooh/sokol-samples/pull/135 + ### 01-Mar-2024: Minor regression fix for yesterdays merge in the sokol_gfx.h Metal backend: diff --git a/util/sokol_imgui.h b/util/sokol_imgui.h index 190ec675..9c041eaf 100644 --- a/util/sokol_imgui.h +++ b/util/sokol_imgui.h @@ -521,6 +521,11 @@ typedef struct simgui_frame_desc_t { float dpi_scale; } simgui_frame_desc_t; +typedef struct simgui_font_tex_desc_t { + sg_filter min_filter; + sg_filter mag_filter; +} simgui_font_tex_desc_t; + SOKOL_IMGUI_API_DECL void simgui_setup(const simgui_desc_t* desc); SOKOL_IMGUI_API_DECL void simgui_new_frame(const simgui_frame_desc_t* desc); SOKOL_IMGUI_API_DECL void simgui_render(void); @@ -543,6 +548,8 @@ SOKOL_IMGUI_API_DECL bool simgui_handle_event(const sapp_event* ev); SOKOL_IMGUI_API_DECL int simgui_map_keycode(sapp_keycode keycode); // returns ImGuiKey_* #endif SOKOL_IMGUI_API_DECL void simgui_shutdown(void); +SOKOL_IMGUI_API_DECL void simgui_create_fonts_texture(const simgui_font_tex_desc_t* desc); +SOKOL_IMGUI_API_DECL void simgui_destroy_fonts_texture(void); #ifdef __cplusplus } // extern "C" @@ -551,6 +558,7 @@ SOKOL_IMGUI_API_DECL void simgui_shutdown(void); inline void simgui_setup(const simgui_desc_t& desc) { return simgui_setup(&desc); } inline simgui_image_t simgui_make_image(const simgui_image_desc_t& desc) { return simgui_make_image(&desc); } inline void simgui_new_frame(const simgui_frame_desc_t& desc) { return simgui_new_frame(&desc); } +inline void simgui_create_fonts_texture(const simgui_font_tex_desc_t& desc) { return simgui_create_fonts_texture(&desc); } #endif #endif /* SOKOL_IMGUI_INCLUDED */ @@ -2338,17 +2346,6 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) { ib_desc.label = "sokol-imgui-indices"; _simgui.ibuf = sg_make_buffer(&ib_desc); - // a default font sampler - sg_sampler_desc font_smp_desc; - _simgui_clear(&font_smp_desc, sizeof(font_smp_desc)); - font_smp_desc.wrap_u = SG_WRAP_CLAMP_TO_EDGE; - font_smp_desc.wrap_v = SG_WRAP_CLAMP_TO_EDGE; - font_smp_desc.min_filter = SG_FILTER_LINEAR; - font_smp_desc.mag_filter = SG_FILTER_LINEAR; - font_smp_desc.mipmap_filter = SG_FILTER_NONE; - font_smp_desc.label = "sokol-imgui-font-sampler"; - _simgui.font_smp = sg_make_sampler(&font_smp_desc); - // a default user-image sampler sg_sampler_desc def_sampler_desc; _simgui_clear(&def_sampler_desc, sizeof(def_sampler_desc)); @@ -2374,35 +2371,67 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) { // default font texture if (!_simgui.desc.no_default_font) { - unsigned char* font_pixels; - int font_width, font_height; - #if defined(__cplusplus) - io->Fonts->GetTexDataAsRGBA32(&font_pixels, &font_width, &font_height); - #else - int bytes_per_pixel; - ImFontAtlas_GetTexDataAsRGBA32(io->Fonts, &font_pixels, &font_width, &font_height, &bytes_per_pixel); - #endif - sg_image_desc font_img_desc; - _simgui_clear(&font_img_desc, sizeof(font_img_desc)); - font_img_desc.width = font_width; - font_img_desc.height = font_height; - font_img_desc.pixel_format = SG_PIXELFORMAT_RGBA8; - font_img_desc.data.subimage[0][0].ptr = font_pixels; - font_img_desc.data.subimage[0][0].size = (size_t)(font_width * font_height) * sizeof(uint32_t); - font_img_desc.label = "sokol-imgui-font-image"; - _simgui.font_img = sg_make_image(&font_img_desc); - - simgui_image_desc_t img_desc; - _simgui_clear(&img_desc, sizeof(img_desc)); - img_desc.image = _simgui.font_img; - img_desc.sampler = _simgui.font_smp; - _simgui.default_font = simgui_make_image(&img_desc); - io->Fonts->TexID = simgui_imtextureid(_simgui.default_font); + simgui_font_tex_desc_t simgui_font_smp_desc; + _simgui_clear(&simgui_font_smp_desc, sizeof(simgui_font_smp_desc)); + simgui_create_fonts_texture(&simgui_font_smp_desc); } sg_pop_debug_group(); } +SOKOL_API_IMPL void simgui_create_fonts_texture(const simgui_font_tex_desc_t* desc) { + SOKOL_ASSERT(desc); + + #if defined(__cplusplus) + ImGuiIO* io = &ImGui::GetIO(); + #else + ImGuiIO* io = igGetIO(); + #endif + + // a default font sampler + sg_sampler_desc font_smp_desc; + _simgui_clear(&font_smp_desc, sizeof(font_smp_desc)); + font_smp_desc.wrap_u = SG_WRAP_CLAMP_TO_EDGE; + font_smp_desc.wrap_v = SG_WRAP_CLAMP_TO_EDGE; + font_smp_desc.min_filter = desc->min_filter; + font_smp_desc.mag_filter = desc->mag_filter; + font_smp_desc.mipmap_filter = SG_FILTER_NONE; + font_smp_desc.label = "sokol-imgui-font-sampler"; + _simgui.font_smp = sg_make_sampler(&font_smp_desc); + + unsigned char* font_pixels; + int font_width, font_height; + #if defined(__cplusplus) + io->Fonts->GetTexDataAsRGBA32(&font_pixels, &font_width, &font_height); + #else + int bytes_per_pixel; + ImFontAtlas_GetTexDataAsRGBA32(io->Fonts, &font_pixels, &font_width, &font_height, &bytes_per_pixel); + #endif + sg_image_desc font_img_desc; + _simgui_clear(&font_img_desc, sizeof(font_img_desc)); + font_img_desc.width = font_width; + font_img_desc.height = font_height; + font_img_desc.pixel_format = SG_PIXELFORMAT_RGBA8; + font_img_desc.data.subimage[0][0].ptr = font_pixels; + font_img_desc.data.subimage[0][0].size = (size_t)(font_width * font_height) * sizeof(uint32_t); + font_img_desc.label = "sokol-imgui-font-image"; + _simgui.font_img = sg_make_image(&font_img_desc); + + simgui_image_desc_t img_desc; + _simgui_clear(&img_desc, sizeof(img_desc)); + img_desc.image = _simgui.font_img; + img_desc.sampler = _simgui.font_smp; + _simgui.default_font = simgui_make_image(&img_desc); + io->Fonts->TexID = simgui_imtextureid(_simgui.default_font); +} + +SOKOL_API_IMPL void simgui_destroy_fonts_texture(void) { + // NOTE: it's valid to call the destroy funcs with SG_INVALID_ID + sg_destroy_sampler(_simgui.font_smp); + sg_destroy_image(_simgui.font_img); + simgui_destroy_image(_simgui.default_font); +} + SOKOL_API_IMPL void simgui_shutdown(void) { SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie); #if defined(__cplusplus) |