Browse Source

Fonts: marked ImFontAtlas::Build() as obsolete

ocornut 8 months ago
parent
commit
df8450d928
4 changed files with 29 additions and 25 deletions
  1. 2 2
      imgui.cpp
  2. 3 3
      imgui.h
  3. 23 20
      imgui_draw.cpp
  4. 1 0
      imgui_internal.h

+ 2 - 2
imgui.cpp

@@ -5243,9 +5243,9 @@ void ImGui::NewFrame()
     // Check that font atlas was built or backend support texture reload in which case we can build now
     ImFontAtlas* atlas = g.IO.Fonts;
     if (!atlas->TexIsBuilt && (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures))
-        atlas->Build();
+        ImFontAtlasBuildMain(atlas);
     else // Legacy backend
-        IM_ASSERT(atlas->TexIsBuilt && "Font Atlas not built! Make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()");
+        IM_ASSERT(atlas->TexIsBuilt && "Backend does not support ImGuiBackendFlags_RendererHasTexUpdates, and font atlas is not built! Update backend OR make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8().");
 
     // Check and assert for various common IO and Configuration mistakes
     ErrorCheckNewFrameSanityChecks();

+ 3 - 3
imgui.h

@@ -3530,15 +3530,15 @@ struct ImFontAtlas
     IMGUI_API void              ClearFonts();               // [OBSOLETE] Clear input+output font data (same as ClearInputData() + glyphs storage, UV coordinates).
     IMGUI_API void              ClearTexData();             // [OBSOLETE] Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory.
 
+    IMGUI_API void              BuildGrowTexture();
+    IMGUI_API void              BuildCompactTexture();
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
     // Build atlas, retrieve pixel data.
     // User is in charge of copying the pixels into graphics memory (e.g. create a texture with your engine). Then store your texture handle with SetTexID().
     // The pitch is always = Width * BytesPerPixels (1 or 4)
     // Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into
     // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste.
     IMGUI_API bool              Build();                    // Build pixels data. This is called automatically for you by the GetTexData*** functions.
-    IMGUI_API void              BuildGrowTexture();
-    IMGUI_API void              BuildCompactTexture();
-#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
     IMGUI_API void              GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel
     IMGUI_API void              GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel
     void                        SetTexID(ImTextureID id)    { TexRef._TexData = NULL; TexRef._TexID = id; } // Called by legacy backends.

+ 23 - 20
imgui_draw.cpp

@@ -2466,9 +2466,9 @@ void ImTextureData::DestroyPixels()
 // - ImFontAtlasTextureBlockCopy()
 // - ImFontAtlasTextureBlockQueueUpload()
 //-----------------------------------------------------------------------------
+// - ImFontAtlas::Build() [legacy]
 // - ImFontAtlas::GetTexDataAsAlpha8() [legacy]
 // - ImFontAtlas::GetTexDataAsRGBA32() [legacy]
-// - ImFontAtlas::Build()
 //-----------------------------------------------------------------------------
 // - ImFontAtlas::AddFont()
 // - ImFontAtlas::AddFontDefault()
@@ -2872,6 +2872,12 @@ void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width,
 {
     GetTexDataAsFormat(this, ImTextureFormat_RGBA32, out_pixels, out_width, out_height, out_bytes_per_pixel);
 }
+
+bool ImFontAtlas::Build()
+{
+    ImFontAtlasBuildMain(this);
+    return true;
+}
 #endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
 
 ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
@@ -3136,31 +3142,28 @@ bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor curso
     return true;
 }
 
-bool ImFontAtlas::Build()
+void ImFontAtlasBuildMain(ImFontAtlas* atlas)
 {
-    IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
-
-    if (TexData && TexData->Format != TexDesiredFormat)
+    IM_ASSERT(!atlas->Locked && "Cannot modify a locked ImFontAtlas!");
+    if (atlas->TexData && atlas->TexData->Format != atlas->TexDesiredFormat)
     {
-        ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(this);
-        ImFontAtlasBuildDestroy(this);
-        ImFontAtlasBuildAddTexture(this, new_tex_size.x, new_tex_size.y);
+        ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas);
+        ImFontAtlasBuildDestroy(atlas);
+        ImFontAtlasBuildAddTexture(atlas, new_tex_size.x, new_tex_size.y);
     }
 
-    if (Builder == NULL)
-        ImFontAtlasBuildInit(this);
+    if (atlas->Builder == NULL)
+        ImFontAtlasBuildInit(atlas);
 
     // Default font is none are specified
-    if (Sources.Size == 0)
-        AddFontDefault();
-
-    // [LEGACY] For backends not supporting RendererHasTextures: preload all glyphs
-    ImFontAtlasBuildUpdateRendererHasTexturesFromContext(this);
-    if (DrawListSharedData && DrawListSharedData->RendererHasTextures == false) // ~ImGuiBackendFlags_RendererHasTextures
-        ImFontAtlasBuildPreloadAllGlyphRanges(this);
-    TexIsBuilt = true;
-
-    return true;
+    if (atlas->Sources.Size == 0)
+        atlas->AddFontDefault();
+
+    // [LEGACY] For backends not supporting RendererHasTexUpdates: preload all glyphs
+    ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
+    if (atlas->DrawListSharedData && atlas->DrawListSharedData->RendererHasTextures == false) // ~ImGuiBackendFlags_RendererHasTextures
+        ImFontAtlasBuildPreloadAllGlyphRanges(atlas);
+    atlas->TexIsBuilt = true;
 }
 
 void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, int* out_oversample_h, int* out_oversample_v)

+ 1 - 0
imgui_internal.h

@@ -3705,6 +3705,7 @@ IMGUI_API void              ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas,
 IMGUI_API void              ImFontAtlasBuildUpdatePointers(ImFontAtlas* atlas);
 IMGUI_API void              ImFontAtlasBuildRenderBitmapFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char);
 
+IMGUI_API void              ImFontAtlasBuildMain(ImFontAtlas* atlas);
 IMGUI_API void              ImFontAtlasBuildInit(ImFontAtlas* atlas);
 IMGUI_API void              ImFontAtlasBuildDestroy(ImFontAtlas* atlas);