CommonHostInterface: Don't draw OSD/FPS as windows

Slight performance improvement.
This commit is contained in:
Connor McLaughlin 2021-02-12 00:42:38 +10:00
parent 9f73791343
commit c8efade20c
2 changed files with 77 additions and 90 deletions

View file

@ -859,72 +859,68 @@ void CommonHostInterface::DrawFPSWindow()
return; return;
} }
const ImVec2 window_size = const float scale = ImGui::GetIO().DisplayFramebufferScale.x;
ImVec2(175.0f * ImGui::GetIO().DisplayFramebufferScale.x, 48.0f * ImGui::GetIO().DisplayFramebufferScale.y); float margin = 10.0f * scale;
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x - window_size.x, 0.0f), ImGuiCond_Always); float spacing = 5.0f * scale;
ImGui::SetNextWindowSize(window_size); float position_y = margin;
ImDrawList* dl = ImGui::GetBackgroundDrawList();
if (!ImGui::Begin("FPSWindow", nullptr, ImFont* font = ImGui::GetFont();
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse | TinyString text;
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMouseInputs | ImVec2 text_size;
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNav))
{
ImGui::End();
return;
}
bool first = true; bool first = true;
#define DRAW_LINE(color) \
do \
{ \
text_size = font->CalcTextSizeA(font->FontSize, std::numeric_limits<float>::max(), -1.0f, text, \
text.GetCharArray() + text.GetLength(), nullptr); \
dl->AddText(font, font->FontSize, ImVec2(ImGui::GetIO().DisplaySize.x - margin - text_size.x, position_y), color, \
text, text.GetCharArray() + text.GetLength()); \
position_y += text_size.y + spacing; \
} while (0)
const System::State state = System::GetState();
if (System::GetState() == System::State::Running)
{
const float speed = System::GetEmulationSpeed();
if (g_settings.display_show_fps) if (g_settings.display_show_fps)
{ {
ImGui::Text("%.2f", System::GetFPS()); text.AppendFormattedString("%.2f", System::GetFPS());
first = false; first = false;
} }
if (g_settings.display_show_vps) if (g_settings.display_show_vps)
{ {
if (first) text.AppendFormattedString("%s%.2f", first ? "" : " / ", System::GetVPS());
{
first = false; first = false;
} }
else
{
ImGui::SameLine();
ImGui::Text("/");
ImGui::SameLine();
}
ImGui::Text("%.2f", System::GetVPS());
}
if (g_settings.display_show_speed) if (g_settings.display_show_speed)
{ {
if (first) text.AppendFormattedString("%s%u%%", first ? "" : " / ", static_cast<u32>(std::round(speed)));
{
first = false; first = false;
} }
else if (!text.IsEmpty())
{ {
ImGui::SameLine(); ImU32 color;
ImGui::Text("/"); if (speed < 95.0f)
ImGui::SameLine(); color = IM_COL32(255, 100, 100, 255);
} else if (speed > 105.0f)
color = IM_COL32(100, 255, 100, 255);
const float speed = System::GetEmulationSpeed();
const u32 rounded_speed = static_cast<u32>(std::round(speed));
if (speed < 90.0f)
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "%u%%", rounded_speed);
else if (speed < 110.0f)
ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), "%u%%", rounded_speed);
else else
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "%u%%", rounded_speed); color = IM_COL32(255, 255, 255, 255);
DRAW_LINE(color);
} }
if (g_settings.display_show_resolution) if (g_settings.display_show_resolution)
{ {
const auto [effective_width, effective_height] = g_gpu->GetEffectiveDisplayResolution(); const auto [effective_width, effective_height] = g_gpu->GetEffectiveDisplayResolution();
const bool interlaced = g_gpu->IsInterlacedDisplayEnabled(); const bool interlaced = g_gpu->IsInterlacedDisplayEnabled();
ImGui::Text("%ux%u (%s)", effective_width, effective_height, interlaced ? "interlaced" : "progressive"); text.Format("%ux%u (%s)", effective_width, effective_height, interlaced ? "interlaced" : "progressive");
DRAW_LINE(IM_COL32(255, 255, 255, 255));
}
} }
ImGui::End(); #undef DRAW_LINE
} }
void CommonHostInterface::AddOSDMessage(std::string message, float duration /*= 2.0f*/) void CommonHostInterface::AddOSDMessage(std::string message, float duration /*= 2.0f*/)
@ -988,12 +984,17 @@ void CommonHostInterface::DrawOSDMessages()
return; return;
const float scale = ImGui::GetIO().DisplayFramebufferScale.x; const float scale = ImGui::GetIO().DisplayFramebufferScale.x;
const float max_width = ImGui::GetIO().DisplaySize.x - (20.0f * scale); const float spacing = 5.0f * scale;
const float margin = 10.0f * scale;
const float padding = 8.0f * scale;
const float rounding = 5.0f * scale;
const float max_width = ImGui::GetIO().DisplaySize.x - margin;
float position_x = margin;
float position_y = margin;
ImDrawList* dl = ImGui::GetBackgroundDrawList();
ImFont* font = ImGui::GetFont();
auto iter = m_osd_messages.begin(); auto iter = m_osd_messages.begin();
float position_x = 10.0f * scale;
float position_y = (10.0f + (static_cast<float>(m_display->GetDisplayTopMargin()))) * scale;
u32 index = 0;
while (iter != m_osd_messages.end()) while (iter != m_osd_messages.end())
{ {
const OSDMessage& msg = *iter; const OSDMessage& msg = *iter;
@ -1005,35 +1006,24 @@ void CommonHostInterface::DrawOSDMessages()
continue; continue;
} }
if (!g_settings.display_show_osd_messages) if (!g_settings.display_show_osd_messages || position_y >= ImGui::GetIO().DisplaySize.y)
{ {
++iter; ++iter;
continue; continue;
} }
const float opacity = std::min(time_remaining, 1.0f); const float opacity = std::min(time_remaining, 1.0f);
const ImVec2 text_size(ImGui::CalcTextSize(msg.text.c_str(), nullptr));
const bool wrapped = (text_size.x > max_width);
ImGui::SetNextWindowPos(ImVec2(position_x, position_y)); const ImVec2 pos(position_x, position_y);
ImGui::SetNextWindowSize(ImVec2(wrapped ? max_width : 0.0f, 0.0f)); const ImVec2 text_size(ImGui::CalcTextSize(msg.text.c_str(), nullptr, false, max_width));
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, opacity); const ImVec2 size(text_size.x + padding * 2.0f, text_size.y + padding * 2.0f);
const ImVec4 text_rect(pos.x + padding, pos.y + padding, pos.x + size.x - padding, pos.y + size.y - padding);
char buf[64]; dl->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), ImGui::GetColorU32(ImGuiCol_WindowBg, opacity),
std::snprintf(buf, sizeof(buf), "osd_%u", index++); rounding);
dl->AddRect(pos, ImVec2(pos.x + size.x, pos.y + size.y), ImGui::GetColorU32(ImGuiCol_Border), rounding);
if (ImGui::Begin(buf, nullptr, window_flags)) dl->AddText(font, font->FontSize, ImVec2(text_rect.x, text_rect.y), ImGui::GetColorU32(ImGuiCol_Text, opacity),
{ msg.text.c_str(), nullptr, max_width, &text_rect);
if (wrapped) position_y += size.y + spacing;
ImGui::TextWrapped("%s", msg.text.c_str());
else
ImGui::TextUnformatted(msg.text.c_str());
position_y += ImGui::GetWindowSize().y + (4.0f * scale);
}
ImGui::End();
ImGui::PopStyleVar();
++iter; ++iter;
} }
} }

View file

@ -2685,8 +2685,6 @@ void DrawOSDMessages()
} }
ImGui::PushFont(g_large_font); ImGui::PushFont(g_large_font);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, LayoutScale(10.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, LayoutScale(10.0f, 10.0f));
const float max_width = LayoutScale(1080.0f); const float max_width = LayoutScale(1080.0f);
const float spacing = LayoutScale(4.0f); const float spacing = LayoutScale(4.0f);
@ -2703,7 +2701,7 @@ void DrawOSDMessages()
return false; return false;
const ImVec2 pos(position_x, position_y); const ImVec2 pos(position_x, position_y);
const ImVec2 text_size(ImGui::CalcTextSize(message.c_str(), nullptr, max_width)); const ImVec2 text_size(ImGui::CalcTextSize(message.c_str(), nullptr, false, max_width));
const ImVec2 size(text_size + LayoutScale(20.0f, 20.0f)); const ImVec2 size(text_size + LayoutScale(20.0f, 20.0f));
const ImVec4 text_rect(pos.x + padding, pos.y + padding, pos.x + size.x - padding, pos.y + size.y - padding); const ImVec4 text_rect(pos.x + padding, pos.y + padding, pos.x + size.x - padding, pos.y + size.y - padding);
ImDrawList* dl = GetDrawListForOverlay(); ImDrawList* dl = GetDrawListForOverlay();
@ -2715,7 +2713,6 @@ void DrawOSDMessages()
return true; return true;
}); });
ImGui::PopStyleVar(2);
ImGui::PopFont(); ImGui::PopFont();
} }