refactor code and support utf8 strings rendering

This commit is contained in:
ohyzha
2024-08-02 23:08:24 +03:00
parent 875ad80337
commit 9589b4d39f
11 changed files with 70 additions and 30 deletions

View File

@@ -40,9 +40,13 @@ namespace OpenVulkano
}
}
Array<char> ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName, std::string& resourcePath)
std::string ResourceLoaderAppDirLinux::GetResourcePath(const std::string& resourceName)
{
resourcePath = GetAppDir() + resourceName;
return Utils::ReadFile(resourcePath, true);
return GetAppDir() + resourceName;
}
Array<char> ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName)
{
return Utils::ReadFile(resourceName, true);
}
}

View File

@@ -13,6 +13,7 @@ namespace OpenVulkano
class ResourceLoaderAppDirLinux final : public ResourceLoader
{
public:
Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
std::string GetResourcePath(const std::string& resourceName) override;
Array<char> GetResource(const std::string& resourceName) override;
};
}

View File

@@ -15,13 +15,13 @@ namespace OpenVulkano
std::vector<std::unique_ptr<ResourceLoader>> m_loaders;
public:
Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override
Array<char> GetResource(const std::string& resourceName) override
{
for(auto& loader : m_loaders)
{
try
{
auto res = loader->GetResource(resourceName, resourcePath);
auto res = loader->GetResource(resourceName);
if (!res.Empty()) return res;
}
catch (const std::exception& e)
@@ -54,6 +54,30 @@ namespace OpenVulkano
}
return false;
}
std::string GetResourcePath(const std::string& resourceName) override
{
try
{
for (auto& loader: m_loaders)
{
auto res = loader->GetResourcePath(resourceName);
if (!res.empty())
{
return res;
}
}
}
catch (const std::exception& e)
{
Logger::FILESYS->error("Error trying to get resource path for '{}'! Error: {}", resourceName, e.what());
}
catch (...)
{
Logger::FILESYS->error("Unknown error trying to get resource path for '{}'!", resourceName);
}
return "";
}
};
ResourceLoader& ResourceLoader::GetInstance()

View File

@@ -15,7 +15,9 @@ namespace OpenVulkano
public:
virtual ~ResourceLoader() = default;
virtual Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) = 0;
virtual Array<char> GetResource(const std::string& resourceName) = 0;
virtual std::string GetResourcePath(const std::string& resourceName) { return ""; }
static ResourceLoader& GetInstance();

View File

@@ -90,12 +90,11 @@ namespace OpenVulkano
return Array<char>(buffer);
}
Array<char> WebResourceLoader::GetResource(const std::string& resourceName, std::string& resourcePath)
Array<char> WebResourceLoader::GetResource(const std::string& resourceName)
{
if (IsUrl(resourceName))
{
std::filesystem::path cacheFilePath = GetCacheFilePath(resourceName);
resourcePath = resourceName;
if (std::filesystem::exists(cacheFilePath)) { return Utils::ReadFile(cacheFilePath.string()); }
else { return DownloadResource(resourceName); }
}

View File

@@ -24,6 +24,6 @@ namespace OpenVulkano
WebResourceLoader();
~WebResourceLoader();
Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
Array<char> GetResource(const std::string& resourceName) override;
};
}

View File

@@ -38,9 +38,13 @@ namespace OpenVulkano
}
}
Array<char> ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName, std::string& resourcePath)
std::string ResourceLoaderAppDirWindows::GetResourcePath(const std::string& resourceName)
{
resourcePath = GetAppDir() + resourceName;
return Utils::ReadFile(resourcePath);
return GetAppDir() + resourceName;
}
Array<char> ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName)
{
return Utils::ReadFile(resourceName);
}
}

View File

@@ -13,6 +13,7 @@ namespace OpenVulkano
class ResourceLoaderAppDirWindows final : public ResourceLoader
{
public:
Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
std::string GetResourcePath(const std::string& resourceName) override;
Array<char> GetResource(const std::string& resourceName) override;
};
}

View File

@@ -59,18 +59,17 @@ namespace OpenVulkano::Scene
m_generator.generate(glyphsGeometry.data(), glyphsGeometry.size());
int idx = 0;
BitmapConstRef<byte, 1> storage = m_generator.atlasStorage();
const BitmapConstRef<byte, 1>& storage = m_generator.atlasStorage();
m_atlasTex.resolution = Math::Vector3ui(storage.width, storage.height, 1);
m_atlasTex.textureBuffer = (msdfgen::byte*) storage.pixels;
m_atlasTex.format = OpenVulkano::DataFormat::R8_UNORM;
m_atlasTex.size = storage.width * storage.height * 1; // 1 channel
for (const auto& glyph: glyphsGeometry)
{
unicode_t c = static_cast<char8_t>(glyph.getCodepoint());
GlyphInfo info;
info.texture.resolution = Math::Vector3ui(storage.width, storage.height, 1);
info.texture.textureBuffer = (msdfgen::byte*)storage.pixels;
info.texture.format = OpenVulkano::DataFormat::R8_UNORM;
info.texture.size = storage.width * storage.height * 1; // 1 channel
info.geometry = glyph;
info.glyphBox = m_generator.getLayout()[idx++];
m_symbols[c] = std::move(info);
m_symbols[glyph.getCodepoint()] = std::move(info);
}
savePng(m_generator.atlasStorage(), outputFile.c_str());
destroyFont(font);

View File

@@ -23,18 +23,19 @@ namespace OpenVulkano::Scene
{
GlyphGeometry geometry;
GlyphBox glyphBox;
Texture texture;
};
class FontAtlasGenerator
{
public:
void GenerateAtlas(const std::string& fontFile, const std::string& outputFile, const Charset& = Charset::ASCII);
const Texture& GetAtlas() const { return m_atlasTex; }
std::map<unicode_t, GlyphInfo>& GetAtlasInfo() { return m_symbols; }
private:
std::pair<FreetypeHandle*, FontHandle*> GetHandlers(const std::string& fontFile);
private:
ImmediateAtlasGenerator<float, 1, sdfGenerator, BitmapAtlasStorage<msdfgen::byte, 1>> m_generator;
Texture m_atlasTex;
std::map<unicode_t, GlyphInfo> m_symbols;
std::string m_loadedFont;
};