From 241e8086fa9f92542314ff3e39cd6e707c7e7a39 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 11 Jan 2015 18:27:05 +0000 Subject: [PATCH] Mde it optional to new() io.Font - however it stills needs to be loaded. --- examples/directx11_example/main.cpp | 1 - examples/directx9_example/main.cpp | 1 - examples/opengl3_example/main.cpp | 1 - examples/opengl_example/main.cpp | 1 - imgui.cpp | 25 +++++++++++++++++-------- imgui.h | 7 ++++--- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/examples/directx11_example/main.cpp b/examples/directx11_example/main.cpp index 4f6c4b72b..bee3602e9 100644 --- a/examples/directx11_example/main.cpp +++ b/examples/directx11_example/main.cpp @@ -465,7 +465,6 @@ void InitImGui() } // Load font - io.Font = new ImFont(); io.Font->LoadDefault(); //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault()); //io.Font->DisplayOffset.y += 0.0f; diff --git a/examples/directx9_example/main.cpp b/examples/directx9_example/main.cpp index d1939ad7f..c6cc01254 100644 --- a/examples/directx9_example/main.cpp +++ b/examples/directx9_example/main.cpp @@ -238,7 +238,6 @@ void InitImGui() } // Load font - io.Font = new ImFont(); io.Font->LoadDefault(); //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault()); //io.Font->DisplayOffset.y += 0.0f; diff --git a/examples/opengl3_example/main.cpp b/examples/opengl3_example/main.cpp index 9f86ebee4..5a4543840 100644 --- a/examples/opengl3_example/main.cpp +++ b/examples/opengl3_example/main.cpp @@ -278,7 +278,6 @@ void InitImGui() io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; // Load font - io.Font = new ImFont(); io.Font->LoadDefault(); //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault()); //io.Font->DisplayOffset.y += 0.0f; diff --git a/examples/opengl_example/main.cpp b/examples/opengl_example/main.cpp index 018616fdc..40f26870a 100644 --- a/examples/opengl_example/main.cpp +++ b/examples/opengl_example/main.cpp @@ -187,7 +187,6 @@ void InitImGui() io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; // Load font - io.Font = new ImFont(); io.Font->LoadDefault(); //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault()); //io.Font->DisplayOffset.y += 0.0f; diff --git a/imgui.cpp b/imgui.cpp index 5b5577006..7cc2c7494 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -417,6 +417,12 @@ ImGuiStyle::ImGuiStyle() Colors[ImGuiCol_TooltipBg] = ImVec4(0.05f, 0.05f, 0.10f, 0.90f); } +// We statically allocate a default font storage for the user. +// This allows the user to avoid newing the default font, while keeping IO.Font a pointer which is easy to swap if needed. +// We cannot new() the font because user may override MemAllocFn after the ImGuiIO() constructor is called. +// For the same reason we cannot call LoadDefault() on the font. +static ImFont GDefaultStaticFont; + ImGuiIO::ImGuiIO() { memset(this, 0, sizeof(*this)); @@ -425,7 +431,7 @@ ImGuiIO::ImGuiIO() IniSavingRate = 5.0f; IniFilename = "imgui.ini"; LogFilename = "imgui_log.txt"; - Font = NULL; + Font = &GDefaultStaticFont; FontGlobalScale = 1.0f; FontAllowUserScaling = false; PixelCenterOffset = 0.0f; @@ -1663,8 +1669,11 @@ void ImGui::Shutdown() } if (g.IO.Font) { - g.IO.Font->~ImFont(); - ImGui::MemFree(g.IO.Font); + if (g.IO.Font != &GDefaultStaticFont) + { + g.IO.Font->~ImFont(); + ImGui::MemFree(g.IO.Font); + } g.IO.Font = NULL; } @@ -6146,9 +6155,9 @@ void ImDrawList::AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 if (text_end == NULL) text_end = text_begin + strlen(text_begin); - const bool push_texture_id = font->TexID != texture_id_stack.back(); - if (push_texture_id) - PushTextureID(font->TexID); + const bool push_texture_id = font->TexID != texture_id_stack.back(); + if (push_texture_id) + PushTextureID(font->TexID); // reserve vertices for worse case const unsigned int char_count = (unsigned int)(text_end - text_begin); @@ -6164,8 +6173,8 @@ void ImDrawList::AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 commands.back().vtx_count -= (unsigned int)(vtx_count_max - vtx_count); vtx_write -= (vtx_count_max - vtx_count); - if (push_texture_id) - PopTextureID(); + if (push_texture_id) + PopTextureID(); } void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv0, const ImVec2& uv1, ImU32 col) diff --git a/imgui.h b/imgui.h index e325a2995..3723eacb8 100644 --- a/imgui.h +++ b/imgui.h @@ -746,14 +746,15 @@ struct ImFont float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale() ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found. - ImTextureID TexID; // = NULL // User reference to texture used by the font (ignore if you aren't using multiple fonts/textures) - // Texture data - // User is in charge of copying the pixels into a GPU texture. + // Texture data: user is in charge of copying the pixels into a GPU texture. // You can set 'TexID' to uniquely identify your texture. TexId is copied to the ImDrawCmd structure which you receive during rendering. + ImTextureID TexID; // User reference to texture used by the font (ignore if you aren't using multiple fonts/textures) unsigned char* TexPixels; // 1 byte, 1 component per pixel. Total byte size of TexWidth * TexHeight int TexWidth; int TexHeight; + + // [Internal] ImVec2 TexExtraDataPos; // Position of our rectangle where we draw non-font graphics ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel (part of the TexExtraData block)