Converted tabs to four spaces

This commit is contained in:
Robert Osfield
2008-11-07 15:01:15 +00:00
parent 3455431129
commit 0ccf7d8383
4 changed files with 550 additions and 550 deletions

View File

@@ -38,10 +38,10 @@ trpgr_Archive::trpgr_Archive()
trpgr_Archive::~trpgr_Archive()
{
if (fp)
fclose(fp);
fclose(fp);
fp = NULL;
if (tileCache)
delete tileCache;
delete tileCache;
}
int32 trpgr_Archive::GetHeaderData(char *dataPtr, int length, FILE *filehandle)
@@ -65,30 +65,30 @@ bool trpgr_Archive::OpenFile(const char *name)
CloseFile();
if (!(fp = fopen(file,"rb")))
return false;
return false;
// Look for a magic # and endianness
int32 magic;
if (fread(&magic,sizeof(int32),1,fp) != 1)
return false;
return false;
headerRead = false;
// Figure out the endianness from the magic number
trpgEndian cpuNess = trpg_cpu_byte_order();
if (magic == GetMagicNumber()) {
ness = cpuNess;
return true;
ness = cpuNess;
return true;
}
if (trpg_byteswap_int(magic) == GetMagicNumber()) {
if (cpuNess == LittleEndian)
ness = BigEndian;
else
ness = LittleEndian;
return true;
if (cpuNess == LittleEndian)
ness = BigEndian;
else
ness = LittleEndian;
return true;
}
if (magic != GetMagicNumber())
return false;
return false;
// Not one of our files
return false;
@@ -106,7 +106,7 @@ trpgrImageHelper* trpgr_Archive::GetNewRImageHelper(trpgEndian ness,char *dir,co
int majorVer,minorVer;
GetHeader()->GetVersion(majorVer,minorVer);
if((majorVer >= TRPG_NOMERGE_VERSION_MAJOR) && (minorVer>=TRPG_NOMERGE_VERSION_MINOR)) {
separateGeo = true;
separateGeo = true;
}
return new trpgrImageHelper(ness,dir,matTable,texTable,separateGeo);
}
@@ -116,10 +116,10 @@ trpgrImageHelper* trpgr_Archive::GetNewRImageHelper(trpgEndian ness,char *dir,co
void trpgr_Archive::CloseFile()
{
if (fp)
fclose(fp);
fclose(fp);
fp = NULL;
if (tileCache)
delete tileCache;
delete tileCache;
tileCache = NULL;
}
@@ -140,53 +140,53 @@ bool trpgr_Archive::ReadSubArchive(int row, int col, trpgEndian cpuNess)
sprintf(blockpath,"%s%s%d%s%d%sarchive.txp",dir,PATHSEPERATOR,col,PATHSEPERATOR,row,PATHSEPERATOR);
FILE *bfp = fopen(blockpath,"rb");
if(!bfp) {
return false;
return false;
}
// Look for a magic # and endianness
int32 bmagic;
if (fread(&bmagic,sizeof(int32),1,bfp) != 1)
return false;
return false;
// The block archive will always be the same endianness as the master
if ( (bmagic != GetMagicNumber()) && (trpg_byteswap_int(bmagic) != GetMagicNumber()) )
return false;
return false;
int32 bheaderSize=0;
if (fread(&bheaderSize,sizeof(int32),1,bfp) != 1)
return false;
return false;
if (ness != cpuNess)
bheaderSize = trpg_byteswap_int(bheaderSize);
bheaderSize = trpg_byteswap_int(bheaderSize);
int bheadLen = bheaderSize;
if (bheadLen < 0)
return false;
return false;
// Read in the header whole
trpgMemReadBuffer bbuf(ness);
bbuf.SetLength(bheadLen);
char *bdata = bbuf.GetDataPtr();
if ((ret = GetHeaderData(bdata,bheadLen,bfp)) != bheadLen)
return false;
//keep track of where this came from in the master table.
return false;
//keep track of where this came from in the master table.
tileTable.SetCurrentBlock(row,col,true);
texTable.SetCurrentBlock(row,col);
bparser.AddCallback(TRPGHEADER,&blockHeader);
bparser.AddCallback(TRPGMATTABLE,&materialTable); // Went back to oldest style for 2.0
//if(!headerHasTexTable) {
bparser.AddCallback(TRPGTEXTABLE2,&texTable); // Added for 2.0
bparser.AddCallback(TRPGTEXTABLE2,&texTable); // Added for 2.0
//}
bparser.AddCallback(TRPGMODELTABLE,&modelTable);
bparser.AddCallback(TRPGLIGHTTABLE,&lightTable); // Added for 2.0
bparser.AddCallback(TRPGRANGETABLE,&rangeTable); // Added for 2.0
bparser.AddCallback(TRPG_TEXT_STYLE_TABLE,&textStyleTable); // Added for 2.1
bparser.AddCallback(TRPG_SUPPORT_STYLE_TABLE,&supportStyleTable);
bparser.AddCallback(TRPG_LABEL_PROPERTY_TABLE,&labelPropertyTable);
bparser.AddCallback(TRPGLIGHTTABLE,&lightTable); // Added for 2.0
bparser.AddCallback(TRPGRANGETABLE,&rangeTable); // Added for 2.0
bparser.AddCallback(TRPG_TEXT_STYLE_TABLE,&textStyleTable); // Added for 2.1
bparser.AddCallback(TRPG_SUPPORT_STYLE_TABLE,&supportStyleTable);
bparser.AddCallback(TRPG_LABEL_PROPERTY_TABLE,&labelPropertyTable);
// Don't read the tile table for v1.0 archives
// It's only really used for 2.0 archives
bparser.AddCallback(TRPGTILETABLE2,&tileTable);
// Parse the buffer
if (!bparser.Parse(bbuf))
return false;
return false;
//close the block archive
fclose(bfp);
@@ -207,7 +207,7 @@ bool trpgr_Archive::ReadHeader(bool readAllBlocks)
int ret;
if (!fp || headerRead)
return false;
return false;
headerRead = true;
@@ -215,19 +215,19 @@ bool trpgr_Archive::ReadHeader(bool readAllBlocks)
trpgEndian cpuNess = trpg_cpu_byte_order();
int32 headerSize;
if (fread(&headerSize,sizeof(int32),1,fp) != 1)
return false;
return false;
if (ness != cpuNess)
headerSize = trpg_byteswap_int(headerSize);
headerSize = trpg_byteswap_int(headerSize);
int headLen = headerSize;
if (headLen < 0)
return false;
return false;
// Read in the header whole
trpgMemReadBuffer buf(ness);
buf.SetLength(headLen);
char *data = buf.GetDataPtr();
if ((ret = GetHeaderData(data,headLen,fp)) != headLen)
return false;
return false;
// Set up a parser
// Catch the tables we need for the archive
@@ -238,52 +238,52 @@ bool trpgr_Archive::ReadHeader(bool readAllBlocks)
parser.AddCallback(TRPGMATTABLE,&materialTable); // Went back to oldest style for 2.0
parser.AddCallback(TRPGMATTABLE2,&oldMatTable); // Added 11-14-98 (1.0 material table)
parser.AddCallback(TRPGTEXTABLE,&oldTexTable);
parser.AddCallback(TRPGTEXTABLE2,&texTable); // Added for 2.0
parser.AddCallback(TRPGTEXTABLE2,&texTable); // Added for 2.0
parser.AddCallback(TRPGMODELTABLE,&modelTable);
parser.AddCallback(TRPGLIGHTTABLE,&lightTable); // Added for 2.0
parser.AddCallback(TRPGRANGETABLE,&rangeTable); // Added for 2.0
parser.AddCallback(TRPG_TEXT_STYLE_TABLE,&textStyleTable); // Added for 2.1
parser.AddCallback(TRPG_SUPPORT_STYLE_TABLE,&supportStyleTable);
parser.AddCallback(TRPG_LABEL_PROPERTY_TABLE,&labelPropertyTable);
parser.AddCallback(TRPGLIGHTTABLE,&lightTable); // Added for 2.0
parser.AddCallback(TRPGRANGETABLE,&rangeTable); // Added for 2.0
parser.AddCallback(TRPG_TEXT_STYLE_TABLE,&textStyleTable); // Added for 2.1
parser.AddCallback(TRPG_SUPPORT_STYLE_TABLE,&supportStyleTable);
parser.AddCallback(TRPG_LABEL_PROPERTY_TABLE,&labelPropertyTable);
// Don't read the tile table for v1.0 archives
// It's only really used for 2.0 archives
parser.AddCallback(TRPGTILETABLE2,&tileTable);
// Parse the buffer
if (!parser.Parse(buf))
return false;
return false;
if(header.GetIsMaster())
{
// bool firstBlock = true;
bool headerHasTexTable = false;
//if the master has textures, we want to use them instead of the tables in the
//block archives
int numTex = 0;
texTable.GetNumTextures(numTex);
if(numTex)
headerHasTexTable = true;
// int numTiles = 0;
//tileTable.
int totalrows,totalcols;
trpg2dPoint mhdr_swExtents;
trpg2dPoint mhdr_neExtents;
trpg3dPoint mhdr_Origin;
// integrate header information from the block header.
header.GetExtents(mhdr_swExtents,mhdr_neExtents);
header.GetOrigin(mhdr_Origin);
header.GetBlocks(totalrows,totalcols);
if(readAllBlocks) {
for(int row=0;row<totalrows;row++) {
for(int col=0;col<totalcols;col++) {
// Read each block -- Warning, this can take a while!!!
ReadSubArchive( row, col, cpuNess);
}
}
}
else {
ReadSubArchive( 0, 0, cpuNess);//Get the first archive!
}
// bool firstBlock = true;
bool headerHasTexTable = false;
//if the master has textures, we want to use them instead of the tables in the
//block archives
int numTex = 0;
texTable.GetNumTextures(numTex);
if(numTex)
headerHasTexTable = true;
// int numTiles = 0;
//tileTable.
int totalrows,totalcols;
trpg2dPoint mhdr_swExtents;
trpg2dPoint mhdr_neExtents;
trpg3dPoint mhdr_Origin;
// integrate header information from the block header.
header.GetExtents(mhdr_swExtents,mhdr_neExtents);
header.GetOrigin(mhdr_Origin);
header.GetBlocks(totalrows,totalcols);
if(readAllBlocks) {
for(int row=0;row<totalrows;row++) {
for(int col=0;col<totalcols;col++) {
// Read each block -- Warning, this can take a while!!!
ReadSubArchive( row, col, cpuNess);
}
}
}
else {
ReadSubArchive( 0, 0, cpuNess);//Get the first archive!
}
}
tileTable.SetCurrentBlock(-1,-1,false);
@@ -292,18 +292,18 @@ bool trpgr_Archive::ReadHeader(bool readAllBlocks)
// If we see an older style material table, convert it to the new style
// This isn't terribly memory efficient, but it does work
if (oldMatTable.isValid())
materialTable = oldMatTable;
materialTable = oldMatTable;
if (oldTexTable.isValid())
texTable = oldTexTable;
texTable = oldTexTable;
// Set up a tile cache, if needed
trpgTileTable::TileMode tileMode;
tileTable.GetMode(tileMode);
if (tileMode == trpgTileTable::Local) {
if (tileCache) delete tileCache;
char fullBase[1024];
sprintf(fullBase,"%s" PATHSEPERATOR "tileFile",dir);
tileCache = GetNewRAppFileCache(fullBase,"tpf");
if (tileCache) delete tileCache;
char fullBase[1024];
sprintf(fullBase,"%s" PATHSEPERATOR "tileFile",dir);
tileCache = GetNewRAppFileCache(fullBase,"tpf");
}
valid = true;
@@ -318,46 +318,46 @@ bool trpgr_Archive::ReadHeader(bool readAllBlocks)
bool trpgr_Archive::ReadTile(uint32 x,uint32 y,uint32 lod,trpgMemReadBuffer &buf)
{
if (!isValid())
return false;
return false;
// Reality check the address
int32 numLods;
header.GetNumLods(numLods);
if (static_cast<int>(lod) >= numLods)
return false;
return false;
trpg2iPoint lodSize;
header.GetLodSize(lod,lodSize);
if (static_cast<int>(x) >= lodSize.x || static_cast<int>(y) >= lodSize.y)
return false;
return false;
trpgTileTable::TileMode tileMode;
tileTable.GetMode(tileMode);
bool status = true;
if (tileMode == trpgTileTable::External || tileMode == trpgTileTable::ExternalSaved) {
status = ReadExternalTile(x, y, lod, buf);
status = ReadExternalTile(x, y, lod, buf);
} else {
// Local tile. Figure out where it is (which file)
int majorVersion, minorVersion;
header.GetVersion(majorVersion, minorVersion);
if(majorVersion == 2 && minorVersion >=1)
{
// Version 2.1
// Tile table contains only lod 0 tiles
if(lod != 0)
status = false;
}
// Local tile. Figure out where it is (which file)
int majorVersion, minorVersion;
header.GetVersion(majorVersion, minorVersion);
if(majorVersion == 2 && minorVersion >=1)
{
// Version 2.1
// Tile table contains only lod 0 tiles
if(lod != 0)
status = false;
}
if(status)
{
trpgwAppAddress addr;
float zmin,zmax;
status = tileTable.GetTile(x,y,lod,addr,zmin,zmax);
if(status)
{
trpgwAppAddress addr;
float zmin,zmax;
status = tileTable.GetTile(x,y,lod,addr,zmin,zmax);
if(status)
status = ReadTile(addr, buf);
}
if(status)
status = ReadTile(addr, buf);
}
}
return status;
@@ -370,42 +370,42 @@ bool trpgr_Archive::ReadExternalTile(uint32 x,uint32 y,uint32 lod,trpgMemReadBuf
int majorVer,minorVer;
header.GetVersion(majorVer,minorVer);
if((majorVer >= TRPG_NOMERGE_VERSION_MAJOR) && (minorVer >= TRPG_NOMERGE_VERSION_MINOR)) {
int blockx,blocky;
unsigned int denom = (1 << lod); // this should work up to lod 31
blockx = x/denom;
blocky = y/denom;
sprintf(filename,"%s" PATHSEPERATOR "%d" PATHSEPERATOR "%d" PATHSEPERATOR "tile_%d_%d_%d.tpt",
dir,blockx,blocky,x,y,lod);
int blockx,blocky;
unsigned int denom = (1 << lod); // this should work up to lod 31
blockx = x/denom;
blocky = y/denom;
sprintf(filename,"%s" PATHSEPERATOR "%d" PATHSEPERATOR "%d" PATHSEPERATOR "tile_%d_%d_%d.tpt",
dir,blockx,blocky,x,y,lod);
}
else {
sprintf(filename,"%s" PATHSEPERATOR "tile_%d_%d_%d.tpt",dir,x,y,lod);
sprintf(filename,"%s" PATHSEPERATOR "tile_%d_%d_%d.tpt",dir,x,y,lod);
}
// Open the file and read the contents
FILE *fp= 0;
try {
if (!(fp = fopen(filename,"rb"))) {
if (!(fp = fopen(filename,"rb"))) {
throw 1;
}
// Find the file end
if (fseek(fp,0,SEEK_END))
throw 1;
// Note: This means tile is capped at 2 gigs
long pos = ftell(fp);
if (fseek(fp,0,SEEK_SET))
throw 1;
// Now we know the size. Read the whole file
buf.SetLength(pos);
char *data = buf.GetDataPtr();
if (fread(data,pos,1,fp) != 1)
throw 1;
fclose(fp);
fp = NULL;
throw 1;
}
// Find the file end
if (fseek(fp,0,SEEK_END))
throw 1;
// Note: This means tile is capped at 2 gigs
long pos = ftell(fp);
if (fseek(fp,0,SEEK_SET))
throw 1;
// Now we know the size. Read the whole file
buf.SetLength(pos);
char *data = buf.GetDataPtr();
if (fread(data,pos,1,fp) != 1)
throw 1;
fclose(fp);
fp = NULL;
}
catch (...) {
if (fp)
fclose(fp);
return false;
if (fp)
fclose(fp);
return false;
}
return true;
@@ -415,13 +415,13 @@ bool trpgr_Archive::ReadTile(const trpgwAppAddress& addr, trpgMemReadBuffer &buf
// Fetch the appendable file from the cache
trpgrAppFile *tf = tileCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!tf)
return false;
return false;
// Fetch the tile
if (!tf->Read(&buf,addr.offset))
return false;
return false;
else
return true;
return true;
}
// Get methods
@@ -474,13 +474,13 @@ trpgEndian trpgr_Archive::GetEndian() const
bool trpgr_Archive::trpgGetTileMBR(uint32 x,uint32 y,uint32 lod,trpg3dPoint &ll,trpg3dPoint &ur) const
{
if (!header.isValid())
return false;
return false;
int32 numLod;
header.GetNumLods(numLod);
trpg2iPoint maxXY;
header.GetLodSize(lod,maxXY);
if (static_cast<int>(x) >= maxXY.x || static_cast<int>(y)>= maxXY.y)
return false;
return false;
trpg3dPoint origin;
header.GetOrigin(origin);
@@ -507,13 +507,13 @@ bool trpgr_Archive::trpgGetTileMBR(uint32 x,uint32 y,uint32 lod,trpg3dPoint &ll,
*/
trpgrImageHelper::trpgrImageHelper(trpgEndian inNess,char *inDir,
const trpgMatTable &inMatTable,const trpgTexTable &inTexTable,bool separateGeoTyp)
const trpgMatTable &inMatTable,const trpgTexTable &inTexTable,bool separateGeoTyp)
{
Init(inNess,inDir,inMatTable,inTexTable,separateGeoTyp);
}
void trpgrImageHelper::Init(trpgEndian inNess,char *inDir,
const trpgMatTable &inMatTable,const trpgTexTable &inTexTable,bool separateGeoTyp)
const trpgMatTable &inMatTable,const trpgTexTable &inTexTable,bool separateGeoTyp)
{
ness = inNess;
strcpy(dir,inDir);
@@ -527,24 +527,24 @@ void trpgrImageHelper::Init(trpgEndian inNess,char *inDir,
sprintf(fullBase,"%s" PATHSEPERATOR "texFile",dir);
texCache = GetNewRAppFileCache(fullBase,"txf");
if(separateGeoTyp) {
sprintf(fullBase,"%s" PATHSEPERATOR "geotypFile",dir);
geotypCache = GetNewRAppFileCache(fullBase,"txf");
sprintf(fullBase,"%s" PATHSEPERATOR "geotypFile",dir);
geotypCache = GetNewRAppFileCache(fullBase,"txf");
}
else {
geotypCache = texCache;
geotypCache = texCache;
}
}
trpgrImageHelper::~trpgrImageHelper()
{
if (texCache) {
delete texCache;
texCache = NULL;
delete texCache;
texCache = NULL;
}
if(separateGeoTyp && geotypCache) {
delete geotypCache;
geotypCache = NULL;
delete geotypCache;
geotypCache = NULL;
}
}
@@ -559,16 +559,16 @@ bool trpgrImageHelper::GetLocalGL(const trpgTexture *tex,char *data,int32 size)
trpgTexture::ImageMode mode;
tex->GetImageMode(mode);
if (mode != trpgTexture::Local)
return false;
return false;
// Fetch data data
trpgwAppAddress addr;
tex->GetImageAddr(addr);
trpgrAppFile *af = geotypCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af)
return false;
return false;
if (!af->Read(data,addr.offset,0,size))
return false;
return false;
return true;
}
@@ -576,37 +576,37 @@ bool trpgrImageHelper::GetLocalGL(const trpgTexture *tex,char *data,int32 size)
bool trpgrImageHelper::GetMipLevelLocalGL(int miplevel, const trpgTexture *tex,char *data,int32 dataSize)
{
if ( miplevel >= tex->CalcNumMipmaps() || miplevel < 0 )
return false;
return false;
// Make sure the texture is Local
trpgTexture::ImageMode mode;
tex->GetImageMode(mode);
if (mode != trpgTexture::Local)
return false;
return false;
// Fetch data data
trpgwAppAddress addr;
tex->GetImageAddr(addr);
trpgrAppFile *af = texCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af)
return false;
return false;
int level_offset = (const_cast<trpgTexture*>(tex))->MipLevelOffset(miplevel);
if (!af->Read(data,addr.offset,level_offset,dataSize))
return false;
return false;
return true;
}
bool trpgrImageHelper::GetImageInfoForLocalMat(const trpgLocalMaterial *locMat,
const trpgMaterial **retMat,const trpgTexture **retTex,int &totSize)
const trpgMaterial **retMat,const trpgTexture **retTex,int &totSize)
{
return GetNthImageInfoForLocalMat(locMat, 0, retMat, retTex, totSize);
}
bool trpgrImageHelper::GetNthImageInfoForLocalMat(const trpgLocalMaterial *locMat, int index,
const trpgMaterial **retMat,const trpgTexture **retTex,int &totSize)
const trpgMaterial **retMat,const trpgTexture **retTex,int &totSize)
{
// Get the base material for the Local Material
int32 matSubTable,matID;
@@ -616,22 +616,22 @@ bool trpgrImageHelper::GetNthImageInfoForLocalMat(const trpgLocalMaterial *locMa
// or overhaul this in some other fashion.
int numTables;
if (!matTable->GetNumTable(numTables))
return false;
return false;
if (index>=numTables)
return false;
return false;
if (index>0) matSubTable=index; // otherwise, leave it alone - could be nonzero
const trpgMaterial *mat = matTable->GetMaterialRef(matSubTable,matID);
if (!mat)
return false;
return false;
// Now get the texture (always the first one)
trpgTextureEnv texEnv;
int32 texID;
if (!mat->GetTexture(0,texID,texEnv))
return false;
return false;
const trpgTexture *tex = texTable->GetTextureRef(texID);
if (!tex)
return false;
return false;
totSize = tex->CalcTotalSize();
@@ -648,13 +648,13 @@ bool trpgrImageHelper::GetImageForLocalMat(const trpgLocalMaterial *locMat,char
bool trpgrImageHelper::GetNthImageForLocalMat(const trpgLocalMaterial *locMat,int index, char *data,int dataSize)
{
if (!locMat->isValid())
return false;
return false;
const trpgMaterial *mat;
const trpgTexture *tex;
int totSize;
if (!GetNthImageInfoForLocalMat(locMat,index,&mat,&tex,totSize))
return false;
return false;
// Determine the type
trpgTexture::ImageMode imageMode;
@@ -662,23 +662,23 @@ bool trpgrImageHelper::GetNthImageForLocalMat(const trpgLocalMaterial *locMat,in
switch (imageMode) {
case trpgTexture::Template:
{
// Read the image data out of the Local image (in an archive somewhere)
trpgwAppAddress addr;
if (!locMat->GetNthAddr(index,addr)) return false;
trpgrAppFile *af = texCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af)
return false;
if (!af->Read(data,addr.offset,0,dataSize))
return false;
// Read the image data out of the Local image (in an archive somewhere)
trpgwAppAddress addr;
if (!locMat->GetNthAddr(index,addr)) return false;
trpgrAppFile *af = texCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af)
return false;
if (!af->Read(data,addr.offset,0,dataSize))
return false;
}
break;
case trpgTexture::Global:
// Note: Not dealing with Global textures yet
return false;
break;
// Note: Not dealing with Global textures yet
return false;
break;
default:
// This is not a valid Local Material
return false;
// This is not a valid Local Material
return false;
};
return true;
@@ -698,10 +698,10 @@ bool trpgrImageHelper::GetNthImageMipLevelForLocalMat(int miplevel, const trpgLo
const trpgTexture *tex;
int totSize;
if (!GetNthImageInfoForLocalMat(locMat,index,&mat,&tex,totSize))
return false;
return false;
if ( miplevel >= tex->CalcNumMipmaps() || miplevel < 0 )
return false;
return false;
// Determine the type
trpgTexture::ImageMode imageMode;
@@ -709,24 +709,24 @@ bool trpgrImageHelper::GetNthImageMipLevelForLocalMat(int miplevel, const trpgLo
switch (imageMode) {
case trpgTexture::Template:
{
// Read the image data out of the Local image (in an archive somewhere)
trpgwAppAddress addr;
if (!locMat->GetNthAddr(index,addr)) return false;
trpgrAppFile *af = texCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af) return false;
// Read the image data out of the Local image (in an archive somewhere)
trpgwAppAddress addr;
if (!locMat->GetNthAddr(index,addr)) return false;
trpgrAppFile *af = texCache->GetFile(ness,addr.file,addr.col,addr.row);
if (!af) return false;
int level_offset = (const_cast<trpgTexture*>(tex))->MipLevelOffset(miplevel);
if (!af->Read(data,addr.offset,level_offset,dataSize))
return false;
int level_offset = (const_cast<trpgTexture*>(tex))->MipLevelOffset(miplevel);
if (!af->Read(data,addr.offset,level_offset,dataSize))
return false;
}
break;
case trpgTexture::Global:
// Note: Not dealing with Global textures yet
return false;
break;
// Note: Not dealing with Global textures yet
return false;
break;
default:
// This is not a valid Local Material
return false;
// This is not a valid Local Material
return false;
};
return true;
@@ -740,7 +740,7 @@ bool trpgrImageHelper::GetImagePath(const trpgTexture *tex,char *fullPath,int pa
nameLen = strlen(name);
if (static_cast<int>(strlen(dir)) + nameLen + 2 > pathLen)
return false;
return false;
sprintf(fullPath,"%s" PATHSEPERATOR "%s",dir,name);

View File

@@ -18,15 +18,15 @@
#include <math.h>
/* trpage_readbuf.cpp
Methods for the trpgReadBuffer and trpgMemReadBuffer classes.
trpgReadBuffer is a virtual base class with a few utility functions.
It's used as generic interface for reading data out of.
trpgMemReadBuffer is a subclass of that which implements methods for
reading out of a chunk of memory. Data is read off of disk and then
dumped into a read buffer for parsing.
If you wanted to read directly from disk, for example, you could
implement a trpgDiskReadBuffer as a subclass of trpgReadBuffer.
*/
Methods for the trpgReadBuffer and trpgMemReadBuffer classes.
trpgReadBuffer is a virtual base class with a few utility functions.
It's used as generic interface for reading data out of.
trpgMemReadBuffer is a subclass of that which implements methods for
reading out of a chunk of memory. Data is read off of disk and then
dumped into a read buffer for parsing.
If you wanted to read directly from disk, for example, you could
implement a trpgDiskReadBuffer as a subclass of trpgReadBuffer.
*/
#include <trpage_io.h>
#include <trpage_swap.h>
@@ -43,9 +43,9 @@ bool trpgReadBuffer::Get(int32 &ret)
if (!GetData((char *)&val,sizeof(int32))) return false;
if (ness != cpuNess)
ret = trpg_byteswap_int(val);
ret = trpg_byteswap_int(val);
else
ret = val;
ret = val;
return true;
}
@@ -54,11 +54,11 @@ bool trpgReadBuffer::Get(int64 &ret)
int64 val;
if (!GetData((char *)&val,sizeof(int64))) return false;
// trpg_byteswap_llong is defined to be 64 bit
// trpg_byteswap_llong is defined to be 64 bit
if (ness != cpuNess)
ret = trpg_byteswap_llong(val);
ret = trpg_byteswap_llong(val);
else
ret = val;
ret = val;
return true;
}
@@ -87,7 +87,7 @@ bool trpgReadBuffer::Get(std::string &str)
if (!Get(len)) return false;
if (len < 0)
return false;
return false;
// Note: Should fix this
char *tmpStr = new char[len+1];
@@ -105,10 +105,10 @@ bool trpgReadBuffer::Get(float32 &ret)
if (!GetData(cval,sizeof(float32))) return false;
try {
if (ness == cpuNess)
memcpy(&ret,cval,4);
else
ret = trpg_byteswap_4bytes_to_float(cval);
if (ness == cpuNess)
memcpy(&ret,cval,4);
else
ret = trpg_byteswap_4bytes_to_float(cval);
}
catch (...) {
}
@@ -121,10 +121,10 @@ bool trpgReadBuffer::Get(float64 &ret)
if (!GetData(cval,sizeof(float64))) return false;
try {
if (ness == cpuNess)
memcpy(&ret,cval,8);
else
ret = trpg_byteswap_8bytes_to_double(cval);
if (ness == cpuNess)
memcpy(&ret,cval,8);
else
ret = trpg_byteswap_8bytes_to_double(cval);
}
catch (...) {
}
@@ -162,9 +162,9 @@ bool trpgReadBuffer::Get(trpgDiskRef &ret)
if (!GetData((char *)&val,sizeof(trpgDiskRef))) return false;
if (ness == cpuNess)
ret = val;
ret = val;
else
ret = trpg_byteswap_llong(val);
ret = trpg_byteswap_llong(val);
return true;
}
@@ -176,9 +176,9 @@ bool trpgReadBuffer::Get(trpgToken &ret)
if (!GetData((char *)&val,sizeof(trpgToken))) return false;
if (ness == cpuNess)
ret = val;
ret = val;
else
ret = trpg_byteswap_short(val);
ret = trpg_byteswap_short(val);
return true;
}
@@ -187,13 +187,13 @@ bool trpgReadBuffer::Get(trpgToken &ret)
bool trpgReadBuffer::GetArray(int len,float32 **arr)
{
if (!GetDataRef((char **)arr,sizeof(float32)*len))
return false;
return false;
// Byteswap in place if necessary
if (ness != cpuNess) {
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=4)
trpg_swap_four(ptr,ptr);
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=4)
trpg_swap_four(ptr,ptr);
}
return true;
@@ -201,39 +201,39 @@ bool trpgReadBuffer::GetArray(int len,float32 **arr)
bool trpgReadBuffer::GetArray(int len,float64 **arr)
{
if (!GetDataRef((char **)arr,sizeof(float64)*len))
return false;
return false;
// Byteswap in place if necessary
if (ness != cpuNess) {
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=8)
trpg_swap_eight(ptr,ptr);
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=8)
trpg_swap_eight(ptr,ptr);
}
return true;
}
bool trpgReadBuffer::GetArray(int len,int32 **arr)
{
if (!GetDataRef((char **)arr,sizeof(int32)*len))
return false;
return false;
// Byteswap in place if necessary
if (ness != cpuNess) {
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=4)
trpg_swap_four(ptr,ptr);
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=4)
trpg_swap_four(ptr,ptr);
}
return true;
}
bool trpgReadBuffer::GetArray(int len,trpgColor **arr)
{
if (!GetDataRef((char **)arr,sizeof(trpgColor)*len))
return false;
return false;
// Byteswap in place if necessary
if (ness != cpuNess) {
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=8)
trpg_swap_four(ptr,ptr);
char *ptr;
int pos;
for (pos=0,ptr = (char *)*arr;pos<len;pos++,ptr+=8)
trpg_swap_four(ptr,ptr);
}
return true;
}
@@ -246,25 +246,25 @@ bool trpgReadBuffer::GetArray(int len,char **arr)
bool trpgReadBuffer::Get(trpg2iPoint &pt)
{
if (!Get(pt.x) || !Get(pt.y))
return false;
return false;
return true;
}
bool trpgReadBuffer::Get(trpg2dPoint &pt)
{
if (!Get(pt.x) || !Get(pt.y))
return false;
return false;
return true;
}
bool trpgReadBuffer::Get(trpg3dPoint &pt)
{
if (!Get(pt.x) || !Get(pt.y) || !Get(pt.z))
return false;
return false;
return true;
}
bool trpgReadBuffer::Get(trpgColor &color)
{
if (!Get(color.red) || !Get(color.green) || !Get(color.blue))
return false;
return false;
return true;
}
@@ -272,7 +272,7 @@ bool trpgReadBuffer::Get(trpgColor &color)
bool trpgReadBuffer::GetToken(trpgToken &tok,int32 &len)
{
if (!Get(tok) || !Get(len))
return false;
return false;
return true;
}
@@ -296,7 +296,7 @@ void trpgReadBuffer::PopLimit()
int len = limits.size();
if (len > 0)
limits.resize(len-1);
limits.resize(len-1);
}
// Skip To Limit
@@ -307,10 +307,10 @@ bool trpgReadBuffer::SkipToLimit()
int len=0;
if (limits.size() != 0)
len = limits[limits.size()-1];
len = limits[limits.size()-1];
if (len > 0)
return Skip(len);
return Skip(len);
return true;
}
@@ -320,8 +320,8 @@ bool trpgReadBuffer::SkipToLimit()
bool trpgReadBuffer::TestLimit(int len)
{
for (unsigned int i=0;i<limits.size();i++)
if (len > limits[i])
return false;
if (len > limits[i])
return false;
return true;
}
@@ -331,7 +331,7 @@ bool trpgReadBuffer::TestLimit(int len)
void trpgReadBuffer::UpdateLimits(int len)
{
for (unsigned int i=0;i<limits.size();i++)
limits[i] -= len;
limits[i] -= len;
}
/* *************************
@@ -348,7 +348,7 @@ trpgMemReadBuffer::trpgMemReadBuffer(trpgEndian in_ness)
trpgMemReadBuffer::~trpgMemReadBuffer()
{
if (data)
delete [] data;
delete [] data;
}
// Empty check
@@ -357,11 +357,11 @@ bool trpgMemReadBuffer::isEmpty()
if (!data) return true;
if (pos >= len)
return true;
return true;
// Also test the limits
for (unsigned int i=0;i<limits.size();i++)
if (limits[i] == 0) return true;
if (limits[i] == 0) return true;
return false;
}
@@ -371,10 +371,10 @@ bool trpgMemReadBuffer::isEmpty()
void trpgMemReadBuffer::SetLength(int newLen)
{
if (newLen > totLen) {
if (data)
delete [] data;
data = new char[newLen];
totLen = newLen;
if (data)
delete [] data;
data = new char[newLen];
totLen = newLen;
}
len = newLen;
pos = 0;
@@ -392,7 +392,7 @@ char *trpgMemReadBuffer::GetDataPtr()
bool trpgMemReadBuffer::GetData(char *ret,int rlen)
{
if (rlen < 0)
return false;
return false;
// Test against limits imposed from without
if (!TestLimit(rlen)) throw 1;
@@ -435,10 +435,10 @@ bool trpgMemReadBuffer::GetDataRef(char **ret,int rlen)
bool trpgMemReadBuffer::Skip(int rlen)
{
if (rlen == 0)
return true;
return true;
if (rlen < 0)
return false;
return false;
// Test against limits
if (!TestLimit(rlen)) return false;
@@ -466,7 +466,7 @@ void trpgrAppFile::Init(trpgEndian inNess,const char *fileName)
cpuNess = trpg_cpu_byte_order();
if (!(fp = fopen(fileName,"rb")))
return;
return;
valid = true;
}
@@ -474,7 +474,7 @@ void trpgrAppFile::Init(trpgEndian inNess,const char *fileName)
trpgrAppFile::~trpgrAppFile()
{
if (fp)
fclose(fp);
fclose(fp);
valid = false;
}
@@ -492,34 +492,34 @@ bool trpgrAppFile::Read(trpgMemReadBuffer *buf,int32 offset)
// Seek to the right location
if (fseek(fp,offset,SEEK_SET))
return false;
return false;
// Read a length
int32 len;
if (fread(&len,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
valid = false;
return false;
}
// Byteswap if necessary
if (ness != cpuNess)
len = trpg_byteswap_int(len);
len = trpg_byteswap_int(len);
if (len < 0) {
valid = false;
return false;
valid = false;
return false;
}
buf->SetLength(len);
char *data = buf->GetDataPtr();
if (!data) {
valid = false;
return false;
valid = false;
return false;
}
if (fread(data,sizeof(char),len,fp) != (uint32)len) {
valid = false;
return false;
if (fread(data,sizeof(char),len,fp) != (uint32)len) {
valid = false;
return false;
}
return true;
@@ -537,41 +537,41 @@ bool trpgrAppFile::Read(char *data,int32 baseOffset,int32 objOffset,int32 dataSi
// Seek to the right place
int result;
if ((result = fseek(fp,baseOffset,SEEK_SET))) {
valid = false;
return false;
valid = false;
return false;
}
// Read the total object length
int32 len;
if (fread(&len,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
valid = false;
return false;
}
// Byteswap if necessary
if (ness != cpuNess)
len = trpg_byteswap_int(len);
len = trpg_byteswap_int(len);
if (len < 0) {
valid = false;
return false;
valid = false;
return false;
}
// It's all right to read less than the whole data block
if (objOffset+dataSize > len)
return false;
return false;
// Skip to the object offset
if (fseek(fp,objOffset,SEEK_CUR)) {
valid = false;
return false;
valid = false;
return false;
}
// Read the raw data
// Note: What about byte swapping?
if (fread(data,sizeof(char),dataSize,fp) != (uint32)dataSize) {
valid = false;
return false;
if (fread(data,sizeof(char),dataSize,fp) != (uint32)dataSize) {
valid = false;
return false;
}
return true;
@@ -601,10 +601,10 @@ trpgrAppFileCache::~trpgrAppFileCache()
{
unsigned int len = files.size();
for (unsigned int i=0;i<len;i++) {
if (files[i].afile) {
delete files[i].afile;
files[i].afile = NULL;
}
if (files[i].afile) {
delete files[i].afile;
files[i].afile = NULL;
}
}
}
@@ -626,69 +626,69 @@ trpgrAppFile *trpgrAppFileCache::GetFile(trpgEndian ness,int id,int col,int row)
int foundID = -1;
unsigned int i;
for (i=0;i<files.size();i++) {
if ((files[i].id == id)&&
(files[i].col == col) &&
(files[i].row == row)) {
foundID = i;
break;
}
if ((files[i].id == id)&&
(files[i].col == col) &&
(files[i].row == row)) {
foundID = i;
break;
}
}
// Found it in cache, just return
if (foundID != -1) {
OpenFile &of = files[foundID];
OpenFile &of = files[foundID];
if (of.afile->isValid())
{
of.lastUsed = timeCount;
return of.afile;
}
else
{
if (of.afile)
delete of.afile;
of.afile = NULL;
}
if (of.afile->isValid())
{
of.lastUsed = timeCount;
return of.afile;
}
else
{
if (of.afile)
delete of.afile;
of.afile = NULL;
}
}
// Didn't find it. Need to reclaim one
// Look for the oldest used
int oldTime=-1,oldID=-1;
for (i=0;i<files.size();i++) {
OpenFile &of = files[i];
if (!of.afile || (oldTime == -1) || (of.lastUsed < oldTime)) {
oldID = i;
oldTime = of.lastUsed;
if (!of.afile)
break;
}
OpenFile &of = files[i];
if (!of.afile || (oldTime == -1) || (of.lastUsed < oldTime)) {
oldID = i;
oldTime = of.lastUsed;
if (!of.afile)
break;
}
}
// Reclaim this one
OpenFile &of = files[oldID];
if (of.afile)
delete of.afile;
delete of.afile;
char fileName[1024];
if(col==-1) {
sprintf(fileName,"%s_%d.%s",baseName,id,ext);
sprintf(fileName,"%s_%d.%s",baseName,id,ext);
}
else {
char dir[1024];
char filebase[1024];
//this is very ugly, but it avoids radical API changes
// find the last PATHSEPERATOR in the baseName string
int len = strlen(baseName);
while(--len > 0) {
if(baseName[len]==PATHSEPERATOR[0]) {
strcpy(filebase,&baseName[len+1]);
strcpy(dir,baseName);
dir[len]='\0';
break;
}
}
sprintf(fileName,"%s" PATHSEPERATOR "%d" PATHSEPERATOR "%d" PATHSEPERATOR "%s_%d.%s",
dir,col,row,filebase,id,ext);
char dir[1024];
char filebase[1024];
//this is very ugly, but it avoids radical API changes
// find the last PATHSEPERATOR in the baseName string
int len = strlen(baseName);
while(--len > 0) {
if(baseName[len]==PATHSEPERATOR[0]) {
strcpy(filebase,&baseName[len+1]);
strcpy(dir,baseName);
dir[len]='\0';
break;
}
}
sprintf(fileName,"%s" PATHSEPERATOR "%d" PATHSEPERATOR "%d" PATHSEPERATOR "%s_%d.%s",
dir,col,row,filebase,id,ext);
}
of.afile = GetNewRAppFile(ness,fileName);
of.id = id;

View File

@@ -534,7 +534,7 @@ bool trpgwArchive::CheckpointHeader()
header.GetLodSize(i,lodSize);
tileTable.SetNumTiles(lodSize.x,lodSize.y,i);
}
}
firstHeaderWrite = false;
}
@@ -554,7 +554,7 @@ bool trpgwArchive::CheckpointHeader()
}
tf.tiles.clear();
}
}
@@ -607,8 +607,8 @@ bool trpgwArchive::CheckpointHeader()
return false;
}
}
if(!modelTable.Write(buf) )
{
strcpy(errMess, "Error writing model table");
@@ -683,7 +683,7 @@ bool trpgwArchive::CheckpointHeader()
}
}
}
break;
break;
}
// Write the disk header
@@ -787,7 +787,7 @@ bool trpgwArchive::IncrementTileFile()
bool trpgwArchive::DesignateTileFile(int id)
{
if (tileMode != TileLocal)
return false;
return false;
// Close the current tile file
if (tileFile) {
@@ -800,7 +800,7 @@ bool trpgwArchive::DesignateTileFile(int id)
sprintf(filename,"%s" PATHSEPERATOR "tileFile_%d.tpf",dir,id);
tileFile = GetNewWAppFile(ness,filename);
if (!tileFile->isValid())
return false;
return false;
// Add another TileFiles entry
tileFiles.resize(tileFiles.size()+1);
@@ -835,7 +835,7 @@ bool trpgwArchive::WriteTile(unsigned int x,unsigned int y,unsigned int lod, flo
return false;
// Write the header first
unsigned int len;
unsigned int len;
const char *data;
if (head) {
data = head->getData();
@@ -1053,52 +1053,52 @@ void trpgwGeomHelper::EndPolygon()
break;
}
ResetPolygon();
ResetPolygon();
}
// Clean out the polygon arrays
void trpgwGeomHelper::ResetPolygon()
{
tmpTex.resize(0);
matPoly.resize(0);
polyTex.resize(0);
polyNorm.resize(0);
polyVert.resize(0);
tmpTex.resize(0);
matPoly.resize(0);
polyTex.resize(0);
polyNorm.resize(0);
polyVert.resize(0);
}
// Set the current color
// Note: Required
void trpgwGeomHelper::SetColor(trpgColor& /*col*/)
{
// tmpColor = col;
// tmpColor = col;
}
// Set the current texture coord
// Note: Required
void trpgwGeomHelper::SetTexCoord(trpg2dPoint &pt)
{
tmpTex.resize(0);
tmpTex.push_back(pt);
tmpTex.resize(0);
tmpTex.push_back(pt);
}
void trpgwGeomHelper::AddTexCoord(trpg2dPoint &pt)
{
tmpTex.push_back(pt);
tmpTex.push_back(pt);
}
// Set the current normal
// Note: required
void trpgwGeomHelper::SetNormal(trpg3dPoint &pt)
{
tmpNorm = pt;
tmpNorm = pt;
}
// Set the current material
// Note: required
void trpgwGeomHelper::SetMaterial(int32 imat)
{
matPoly.resize(0);
matPoly.push_back(imat);
matPoly.resize(0);
matPoly.push_back(imat);
}
void trpgwGeomHelper::AddMaterial(int32 imat)
@@ -1208,9 +1208,9 @@ void trpgwGeomHelper::FlushGeom()
It will produce valid output, but not particularly optimized.
*/
#define ADDVERT(dest,vt) { \
dest.AddVertex((trpgGeometry::DataType)dtype,vt.v); \
dest.AddNormal((trpgGeometry::DataType)dtype,vt.n); \
dest.AddTexCoord((trpgGeometry::DataType)dtype,vt.tex); \
dest.AddVertex((trpgGeometry::DataType)dtype,vt.v); \
dest.AddNormal((trpgGeometry::DataType)dtype,vt.n); \
dest.AddTexCoord((trpgGeometry::DataType)dtype,vt.tex); \
}
class optVert {
public:
@@ -1493,20 +1493,20 @@ trpgwAppFile * trpgwImageHelper::IncrementTextureFile(bool geotyp)
{
char filename[1024];
trpgwAppFile *thefile = texFile;
if(geotyp && separateGeoTypical) {
thefile = geotypFile;
sprintf(filename,"%s" PATHSEPERATOR "geotypFile_%d.txf",dir,static_cast<int>(geotypFileIDs.size()));
sprintf(filename,"%s" PATHSEPERATOR "geotypFile_%d.txf",dir,static_cast<int>(geotypFileIDs.size()));
}
else {
sprintf(filename,"%s" PATHSEPERATOR "texFile_%d.txf",dir,static_cast<int>(texFileIDs.size()));
sprintf(filename,"%s" PATHSEPERATOR "texFile_%d.txf",dir,static_cast<int>(texFileIDs.size()));
}
// Closes the current texture file
if (thefile) delete thefile;
thefile = NULL;
// Open the next one
// Open the next one
thefile = GetNewWAppFile(ness,filename,true);
if (!thefile->isValid())
return NULL;
@@ -1555,7 +1555,7 @@ bool trpgwImageHelper::DesignateTextureFile(int id)
return false;
geotypFileIDs.push_back(id);
return true;
}
@@ -1580,7 +1580,7 @@ bool trpgwImageHelper::WriteToArchive(const trpgTexture &tex,char *data,trpgwApp
if (! (thefile=IncrementTextureFile(geotyp && separateGeoTypical)))
return false;
}
while (maxTexFileLen > 0 && thefile->GetLengthWritten() > maxTexFileLen) {
if (!(thefile=IncrementTextureFile(geotyp && separateGeoTypical)))
return false;

View File

@@ -18,16 +18,16 @@
#include <string.h>
/* trpage_writebuf.cpp
This source file contains the implementation of trpgMemWriteBuffer.
That is a subclass of trpgWriteBuffer, which implements an interface
definition for an object which can accept data to be written.
The Mem version is used to write (for example) a tile's worth of data.
That data can then be written to a file (or sent over the network).
You should not need to change this implementation. Simply sublcass
trpgWriteBuffer and implement all of the required methods. The
resulting class can then be used in all the places a trpgWriteBuffer
is required.
*/
This source file contains the implementation of trpgMemWriteBuffer.
That is a subclass of trpgWriteBuffer, which implements an interface
definition for an object which can accept data to be written.
The Mem version is used to write (for example) a tile's worth of data.
That data can then be written to a file (or sent over the network).
You should not need to change this implementation. Simply sublcass
trpgWriteBuffer and implement all of the required methods. The
resulting class can then be used in all the places a trpgWriteBuffer
is required.
*/
#include <trpage_io.h>
#include <trpage_swap.h>
@@ -39,158 +39,158 @@
// Constructor
trpgMemWriteBuffer::trpgMemWriteBuffer(trpgEndian in_ness)
{
ness = in_ness;
cpuNess = trpg_cpu_byte_order();
data = NULL;
curLen = totLen = 0;
ness = in_ness;
cpuNess = trpg_cpu_byte_order();
data = NULL;
curLen = totLen = 0;
}
// Destructor
trpgMemWriteBuffer::~trpgMemWriteBuffer()
{
if (data)
delete [] data;
data = NULL;
if (data)
delete [] data;
data = NULL;
}
/* Length()
Return the length of the given buffer.
*/
Return the length of the given buffer.
*/
int trpgMemWriteBuffer::length() const
{
return curLen;
return curLen;
}
/* getData()
Return a pointer to the memory buffer.
*/
Return a pointer to the memory buffer.
*/
const char *trpgMemWriteBuffer::getData() const
{
return data;
return data;
}
/* Length()
Set the maximum buffer length.
*/
Set the maximum buffer length.
*/
void trpgMemWriteBuffer::setLength(unsigned int len)
{
if ((int)len > totLen) {
char *old_data = data;
int oldLen = totLen;
totLen = 2*len;
data = new char[totLen];
if ((int)len > totLen) {
char *old_data = data;
int oldLen = totLen;
totLen = 2*len;
data = new char[totLen];
if (old_data) {
memcpy(data,old_data,oldLen);
delete [] old_data;
}
}
if (old_data) {
memcpy(data,old_data,oldLen);
delete [] old_data;
}
}
}
/* append()
Append the given data to our buffer.
*/
Append the given data to our buffer.
*/
void trpgMemWriteBuffer::append(unsigned int len,const char *val)
{
if (len == 0) return;
setLength(curLen+len);
memcpy(&data[curLen],val,len);
curLen += len;
if (len == 0) return;
setLength(curLen+len);
memcpy(&data[curLen],val,len);
curLen += len;
}
/* set()
Set a specific portion of the buffer to a given value.
*/
Set a specific portion of the buffer to a given value.
*/
void trpgMemWriteBuffer::set(unsigned int pos,unsigned int len,const char *val)
{
if (len == 0) return;
if (pos+len > (unsigned int)curLen) return;
if (len == 0) return;
if (pos+len > (unsigned int)curLen) return;
memcpy(&data[pos],val,len);
memcpy(&data[pos],val,len);
}
/* --- replacement virtual functions --- */
/* Reset()
Drop whatever's being stored.
*/
Drop whatever's being stored.
*/
void trpgMemWriteBuffer::Reset()
{
curLen = 0;
curLen = 0;
}
// Add(Int32)
void trpgMemWriteBuffer::Add(int32 val)
{
if (ness != cpuNess)
val = trpg_byteswap_int(val);
append(sizeof(int32),(const char *)&val);
if (ness != cpuNess)
val = trpg_byteswap_int(val);
append(sizeof(int32),(const char *)&val);
}
// Add(int64)
void trpgMemWriteBuffer::Add(int64 val)
{
if (ness != cpuNess)
val = trpg_byteswap_llong(val);
append(sizeof(int64),(const char *)&val);
if (ness != cpuNess)
val = trpg_byteswap_llong(val);
append(sizeof(int64),(const char *)&val);
}
// Add(string)
// [len] [value...]
void trpgMemWriteBuffer::Add(const char *val)
{
int32 len = (val ? strlen(val) : 0),vlen = len;
if (ness != cpuNess)
vlen = trpg_byteswap_int(vlen);
append(sizeof(int32),(const char *)&vlen);
append(len,val);
int32 len = (val ? strlen(val) : 0),vlen = len;
if (ness != cpuNess)
vlen = trpg_byteswap_int(vlen);
append(sizeof(int32),(const char *)&vlen);
append(len,val);
}
// Add(std::string)
void trpgMemWriteBuffer::Add(std::string &val)
{
Add(val.c_str());
Add(val.c_str());
}
// Add(float32)
void trpgMemWriteBuffer::Add(float32 val)
{
char cval[4];
if (ness != cpuNess)
trpg_byteswap_float_to_4bytes(val,cval);
else
memcpy(cval,&val,4);
char cval[4];
if (ness != cpuNess)
trpg_byteswap_float_to_4bytes(val,cval);
else
memcpy(cval,&val,4);
append(sizeof(float32),cval);
append(sizeof(float32),cval);
}
// Add(float64)
void trpgMemWriteBuffer::Add(float64 val)
{
char cval[8];
if (ness != cpuNess)
trpg_byteswap_double_to_8bytes(val,cval);
else
memcpy(cval,&val,8);
char cval[8];
if (ness != cpuNess)
trpg_byteswap_double_to_8bytes(val,cval);
else
memcpy(cval,&val,8);
append(sizeof(float64),cval);
append(sizeof(float64),cval);
}
// Add(int8)
void trpgMemWriteBuffer::Add(uint8 val)
{
// No byte swapping needed
append(sizeof(uint8),(const char *)&val);
// No byte swapping needed
append(sizeof(uint8),(const char *)&val);
}
//#if (bool != int32)
// Add(bool)
void trpgMemWriteBuffer::Add(bool val)
{
uint8 ival;
uint8 ival;
ival = (val ? 1 : 0);
Add(ival);
ival = (val ? 1 : 0);
Add(ival);
}
//#endif
@@ -198,83 +198,83 @@ void trpgMemWriteBuffer::Add(bool val)
// Add(trpgDiskRef)
void trpgMemWriteBuffer::Add(trpgDiskRef val)
{
if (ness != cpuNess)
val = trpg_byteswap_llong(val);
if (ness != cpuNess)
val = trpg_byteswap_llong(val);
append(sizeof(trpgDiskRef),(const char *)&val);
append(sizeof(trpgDiskRef),(const char *)&val);
}
#endif
// Add(trpgToken)
void trpgMemWriteBuffer::Add(trpgToken val)
{
if (ness != cpuNess)
val = trpg_byteswap_short(val);
if (ness != cpuNess)
val = trpg_byteswap_short(val);
append(sizeof(trpgToken),(const char *)&val);
append(sizeof(trpgToken),(const char *)&val);
}
// Add(tx2iPoint)
void trpgWriteBuffer::Add(const trpg2iPoint &val)
{
Add((int32)val.x);
Add((int32)val.y);
Add((int32)val.x);
Add((int32)val.y);
}
// Add(tx2dPoint)
void trpgWriteBuffer::Add(const trpg2dPoint &val)
{
Add((float64)val.x);
Add((float64)val.y);
Add((float64)val.x);
Add((float64)val.y);
}
// Add(trpg3dPoint)
void trpgWriteBuffer::Add(const trpg3dPoint &val)
{
Add((float64)val.x);
Add((float64)val.y);
Add((float64)val.z);
Add((float64)val.x);
Add((float64)val.y);
Add((float64)val.z);
}
// Add(trpgColor)
void trpgWriteBuffer::Add(const trpgColor &val)
{
Add(val.red);
Add(val.green);
Add(val.blue);
Add(val.red);
Add(val.green);
Add(val.blue);
}
// Add(std::string)
void trpgWriteBuffer::Add(const std::string &val)
{
Add(val.c_str());
Add(val.c_str());
}
/* Push()
Starts defining a new object.
Need to keep track of where length goes.
*/
Starts defining a new object.
Need to keep track of where length goes.
*/
void trpgMemWriteBuffer::Begin(trpgToken tok)
{
Add(tok);
lengths.push_back(curLen);
Add((int32)0);
Add(tok);
lengths.push_back(curLen);
Add((int32)0);
}
/* Push()
Pushes a level on the stack. For defining children.
*/
Pushes a level on the stack. For defining children.
*/
void trpgMemWriteBuffer::Push()
{
Add((trpgToken)TRPG_PUSH);
Add((trpgToken)TRPG_PUSH);
}
/* Pop()
Pops a level off the "stack".
*/
Pops a level off the "stack".
*/
void trpgMemWriteBuffer::Pop()
{
Add((trpgToken)TRPG_POP);
Add((trpgToken)TRPG_POP);
}
/* Will take out a pop from the end of the buffer, if there is one */
@@ -307,60 +307,60 @@ bool trpgMemWriteBuffer::UnPush()
}
/* End()
Finished defining an object.
Write the length out where appropriate.
*/
Finished defining an object.
Write the length out where appropriate.
*/
void trpgMemWriteBuffer::End()
{
if (lengths.size() == 0)
// Note: say something clever here
return;
if (lengths.size() == 0)
// Note: say something clever here
return;
int id = lengths.size()-1;
int32 len = curLen - lengths[id];
int32 rlen = len-sizeof(int32);
if (ness != cpuNess)
rlen = trpg_byteswap_int(rlen);
set(curLen - len,sizeof(int32),(const char *)&rlen);
lengths.resize(id);
int id = lengths.size()-1;
int32 len = curLen - lengths[id];
int32 rlen = len-sizeof(int32);
if (ness != cpuNess)
rlen = trpg_byteswap_int(rlen);
set(curLen - len,sizeof(int32),(const char *)&rlen);
lengths.resize(id);
}
/* Appendable File
This class is used as a simple appendable file. It's used
for dumping tile and texture (or most anything) into.
This class is used as a simple appendable file. It's used
for dumping tile and texture (or most anything) into.
*/
trpgwAppFile::trpgwAppFile(trpgEndian inNess,const char *fileName,bool reuse)
{
Init(inNess,fileName,reuse);
Init(inNess,fileName,reuse);
}
void trpgwAppFile::Init(trpgEndian inNess,const char *fileName,bool reuse)
{
valid = false;
ness = inNess;
cpuNess = trpg_cpu_byte_order();
valid = false;
ness = inNess;
cpuNess = trpg_cpu_byte_order();
if (reuse==false) {
if (!(fp = fopen(fileName,"wb")))
return;
lengthSoFar = 0;
valid = true;
} else {
if (!(fp = fopen(fileName,"ab")))
return;
// ftell is still zero, dammit. Arg.
fseek(fp,0,SEEK_END);
lengthSoFar = ftell(fp);
valid = true;
}
if (reuse==false) {
if (!(fp = fopen(fileName,"wb")))
return;
lengthSoFar = 0;
valid = true;
} else {
if (!(fp = fopen(fileName,"ab")))
return;
// ftell is still zero, dammit. Arg.
fseek(fp,0,SEEK_END);
lengthSoFar = ftell(fp);
valid = true;
}
}
trpgwAppFile::~trpgwAppFile()
{
if (fp)
fclose(fp);
valid = false;
if (fp)
fclose(fp);
valid = false;
}
// Return the amount of data written so far (in total)
@@ -372,87 +372,87 @@ int trpgwAppFile::GetLengthWritten()
// Append the given buffer to our appendable file
bool trpgwAppFile::Append(const trpgMemWriteBuffer *buf1,const trpgMemWriteBuffer *buf2)
{
if (!isValid()) return false;
if (!isValid()) return false;
// Get the total length
int totLen = buf1->length() + (buf2 ? buf2->length() : 0);
// Get the total length
int totLen = buf1->length() + (buf2 ? buf2->length() : 0);
// Write the length out
if (fwrite(&totLen,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
}
// Write the length out
if (fwrite(&totLen,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
}
// Write the data out
const char *data = buf1->getData();
unsigned int len = buf1->length();
if (fwrite(data,sizeof(char),len,fp) != len) {
valid = false;
return false;
}
if (buf2) {
data = buf2->getData();
len = buf2->length();
if (fwrite(data,sizeof(char),len,fp) != len) {
valid = false;
return false;
}
}
// Write the data out
const char *data = buf1->getData();
unsigned int len = buf1->length();
if (fwrite(data,sizeof(char),len,fp) != len) {
valid = false;
return false;
}
if (buf2) {
data = buf2->getData();
len = buf2->length();
if (fwrite(data,sizeof(char),len,fp) != len) {
valid = false;
return false;
}
}
lengthSoFar += totLen;
lengthSoFar += totLen;
return true;
return true;
}
// Append the given raw data to our appendable file
bool trpgwAppFile::Append(const char *data,int size)
{
if (!isValid()) return false;
if (!isValid()) return false;
if (!data)
return false;
if (!data)
return false;
// Write the length out
if (fwrite(&size,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
}
// Write the length out
if (fwrite(&size,sizeof(int32),1,fp) != 1) {
valid = false;
return false;
}
// Write the data out
if (fwrite(data,sizeof(char),size,fp) != (size_t)size) {
valid = false;
return false;
}
// Write the data out
if (fwrite(data,sizeof(char),size,fp) != (size_t)size) {
valid = false;
return false;
}
lengthSoFar += size;
lengthSoFar += size;
return true;
return true;
}
// Flushes the current tile file
bool trpgwAppFile::Flush()
{
// Flush the File *
if (fp)
fflush(fp);
// Flush the File *
if (fp)
fflush(fp);
return true;
return true;
}
// Return the current file position
int64 trpgwAppFile::Pos() const
{
if (!isValid())
return 0;
// Note: This means an appendable file is capped at 2GB
long pos = ftell(fp);
if (!isValid())
return 0;
// Note: This means an appendable file is capped at 2GB
long pos = ftell(fp);
return pos;
return pos;
}
// Validity check
bool trpgwAppFile::isValid() const
{
return valid;
return valid;
}