Use zone allocator instead of static mem allocator

This commit is contained in:
FutureRave 2022-01-27 17:11:21 +00:00
parent 18dca4506f
commit 3bb39483dc
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
3 changed files with 28 additions and 19 deletions

View File

@ -634,7 +634,7 @@ namespace Components
LUID luid; LUID luid;
TOKEN_PRIVILEGES tp = { 0 }; TOKEN_PRIVILEGES tp = { 0 };
DWORD cb = sizeof(TOKEN_PRIVILEGES); DWORD cb = sizeof(TOKEN_PRIVILEGES);
if (!LookupPrivilegeValueA(nullptr, SE_DEBUG_NAME, &luid)) return; if (!LookupPrivilegeValueW(nullptr, SE_DEBUG_NAME, &luid)) return;
tp.PrivilegeCount = 1; tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid; tp.Privileges[0].Luid = luid;

View File

@ -53,15 +53,15 @@ namespace Assets
auto& glyph = glyphs[i++]; auto& glyph = glyphs[i++];
glyph.letter = ch; glyph.letter = ch;
glyph.s0 = x / static_cast<float>(pw); glyph.s0 = static_cast<float>(x / pw);
glyph.s1 = (x + gw) / static_cast<float>(pw); glyph.s1 = static_cast<float>((x + gw) / pw);
glyph.t0 = y / static_cast<float>(ph); glyph.t0 = static_cast<float>(y / ph);
glyph.t1 = (y + gh) / static_cast<float>(ph); glyph.t1 = static_cast<float>((y + gh) / ph);
glyph.pixelWidth = static_cast<char>(gw); glyph.pixelWidth = static_cast<char>(gw);
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);
} }
} }

View File

@ -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)));