diff --git a/src/Components/Modules/AssetInterfaces/IFont_s.cpp b/src/Components/Modules/AssetInterfaces/IFont_s.cpp index 882cccea..2a2d7fde 100644 --- a/src/Components/Modules/AssetInterfaces/IFont_s.cpp +++ b/src/Components/Modules/AssetInterfaces/IFont_s.cpp @@ -61,7 +61,7 @@ namespace Assets glyph.pixelHeight = static_cast(gh); glyph.x0 = static_cast(x0); glyph.y0 = static_cast(y0 + yOffset); - glyph.dx = static_cast(roundf(scale * advance)); + glyph.dx = static_cast(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(w * h); + auto* pixels = builder->getAllocator()->allocateArray(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(); + 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(); + std::memcpy(material, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material, sizeof(Game::Material)); + + material->textureTable = builder->getAllocator()->allocate(); 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(); + 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(); + auto* font = builder->getAllocator()->allocate(); font->fontName = fontName; font->pixelHeight = size; font->material = material; font->glowMaterial = glowMaterial; font->glyphCount = charset.size(); - font->glyphs = Utils::Memory::AllocateArray(charset.size()); + font->glyphs = builder->getAllocator()->allocateArray(charset.size()); // Generate glyph data int result = PackFonts(reinterpret_cast(fontFile.getBuffer().data()), charset, font->glyphs, static_cast(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); } } diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index f5eb5e50..88bb7007 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -81,10 +81,12 @@ namespace Utils this->pool.push_back(data); return data; } + template inline T* allocate() { return this->allocateArray(1); } + template inline T* allocateArray(size_t count = 1) { return static_cast(this->allocate(count * sizeof(T)));