diff options
| author | Nicholas Estelami <NEstelami@users.noreply.github.com> | 2021-04-13 07:49:30 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-13 07:49:30 -0400 |
| commit | 420508d2d872e8653081d95a43bf2ad8f85d894c (patch) | |
| tree | 9019e0a87ad719c4a3b1e38c61e7b67feebfa63a /ZAPD/ZTexture.cpp | |
| parent | 95bf5fc0080135f20f3ab17082f79b9e539e80a5 (diff) | |
Refactoring and various features added (#108)
* ZAPD prints out git hash
* Refactor: ZResource constructor sets parent always.
ZOverlay also no longer a ZResource, since it did not make much sense.
* Organized ZResource a bit
* A number of bug fixes, cleanup, and feature improvements.
* Prepping for legacy dlist disasm
* Fixed some merging issues. Fixed compilation issues with MSVC. Added in proper support for legacy display list disassembler. Refactored parts of ZFile.
* Merge with upstream
* Finished ZFile refactor, and other tidying.
* Fixed up merge issues
* Minor cleanup
* Got rid of libgfxd_win.a
* Fixed matching issue
* Fixed up a few bugs
* Fixed issue with exceptions
* Fixed up merge issues with ZPrerender, along with cleaning up some code
* Additional cleanup
* Fixed merge issues with ZMtx and fixed a bug with ZDisplayList
* Fixes based on PR feedback
* run clang-format (#7)
Signed-off-by: angie <angheloalf95@gmail.com>
* Fixed merge issues with Roz's PR
* Misc fixes
* Fixed MM bug, removed redundant file.
* Removed some commented out code
Co-authored-by: Jack Walker <7463599+Jack-Walker@users.noreply.github.com>
Co-authored-by: Anghelo Carvajal <anghelo.carvajal.14@sansano.usm.cl>
Diffstat (limited to 'ZAPD/ZTexture.cpp')
| -rw-r--r-- | ZAPD/ZTexture.cpp | 102 |
1 files changed, 55 insertions, 47 deletions
diff --git a/ZAPD/ZTexture.cpp b/ZAPD/ZTexture.cpp index b98ea36..58e0eed 100644 --- a/ZAPD/ZTexture.cpp +++ b/ZAPD/ZTexture.cpp @@ -4,17 +4,21 @@ #include "ZTexture.h" #include "BitConverter.h" +#include "CRC32.h" #include "File.h" +#include "Globals.h" #include "Path.h" -#include "StringHelper.h" +#include <Directory.h> #include "stb_image.h" #include "stb_image_write.h" using namespace std; using namespace tinyxml2; -ZTexture::ZTexture() : ZResource() +REGISTER_ZFILENODE(Texture, ZTexture); + +ZTexture::ZTexture(ZFile* nParent) : ZResource(nParent) { bmpRgb = nullptr; bmpRgba = nullptr; @@ -43,29 +47,24 @@ ZTexture::~ZTexture() type = TextureType::Error; } -// EXTRACT MODE -ZTexture* ZTexture::ExtractFromXML(XMLElement* reader, vector<uint8_t> nRawData, int nRawDataIndex, - string nRelPath) +void ZTexture::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, + const int nRawDataIndex, const std::string& nRelPath) { - ZTexture* tex = new ZTexture(); - - tex->ParseXML(reader); - tex->rawDataIndex = nRawDataIndex; - tex->rawData = vector<uint8_t>(nRawData.data() + tex->rawDataIndex, - nRawData.data() + tex->rawDataIndex + tex->GetRawDataSize()); - - tex->relativePath = nRelPath; + ParseXML(reader); + rawDataIndex = nRawDataIndex; + rawData = vector<uint8_t>(nRawData.data() + rawDataIndex, + nRawData.data() + rawDataIndex + GetRawDataSize()); - tex->FixRawData(); - tex->PrepareBitmap(); + relativePath = nRelPath; - return tex; + FixRawData(); + PrepareBitmap(); } ZTexture* ZTexture::FromBinary(TextureType nType, std::vector<uint8_t> nRawData, int nRawDataIndex, - std::string nName, int nWidth, int nHeight) + std::string nName, int nWidth, int nHeight, ZFile* nParent) { - ZTexture* tex = new ZTexture(); + ZTexture* tex = new ZTexture(nParent); tex->width = nWidth; tex->height = nHeight; @@ -83,10 +82,10 @@ ZTexture* ZTexture::FromBinary(TextureType nType, std::vector<uint8_t> nRawData, return tex; } -// BUILD MODE +// Build Source File Mode ZTexture* ZTexture::BuildFromXML(XMLElement* reader, string inFolder, bool readFile) { - ZTexture* tex = new ZTexture(); + ZTexture* tex = new ZTexture(nullptr); tex->ParseXML(reader); @@ -99,7 +98,7 @@ ZTexture* ZTexture::BuildFromXML(XMLElement* reader, string inFolder, bool readF ZTexture* ZTexture::FromPNG(string pngFilePath, TextureType texType) { int comp; - ZTexture* tex = new ZTexture(); + ZTexture* tex = new ZTexture(nullptr); tex->type = texType; tex->name = StringHelper::Split(Path::GetFileNameWithoutExtension(pngFilePath), ".")[0]; @@ -149,7 +148,7 @@ ZTexture* ZTexture::FromPNG(string pngFilePath, TextureType texType) ZTexture* ZTexture::FromHLTexture(HLTexture* hlTex) { - ZTexture* tex = new ZTexture(); + ZTexture* tex = new ZTexture(nullptr); tex->width = hlTex->width; tex->height = hlTex->height; @@ -776,58 +775,65 @@ TextureType ZTexture::GetTextureType() void ZTexture::Save(const std::string& outFolder) { + CalcHash(); + + std::string outPath = outFolder; + + // POOL CHECK + if (Globals::Instance->cfg->texturePool.find(hash) != Globals::Instance->cfg->texturePool.end()) + { + outPath = Path::GetDirectoryName(Globals::Instance->cfg->texturePool[hash]); + outName = Path::GetFileNameWithoutExtension(Globals::Instance->cfg->texturePool[hash]); + } + + if (!Directory::Exists(outPath)) + Directory::CreateDirectory(outPath); + if (type == TextureType::RGBA32bpp) - stbi_write_png((outFolder + "/" + outName + ".rgba32.png").c_str(), width, height, 4, - bmpRgba, width * 4); + stbi_write_png((outPath + "/" + outName + ".rgba32.png").c_str(), width, height, 4, bmpRgba, + width * 4); else if (type == TextureType::RGBA16bpp) - stbi_write_png((outFolder + "/" + outName + ".rgb5a1.png").c_str(), width, height, 4, - bmpRgba, width * 4); + stbi_write_png((outPath + "/" + outName + ".rgb5a1.png").c_str(), width, height, 4, bmpRgba, + width * 4); else if (type == TextureType::Grayscale8bpp) - stbi_write_png((outFolder + "/" + outName + ".i8.png").c_str(), width, height, 3, bmpRgb, + stbi_write_png((outPath + "/" + outName + ".i8.png").c_str(), width, height, 3, bmpRgb, width * 3); else if (type == TextureType::Grayscale4bpp) - stbi_write_png((outFolder + "/" + outName + ".i4.png").c_str(), width, height, 3, bmpRgb, + stbi_write_png((outPath + "/" + outName + ".i4.png").c_str(), width, height, 3, bmpRgb, width * 3); else if (type == TextureType::GrayscaleAlpha16bpp) - stbi_write_png((outFolder + "/" + outName + ".ia16.png").c_str(), width, height, 4, bmpRgba, + stbi_write_png((outPath + "/" + outName + ".ia16.png").c_str(), width, height, 4, bmpRgba, width * 4); else if (type == TextureType::GrayscaleAlpha8bpp) - stbi_write_png((outFolder + "/" + outName + ".ia8.png").c_str(), width, height, 4, bmpRgba, + stbi_write_png((outPath + "/" + outName + ".ia8.png").c_str(), width, height, 4, bmpRgba, width * 4); else if (type == TextureType::GrayscaleAlpha4bpp) - stbi_write_png((outFolder + "/" + outName + ".ia4.png").c_str(), width, height, 4, bmpRgba, + stbi_write_png((outPath + "/" + outName + ".ia4.png").c_str(), width, height, 4, bmpRgba, width * 4); else if (type == TextureType::Palette4bpp) - stbi_write_png((outFolder + "/" + outName + ".ci4.png").c_str(), width, height, 3, bmpRgb, + stbi_write_png((outPath + "/" + outName + ".ci4.png").c_str(), width, height, 3, bmpRgb, width * 3); else if (type == TextureType::Palette8bpp) - stbi_write_png((outFolder + "/" + outName + ".ci8.png").c_str(), width, height, 3, bmpRgb, + stbi_write_png((outPath + "/" + outName + ".ci8.png").c_str(), width, height, 3, bmpRgb, width * 3); // if (outName != name && outName != "") // File::WriteAllText(outFolder + "/" + outName + ".cfg", name.c_str()); } -// HOTSPOT string ZTexture::GetSourceOutputCode(const std::string& prefix) { sourceOutput = ""; - // sprintf(line, "%s:\n", name.c_str()); - // sourceOutput += line; - - // TODO: TEMP relativePath = "build/assets/" + relativePath; FixRawData(); - // sourceOutput += StringHelper::Sprintf("u64 %s[] = \n{\n", name.c_str()); - uint8_t* rawDataArr = rawData.data(); for (size_t i = 0; i < rawData.size(); i += 8) { if (i % 32 == 0) - sourceOutput += "\t"; + sourceOutput += " "; sourceOutput += StringHelper::Sprintf("0x%016llX, ", BitConverter::ToUInt64BE(rawDataArr, i)); @@ -836,11 +842,6 @@ string ZTexture::GetSourceOutputCode(const std::string& prefix) sourceOutput += StringHelper::Sprintf(" // 0x%06X \n", rawDataIndex + ((i / 32) * 32)); } - // Ensure there's always a trailing line feed to prevent dumb warnings. - sourceOutput += "\n"; - - // sourceOutput += "};\n"; - return sourceOutput; } @@ -854,9 +855,16 @@ ZResourceType ZTexture::GetResourceType() return ZResourceType::Texture; } +std::string ZTexture::GetSourceTypeName() +{ + return "u64"; +} + void ZTexture::CalcHash() { - hash = 0; + hash = CRC32B(rawData.data(), GetRawDataSize()); + // File::WriteAllText(StringHelper::Sprintf("%s/%s.txt", Globals::Instance->outputPath.c_str(), + // outName.c_str()), StringHelper::Sprintf("%08lX", hash)); hash = 0; } std::string ZTexture::GetExternalExtension() |
