#include #include #include #include #include "txd.h" unsigned int unknown[1][4] = { {0x21312bc0, 0x20059806, 0x00000100, 0x00004800}, }; void writedword(int dword, FILE *file) { fwrite(&dword, 4, 1, file); } void WritePixelHeader(int width, int height, int bpp, unsigned char *indices, int isswizzled, FILE *txd) { int mul; if (bpp == 4) mul = 8; else if (bpp == 8) mul = 4; else exit(3); if (isswizzled) { width /= 2; height /= 2; } writedword(0x00000003, txd); writedword(0x10000000, txd); writedword(0x0000000E, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000051, txd); writedword(0x00000000, txd); writedword(width, txd); writedword(height, txd); writedword(0x00000052, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000053, txd); writedword(0x00000000, txd); writedword(width*height/mul, txd); writedword(0x08000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); } void WritePixels(int width, int height, int bpp, unsigned char *indices, int isswizzled, FILE *txd) { int i; int temp; temp = width * height * bpp / 8; printf("%d\n", temp); for (i = 0; i < temp; i++) putc(indices[i], txd); } void WritePaletteHeader(int bpp, unsigned char colors[][4], int isswizzled, FILE *txd) { int width, height; if (bpp == 8) { width = 16; height = 16; } else if (rwvers == 0x310) { width = 8; height = 2; } else if (rwvers == 0x0C02FFFF) { width = 8; height = 3; } else exit(3); writedword(0x00000003, txd); writedword(0x10000000, txd); writedword(0x0000000E, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000051, txd); writedword(0x00000000, txd); writedword(width, txd); writedword(height, txd); writedword(0x00000052, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); writedword(0x00000053, txd); writedword(0x00000000, txd); writedword(width*height/4, txd); writedword(0x08000000, txd); writedword(0x00000000, txd); writedword(0x00000000, txd); } void WritePalette(int bpp, unsigned char colors[][4], FILE *txd) { int i; int temp; temp = (int) pow((double) 2, (double) bpp); for (i = 0; i < temp; i++) { putc(colors[i][0], txd); putc(colors[i][1], txd); putc(colors[i][2], txd); putc(colors[i][3], txd); } } void MakeTexture(int width, int height, int bpp, unsigned char *indices, unsigned char colors[][4], char *name, char *alpha, FILE *txd) { int sectionsize; char *platform; int namelength, alphalength; char *newname, *newalpha; int pallength; int isswizzled; platform = "PS2\0"; pallength = (int) pow((double) 2, (double) bpp); namelength = strlen(name); alphalength = strlen(alpha); /* Make length multiple of four */ namelength += (4 - (namelength % 4)); alphalength += (4 - (alphalength % 4)); if (namelength == 0) namelength = 4; if (alphalength == 0) alphalength = 4; /* Make buffers for 4 byte aligned strings */ newname = (char *) malloc(namelength); newalpha = (char *) malloc(alphalength); strcpy(newname, name); strcpy(newalpha, alpha); /* Write to the file */ fseek(txd, 0, SEEK_END); sectionsize = 8*12; /* section headers */ sectionsize += alphalength + namelength; /* texture and alpha name */ sectionsize += 4; /* Sky Mimap Val */ sectionsize += 8; /* first struct */ sectionsize += 64; /* second struct */ sectionsize += 2*80; /* blocks before palette and indices */ sectionsize += pallength*4; /* palette */ sectionsize += width*height; WriteHeader(TEXTURENATIVE, sectionsize, rwvers, txd); WriteHeader(STRUCT, 8, rwvers, txd); fwrite(platform, 1, 4, txd); writedword(0x1106, txd); WriteHeader(STRING, namelength, rwvers, txd); fwrite(newname, namelength, 1, txd); WriteHeader(STRING, alphalength, rwvers, txd); fwrite(newalpha, alphalength, 1, txd); sectionsize = 2*12 + 64 + 2*80 + pallength*4 + width*height; WriteHeader(STRUCT, sectionsize, rwvers, txd); WriteHeader(STRUCT, 64, rwvers, txd); fwrite(&width, 4, 1, txd); fwrite(&height, 4, 1, txd); fwrite(&bpp, 4, 1, txd); if (bpp == 4) { writedword(0x00022504, txd); } else if (bpp == 8) { writedword(0x00024504, txd); } else exit(2); writedword(unknown[0][0], txd); writedword(unknown[0][1], txd); writedword(unknown[0][2], txd); writedword(0, txd); writedword(0x00004000, txd); writedword(0x00400004, txd); writedword(0x00004000, txd); writedword(0x00400004, txd); writedword(width*height*bpp/8 + 0x50, txd); writedword(pallength*4 + 0x50, txd); writedword(unknown[0][3], txd); writedword(0x00000FC0, txd); sectionsize = 2*80 + 256*4 + width*height; WriteHeader(STRUCT, sectionsize, rwvers, txd); isswizzled = 1; WritePixelHeader(width, height, bpp, indices, isswizzled, txd); WritePixels(width, height, bpp, indices, isswizzled, txd); WritePaletteHeader(bpp, colors, isswizzled, txd); WritePalette(bpp, colors, txd); WriteHeader(EXTENSION, 16, rwvers, txd); WriteHeader(SKYMIPMAPVAL, 4, rwvers, txd); putc(0xC0, txd); putc(0x0F, txd); putc(0x00, txd); putc(0x00, txd); } void UpdateTexCount(FILE *txd, int texcount) { int oldaddr; oldaddr = ftell(txd); fseek(txd, 24, 0); fwrite(&texcount, 4, 1, txd); fseek(txd, oldaddr, 0); } void UpdateTexDict(FILE *txd) { int filesize; int oldaddr; oldaddr = ftell(txd); /* Update section size of Texture Dictionary */ fseek(txd, 0, SEEK_END); filesize = ftell(txd); fseek(txd, 4, 0); filesize -= 12; fwrite(&filesize, 4, 1, txd); fseek(txd, oldaddr, 0); }