Skip to content

Commit

Permalink
Mainly fixed 2 things on "On Display Messages":
Browse files Browse the repository at this point in the history
-They might have never drawn if DrawMessages wasn't called before they actually expired.
-Their fade was wrong if the duration of the message was less than the fade time.

This makes them much more useful for debugging, I know there might be other means of debugging like logs and imgui, but this was the simplest so that's what I used.
If you want to print the same message every frame, but with a slightly different value to see the changes, it now work.

To compensate for the fact that they are now always rendered once, i've cleaned all the messages before the rendering window opens, but alternatively we could have a "force expire" time, where they would never be drawn.
  • Loading branch information
Filoppi committed Jul 1, 2020
1 parent 2147707 commit de3e746
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Source/Core/Core/Core.cpp
Expand Up @@ -414,6 +414,9 @@ static void FifoPlayerThread(const std::optional<std::string>& savestate_path,
// See the BootManager.cpp file description for a complete call schedule.
static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi)
{
// Clear messages printed before the window opened
OSD::ClearMessages();

const SConfig& core_parameter = SConfig::GetInstance();
if (s_on_state_changed_callback)
s_on_state_changed_callback(State::Starting);
Expand Down
33 changes: 20 additions & 13 deletions Source/Core/VideoCommon/OnScreenDisplay.cpp
Expand Up @@ -19,19 +19,22 @@

namespace OSD
{
constexpr float LEFT_MARGIN = 10.0f; // Pixels to the left of OSD messages.
constexpr float TOP_MARGIN = 10.0f; // Pixels above the first OSD message.
constexpr float WINDOW_PADDING = 4.0f; // Pixels between subsequent OSD messages.
constexpr float LEFT_MARGIN = 10.0f; // Pixels to the left of OSD messages.
constexpr float TOP_MARGIN = 10.0f; // Pixels above the first OSD message.
constexpr float WINDOW_PADDING = 4.0f; // Pixels between subsequent OSD messages.
constexpr float MESSAGE_FADE_TIME = 1000.f; // Ms to fade OSD messages at the end of their life.

struct Message
{
Message() = default;
Message(std::string text_, u32 timestamp_, u32 color_)
: text(std::move(text_)), timestamp(timestamp_), color(color_)
Message(std::string text_, u32 timestamp_, u32 duration_, u32 color_)
: text(std::move(text_)), timestamp(timestamp_), duration(duration_), color(color_)
{
}
std::string text;
u32 timestamp = 0;
u32 duration = 0;
bool ever_drawn = false;
u32 color = 0;
};
static std::multimap<MessageType, Message> s_messages;
Expand All @@ -45,7 +48,7 @@ static ImVec4 RGBAToImVec4(const u32 rgba)
static_cast<float>((rgba >> 24) & 0xFF) / 255.0f);
}

static float DrawMessage(int index, const Message& msg, const ImVec2& position, int time_left)
static float DrawMessage(int index, Message& msg, const ImVec2& position, int time_left)
{
// We have to provide a window name, and these shouldn't be duplicated.
// So instead, we generate a name based on the number of messages drawn.
Expand All @@ -55,9 +58,10 @@ static float DrawMessage(int index, const Message& msg, const ImVec2& position,
ImGui::SetNextWindowPos(position);
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));

// Gradually fade old messages away.
const float alpha = std::min(1.0f, std::max(0.0f, time_left / 1024.0f));
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
// Gradually fade old messages away (except in their first frame)
const float fade_time = std::max(std::min(MESSAGE_FADE_TIME, (float)msg.duration), 1.f);
const float alpha = std::clamp(time_left / fade_time, 0.0f, 1.0f);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, msg.ever_drawn ? alpha : 1.0);

float window_height = 0.0f;
if (ImGui::Begin(window_name.c_str(), nullptr,
Expand All @@ -75,21 +79,23 @@ static float DrawMessage(int index, const Message& msg, const ImVec2& position,
ImGui::End();
ImGui::PopStyleVar();

msg.ever_drawn = true;

return window_height;
}

void AddTypedMessage(MessageType type, std::string message, u32 ms, u32 rgba)
{
std::lock_guard lock{s_messages_mutex};
s_messages.erase(type);
s_messages.emplace(type, Message(std::move(message), Common::Timer::GetTimeMs() + ms, rgba));
s_messages.emplace(type, Message(std::move(message), Common::Timer::GetTimeMs() + ms, ms, rgba));
}

void AddMessage(std::string message, u32 ms, u32 rgba)
{
std::lock_guard lock{s_messages_mutex};
s_messages.emplace(MessageType::Typeless,
Message(std::move(message), Common::Timer::GetTimeMs() + ms, rgba));
Message(std::move(message), Common::Timer::GetTimeMs() + ms, ms, rgba));
}

void DrawMessages()
Expand All @@ -104,10 +110,11 @@ void DrawMessages()

for (auto it = s_messages.begin(); it != s_messages.end();)
{
const Message& msg = it->second;
Message& msg = it->second;
const int time_left = static_cast<int>(msg.timestamp - now);

if (time_left <= 0)
// Make sure we draw them at least once if they were printed with 0ms
if (time_left <= 0 && msg.ever_drawn)
{
it = s_messages.erase(it);
continue;
Expand Down

0 comments on commit de3e746

Please sign in to comment.