Refactored Challenge Icons to handle icon updates

If an icon is displayed on screen before it downloads, it was displaying a default icon but it would fail to load the actual icon even after it was downloaded. This fixes that.
This commit is contained in:
LillyJadeKatrin
2024-06-13 00:03:46 -04:00
parent 6c5ceaa06d
commit 3d5a1f7d33
4 changed files with 55 additions and 36 deletions

View File

@ -334,11 +334,28 @@ void OnScreenUI::DrawDebugText()
void OnScreenUI::DrawChallengesAndLeaderboards()
{
#ifdef USE_RETRO_ACHIEVEMENTS
std::lock_guard lg{AchievementManager::GetInstance().GetLock()};
const auto& challenge_icons = AchievementManager::GetInstance().GetChallengeIcons();
const auto& leaderboard_progress = AchievementManager::GetInstance().GetActiveLeaderboards();
auto& instance = AchievementManager::GetInstance();
std::lock_guard lg{instance.GetLock()};
if (instance.AreChallengesUpdated())
{
instance.ResetChallengesUpdated();
const auto& challenges = instance.GetActiveChallenges();
m_challenge_texture_map.clear();
for (const auto& name : challenges)
{
const auto& icon = instance.GetAchievementBadge(name, false);
const u32 width = icon.width;
const u32 height = icon.height;
TextureConfig tex_config(width, height, 1, 1, 1, AbstractTextureFormat::RGBA8, 0,
AbstractTextureType::Texture_2DArray);
auto res = m_challenge_texture_map.insert_or_assign(name, g_gfx->CreateTexture(tex_config));
res.first->second->Load(0, width, height, width, icon.data.data(),
sizeof(u32) * width * height);
}
}
float leaderboard_y = ImGui::GetIO().DisplaySize.y;
if (!challenge_icons.empty())
if (!m_challenge_texture_map.empty())
{
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y), 0,
ImVec2(1.0, 1.0));
@ -349,37 +366,17 @@ void OnScreenUI::DrawChallengesAndLeaderboards()
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing))
{
for (const auto& [name, icon] : challenge_icons)
{
if (m_challenge_texture_map.find(name) != m_challenge_texture_map.end())
continue;
const u32 width = icon->width;
const u32 height = icon->height;
TextureConfig tex_config(width, height, 1, 1, 1, AbstractTextureFormat::RGBA8, 0,
AbstractTextureType::Texture_2DArray);
auto res = m_challenge_texture_map.insert_or_assign(name, g_gfx->CreateTexture(tex_config));
res.first->second->Load(0, width, height, width, icon->data.data(),
sizeof(u32) * width * height);
}
for (auto& [name, texture] : m_challenge_texture_map)
{
auto icon_itr = challenge_icons.find(name);
if (icon_itr == challenge_icons.end())
{
m_challenge_texture_map.erase(name);
continue;
}
if (texture)
{
ImGui::Image(texture.get(), ImVec2(static_cast<float>(icon_itr->second->width),
static_cast<float>(icon_itr->second->height)));
}
ImGui::Image(texture.get(), ImVec2(static_cast<float>(texture->GetWidth()),
static_cast<float>(texture->GetHeight())));
}
leaderboard_y -= ImGui::GetWindowHeight();
}
ImGui::End();
}
const auto& leaderboard_progress = instance.GetActiveLeaderboards();
if (!leaderboard_progress.empty())
{
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x, leaderboard_y), 0,