diff options
| author | David Caruso <dcaruso@meta.com> | 2024-02-21 23:22:42 -0800 |
|---|---|---|
| committer | David Caruso <dcaruso@meta.com> | 2024-02-21 23:22:42 -0800 |
| commit | 6d2eb39e0b648d09c268dd2a34a72ef3ca1acbab (patch) | |
| tree | 67cfeec298e4ba310019de0ada99663375f7301b /util | |
| parent | 6a13a96d58a1d2ad88161d9dddb7843e541803a9 (diff) | |
[sokol_imgui] add optional backend interface for font creation/destruction
This split the font texture creation in a API function and add a function for the font texture destruction. Those functions are optional function of the backends.
For instance implemented for opengl3 on the main imgui repo:
https://github.com/ocornut/imgui/blob/659fb41d0a23efbb9ea6cf74f51ecae0a51575b5/backends/imgui_impl_opengl3.h#L39C1-L42C3
Those functions are useful in two cases:
- when testing different fonts raterization config, it is convenient to recreate the texture dynamically. The imgui's author provides an example of such application: https://gist.github.com/ocornut/b3a9ecf13502fd818799a452969649ad
- when creating non default font, this allows to remove the burdern of the boilerplate of uploading the font on GPU to the user of sokol_imgui.
Diffstat (limited to 'util')
| -rw-r--r-- | util/sokol_imgui.h | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/util/sokol_imgui.h b/util/sokol_imgui.h index 93b697fa..3c7cb99c 100644 --- a/util/sokol_imgui.h +++ b/util/sokol_imgui.h @@ -543,6 +543,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(void); +SOKOL_IMGUI_API_DECL void simgui_destroy_fonts_texture(void); #ifdef __cplusplus } // extern "C" @@ -2368,35 +2370,51 @@ 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_create_fonts_texture(); } sg_pop_debug_group(); } +SOKOL_API_IMPL void simgui_create_fonts_texture(void) { + #if defined(__cplusplus) + ImGuiIO* io = &ImGui::GetIO(); + #else + ImGuiIO* io = igGetIO(); + #endif + + 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_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) |