summaryrefslogtreecommitdiff
path: root/ZAPD/ZRoom/Commands/SetMesh.cpp
diff options
context:
space:
mode:
authorAnghelo Carvajal <angheloalf95@gmail.com>2021-04-15 07:49:28 -0400
committerGitHub <noreply@github.com>2021-04-15 07:49:28 -0400
commit179af7d11fd27b046edfb1bbadade865033583d7 (patch)
tree316a55e5c0857e26184d6758a324483e029e8849 /ZAPD/ZRoom/Commands/SetMesh.cpp
parent96ffc1e62720f0f43eb4885e6e7dbaf76a2f6cd2 (diff)
Rename "Prerender" to "Background", properly extract PolygonType1 multi format and change some structs used. (#107)
* Extract BgImage Signed-off-by: angie <angheloalf95@gmail.com> * improve the output a bit Signed-off-by: angie <angheloalf95@gmail.com> * extract backgrounds for multipoly Signed-off-by: angie <angheloalf95@gmail.com> * Extract the dlists of PolygonDlist Signed-off-by: angie <angheloalf95@gmail.com> * commands in the next line Signed-off-by: angie <angheloalf95@gmail.com> * Move a lot of repeated code into a class Signed-off-by: angie <angheloalf95@gmail.com> * Use PolygonDlist instead of MeshEntry0 Signed-off-by: angie <angheloalf95@gmail.com> * Rename prerender to background Signed-off-by: angie <angheloalf95@gmail.com> * format Signed-off-by: angie <angheloalf95@gmail.com> * last cleanup Signed-off-by: angie <angheloalf95@gmail.com> * Handle jpg padding Signed-off-by: angie <angheloalf95@gmail.com> * Add a few checks and warnings for jpeg validity Signed-off-by: Angie <angheloalf95@gmail.com> * typo Signed-off-by: Angie <angheloalf95@gmail.com> * fix merge error Signed-off-by: angie <angheloalf95@gmail.com> * Fix background extraction that somehow was broken Signed-off-by: angie <angheloalf95@gmail.com> * re-add the trailling line feed Signed-off-by: angie <angheloalf95@gmail.com> * add trailing line feed again Signed-off-by: angie <angheloalf95@gmail.com> * fix an already fixed warning Signed-off-by: angie <angheloalf95@gmail.com> * fix crash when no config file is provided * run format
Diffstat (limited to 'ZAPD/ZRoom/Commands/SetMesh.cpp')
-rw-r--r--ZAPD/ZRoom/Commands/SetMesh.cpp716
1 files changed, 432 insertions, 284 deletions
diff --git a/ZAPD/ZRoom/Commands/SetMesh.cpp b/ZAPD/ZRoom/Commands/SetMesh.cpp
index cd5f6b9..1a8c99e 100644
--- a/ZAPD/ZRoom/Commands/SetMesh.cpp
+++ b/ZAPD/ZRoom/Commands/SetMesh.cpp
@@ -5,7 +5,7 @@
#include "../../StringHelper.h"
#include "../../ZFile.h"
#include "../ZRoom.h"
-#include "ZPrerender.h"
+#include "ZBackground.h"
using namespace std;
@@ -17,19 +17,17 @@ SetMesh::SetMesh(ZRoom* nZRoom, std::vector<uint8_t> rawData, int rawDataIndex,
segmentOffset = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, rawDataIndex + 4));
string declaration = "";
- int8_t meshHeaderType = rawData[segmentOffset + 0];
+ meshHeaderType = rawData[segmentOffset + 0];
if (meshHeaderType == 0)
{
- MeshHeader0* meshHeader0 = new MeshHeader0();
- meshHeader0->headerType = 0;
- meshHeader0->entries = vector<MeshEntry0*>();
-
- meshHeader0->dListStart = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, segmentOffset + 4));
- meshHeader0->dListEnd = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, segmentOffset + 8));
+ uint32_t dListStart = Seg2Filespace(BitConverter::ToInt32BE(rawData, segmentOffset + 4),
+ zRoom->parent->baseAddress);
+ uint32_t dListEnd = Seg2Filespace(BitConverter::ToInt32BE(rawData, segmentOffset + 8),
+ zRoom->parent->baseAddress);
int8_t numEntries = rawData[segmentOffset + 1];
- uint32_t currentPtr = meshHeader0->dListStart;
+ uint32_t currentPtr = dListStart;
// Hack for Syotes
for (int i = 0; i < abs(segAddressOffset); i++)
@@ -38,220 +36,80 @@ SetMesh::SetMesh(ZRoom* nZRoom, std::vector<uint8_t> rawData, int rawDataIndex,
segmentOffset--;
}
- for (int i = 0; i < numEntries; i++)
+ if (numEntries > 0)
{
- MeshEntry0* entry = new MeshEntry0();
- entry->opaqueDListAddr = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 0));
- entry->translucentDListAddr =
- GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 4));
+ std::string polyGfxBody = "";
+ std::string polyGfxType = "";
+ int polyGfxSize = 0;
- if (entry->opaqueDListAddr != 0)
+ for (int i = 0; i < numEntries; i++)
{
- entry->opaqueDList = new ZDisplayList(
- rawData, entry->opaqueDListAddr,
- ZDisplayList::GetDListLength(rawData, entry->opaqueDListAddr,
- Globals::Instance->game == ZGame::OOT_SW97 ?
- DListType::F3DEX :
- DListType::F3DZEX),
- zRoom->parent);
- entry->opaqueDList->scene = zRoom->scene;
- GenDListDeclarations(rawData, entry->opaqueDList);
- }
+ PolygonDlist polyGfxList(zRoom->GetName(), rawData, currentPtr, zRoom->parent,
+ zRoom);
+ if (polyGfxList.opaDList != nullptr)
+ {
+ GenDListDeclarations(rawData, polyGfxList.opaDList);
+ }
+ if (polyGfxList.xluDList != nullptr)
+ {
+ GenDListDeclarations(rawData, polyGfxList.xluDList);
+ }
+ polyGfxBody += polyGfxList.GetBodySourceCode(true);
+ polyGfxType = polyGfxList.GetSourceTypeName();
+ polyGfxSize = polyGfxList.GetRawDataSize();
- if (entry->translucentDListAddr != 0)
- {
- entry->translucentDList = new ZDisplayList(
- rawData, entry->translucentDListAddr,
- ZDisplayList::GetDListLength(rawData, entry->translucentDListAddr,
- Globals::Instance->game == ZGame::OOT_SW97 ?
- DListType::F3DEX :
- DListType::F3DZEX),
- zRoom->parent);
- entry->translucentDList->scene = zRoom->scene;
- GenDListDeclarations(rawData, entry->translucentDList);
+ currentPtr += polyGfxList.GetRawDataSize();
}
- meshHeader0->entries.push_back(entry);
-
- currentPtr += 8;
+ zRoom->parent->AddDeclarationArray(
+ dListStart, DeclarationAlignment::Align4, numEntries * polyGfxSize, polyGfxType,
+ StringHelper::Sprintf("%s%s0x%06X", zRoom->GetName().c_str(), polyGfxType.c_str(),
+ dListStart),
+ numEntries, polyGfxBody);
}
- declaration += StringHelper::Sprintf("{ 0 }, 0x%02X, ", meshHeader0->entries.size());
+ declaration = "\n ";
+ declaration += StringHelper::Sprintf("{ 0 }, 0x%02X, ", numEntries);
+ declaration += "\n ";
- if (meshHeader0->dListStart != 0)
- declaration += StringHelper::Sprintf("(u32)&%sMeshDListEntry0x%06X, ",
- zRoom->GetName().c_str(), meshHeader0->dListStart);
- else
- declaration += "0, ";
+ std::string entriesStr = "NULL";
- if (meshHeader0->dListEnd != 0)
- declaration += StringHelper::Sprintf(
- "(u32)&(%sMeshDListEntry0x%06X) + sizeof(%sMeshDListEntry0x%06X)",
- zRoom->GetName().c_str(), meshHeader0->dListStart, zRoom->GetName().c_str(),
- meshHeader0->dListStart);
+ if (dListStart != 0)
+ {
+ entriesStr = zRoom->parent->GetDeclaration(dListStart)->varName;
+ }
+ declaration += StringHelper::Sprintf("%s, ", entriesStr.c_str());
+ declaration += "\n ";
+
+ if (dListEnd != 0)
+ declaration += StringHelper::Sprintf("%s + ARRAY_COUNT(%s), ", entriesStr.c_str(),
+ entriesStr.c_str());
else
- declaration += "0";
+ declaration += "NULL, ";
+ declaration += "\n";
zRoom->parent->AddDeclaration(
segmentOffset, DeclarationAlignment::Align16, 12, "MeshHeader0",
StringHelper::Sprintf("%sMeshHeader0x%06X", zRoom->GetName().c_str(), segmentOffset),
declaration);
- declaration = "";
-
- for (size_t i = 0; i < meshHeader0->entries.size(); i++)
- {
- if (meshHeader0->entries[i]->opaqueDListAddr != 0)
- declaration +=
- StringHelper::Sprintf(" { (u32)%sDL_%06X, ", zRoom->GetName().c_str(),
- meshHeader0->entries[i]->opaqueDListAddr);
- else
- declaration += " { 0, ";
-
- if (meshHeader0->entries[i]->translucentDListAddr != 0)
- declaration +=
- StringHelper::Sprintf("(u32)%sDL_%06X },\n", zRoom->GetName().c_str(),
- meshHeader0->entries[i]->translucentDListAddr);
- else
- declaration += "0 },\n";
- }
-
- zRoom->parent->AddDeclarationArray(
- meshHeader0->dListStart, DeclarationAlignment::None, DeclarationPadding::None,
- (meshHeader0->entries.size() * 8) + 0, "MeshEntry0",
- StringHelper::Sprintf("%sMeshDListEntry0x%06X", zRoom->GetName().c_str(),
- meshHeader0->dListStart),
- meshHeader0->entries.size(), declaration);
-
- zRoom->parent->AddDeclaration(meshHeader0->dListStart + (meshHeader0->entries.size() * 8) +
- 0,
- DeclarationAlignment::None, DeclarationPadding::Pad16, 4,
- "static s32", "terminatorMaybe", " 0x01000000 ");
-
- meshHeader = meshHeader0;
+ zRoom->parent->AddDeclaration(dListStart + (numEntries * 8), DeclarationAlignment::None,
+ DeclarationPadding::Pad16, 4, "static s32", "terminatorMaybe",
+ "0x01000000");
}
else if (meshHeaderType == 1)
{
- MeshHeader1Base* meshHeader1 = nullptr;
-
- uint8_t fmt = rawData[segmentOffset + 1];
-
- if (fmt == 1) // Single Format
+ PolygonType1 polygon1(zRoom->GetName().c_str(), rawData, segmentOffset, zRoom->parent,
+ zRoom);
+ if (polygon1.polyGfxList.opaDList != nullptr)
{
- MeshHeader1Single* headerSingle = new MeshHeader1Single();
- std::string headerSingleStr = StringHelper::Sprintf(
- "%sMeshHeader0x%06X", zRoom->GetName().c_str(), segmentOffset);
-
- headerSingle->headerType = 1;
- headerSingle->format = fmt;
- headerSingle->entryRecord =
- BitConverter::ToInt32BE(rawData, segmentOffset + 4); // &0x00FFFFFF;
-
- headerSingle->imagePtr =
- BitConverter::ToInt32BE(rawData, segmentOffset + 8); // &0x00FFFFFF;
- headerSingle->unknown = BitConverter::ToInt32BE(rawData, segmentOffset + 12);
- headerSingle->unknown2 = BitConverter::ToInt32BE(rawData, segmentOffset + 16);
- headerSingle->bgWidth = BitConverter::ToInt16BE(rawData, segmentOffset + 20);
- headerSingle->bgHeight = BitConverter::ToInt16BE(rawData, segmentOffset + 22);
- headerSingle->imageFormat = rawData[segmentOffset + 24];
- headerSingle->imageSize = rawData[segmentOffset + 25];
- headerSingle->imagePal = BitConverter::ToInt16BE(rawData, segmentOffset + 26);
- headerSingle->imageFlip = BitConverter::ToInt16BE(rawData, segmentOffset + 28);
-
- declaration = "\n";
- std::string entryRecordStr = "NULL";
- if (headerSingle->entryRecord != 0)
- {
- uint32_t entryRecordAddress =
- Seg2Filespace(headerSingle->entryRecord, zRoom->parent->baseAddress);
- Declaration* decl = zRoom->parent->GetDeclaration(entryRecordAddress);
-
- if (decl == nullptr)
- {
- PolygonDlist* gfxList = new PolygonDlist(headerSingleStr, rawData,
- entryRecordAddress, zRoom->parent);
- gfxList->DeclareAndGenerateOutputCode();
- entryRecordStr = "&" + gfxList->GetName();
- }
- else
- {
- entryRecordStr = "&" + decl->varName;
- }
- }
- declaration +=
- StringHelper::Sprintf(" { { 1 }, 1, %s }, \n", entryRecordStr.c_str());
-
- std::string imagePtrStr = "NULL";
- if (headerSingle->imagePtr != 0)
- {
- uint32_t imagePtrAddress =
- Seg2Filespace(headerSingle->imagePtr, zRoom->parent->baseAddress);
- Declaration* decl = zRoom->parent->GetDeclaration(imagePtrAddress);
-
- if (decl == nullptr)
- {
- ZPrerender* prerender =
- new ZPrerender(headerSingleStr, rawData, imagePtrAddress, zRoom->parent);
- prerender->DeclareVar(headerSingleStr, "");
- zRoom->parent->resources.push_back(prerender);
- imagePtrStr = prerender->GetName();
- }
- else
- {
- imagePtrStr = decl->varName;
- }
- }
- declaration += StringHelper::Sprintf(" %s, \n", imagePtrStr.c_str());
-
- declaration += StringHelper::Sprintf(" 0x%06X, 0x%06X, \n", headerSingle->unknown,
- headerSingle->unknown2);
- declaration += StringHelper::Sprintf(" %i, %i, %i, %i, %i, %i\n",
- headerSingle->bgWidth, headerSingle->bgHeight,
- headerSingle->imageFormat, headerSingle->imageSize,
- headerSingle->imagePal, headerSingle->imageFlip);
-
- zRoom->parent->AddDeclaration(segmentOffset, DeclarationAlignment::None,
- DeclarationPadding::Pad16, 0x1E, "MeshHeader1Single",
- headerSingleStr, declaration);
-
- meshHeader1 = headerSingle;
+ GenDListDeclarations(rawData, polygon1.polyGfxList.opaDList);
}
- else if (fmt == 2) // Multi-Format
+ if (polygon1.polyGfxList.xluDList != nullptr)
{
- MeshHeader1Multi* headerMulti = new MeshHeader1Multi();
-
- headerMulti->headerType = 1;
- headerMulti->format = fmt;
- headerMulti->entryRecord =
- BitConverter::ToInt32BE(rawData, segmentOffset + 4); // &0x00FFFFFF;
-
- headerMulti->bgCnt = rawData[segmentOffset + 8];
- headerMulti->bgRecordPtr = BitConverter::ToInt32BE(rawData, segmentOffset + 12);
-
- declaration += StringHelper::Sprintf("{ { 1 }, 2, 0x%06X }, 0x%06X, 0x%06X",
- headerMulti->entryRecord, headerMulti->bgCnt,
- headerMulti->bgRecordPtr);
-
- zRoom->parent->AddDeclaration(segmentOffset, DeclarationAlignment::None,
- DeclarationPadding::Pad16, 16, "MeshHeader1Multi",
- StringHelper::Sprintf("%sMeshHeader0x%06X",
- zRoom->GetName().c_str(),
- segmentOffset),
- declaration);
-
- meshHeader1 = headerMulti;
+ GenDListDeclarations(rawData, polygon1.polyGfxList.xluDList);
}
- else // UH OH
- {
- if (Globals::Instance->verbosity >= VERBOSITY_INFO)
- fprintf(stderr, "WARNING: MeshHeader FMT %i not implemented!\n", fmt);
- }
-
- meshHeader1->headerType = 1;
- meshHeader1->format = fmt;
- meshHeader1->entryRecord = BitConverter::ToInt32BE(rawData, segmentOffset + 4) & 0x00FFFFFF;
-
- meshHeader = meshHeader1;
+ polygon1.DeclareAndGenerateOutputCode();
}
else if (meshHeaderType == 2)
{
@@ -273,9 +131,9 @@ SetMesh::SetMesh(ZRoom* nZRoom, std::vector<uint8_t> rawData, int rawDataIndex,
entry->playerXMin = BitConverter::ToInt16BE(rawData, currentPtr + 4);
entry->playerZMin = BitConverter::ToInt16BE(rawData, currentPtr + 6);
- entry->opaqueDListAddr = BitConverter::ToInt32BE(rawData, currentPtr + 8) & 0x00FFFFFF;
+ entry->opaqueDListAddr = GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 8));
entry->translucentDListAddr =
- BitConverter::ToInt32BE(rawData, currentPtr + 12) & 0x00FFFFFF;
+ GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr + 12));
if (entry->opaqueDListAddr != 0)
{
@@ -323,6 +181,7 @@ SetMesh::SetMesh(ZRoom* nZRoom, std::vector<uint8_t> rawData, int rawDataIndex,
meshHeader2->dListStart);
else
declaration += "0";
+ declaration += "\n";
zRoom->parent->AddDeclaration(
segmentOffset, DeclarationAlignment::None, 12, "MeshHeader2",
@@ -393,8 +252,7 @@ void SetMesh::GenDListDeclarations(std::vector<uint8_t> rawData, ZDisplayList* d
if (Globals::Instance->game == ZGame::MM_RETAIL)
alignment = DeclarationAlignment::None;
zRoom->parent->AddDeclarationArray(
- vtxEntry.first, alignment,
- dList->vertices[vtxEntry.first].size() * 16, "static Vtx",
+ vtxEntry.first, alignment, dList->vertices[vtxEntry.first].size() * 16, "static Vtx",
StringHelper::Sprintf("%sVtx_%06X", zRoom->GetName().c_str(), vtxEntry.first),
dList->vertices[vtxEntry.first].size(), vtxEntry.second);
}
@@ -431,83 +289,18 @@ string SetMesh::GenerateSourceCodePass1(string roomName, int baseAddress)
{
string sourceOutput = "";
- sourceOutput +=
- StringHelper::Sprintf("%s %i, (u32)&%sMeshHeader0x%06X",
- ZRoomCommand::GenerateSourceCodePass1(roomName, baseAddress).c_str(),
- data, zRoom->GetName().c_str(), segmentOffset);
+ Declaration* decl = zRoom->parent->GetDeclaration(segmentOffset);
- /*if (meshHeader->headerType == 0)
- {
- MeshHeader0* meshHeader0 = (MeshHeader0*)meshHeader;
+ sourceOutput += StringHelper::Sprintf(
+ "%s %i, &%s", ZRoomCommand::GenerateSourceCodePass1(roomName, baseAddress).c_str(), data,
+ decl->varName.c_str());
- }
- else
- {
- sourceOutput += "// SetMesh UNIMPLEMENTED HEADER TYPE!\n";
- }
-*/
return sourceOutput;
}
string SetMesh::GenerateExterns()
{
- string sourceOutput = "";
-
- if (meshHeader->headerType == 0)
- {
- MeshHeader0* meshHeader0 = (MeshHeader0*)meshHeader;
-
- sourceOutput += StringHelper::Sprintf("extern MeshHeader0 %sMeshHeader0x%06X;\n",
- zRoom->GetName().c_str(), segmentOffset);
- sourceOutput += StringHelper::Sprintf("extern MeshEntry0 %sMeshDListEntry0x%06X[%i];\n",
- zRoom->GetName().c_str(), meshHeader0->dListStart,
- meshHeader0->entries.size());
-
- for (MeshEntry0* entry : meshHeader0->entries)
- {
- if (entry->opaqueDList != nullptr)
- sourceOutput += GenDListExterns(entry->opaqueDList);
-
- if (entry->translucentDList != nullptr)
- sourceOutput += GenDListExterns(entry->translucentDList);
- }
- }
- else if (meshHeader->headerType == 1)
- {
- MeshHeader1Base* meshHeader1 = (MeshHeader1Base*)meshHeader;
-
- if (meshHeader1->format == 1)
- sourceOutput += StringHelper::Sprintf("extern MeshHeader1Single %sMeshHeader0x%06X;\n",
- zRoom->GetName().c_str(), segmentOffset);
- else if (meshHeader1->format == 2)
- sourceOutput += StringHelper::Sprintf("extern MeshHeader1Multi %sMeshHeader0x%06X;\n",
- zRoom->GetName().c_str(), segmentOffset);
- }
- else if (meshHeader->headerType == 2)
- {
- MeshHeader2* meshHeader2 = (MeshHeader2*)meshHeader;
-
- sourceOutput += StringHelper::Sprintf("extern MeshHeader2 %sMeshHeader0x%06X;\n",
- zRoom->GetName().c_str(), segmentOffset);
- sourceOutput += StringHelper::Sprintf("extern MeshEntry2 %sMeshDListEntry0x%06X[%i];\n",
- zRoom->GetName().c_str(), meshHeader2->dListStart,
- meshHeader2->entries.size());
-
- for (MeshEntry2* entry : meshHeader2->entries)
- {
- if (entry->opaqueDList != nullptr)
- sourceOutput += GenDListExterns(entry->opaqueDList);
-
- if (entry->translucentDList != nullptr)
- sourceOutput += GenDListExterns(entry->translucentDList);
- }
- }
- else
- {
- // sourceOutput += "// SetMesh UNIMPLEMENTED HEADER TYPE!\n";
- }
-
- return sourceOutput;
+ return "";
}
int32_t SetMesh::GetRawDataSize()
@@ -526,21 +319,19 @@ RoomCommand SetMesh::GetRoomCommand()
}
PolygonDlist::PolygonDlist(const std::string& prefix, const std::vector<uint8_t>& nRawData,
- int nRawDataIndex, ZFile* nParent)
+ int nRawDataIndex, ZFile* nParent, ZRoom* nRoom)
{
rawData.assign(nRawData.begin(), nRawData.end());
rawDataIndex = nRawDataIndex;
parent = nParent;
+ room = nRoom;
name = GetDefaultName(prefix.c_str(), rawDataIndex);
ParseRawData();
- // TODO: ZAPD refuses to extract this DList for some reason.
- // (not even a crash/error, it just simply doesn't do it).
- // For now, it will just export the address.
- // opaDList = MakeDlist(opa, prefix);
- // xluDList = MakeDlist(xlu, prefix);
+ opaDList = MakeDlist(opa, prefix);
+ xluDList = MakeDlist(xlu, prefix);
}
void PolygonDlist::ParseRawData()
@@ -565,9 +356,9 @@ ZDisplayList* PolygonDlist::MakeDlist(segptr_t ptr, const std::string& prefix)
string dListStr = StringHelper::Sprintf("%sPolygonDlist_%06X", prefix.c_str(), dlistAddress);
dlist->SetName(dListStr);
- dlist->GetSourceOutputCode(prefix + "PolygonDlist");
+ dlist->scene = room->scene;
+ dlist->GetSourceOutputCode(prefix);
- // parent->resources.push_back(dlist);
return dlist;
}
@@ -587,11 +378,19 @@ void PolygonDlist::DeclareVar(const std::string& prefix, const std::string& body
GetSourceTypeName(), auxName, bodyStr);
}
-std::string PolygonDlist::GetBodySourceCode()
+std::string PolygonDlist::GetBodySourceCode(bool arrayElement)
{
- std::string bodyStr = "\n";
+ std::string bodyStr = "";
std::string opaStr = "NULL";
std::string xluStr = "NULL";
+ if (arrayElement)
+ {
+ bodyStr += " { \n ";
+ }
+ else
+ {
+ bodyStr += "\n";
+ }
if (opa != 0)
{
@@ -619,14 +418,24 @@ std::string PolygonDlist::GetBodySourceCode()
}
bodyStr += StringHelper::Sprintf(" %s, \n", opaStr.c_str());
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+
bodyStr += StringHelper::Sprintf(" %s, \n", xluStr.c_str());
+ if (arrayElement)
+ {
+ bodyStr += " },";
+ }
+
return bodyStr;
}
void PolygonDlist::DeclareAndGenerateOutputCode()
{
- std::string bodyStr = GetBodySourceCode();
+ std::string bodyStr = GetBodySourceCode(false);
Declaration* decl = parent->GetDeclaration(rawDataIndex);
if (decl == nullptr)
@@ -653,3 +462,342 @@ std::string PolygonDlist::GetName()
{
return name;
}
+
+BgImage::BgImage(bool nIsSubStruct, const std::string& prefix, const std::vector<uint8_t>& nRawData,
+ int nRawDataIndex, ZFile* nParent)
+{
+ rawData.assign(nRawData.begin(), nRawData.end());
+ rawDataIndex = nRawDataIndex;
+ parent = nParent;
+ isSubStruct = nIsSubStruct;
+
+ name = GetDefaultName(prefix.c_str(), rawDataIndex);
+
+ ParseRawData();
+ sourceBackground = MakeBackground(source, prefix);
+}
+
+void BgImage::ParseRawData()
+{
+ size_t pad = 0x00;
+ if (!isSubStruct)
+ {
+ pad = 0x04;
+
+ unk_00 = BitConverter::ToUInt16BE(rawData, rawDataIndex + 0x00);
+ id = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x02);
+ }
+ source = BitConverter::ToUInt32BE(rawData, rawDataIndex + pad + 0x00);
+ unk_0C = BitConverter::ToUInt32BE(rawData, rawDataIndex + pad + 0x04);
+ tlut = BitConverter::ToUInt32BE(rawData, rawDataIndex + pad + 0x08);
+ width = BitConverter::ToUInt16BE(rawData, rawDataIndex + pad + 0x0C);
+ height = BitConverter::ToUInt16BE(rawData, rawDataIndex + pad + 0x0E);
+ fmt = BitConverter::ToUInt8BE(rawData, rawDataIndex + pad + 0x10);
+ siz = BitConverter::ToUInt8BE(rawData, rawDataIndex + pad + 0x11);
+ mode0 = BitConverter::ToUInt16BE(rawData, rawDataIndex + pad + 0x12);
+ tlutCount = BitConverter::ToUInt16BE(rawData, rawDataIndex + pad + 0x14);
+}
+
+ZBackground* BgImage::MakeBackground(segptr_t ptr, const std::string& prefix)
+{
+ if (ptr == 0)
+ {
+ return nullptr;
+ }
+
+ uint32_t backAddress = Seg2Filespace(ptr, parent->baseAddress);
+
+ ZBackground* background = new ZBackground(prefix, rawData, backAddress, parent);
+ background->DeclareVar(prefix, "");
+ parent->resources.push_back(background);
+
+ return background;
+}
+
+int BgImage::GetRawDataSize()
+{
+ return 0x1C;
+}
+
+std::string BgImage::GetBodySourceCode(bool arrayElement)
+{
+ std::string bodyStr = "";
+ if (arrayElement)
+ {
+ bodyStr += " { \n ";
+ }
+
+ if (!isSubStruct)
+ {
+ bodyStr += StringHelper::Sprintf("0x%04X, ", unk_00);
+ bodyStr += StringHelper::Sprintf("%i, ", id);
+ bodyStr += "\n ";
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+ }
+
+ std::string backgroundName = "NULL";
+ if (source != 0)
+ {
+ uint32_t address = Seg2Filespace(source, parent->baseAddress);
+ Declaration* decl = parent->GetDeclaration(address);
+
+ if (decl == nullptr)
+ {
+ backgroundName += StringHelper::Sprintf("0x%08X, ", source);
+ }
+ else
+ {
+ backgroundName = decl->varName;
+ }
+ }
+ bodyStr += StringHelper::Sprintf("%s, ", backgroundName.c_str());
+ bodyStr += "\n ";
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+
+ bodyStr += StringHelper::Sprintf("0x%08X, ", unk_0C);
+ bodyStr += StringHelper::Sprintf("0x%08X, ", tlut);
+ bodyStr += "\n ";
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+
+ bodyStr += StringHelper::Sprintf("%i, ", width);
+ bodyStr += StringHelper::Sprintf("%i, ", height);
+ bodyStr += "\n ";
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+
+ bodyStr += StringHelper::Sprintf("%i, ", fmt);
+ bodyStr += StringHelper::Sprintf("%i, ", siz);
+ bodyStr += "\n ";
+ if (arrayElement)
+ {
+ bodyStr += " ";
+ }
+
+ bodyStr += StringHelper::Sprintf("0x%04X, ", mode0);
+ bodyStr += StringHelper::Sprintf("0x%04X, ", tlutCount);
+ if (arrayElement)
+ {
+ bodyStr += " \n }, ";
+ }
+
+ return bodyStr;
+}
+
+std::string BgImage::GetDefaultName(const std::string& prefix, uint32_t address)
+{
+ return StringHelper::Sprintf("%sBgImage_%06X", prefix.c_str(), address);
+}
+
+std::string BgImage::GetSourceTypeName()
+{
+ return "BgImage";
+}
+
+std::string BgImage::GetName()
+{
+ return name;
+}
+
+PolygonType1::PolygonType1(const std::string& prefix, const std::vector<uint8_t>& nRawData,
+ int nRawDataIndex, ZFile* nParent, ZRoom* nRoom)
+{
+ rawData.assign(nRawData.begin(), nRawData.end());
+ rawDataIndex = nRawDataIndex;
+ parent = nParent;
+
+ name = GetDefaultName(prefix.c_str(), rawDataIndex);
+
+ ParseRawData();
+
+ if (dlist != 0)
+ {
+ polyGfxList =
+ PolygonDlist(prefix, rawData, Seg2Filespace(dlist, parent->baseAddress), parent, nRoom);
+ }
+
+ uint32_t listAddress;
+ std::string bgImageArrayBody = "";
+ switch (format)
+ {
+ case 1:
+ single = BgImage(true, prefix, nRawData, nRawDataIndex + 0x08, parent);
+ break;
+
+ case 2:
+ if (list != 0)
+ {
+ listAddress = Seg2Filespace(list, parent->baseAddress);
+ for (size_t i = 0; i < count; ++i)
+ {
+ BgImage bg(false, prefix, rawData, listAddress + i * BgImage::GetRawDataSize(),
+ parent);
+ multiList.push_back(bg);
+ bgImageArrayBody += bg.GetBodySourceCode(true);
+ if (i + 1 < count)
+ {
+ bgImageArrayBody += "\n";
+ }
+ }
+
+ Declaration* decl = parent->GetDeclaration(listAddress);
+ if (decl == nullptr)
+ {
+ parent->AddDeclarationArray(
+ listAddress, DeclarationAlignment::Align4, count * BgImage::GetRawDataSize(),
+ BgImage::GetSourceTypeName(), multiList.at(0).GetName().c_str(), count,
+ bgImageArrayBody);
+ }
+ }
+ break;
+
+ default:
+ throw std::runtime_error(StringHelper::Sprintf(
+ "Error in PolygonType1::PolygonType1\n\t Unknown format: %i\n", format));
+ break;
+ }
+}
+
+void PolygonType1::ParseRawData()
+{
+ type = BitConverter::ToUInt8BE(rawData, rawDataIndex);
+ format = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x01);
+ dlist = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x04);
+
+ if (format == 2)
+ {
+ count = BitConverter::ToUInt8BE(rawData, rawDataIndex + 0x08);
+ list = BitConverter::ToUInt32BE(rawData, rawDataIndex + 0x0C);
+ }
+}
+
+int PolygonType1::GetRawDataSize()
+{
+ switch (format)
+ {
+ case 1:
+ return 0x20;
+
+ case 2:
+ return 0x10;
+ }
+ return 0x20;
+}
+
+void PolygonType1::DeclareVar(const std::string& prefix, const std::string& bodyStr)
+{
+ std::string auxName = name;
+ if (name == "")
+ {
+ auxName = GetDefaultName(prefix, rawDataIndex);
+ }
+ parent->AddDeclaration(rawDataIndex, DeclarationAlignment::Align4, GetRawDataSize(),
+ GetSourceTypeName(), auxName, bodyStr);
+}
+
+std::string PolygonType1::GetBodySourceCode()
+{
+ std::string bodyStr = "\n ";
+
+ bodyStr += "{ ";
+ bodyStr += StringHelper::Sprintf("%i, %i, ", type, format);
+
+ std::string dlistStr = "NULL";
+ if (dlist != 0)
+ {
+ uint32_t entryRecordAddress = Seg2Filespace(dlist, parent->baseAddress);
+ Declaration* decl = parent->GetDeclaration(entryRecordAddress);
+
+ if (decl == nullptr)
+ {
+ polyGfxList.DeclareAndGenerateOutputCode();
+ dlistStr = "&" + polyGfxList.GetName();
+ }
+ else
+ {
+ dlistStr = "&" + decl->varName;
+ }
+ }
+ bodyStr += StringHelper::Sprintf("%s, ", dlistStr.c_str());
+ bodyStr += "}, \n";
+
+ std::string listStr = "NULL";
+ // bodyStr += " { \n";
+ switch (format)
+ {
+ case 1:
+ bodyStr += " " + single.GetBodySourceCode(false) + "\n";
+ break;
+ case 2:
+ if (list != 0)
+ {
+ uint32_t listAddress = Seg2Filespace(list, parent->baseAddress);
+ Declaration* decl = parent->GetDeclaration(listAddress);
+ if (decl != nullptr)
+ {
+ listStr = decl->varName;
+ }
+ else
+ {
+ listStr = StringHelper::Sprintf("0x%08X", list);
+ }
+ }
+ bodyStr += StringHelper::Sprintf(" %i, %s, \n", count, listStr.c_str());
+ break;
+
+ default:
+ break;
+ }
+ // bodyStr += " } \n";
+
+ return bodyStr;
+}
+
+void PolygonType1::DeclareAndGenerateOutputCode()
+{
+ std::string bodyStr = GetBodySourceCode();
+
+ Declaration* decl = parent->GetDeclaration(rawDataIndex);
+ if (decl == nullptr)
+ {
+ DeclareVar("", bodyStr);
+ }
+ else
+ {
+ decl->text = bodyStr;
+ }
+}
+
+std::string PolygonType1::GetDefaultName(const std::string& prefix, uint32_t address)
+{
+ return StringHelper::Sprintf("%sPolygonType1_%06X", prefix.c_str(), address);
+}
+
+std::string PolygonType1::GetSourceTypeName()
+{
+ switch (format)
+ {
+ case 1:
+ return "MeshHeader1Single";
+
+ case 2:
+ return "MeshHeader1Multi";
+ }
+ return "ERROR";
+ // return "PolygonType1";
+}
+
+std::string PolygonType1::GetName()
+{
+ return name;
+}