Populate cached images into the texture atlas

This commit is contained in:
Dan Paulat 2023-07-30 22:31:05 -05:00
parent 0f9fbdbf63
commit 4c093d65f6
5 changed files with 104 additions and 18 deletions

View file

@ -459,14 +459,23 @@ void PlacefileManager::Impl::LoadResources(
const QUrl baseUrl =
QUrl::fromUserInput(QString::fromStdString(placefile->name()));
// TODO: Parallelize
for (auto& iconFile : iconFiles)
{
QUrl fileUrl = QUrl(QString::fromStdString(iconFile->filename_));
QUrl resolvedUrl = baseUrl.resolved(fileUrl);
std::vector<std::string> urlStrings;
urlStrings.reserve(iconFiles.size());
ResourceManager::LoadImageResource(resolvedUrl.toString().toStdString());
}
std::transform(iconFiles.cbegin(),
iconFiles.cend(),
std::back_inserter(urlStrings),
[&baseUrl](auto& iconFile)
{
// Resolve target URL relative to base URL
QUrl fileUrl =
QUrl(QString::fromStdString(iconFile->filename_));
QUrl resolvedUrl = baseUrl.resolved(fileUrl);
return resolvedUrl.toString().toStdString();
});
ResourceManager::LoadImageResources(urlStrings);
}
} // namespace manager

View file

@ -5,6 +5,9 @@
#include <scwx/qt/util/texture_atlas.hpp>
#include <scwx/util/logger.hpp>
#include <execution>
#include <mutex>
#include <QFontDatabase>
#include <imgui.h>
@ -61,10 +64,36 @@ std::shared_ptr<util::Font> Font(types::Font font)
return nullptr;
}
void LoadImageResource(const std::string& urlString)
bool LoadImageResource(const std::string& urlString)
{
util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance();
textureAtlas.CacheTexture(urlString, urlString);
return textureAtlas.CacheTexture(urlString, urlString);
}
void LoadImageResources(const std::vector<std::string>& urlStrings)
{
std::mutex m {};
bool textureCached = false;
std::for_each(std::execution::par_unseq,
urlStrings.begin(),
urlStrings.end(),
[&](auto& urlString)
{
bool value = LoadImageResource(urlString);
if (value)
{
std::unique_lock lock {m};
textureCached = true;
}
});
if (textureCached)
{
util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance();
textureAtlas.BuildAtlas(1024, 1024);
}
}
static void LoadFonts()
@ -90,7 +119,7 @@ static void LoadTextures()
":/res/textures/lines/default-1x7.png");
textureAtlas.RegisterTexture("lines/test-pattern",
":/res/textures/lines/test-pattern.png");
textureAtlas.BuildAtlas(8, 8);
textureAtlas.BuildAtlas(1024, 1024);
}
} // namespace ResourceManager

View file

@ -18,7 +18,8 @@ void Shutdown();
int FontId(types::Font font);
std::shared_ptr<util::Font> Font(types::Font font);
void LoadImageResource(const std::string& urlString);
bool LoadImageResource(const std::string& urlString);
void LoadImageResources(const std::vector<std::string>& urlStrings);
} // namespace ResourceManager
} // namespace manager