SBSPSS/Utils/Libs/DaveLib/DaveLib.cpp

218 lines
4.4 KiB
C++
Raw Normal View History

2001-02-08 15:04:49 +01:00
/*************************/
/*** Dave Lib Routines ***/
/*************************/
#include <ginio.h>
#include <gintex.h>
#include "stdio.h"
#include <math.h>
#include <repread.h>
#include <misc.hpp>
#include <conio.h>
#include <iostream.h>
#include <vector>
#include <gfname.hpp>
#include "psxtypes.h"
#include "FaceStore.h"
2001-03-01 18:03:15 +01:00
#include <io.h>
2001-02-08 15:04:49 +01:00
#include "..\..\..\tools\data\include\dStructs.h"
#include "DaveLib.h"
GString OutStr,IncludeFile;
bool DebugOn= false;
float Scale=1.0;
int StripLength=3;
FileCycle MyFiles;
//***************************************************************************
void DebugStr() {if (DebugOn) std::cout<<endl;}
void DebugStr(char *Txt) {if (DebugOn) std::cout<<Txt<<endl;}
void DebugStr(char *Txt,int Param) {if (DebugOn) std::cout<<Txt<<" "<<Param<<endl;}
void DebugStr2(char *Txt,int Param) {if (DebugOn) std::cout<<Txt<<" "<<Param<<" ";}
//***************************************************************************
int round(float f)
{
if (f<0)
{
return (int)(f-0.5f);
}
else
{
return (int)(f+0.5f);
}
}
//***************************************************************************
2001-04-01 22:22:49 +02:00
/*
2001-02-08 15:04:49 +01:00
void alignFile( FILE *f, int align )
{
long fp = ftell(f);
if (fp&(align-1))
{
char buffer[] = "*********************************";
int s = align - (fp & (align-1));
fwrite(buffer, s, 1, f);
}
}
2001-04-01 22:22:49 +02:00
*/
//***************************************************************************
void PadFile(FILE *File)
{
int Pad=ftell(File) & 3;
if (Pad)
{
fwrite(&Pad,Pad,1,File);
}
}
2001-02-08 15:04:49 +01:00
//***************************************************************************
GString CheckFileString(const char * Str)
{
GString RetStr;
if (strlen(Str)>3 && (Str[0]=='-' || Str[0]=='/') && (Str[2]==':' || Str[2]==':'))
RetStr=&Str[3];
else
GObject::Error(ERR_FATAL,"Problem with cmd line option %s",Str);
return(RetStr);
}
//***************************************************************************
bool CheckPlusMinus(const char * Str)
{
if (strlen(Str)==3 && (Str[0]=='-' || Str[0]=='/') && (Str[2]=='-' || Str[2]=='+'))
{
}
else
GObject::Error(ERR_FATAL,"Problem with cmd line option %s",Str);
return(Str[2]=='+');
}
//***************************************************************************
int ZeroAndCountCommas(char * Str)
{
int Len=strlen(Str);
int NumOfCommas=0;
for (int f=0;f<Len;f++)
{
if (Str[f]==',')
{
NumOfCommas++;
Str[f]=0;
}
}
return(NumOfCommas);
}
2001-03-01 18:03:15 +01:00
//***************************************************************************
char *FindFile(const char *Name)
{
int Handle;
GFName DirName(Name);
GString Filename,Path;
_finddata_t FindBlock;
int Status;
// Does this file exist
FILE *File=fopen(Name,"rb");
if (File)
{
fclose(File);
return((char*)Name);
}
// Recurse dirs to find file
Filename=DirName.File();
Filename+=".";
Filename+=DirName.Ext();
DirName.File("*");
DirName.Ext("*");
Path=DirName.FullName();
Name=0;
Status=Handle= _findfirst(Path,&FindBlock);
while (Status!=-1 && !Name)
{
if (FindBlock.attrib&_A_SUBDIR)
{
if (strcmp(FindBlock.name,".") && strcmp(FindBlock.name,".."))
{
GString NewName;
NewName=DirName.Drive();
NewName+=DirName.Dir();
NewName+=FindBlock.name;
NewName+="/";
NewName+=Filename;
Name=FindFile(NewName);
}
}
if (!Name) Status=_findnext(Handle,&FindBlock);
}
return((char*)Name);
}
2001-04-01 22:22:49 +02:00
/**************************************************************************************/
struct sTgaHdr
{
char id; // 0
char colmaptype; // 1
char imagetype; // 2
char fei[2]; // 3
char cml[2]; // 5
char cmes; // 7
short xorig; // 8
short yorig; // 10
short width; // 12
short height; // 14
char depth; // 15
char imagedesc; // 16
};
void SaveTGA(char *Filename,int W,int H,u8 *Data,bool IsBGR)
{
FILE *File;
sTgaHdr FileHdr;
File=fopen(Filename,"wb");
memset(&FileHdr,0 ,sizeof(sTgaHdr));
FileHdr.imagetype= 2; //imagetype
FileHdr.width = W;
FileHdr.height= H;
FileHdr.depth=24;
fwrite(&FileHdr,sizeof(sTgaHdr),1,File);
if (!IsBGR)
{
fwrite(Data,W*H*3,1,File);
}
else
{
int Size=W*H;
for (int i=0; i<Size;i++)
{
fwrite(&Data[2],1,1,File);
fwrite(&Data[1],1,1,File);
fwrite(&Data[0],1,1,File);
Data+=3;
}
}
fclose(File);
}
//***************************************************