⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flttohot.cpp

📁 空战游戏flacon源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include "header.h"
#include "namelist.h"

struct materialPalettePtr {
	aflt_MaterialRecord *materialPalette;
	materialPalettePtr	*next;
};

struct rgbaRecord {
	int	r, g, b, a;
};

struct recordPtr {
	char	*record;
	recordPtr	*next;
};

struct treePtr {
	char	*record;
	int		counter, beadcounter, objectid;
	treePtr	*parent, *child, *next, *prev;
	treePtr *subface;
};

struct vertexPtr {
	int	vertexOffset;
	int	vertexNo;
	vertexPtr *next;
};

void getMaterialFromTable (rgbaRecord *col, int matindex);
int	getColorFromTable (int index);
int findVertexNo (int offset);
int convertColor (int color, int intensity);
int checkForLongID (treePtr *tree);
int saveRecordHeader (CFileIO& myfile, char *rec);
void buildTreeRecord (char *rec);
void deleteTreeRecord (treePtr *tree);

void processBounding (treePtr *tree);
void processPolygon (short rectype, treePtr *tree);
void processTreeRecord (treePtr *tree);
void processRecord (treePtr *tree);
void processInfo (treePtr *tree);
void processRecord1 (treePtr *tree);
void processInfo1 (treePtr *tree);
void processInfoSubface (treePtr *tree);
void printTreeRecord (CFileIO& myfile, treePtr *tree);
void printRecord (CFileIO& myfile, treePtr *tree, GLint count);
void printInfo (CFileIO& myfile, treePtr *tree, GLint count);
void printRecord1 (CFileIO& myfile, treePtr *tree, GLint count);
void printInfo1 (CFileIO& myfile, treePtr *tree, GLint count);
void printInfoSubface (CFileIO& myfile, treePtr *tree, GLint count);

void processBeadElement (CFileIO& myfile, treePtr *tree);
void storeVertex (int offset, int no);
void setobjectid (treePtr *tree, GLint objectid);

materialPalettePtr *headmaterialPalette, *currentmaterialPalette;
materialPalettePtr *newmaterialPalette;
recordPtr *headRecord, *currentRecord, *newRecord;
treePtr *treeRoot, *currentTree, *newTree;
treePtr *subfaceRoot, *currentsubfacetree, *currentsubface;
treePtr	*subfaceList[1000];		// only 1000 subfaces allowed
vertexPtr *headVertex, *currentVertex, *newVertex;
char *colorTableRecord;
flt_MaterialRecord *materialTableRecord;
char 	buffer[1024];
int	startflag;
int	vertexno, textureno;
int	inpushmode, inpushsubfacemode, insubfacemode, subfacecounter, usetextureflag;
int	beadcounter, matrixcounter;
int	dofcounter, groupcounter, objectcounter, bspcounter, polygoncounter;
int	switchcounter, bboxcounter, bspherecounter;
int	vertexOffset;
int	fltversion, warningflag;

GLfloat ScaleFactor;
GLint	DisableMask, tSetTotal;
char ansfile[80], colorpatch[80], polyfilename[80];
char dynamicvertex[80];

NameList	polynameList;

void main (int argc, char *argv[])
{
	if (argc < 2) {
		printf ("\nFLTtoHOT v%1.2f by Erick Jap\n", (float) HOT_VERSION/100.f);
		puts ("Usage: FLTtoHOT fltfile [options]");
		puts ("options:");
		puts ("-ohotfile     --> where hotfile is output file");
		puts ("-sScale       --> (Default scale is 1)");
		puts ("-aAnsfile     --> Animation file");
		puts ("-pcolorpatch  --> Vertex Color patch file");
		puts ("-vvertexfile  --> Dynamic Vertex file");
		puts ("-tTexSetNum   --> Number of texture set");
		puts ("-ddisablemask --> Disable mask (1 = disable)");
		puts ("-ffilename    --> Filename consisting of the polygon name");
		puts ("-w            --> Display warning when structure size is different");
		puts ("DisableMask bits:");
		puts ("0x01000000 --> disable shading");
		puts ("0x02000000 --> disable smooth shading");
		puts ("0x04000000 --> disable texture");
		puts ("0x08000000 --> disable transparent texture (chromakey)");
		puts ("0x10000000 --> disable alpha blend");
		puts ("0x20000000 --> disable gouraud (vertex color)");
		puts ("0x40000000 --> always enable perspective texture");
		puts ("0x80000000 --> always enable texture");
		exit (1);
	}

	char infile[80], outfile[80];
	glConcatFileExtension ((GLbyte *) infile, (GLbyte *) argv[1], (GLbyte *) "FLT");
	glConcatFileExtension ((GLbyte *) outfile, (GLbyte *) argv[1], (GLbyte *) "HOT");

	printf ("Converting file %s to %s\n\n", infile, outfile);

	polyfilename[0] = 0;
	ansfile[0] = 0;
	colorpatch[0] = 0;
	dynamicvertex[0] = 0;
	ScaleFactor = (GLfloat) 1.0;
	DisableMask = 0;
	tSetTotal = 1;
	warningflag = 0;
	GLint i;
	for (i=2;i < argc;i++) {
		if (argv[i][0] == '-') {
			if (argv[i][1] == 'f' || argv[i][1] == 'F') {
				strcpy (polyfilename, &(argv[i][2]));
			}
			else if (argv[i][1] == 's' || argv[i][1] == 'S') {
				ScaleFactor = (GLfloat) atof (&(argv[i][2]));
			}
			else if (argv[i][1] == 't' || argv[i][1] == 'T') {
				tSetTotal = atoi (&(argv[i][2]));
			}
			else if (argv[i][1] == 'a' || argv[i][1] == 'A') {
				strcpy (ansfile, &(argv[i][2]));
			}
			else if (argv[i][1] == 'p' || argv[i][1] == 'P') {
				strcpy (colorpatch, &(argv[i][2]));
			}
			else if (argv[i][1] == 'v' || argv[i][1] == 'V') {
				strcpy (dynamicvertex, &(argv[i][2]));
			}
			else if (argv[i][1] == 'o' || argv[i][1] == 'O') {
				glConcatFileExtension ((GLbyte *) outfile, (GLbyte *) &(argv[i][2]), (GLbyte *) "HOT");
			}
			else if (argv[i][1] == 'd' || argv[i][1] == 'D') {
				DisableMask = (GLint) glConvertHexStringToInt ((GLbyte *) &(argv[i][2]));
			}
			else if (argv[i][1] == 'w' || argv[i][1] == 'W') {
				warningflag = 1;
			}
		}
	}
	polynameList.ReadNameList (polyfilename);

	CFileIO myfile;
	if (!myfile.openread (infile)) {
		printf ("Can not open input file %s\n", infile);
		exit (1);
	}
	if (!myfile.openwrite (outfile)) {
		printf ("Can not open output file %s\n", outfile);
		exit (1);
	}

	sprintf (buffer, "HOT file format v%1.2f by Erick Jap ", (float) HOT_VERSION/100.f);
	myfile.writedata (buffer);
	myfile.writedata ("(last updated on April 23, 1997)\n\n");
	myfile.writedata ("Input File = ");
	myfile.writedata (infile);
	myfile.writedata ("\n\n");

	puts ("Reading data");

	while (!myfile.eof()) {
		short rectype = swap_short (myfile.read_short ());
		short reclen = swap_short (myfile.read_short ());
		if (rectype == OPCODE_HEADER) {
			char *buf = (char *) glAllocateMemory (reclen);
			if (!buf) {
				printf ("\nCan not allocate memory for header\n");
				exit (1);
			}
			short *sbuf = (short *) buf;
			*sbuf++ = rectype;
			*sbuf++ = reclen;
			if (reclen > 4) myfile.readdata ((char *) sbuf, reclen-4);
			flt_HeaderRecord *header;
			header = (flt_HeaderRecord *) buf;
			fltversion = swap_int(header -> formatRev);
			glReleaseMemory (buf);
			break;
		}
		if (reclen > 4) myfile.movefileptr (reclen-4, FILEIO_SEEK_CUR);
	}
	myfile.movefileptr ();

	usetextureflag = 0;
	while (!myfile.eof()) {
		short rectype = getRecordType(myfile);
		if (rectype == OPCODE_TEXTURE_REFERENCE_RECORD) {
			usetextureflag = 1;
			break;
		}
	}
	myfile.movefileptr ();

	while (!myfile.eof()) {
		short rectype = swap_short (myfile.read_short ());
		short reclen = swap_short (myfile.read_short ());
		if (rectype == OPCODE_TEXT_COMMENT) {
			char tempbuf[256];
			memset (buffer, 0, 1024);
			myfile.read_string (buffer);
			int stringlen = 0;
			while (1) {
				if (!buffer[stringlen]) break;
				sscanf (buffer+stringlen, "%s", tempbuf);
				stringlen += strlen (tempbuf) + 1;
				strupr (tempbuf);
				if (!strcmp (tempbuf, "ANIMATION")) {
					sscanf (buffer+stringlen, "%s", ansfile);
					stringlen += strlen (ansfile) + 1;
				}
				else if (!strcmp (tempbuf, "VERTEXCOLORPATCH")) {
					sscanf (buffer+stringlen, "%s", colorpatch);
					stringlen += strlen (colorpatch) + 1;
				}
				else if (!strcmp (tempbuf, "DYNAMICVERTEX")) {
					sscanf (buffer+stringlen, "%s", dynamicvertex);
					stringlen += strlen (dynamicvertex) + 1;
				}
				else if (!strcmp (tempbuf, "DISABLEMASK")) {
					sscanf (buffer+stringlen, "%s", tempbuf);
					stringlen += strlen (tempbuf) + 1;
					DisableMask = (GLint) glConvertHexStringToInt ((GLbyte *) tempbuf);
				}
				else if (!strcmp (tempbuf, "TEXTURESET")) {
					sscanf (buffer+stringlen, "%s", tempbuf);
					stringlen += strlen (tempbuf) + 1;
					tSetTotal = atoi (tempbuf);
				}
				else if (!strcmp (tempbuf, "SCALE")) {
					sscanf (buffer+stringlen, "%s", tempbuf);
					stringlen += strlen (tempbuf) + 1;
					ScaleFactor = (GLfloat) atof (tempbuf);
				}
			}
			break;
		}
		if (reclen > 4) myfile.movefileptr (reclen-4, FILEIO_SEEK_CUR);
	}
	myfile.movefileptr ();

	puts ("Processing data");
	colorTableRecord = 0;
	materialTableRecord = 0;
	headmaterialPalette = currentmaterialPalette = 0;
	headRecord = currentRecord = 0;
	headVertex = currentVertex = 0;
	vertexno = 0;
	textureno = 0;
	startflag = 0;
	vertexOffset = 0;

	while (!myfile.eof()) {
		char *record = getRecord (myfile, fltversion, warningflag);
		if (!record) exit (1);
		swapRecord (record, fltversion);
		int i = saveRecordHeader (myfile, record);
		if (i) {
			newRecord = (recordPtr *) glAllocateMemory (sizeof(recordPtr));
			newRecord -> record = record;
			newRecord -> next = 0;
			if (headRecord) {
				currentRecord -> next = newRecord;
				currentRecord = newRecord;
			}
			else headRecord = currentRecord = newRecord;
		}
		else {
			short *srec = (short *) record;
			short rectype = *srec;
			if (rectype == OPCODE_COLOR_TABLE) {
				colorTableRecord = record;
			}
			else if (rectype == OPCODE_MATERIAL_TABLE) {
				materialTableRecord = (flt_MaterialRecord *) record;
			}
			else if (rectype == OPCODE_MATERIAL_PALETTE) {
				newmaterialPalette = (materialPalettePtr *) glAllocateMemory (sizeof(materialPalettePtr));
				newmaterialPalette -> next = 0;
				newmaterialPalette -> materialPalette = (aflt_MaterialRecord *) record;
				if (headmaterialPalette) {
					currentmaterialPalette -> next = newmaterialPalette;
					currentmaterialPalette = newmaterialPalette;
				}
				else {
					headmaterialPalette = currentmaterialPalette = newmaterialPalette;
				}
			}
			else glReleaseMemory ((char *) record);
		}
	}

	if (!colorTableRecord) {
		puts ("Error! Can not find Color Table Record");
	}

	if (startflag & 3) {
		myfile.writedata ("ZZZZ\n\n");
		if (startflag & 1) startflag &= ~1;
		else if (startflag & 2) startflag &= ~2;
	}

	puts ("Building tree");
	inpushmode = 0;
	insubfacemode = 0;
	inpushsubfacemode = 0;
	subfacecounter = 0;
	treeRoot = 0;
	subfaceRoot = 0;
	newRecord = headRecord;
	while (newRecord) {
		currentRecord = newRecord;
		buildTreeRecord (newRecord -> record);
		newRecord = newRecord -> next;
		glReleaseMemory ((char *) currentRecord);
	}

	dofcounter = groupcounter = objectcounter = bspcounter = polygoncounter = 0;
	switchcounter = bboxcounter = bspherecounter = 0;
	beadcounter = 0;
	matrixcounter = 0;

	puts ("Processing Info");
	processBounding (treeRoot);
	processInfo (treeRoot);
	processInfo1 (treeRoot);
	printInfo (myfile, treeRoot, 0);
	printInfo1 (myfile, treeRoot, 0);

	puts ("Processing Bead Element");
	processBeadElement (myfile, treeRoot);

	puts ("Clean up");
	glReleaseMemory ((char *) colorTableRecord);
	glReleaseMemory ((char *) materialTableRecord);

	while (headmaterialPalette) {
		currentmaterialPalette = headmaterialPalette;
		headmaterialPalette = headmaterialPalette -> next;
		glReleaseMemory (currentmaterialPalette);
	}

	while (headVertex) {
		newVertex = headVertex;
		headVertex = headVertex -> next;
		glReleaseMemory ((char *) newVertex);
	}
	deleteTreeRecord (treeRoot); 
	puts ("Done");
	exit (0);
}

int findVertexNo (int offset)
{
	newVertex = headVertex;
	while (newVertex) {
		if (newVertex -> vertexOffset == offset)
			return (newVertex -> vertexNo);
		newVertex = newVertex -> next;
	}
	return 0;
}

void storeVertex (int offset, int no)
{
	newVertex = (vertexPtr *) glAllocateMemory (sizeof (vertexPtr));
	newVertex -> vertexOffset = offset;
	newVertex -> vertexNo = no;
	newVertex -> next = 0;
	if (headVertex) {
		currentVertex -> next = newVertex;
		currentVertex = newVertex;
	}
	else headVertex = currentVertex = newVertex;
}

void deleteTreeRecord (treePtr *tree) 
{
	if (tree) {
		deleteTreeRecord (tree -> subface);
		deleteTreeRecord (tree -> child);
		deleteTreeRecord (tree -> next);
		glReleaseMemory ((char *) tree -> record);
		glReleaseMemory ((char *) tree);
	}
}

// set objectid for every polygons
void processPolygon (short rectype, treePtr *tree)
{
	short rectype1 = rectype;
	if (rectype == OPCODE_LONG_IDENTIFIER) {
		short *srec = (short *) tree -> prev -> record;
		rectype1 = *srec;
	}
	if (rectype1 == OPCODE_OBJECT) {
		treePtr *polytree = tree -> child;
		if (polytree) {
			short *srec2 = (short *) polytree -> record;
			short rectype2 = *srec2;
			if (rectype2 == OPCODE_POLYGON) {
				setobjectid (polytree, tree -> counter);
			}
		}
	}
}

void setobjectid (treePtr *tree, GLint objectid)
{
	if (tree) {
		tree -> objectid = objectid;
		setobjectid (tree -> subface, objectid);
		setobjectid (tree -> next, objectid);
	}
}

void processBounding (treePtr *tree) 
{
	if (tree) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -