Browse Source

Added asserts to catch illegal modifications of the font atlas between NewFrame() and EndFrame()/Render(). (#1958)

pull/1961/head
omar 6 years ago
parent
commit
4ebd442a24
  1. 4
      imgui.cpp
  2. 1
      imgui.h
  3. 9
      imgui_draw.cpp

4
imgui.cpp

@ -3810,6 +3810,7 @@ void ImGui::NewFrame()
g.WindowsActiveCount = 0; g.WindowsActiveCount = 0;
// Setup current font and draw list // Setup current font and draw list
g.IO.Fonts->Locked = true;
SetCurrentFont(GetDefaultFont()); SetCurrentFont(GetDefaultFont());
IM_ASSERT(g.Font->IsLoaded()); IM_ASSERT(g.Font->IsLoaded());
g.DrawListSharedData.ClipRectFullscreen = ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); g.DrawListSharedData.ClipRectFullscreen = ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y);
@ -4451,6 +4452,9 @@ void ImGui::EndFrame()
IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); // we done something wrong IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); // we done something wrong
g.Windows.swap(g.WindowsSortBuffer); g.Windows.swap(g.WindowsSortBuffer);
// Unlock font atlas
g.IO.Fonts->Locked = false;
// Clear Input data for next frame // Clear Input data for next frame
g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f;
memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters)); memset(g.IO.InputCharacters, 0, sizeof(g.IO.InputCharacters));

1
imgui.h

@ -1852,6 +1852,7 @@ struct ImFontAtlas
// Members // Members
//------------------------------------------- //-------------------------------------------
bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert.
ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_) ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_)
ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure. ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure.
int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height. int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.

9
imgui_draw.cpp

@ -1378,6 +1378,7 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3
ImFontAtlas::ImFontAtlas() ImFontAtlas::ImFontAtlas()
{ {
Locked = false;
Flags = 0x00; Flags = 0x00;
TexID = NULL; TexID = NULL;
TexDesiredWidth = 0; TexDesiredWidth = 0;
@ -1394,11 +1395,13 @@ ImFontAtlas::ImFontAtlas()
ImFontAtlas::~ImFontAtlas() ImFontAtlas::~ImFontAtlas()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
Clear(); Clear();
} }
void ImFontAtlas::ClearInputData() void ImFontAtlas::ClearInputData()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
for (int i = 0; i < ConfigData.Size; i++) for (int i = 0; i < ConfigData.Size; i++)
if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas)
{ {
@ -1421,6 +1424,7 @@ void ImFontAtlas::ClearInputData()
void ImFontAtlas::ClearTexData() void ImFontAtlas::ClearTexData()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
if (TexPixelsAlpha8) if (TexPixelsAlpha8)
ImGui::MemFree(TexPixelsAlpha8); ImGui::MemFree(TexPixelsAlpha8);
if (TexPixelsRGBA32) if (TexPixelsRGBA32)
@ -1431,6 +1435,7 @@ void ImFontAtlas::ClearTexData()
void ImFontAtlas::ClearFonts() void ImFontAtlas::ClearFonts()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
for (int i = 0; i < Fonts.Size; i++) for (int i = 0; i < Fonts.Size; i++)
IM_DELETE(Fonts[i]); IM_DELETE(Fonts[i]);
Fonts.clear(); Fonts.clear();
@ -1485,6 +1490,7 @@ void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_wid
ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0);
IM_ASSERT(font_cfg->SizePixels > 0.0f); IM_ASSERT(font_cfg->SizePixels > 0.0f);
@ -1546,6 +1552,7 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
size_t data_size = 0; size_t data_size = 0;
void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0); void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0);
if (!data) if (!data)
@ -1567,6 +1574,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels,
// NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). // NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build().
ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
IM_ASSERT(font_cfg.FontData == NULL); IM_ASSERT(font_cfg.FontData == NULL);
font_cfg.FontData = ttf_data; font_cfg.FontData = ttf_data;
@ -1660,6 +1668,7 @@ bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* ou
bool ImFontAtlas::Build() bool ImFontAtlas::Build()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
return ImFontAtlasBuildWithStbTruetype(this); return ImFontAtlasBuildWithStbTruetype(this);
} }

Loading…
Cancel
Save