1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-24 15:50:25 +01:00

Internals: wrapped used of g.TempBuffer into ImFormatStringToTempBuffer/ImFormatStringToTempBufferV helpers.

This leaves us room for growing the buffer if needed, and gives us a resizable buffer available for other work.
This commit is contained in:
ocornut 2022-06-07 19:16:00 +02:00
parent 64d6c30562
commit 23a785aeb3
3 changed files with 48 additions and 26 deletions

View File

@ -1704,6 +1704,25 @@ int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args)
} }
#endif // #ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS #endif // #ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS
void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, const char* fmt, ...)
{
ImGuiContext& g = *GImGui;
va_list args;
va_start(args, fmt);
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
va_end(args);
}
void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args)
{
ImGuiContext& g = *GImGui;
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
}
// CRC32 needs a 1KB lookup table (not cache friendly) // CRC32 needs a 1KB lookup table (not cache friendly)
// Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily: // Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily:
// - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe. // - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe.
@ -4482,6 +4501,7 @@ void ImGui::Initialize()
// Create default viewport // Create default viewport
ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)();
g.Viewports.push_back(viewport); g.Viewports.push_back(viewport);
g.TempBuffer.resize(1024 * 3 + 1, 0);
#ifdef IMGUI_HAS_DOCK #ifdef IMGUI_HAS_DOCK
#endif #endif
@ -5178,15 +5198,16 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b
SetNextWindowSize(size); SetNextWindowSize(size);
// Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value.
const char* temp_window_name;
if (name) if (name)
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%s_%08X", parent_window->Name, name, id); ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%s_%08X", parent_window->Name, name, id);
else else
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%08X", parent_window->Name, id); ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%08X", parent_window->Name, id);
const float backup_border_size = g.Style.ChildBorderSize; const float backup_border_size = g.Style.ChildBorderSize;
if (!border) if (!border)
g.Style.ChildBorderSize = 0.0f; g.Style.ChildBorderSize = 0.0f;
bool ret = Begin(g.TempBuffer, NULL, flags); bool ret = Begin(temp_window_name, NULL, flags);
g.Style.ChildBorderSize = backup_border_size; g.Style.ChildBorderSize = backup_border_size;
ImGuiWindow* child_window = g.CurrentWindow; ImGuiWindow* child_window = g.CurrentWindow;
@ -13219,8 +13240,8 @@ void ImGui::ShowStackToolWindow(bool* p_open)
if (tool->CopyToClipboardOnCtrlC && IsKeyDown(ImGuiKey_ModCtrl) && IsKeyPressed(ImGuiKey_C)) if (tool->CopyToClipboardOnCtrlC && IsKeyDown(ImGuiKey_ModCtrl) && IsKeyPressed(ImGuiKey_C))
{ {
tool->CopyToClipboardLastTime = (float)g.Time; tool->CopyToClipboardLastTime = (float)g.Time;
char* p = g.TempBuffer; char* p = g.TempBuffer.Data;
char* p_end = p + IM_ARRAYSIZE(g.TempBuffer); char* p_end = p + g.TempBuffer.Size;
for (int stack_n = 0; stack_n < tool->Results.Size && p + 3 < p_end; stack_n++) for (int stack_n = 0; stack_n < tool->Results.Size && p + 3 < p_end; stack_n++)
{ {
*p++ = '/'; *p++ = '/';
@ -13234,7 +13255,7 @@ void ImGui::ShowStackToolWindow(bool* p_open)
} }
} }
*p = '\0'; *p = '\0';
SetClipboardText(g.TempBuffer); SetClipboardText(g.TempBuffer.Data);
} }
// Display decorated stack // Display decorated stack
@ -13252,8 +13273,8 @@ void ImGui::ShowStackToolWindow(bool* p_open)
TableNextColumn(); TableNextColumn();
Text("0x%08X", (n > 0) ? tool->Results[n - 1].ID : 0); Text("0x%08X", (n > 0) ? tool->Results[n - 1].ID : 0);
TableNextColumn(); TableNextColumn();
StackToolFormatLevelInfo(tool, n, true, g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer)); StackToolFormatLevelInfo(tool, n, true, g.TempBuffer.Data, g.TempBuffer.Size);
TextUnformatted(g.TempBuffer); TextUnformatted(g.TempBuffer.Data);
TableNextColumn(); TableNextColumn();
Text("0x%08X", info->ID); Text("0x%08X", info->ID);
if (n == tool->Results.Size - 1) if (n == tool->Results.Size - 1)

View File

@ -331,6 +331,8 @@ static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c =
// Helpers: Formatting // Helpers: Formatting
IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3);
IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3);
IMGUI_API void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, const char* fmt, ...) IM_FMTARGS(3);
IMGUI_API void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args) IM_FMTLIST(3);
IMGUI_API const char* ImParseFormatFindStart(const char* format); IMGUI_API const char* ImParseFormatFindStart(const char* format);
IMGUI_API const char* ImParseFormatFindEnd(const char* format); IMGUI_API const char* ImParseFormatFindEnd(const char* format);
IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size); IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size);
@ -1809,7 +1811,7 @@ struct ImGuiContext
int WantCaptureMouseNextFrame; // Explicit capture override via SetNextFrameWantCaptureMouse()/SetNextFrameWantCaptureKeyboard(). Default to -1. int WantCaptureMouseNextFrame; // Explicit capture override via SetNextFrameWantCaptureMouse()/SetNextFrameWantCaptureKeyboard(). Default to -1.
int WantCaptureKeyboardNextFrame; // " int WantCaptureKeyboardNextFrame; // "
int WantTextInputNextFrame; int WantTextInputNextFrame;
char TempBuffer[1024 * 3 + 1]; // Temporary text buffer ImVector<char> TempBuffer; // Temporary text buffer
ImGuiContext(ImFontAtlas* shared_font_atlas) ImGuiContext(ImFontAtlas* shared_font_atlas)
{ {
@ -1958,7 +1960,6 @@ struct ImGuiContext
FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0;
FramerateSecPerFrameAccum = 0.0f; FramerateSecPerFrameAccum = 0.0f;
WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1;
memset(TempBuffer, 0, sizeof(TempBuffer));
} }
}; };

View File

@ -276,9 +276,9 @@ void ImGui::TextV(const char* fmt, va_list args)
return; return;
// FIXME-OPT: Handle the %s shortcut? // FIXME-OPT: Handle the %s shortcut?
ImGuiContext& g = *GImGui; const char* text, *text_end;
const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText);
} }
void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) void ImGui::TextColored(const ImVec4& col, const char* fmt, ...)
@ -359,8 +359,8 @@ void ImGui::LabelTextV(const char* label, const char* fmt, va_list args)
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const float w = CalcItemWidth(); const float w = CalcItemWidth();
const char* value_text_begin = &g.TempBuffer[0]; const char* value_text_begin, *value_text_end;
const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); ImFormatStringToTempBufferV(&value_text_begin, &value_text_end, fmt, args);
const ImVec2 value_size = CalcTextSize(value_text_begin, value_text_end, false); const ImVec2 value_size = CalcTextSize(value_text_begin, value_text_end, false);
const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImVec2 label_size = CalcTextSize(label, NULL, true);
@ -395,8 +395,8 @@ void ImGui::BulletTextV(const char* fmt, va_list args)
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const char* text_begin = g.TempBuffer; const char* text_begin, *text_end;
const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); ImFormatStringToTempBufferV(&text_begin, &text_end, fmt, args);
const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); const ImVec2 label_size = CalcTextSize(text_begin, text_end, false);
const ImVec2 total_size = ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x * 2) : 0.0f), label_size.y); // Empty text doesn't add padding const ImVec2 total_size = ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x * 2) : 0.0f), label_size.y); // Empty text doesn't add padding
ImVec2 pos = window->DC.CursorPos; ImVec2 pos = window->DC.CursorPos;
@ -1910,9 +1910,9 @@ static const char* PatchFormatStringFloatToInt(const char* fmt)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (fmt_start == fmt && fmt_end[0] == 0) if (fmt_start == fmt && fmt_end[0] == 0)
return "%d"; return "%d";
ImGuiContext& g = *GImGui; const char* tmp_format;
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision. ImFormatStringToTempBuffer(&tmp_format, NULL, "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision.
return g.TempBuffer; return tmp_format;
#else #else
IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d" IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d"
#endif #endif
@ -5835,9 +5835,9 @@ bool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char
if (window->SkipItems) if (window->SkipItems)
return false; return false;
ImGuiContext& g = *GImGui; const char* label, *label_end;
const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); ImFormatStringToTempBufferV(&label, &label_end, fmt, args);
return TreeNodeBehavior(window->GetID(str_id), flags, g.TempBuffer, label_end); return TreeNodeBehavior(window->GetID(str_id), flags, label, label_end);
} }
bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args)
@ -5846,9 +5846,9 @@ bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char
if (window->SkipItems) if (window->SkipItems)
return false; return false;
ImGuiContext& g = *GImGui; const char* label, *label_end;
const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); ImFormatStringToTempBufferV(&label, &label_end, fmt, args);
return TreeNodeBehavior(window->GetID(ptr_id), flags, g.TempBuffer, label_end); return TreeNodeBehavior(window->GetID(ptr_id), flags, label, label_end);
} }
bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags)