/*********************/ /*** MkLevel Class ***/ /*********************/ #include "stdio.h" #include #include #include #include #include #include #include "MkLevel.h" #include "Layers\MkLevelLayer.h" #include "Layers\MkLevelLayerTile.h" #include "Layers\MkLevelLayer3d.h" #include "Layers\MkLevelLayerShade.h" #include "Layers\MkLevelLayerCollision.h" #include "Layers\MkLevelLayerActor.h" #include "Layers\MkLevelLayerItem.h" #include "Layers\MkLevelLayerPlatform.h" #include "Layers\MkLevelLayerFX.h" #include "Layers\MkLevelLayerTrigger.h" #include "Layers\MkLevelLayerHazard.h" //*************************************************************************** const GString ConfigFilename="MkLevel.ini"; sExpLayerTile BlankTile2d={-1,-1}; sExpLayerTile BlankTile3d={0,0}; //*************************************************************************** CMkLevel::CMkLevel() { memset(&LevelHdr,0,sizeof(sLevelHdr)); Tile2dList.Add(BlankTile2d); Tile3dList.Add(BlankTile3d); OutTile3dList.resize(1); OutTile3dList[0].TriCount=0; OutTile3dList[0].QuadCount=0; } //*************************************************************************** CMkLevel::~CMkLevel() { } //*************************************************************************** void CMkLevel::SetAppDir(const char *AppPath) { #ifdef _DEBUG AppDir="\\spongebob\\tools\\data\\bin\\"; #else GFName Path=AppPath; Path.File(""); Path.Ext(""); AppDir=Path.FullName(); #endif } //*************************************************************************** void CMkLevel::Init(const char *Filename,const char *OutFilename,const char *IncDir,int TPBase,int TPW,int TPH) { // Setup filenames and paths GFName Path; InFilename=Filename; Path=Filename; LevelName=Path.File(); Path.File(""); Path.Ext(""); InPath=Path.FullName(); Path=OutFilename; Path.Ext(""); LevelFullName=Path.File(); LevelFullName.Upper(); OutName=Path.FullName(); OutIncName=IncDir; OutIncName+=Path.File(); OutIncName+="_INF.h"; // Load ini file Config.LoadAndImport(GString(AppDir+ConfigFilename)); // Setup Texgrab if (TPBase==-1 || TPW==-1 || TPH==-1) GObject::Error(ERR_FATAL,"TPage not set\n"); TexGrab.SetTPage(TPBase,TPW,TPH); TexGrab.SetOutFile(GString(OutName+".Tex")); TexGrab.SetDebug(DebugOn); TexGrab.SetDebugOut(GString(OutName+".Lbm")); TexGrab.NoSort(); TexGrab.AnimatedHeadersOnly(true); TexGrab.DontOutputBoxes(true); //!!! TexGrab.AllowRotate(true); TexGrab.AllowRotate(false); //!!! TexGrab.ShrinkToFit(true); TexGrab.ShrinkToFit(false); // Setup TriList OutFaceList.SetTexGrab(TexGrab); // Set up other stuph FlatFace[0].vtx[0].x=+0.5f; FlatFace[0].vtx[0].y=+1.0f; FlatFace[0].vtx[0].z=-4.0f; FlatFace[0].vtx[1].x=-0.5f; FlatFace[0].vtx[1].y= 0.0f; FlatFace[0].vtx[1].z=-4.0f; FlatFace[0].vtx[2].x=+0.5f; FlatFace[0].vtx[2].y= 0.0f; FlatFace[0].vtx[2].z=-4.0f; FlatFace[0].uv[0][0]=1; FlatFace[0].uv[0][1]=1; FlatFace[0].uv[1][0]=0; FlatFace[0].uv[1][1]=0; FlatFace[0].uv[2][0]=1; FlatFace[0].uv[2][1]=0; FlatFace[0].Flags=0; FlatFace[1].vtx[0].x=-0.5f; FlatFace[1].vtx[0].y= 0.0f; FlatFace[1].vtx[0].z=-4.0f; FlatFace[1].vtx[1].x=+0.5f; FlatFace[1].vtx[1].y=+1.0f; FlatFace[1].vtx[1].z=-4.0f; FlatFace[1].vtx[2].x=-0.5f; FlatFace[1].vtx[2].y=+1.0f; FlatFace[1].vtx[2].z=-4.0f; FlatFace[1].uv[0][0]=0; FlatFace[1].uv[0][1]=0; FlatFace[1].uv[1][0]=1; FlatFace[1].uv[1][1]=1; FlatFace[1].uv[2][0]=0; FlatFace[1].uv[2][1]=1; FlatFace[1].Flags=0; } //*************************************************************************** int CMkLevel::AddModel(GString &Filename) { GFName Path=Filename; sMkLevelModel ThisModel; int Idx; CScene Scene; ThisModel.Name=GFName(Filename).File(); Idx=ModelList.Find(ThisModel); if (Idx!=-1) { return(Idx); } Idx=ModelList.size(); Path.File(""); Path.Ext(""); GString RootPath=Path.FullName(); // Load Model and add int TriStart=ModelFaceList.GetFaceCount();; Scene.Load(Filename); BuildModel(Scene,RootPath,0); ThisModel.TriStart=TriStart; ThisModel.TriCount=ModelFaceList.GetFaceCount()-TriStart; ModelList.Add(ThisModel); return(Idx); } //*************************************************************************** void CMkLevel::BuildModel(CScene &Scene,GString &RootPath,int Node) { CNode &ThisNode=Scene.GetNode(Node); vector const &NodeTriList = ThisNode.GetTris(); vector const &NodeVtxList = ThisNode.GetPts(); vector const &NodeMatList = ThisNode.GetTriMaterial(); vector const &NodeUVList = ThisNode.GetUVTris(); vector const &SceneTexList= Scene.GetTexList(); vector const &SceneUsedMatList=Scene.GetUsedMaterialIdx(); vector const &SceneMaterials=Scene.GetMaterials(); int TriCount=NodeTriList.size(); for (int T=0; TSceneTexList.size()) GObject::Error(ERR_FATAL,"Crap Material ID, wanted %i, only have %i\n",Mat,SceneTexList.size()); GString TexName=RootPath+SceneTexList[Mat]; CFace &F=ModelFaceList.AddFace( NodeVtxList, NodeTriList[T], NodeUVList[T], TexName,SceneMaterials[Mat].Flags,false); } int ChildCount=ThisNode.GetPruneChildCount(); for (int Loop=0;Loop &List,char *TexPtr,int Count) { char FullName[1024]; GString FilePath; for (int i=0; iTileW; TileH=FileHdr->TileH; RGBSize=TileW*TileH*3; // Load SetNames LoadStrList(InSetNameList,(char*) &ByteHdr[FileHdr->SetNameOfs],FileHdr->SetNameCount); // Load TexNames LoadStrList(InTexNameList,(char*) &ByteHdr[FileHdr->TexNameOfs],FileHdr->TexNameCount); // Load Tiles u8 *TilePtr=(u8*) &ByteHdr[FileHdr->TileOfs]; InTileList.resize(FileHdr->TileCount); for (i=0; iTileCount; i++) { sExpTile *ThisTilePtr=(sExpTile*)TilePtr; sExpTile &InTile=InTileList[i]; InTile=*ThisTilePtr; // Skip RGB Image, not needed (and too buggy) // InTile.RGB=(u8*)malloc(RGBSize); // memcpy(InTile.RGB,TilePtr+sizeof(sExpTile),RGBSize); InTile.RGB=0; TilePtr+=RGBSize+sizeof(sExpTile); } // Load Tris sExpTri *TriPtr=(sExpTri*) &ByteHdr[FileHdr->TriOfs]; InTriList.resize(FileHdr->TriCount); for (i=0; iTriCount; i++) { InTriList[i]=TriPtr[i]; } // Load 2d bitmaps ListSize=InSetNameList.size(); BmpList.resize(ListSize); for (i=0; iLayerCount; i++) { sExpLayerHdr *LayerHdr=(sExpLayerHdr *)&ByteHdr[FileHdr->LayerOfs[i]]; switch(LayerHdr->Type) { case LAYER_TYPE_TILE: switch (LayerHdr->SubType) { case LAYER_SUBTYPE_ACTION: LayerList.push_back(new CMkLevelLayer3d(LayerHdr)); break; case LAYER_SUBTYPE_MID: LayerList.push_back(new CMkLevelLayerTile(LayerHdr)); break; case LAYER_SUBTYPE_BACK: default: break; } break; case LAYER_TYPE_COLLISION: LayerList.push_back(new CMkLevelLayerCollision(LayerHdr)); break; case LAYER_TYPE_SHADE: LayerList.push_back(new CMkLevelLayerShade(LayerHdr)); break; case LAYER_TYPE_ACTOR: LayerList.push_back(new CMkLevelLayerActor(LayerHdr)); break; case LAYER_TYPE_ITEM: LayerList.push_back(new CMkLevelLayerItem(LayerHdr)); break; case LAYER_TYPE_PLATFORM: LayerList.push_back(new CMkLevelLayerPlatform(LayerHdr)); break; case LAYER_TYPE_TRIGGER: LayerList.push_back(new CMkLevelLayerTrigger(LayerHdr)); break; case LAYER_TYPE_FX: LayerList.push_back(new CMkLevelLayerFX(LayerHdr)); break; case LAYER_TYPE_HAZARD: LayerList.push_back(new CMkLevelLayerHazard(LayerHdr)); break; default: GObject::Error(ERR_FATAL,"Unknown Layer Type\n"); } } } //*************************************************************************** //*** Process *************************************************************** //*************************************************************************** void CMkLevel::Process() { printf("PreProcess Layers\n"); PreProcessLayers(); printf("Process Models\n"); ProcessModels(); printf("Process Tilebank\n"); ProcessTileBanks(); printf("Process Layers\n"); ProcessLayers(); OutFaceList.Process(); } //*************************************************************************** void CMkLevel::PreProcessLayers() { int LayerCount=LayerList.size(); for (int i=0; iPreProcess(this); } } //*************************************************************************** void CMkLevel::ProcessLayers() { int LayerCount=LayerList.size(); for (int i=0; iProcess(this); } } //*************************************************************************** void CMkLevel::ProcessTileBanks() { PreProcessTileBank2d(); PreProcessTileBank3d(); TexGrab.Process(); ProcessTileBank2d(); ProcessTileBank3d(); } //*************************************************************************** void CMkLevel::PreProcessTileBank2d() { int i,ListSize=Tile2dList.size(); Tile2dList[0].Tile=-1; // Extract Tex data from list for (i=1; i &TexInfo=TexGrab.GetTexInfo(); OutTile2dList.resize(ListSize); for (i=1; iIsType(Type,SubType)) return(LayerList[i]); } return(0); } //*************************************************************************** int CMkLevel::Create3dTile(sExpLayerTile &InTile) { sExpTile &SrcTile=InTileList[InTile.Tile]; CFace F; int i,ListSize,p; CList SortList; CList ZPosList; sTile3d ThisTile; int TileID=OutTile3dList.size(); ThisTile.TriStart=OutFaceList.GetFaceCount(); ThisTile.QuadCount=0; ThisTile.QuadStart=0; if (SrcTile.TriCount) { ThisTile.TriCount=SrcTile.TriCount; for (i=0; iThisZPos) break; } SortList.insert(ListPos,ThisTri); ZPosList.insert(ListPos,ThisZPos); } } else { // create flat tile (This is WRONG, flip tex are gen, need to flip goem) // int TexID=Create2dTex(InTile.Tile,InTile.Flags); int TexID=Create2dTex(InTile.Tile,0); ThisTile.TriCount=2; for (int i=0; i<2; i++) { FlatFace[i].Flags=0; FlatFace[i].TexID=TexID; SortList.push_back(FlatFace[i]); } // InTile.Flags=0; } // Add sorted list to main list ListSize=SortList.size(); for (i=0; iBmpW) { printf("AARGH!!! %s(%i) wants X=%i,tile is only %i Wide\n",Name,SrcTile.Set,SrcTile.XOfs*16,BmpW); SrcTile.XOfs=0; } if (SrcTile.YOfs*16>BmpH) { printf("AARGH!!! %s(%i) wants Y=%i,tile is only %i High\n",Name,SrcTile.Set,SrcTile.YOfs*16,BmpH); SrcTile.YOfs=0; } MakeTexName(SrcTile,Flags,TexName); ThisRect.X=SrcTile.XOfs*16; ThisRect.Y=SrcTile.YOfs*16; ThisRect.W=16; ThisRect.H=16; ThisFrame.Grab(InFrame,ThisRect); if (Flags& PC_TILE_FLAG_MIRROR_X) ThisFrame.FlipX(); if (Flags & PC_TILE_FLAG_MIRROR_Y) ThisFrame.FlipY(); TexID=TexGrab.AddMemFrame(TexName,ThisFrame); #ifdef _DEBUG if (0) { if (!ThisFrame.IsBlank()) { char DbgName[256]; sprintf(DbgName,"/x/%s.lbm",TexName); ThisFrame.SaveLbm(DbgName); } } #endif return(TexID); } //*************************************************************************** void CMkLevel::MakeTexName(sExpTile &SrcTile,int Flags,GString &OutStr) { char NewName[256]; GString Name=GFName(InSetNameList[SrcTile.Set]).File(); sprintf(NewName,"%s_%02d_%02d_%01d_",Name,SrcTile.XOfs,SrcTile.YOfs,Flags&PC_TILE_FLAG_MIRROR_XY); OutStr=NewName; } //*************************************************************************** /* int CMkLevel::FindRGBMatch(sMkLevelTex &ThisTex) { int i,ListSize=Tex2dList.size(); int Size=TileW*TileH; u8 *RGBPtr=ThisTex.RGB; if (!RGBPtr) printf("HA HA\n"); // Create Checksum for this tile ThisTex.RChk=0; ThisTex.GChk=0; ThisTex.BChk=0; for (i=0; i %i\n",MinOT,MaxOT); // VtxList LevelHdr.VtxList=(sVtx*)WriteVtxList(); // LevelHdr.VtxList=(sVtx*)OutFaceList.WriteVtxList(File); } //*************************************************************************** int ZMin=9999,ZMax=0; int CMkLevel::WriteTriList() { vector &TriList=OutFaceList.GetOutTriList(); vector const &VtxList=OutFaceList.GetVtxList(); int ThisPos=ftell(File); int i,ListSize=TriList.size(); int ZOfs=+4*Scale; for (i=0;iZ[p]) ZMin=Z[p]; if (ZMaxOtOfs) MinOT=OtOfs; if (MaxOT15) OtOfs=15; if (OtOfs<0) OtOfs=0; // if (OtOfs>15) OtOfs=15; T.OTOfs=OtOfs; // Write It fwrite(&T,1,sizeof(sTri),File); } printf("Tri %i\n",ListSize); printf("ZMin %i ZMax %i\n",ZMin,ZMax); return(ThisPos); } //*************************************************************************** int CMkLevel::WriteQuadList() { vector &QuadList=OutFaceList.GetOutQuadList(); vector const &VtxList=OutFaceList.GetVtxList(); int ThisPos=ftell(File); int i,ListSize=QuadList.size(); for (i=0;iOtOfs) MinOT=OtOfs; if (MaxOT63) OtOfs=63; // Write It fwrite(&Q,1,sizeof(sQuad),File); } printf("Quad %i\n",ListSize); return(ThisPos); } //*************************************************************************** int CMkLevel::WriteVtxList() { vector const &VtxList=OutFaceList.GetVtxList(); int i,ListSize=VtxList.size(); int Pos=ftell(File); //sVtx Ofs; // Ofs.vx=-0; // Ofs.vy=-0; // Ofs.vz=-4*Scale; for (i=0; iWrite(File,LayerName,LevelName); PadFile(File); return(Ofs); } //*************************************************************************** int CMkLevel::WriteThings(int Type,const char *LayerName) { CMkLevelLayer *ThisLayer=FindLayer(Type,LAYER_SUBTYPE_NONE); int Ofs; if (!ThisLayer) { GFName Name=InFilename; if (LayerName) GObject::Error(ERR_WARNING,"No %s Layer Found in %s!!\n",LayerName,Name.File()); return(0); } Ofs=ThisLayer->Write(File,LayerName,LevelName); // printf("%s %i\n",LayerName,Ofs); PadFile(File); return(Ofs); } //*************************************************************************** int CMkLevel::WriteModelList() { int i,ListSize=ModelList.size(); int Ofs=ftell(File); for (i=0; i%i,%i)\n",ThisModel.Name,i+1,ListSize,Out.TriCount,Out.BBox.XMin,Out.BBox.YMin,Out.BBox.XMax,Out.BBox.YMax); fwrite(&Out,1,sizeof(sModel),File); } return(Ofs); } //*************************************************************************** void CMkLevel::CalcModelBBox(sMkLevelModel &ThisModel,sBBox &BBox) { vector &TriList=OutFaceList.GetOutTriList(); vector const &VtxList=OutFaceList.GetVtxList(); int Vtx[3]; BBox.XMin=+32000; BBox.XMax=-32000; BBox.YMin=+32000; BBox.YMax=-32000; for (int T=0; T+ThisVtx.vx) BBox.XMin=+ThisVtx.vx; if (BBox.XMax<+ThisVtx.vx) BBox.XMax=+ThisVtx.vx; if (BBox.YMin>-ThisVtx.vy) BBox.YMin=-ThisVtx.vy; if (BBox.YMax<-ThisVtx.vy) BBox.YMax=-ThisVtx.vy; } } } //*************************************************************************** //*** Inf File ************************************************************** //*************************************************************************** void CMkLevel::AddInfItem(const char *Name,int Val) { sInfItem Item; GString ReplaceBadFileChars(GString s); // Dodgy extern from TexGrab Lib! Item.Name=ReplaceBadFileChars(Name); Item.Name.Upper(); Item.Val=Val; InfList.Add(Item); } //*************************************************************************** void CMkLevel::WriteIncFile() { GString DefStr; DefStr=LevelFullName+"_INF"; File=fopen(OutIncName,"wt"); fprintf(File,"// %s Info Header\n",LevelFullName); fprintf(File,"\n"); fprintf(File,"#ifndef\t__%s_INF_HEADER__\n",LevelFullName); fprintf(File,"#define\t__%s_INF_HEADER__\n",LevelFullName); fprintf(File,"\n"); fprintf(File,"\n"); fprintf(File,"enum\t%s\n",DefStr); fprintf(File,"{\n"); int ListSize=InfList.size(); for (int i=0; i