Merge pull request #179 from diamante0018/fix-font-asset

[IFont] Use Zone allocator instead of static mem allocator
This commit is contained in:
Dss0 2022-03-12 11:44:35 +01:00 committed by GitHub
commit e7474b4543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View File

@ -61,7 +61,7 @@ namespace Assets
glyph.pixelHeight = static_cast<char>(gh);
glyph.x0 = static_cast<char>(x0);
glyph.y0 = static_cast<char>(y0 + yOffset);
glyph.dx = static_cast<char>(roundf(scale * advance));
glyph.dx = static_cast<char>(std::roundf(scale * advance));
// Advance to next col
x = x + gw + 1;
@ -92,7 +92,7 @@ namespace Assets
}
}
void IFont_s::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone*)
void IFont_s::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
Components::FileSystem::File fontDefFile(Utils::String::VA("%s.json", name.data()));
Components::FileSystem::File fontFile(Utils::String::VA("%s.ttf", name.data()));
@ -105,11 +105,13 @@ namespace Assets
if (!errors.empty())
{
Components::Logger::Error("Font define %s is broken: %s.", name.data(), errors.data());
return;
}
if (!fontDef.is_object())
{
Components::Logger::Error("Font define %s is invaild.", name.data(), errors.data());
return;
}
int w = fontDef["textureWidth"].int_value();
@ -118,22 +120,28 @@ namespace Assets
int size = fontDef["size"].int_value();
int yOffset = fontDef["yOffset"].int_value();
uint8_t* pixels = Utils::Memory::AllocateArray<uint8_t>(w * h);
auto* pixels = builder->getAllocator()->allocateArray<uint8_t>(w * h);
// Setup assets
auto* texName = Utils::Memory::DuplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */));
auto* fontName = Utils::Memory::DuplicateString(name.data());
auto* glowMaterialName = Utils::Memory::DuplicateString(Utils::String::VA("%s_glow", name.data()));
const auto* texName = builder->getAllocator()->duplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */));
const auto* fontName = builder->getAllocator()->duplicateString(name.data());
const auto* glowMaterialName = builder->getAllocator()->duplicateString(Utils::String::VA("%s_glow", name.data()));
auto* image = builder->getAllocator()->allocate<Game::GfxImage>();
std::memcpy(image, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_IMAGE, "gamefonts_pc").image, sizeof(Game::GfxImage));
auto* image = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_IMAGE, "gamefonts_pc").image);
image->name = texName;
auto* material = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material);
material->textureTable = Utils::Memory::Duplicate(material->textureTable);
auto* material = builder->getAllocator()->allocate<Game::Material>();
std::memcpy(material, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material, sizeof(Game::Material));
material->textureTable = builder->getAllocator()->allocate<Game::MaterialTextureDef>();
material->textureTable->u.image = image;
material->info.name = fontName;
auto* glowMaterial = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc_glow").material);
auto* glowMaterial = builder->getAllocator()->allocate<Game::Material>();
std::memcpy(glowMaterial, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc_glow").material, sizeof(Game::Material));
glowMaterial->textureTable = material->textureTable;
glowMaterial->info.name = glowMaterialName;
@ -161,14 +169,14 @@ namespace Assets
charset.push_back(i);
}
auto* font = Utils::Memory::Allocate<Game::Font_s>();
auto* font = builder->getAllocator()->allocate<Game::Font_s>();
font->fontName = fontName;
font->pixelHeight = size;
font->material = material;
font->glowMaterial = glowMaterial;
font->glyphCount = charset.size();
font->glyphs = Utils::Memory::AllocateArray<Game::Glyph>(charset.size());
font->glyphs = builder->getAllocator()->allocateArray<Game::Glyph>(charset.size());
// Generate glyph data
int result = PackFonts(reinterpret_cast<const uint8_t*>(fontFile.getBuffer().data()), charset, font->glyphs, static_cast<float>(size), pixels, w, h, yOffset);
@ -239,7 +247,6 @@ namespace Assets
}
Utils::IO::WriteFile(Utils::String::VA("userraw\\images\\%s.iwi", texName), outIwi);
Utils::Memory::Free(pixels);
}
}

View File

@ -81,10 +81,12 @@ namespace Utils
this->pool.push_back(data);
return data;
}
template <typename T> inline T* allocate()
{
return this->allocateArray<T>(1);
}
template <typename T> inline T* allocateArray(size_t count = 1)
{
return static_cast<T*>(this->allocate(count * sizeof(T)));