Merge pull request #179 from diamante0018/fix-font-asset
[IFont] Use Zone allocator instead of static mem allocator
This commit is contained in:
commit
e7474b4543
@ -61,7 +61,7 @@ namespace Assets
|
|||||||
glyph.pixelHeight = static_cast<char>(gh);
|
glyph.pixelHeight = static_cast<char>(gh);
|
||||||
glyph.x0 = static_cast<char>(x0);
|
glyph.x0 = static_cast<char>(x0);
|
||||||
glyph.y0 = static_cast<char>(y0 + yOffset);
|
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
|
// Advance to next col
|
||||||
x = x + gw + 1;
|
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 fontDefFile(Utils::String::VA("%s.json", name.data()));
|
||||||
Components::FileSystem::File fontFile(Utils::String::VA("%s.ttf", name.data()));
|
Components::FileSystem::File fontFile(Utils::String::VA("%s.ttf", name.data()));
|
||||||
@ -105,11 +105,13 @@ namespace Assets
|
|||||||
if (!errors.empty())
|
if (!errors.empty())
|
||||||
{
|
{
|
||||||
Components::Logger::Error("Font define %s is broken: %s.", name.data(), errors.data());
|
Components::Logger::Error("Font define %s is broken: %s.", name.data(), errors.data());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fontDef.is_object())
|
if (!fontDef.is_object())
|
||||||
{
|
{
|
||||||
Components::Logger::Error("Font define %s is invaild.", name.data(), errors.data());
|
Components::Logger::Error("Font define %s is invaild.", name.data(), errors.data());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int w = fontDef["textureWidth"].int_value();
|
int w = fontDef["textureWidth"].int_value();
|
||||||
@ -118,22 +120,28 @@ namespace Assets
|
|||||||
int size = fontDef["size"].int_value();
|
int size = fontDef["size"].int_value();
|
||||||
int yOffset = fontDef["yOffset"].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
|
// Setup assets
|
||||||
auto* texName = Utils::Memory::DuplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */));
|
const auto* texName = builder->getAllocator()->duplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */));
|
||||||
auto* fontName = Utils::Memory::DuplicateString(name.data());
|
const auto* fontName = builder->getAllocator()->duplicateString(name.data());
|
||||||
auto* glowMaterialName = Utils::Memory::DuplicateString(Utils::String::VA("%s_glow", 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;
|
image->name = texName;
|
||||||
|
|
||||||
auto* material = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material);
|
auto* material = builder->getAllocator()->allocate<Game::Material>();
|
||||||
material->textureTable = Utils::Memory::Duplicate(material->textureTable);
|
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->textureTable->u.image = image;
|
||||||
material->info.name = fontName;
|
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->textureTable = material->textureTable;
|
||||||
glowMaterial->info.name = glowMaterialName;
|
glowMaterial->info.name = glowMaterialName;
|
||||||
|
|
||||||
@ -161,14 +169,14 @@ namespace Assets
|
|||||||
charset.push_back(i);
|
charset.push_back(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* font = Utils::Memory::Allocate<Game::Font_s>();
|
auto* font = builder->getAllocator()->allocate<Game::Font_s>();
|
||||||
|
|
||||||
font->fontName = fontName;
|
font->fontName = fontName;
|
||||||
font->pixelHeight = size;
|
font->pixelHeight = size;
|
||||||
font->material = material;
|
font->material = material;
|
||||||
font->glowMaterial = glowMaterial;
|
font->glowMaterial = glowMaterial;
|
||||||
font->glyphCount = charset.size();
|
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
|
// 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);
|
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::IO::WriteFile(Utils::String::VA("userraw\\images\\%s.iwi", texName), outIwi);
|
||||||
Utils::Memory::Free(pixels);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,10 +81,12 @@ namespace Utils
|
|||||||
this->pool.push_back(data);
|
this->pool.push_back(data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> inline T* allocate()
|
template <typename T> inline T* allocate()
|
||||||
{
|
{
|
||||||
return this->allocateArray<T>(1);
|
return this->allocateArray<T>(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> inline T* allocateArray(size_t count = 1)
|
template <typename T> inline T* allocateArray(size_t count = 1)
|
||||||
{
|
{
|
||||||
return static_cast<T*>(this->allocate(count * sizeof(T)));
|
return static_cast<T*>(this->allocate(count * sizeof(T)));
|
||||||
|
Loading…
Reference in New Issue
Block a user