Original Post
Hi there! I´m making a function to load a 32 x 32 pixel TGA file (with an alpha channel) and store it in a structure. Since I already know that all the TGA files I´ll load with that function are 32x32 pixels, I´ve decided to optimize the function a little bit by skiping all the header information and extracting just the color data. The function is quite short and the data structure is very simple but, although I´ve spent countless hours trying to make it work, I´m afraid I have not been successful since the data is not properly loaded inside the data structure. Can somebody help me? I´ll post the function and the data structure:
typedef struct _Tile {
unsigned char ucImageData[32*32*3]; // Allocate space for the R,G,B channels.
unsigned char ucCollisionMask[32*32]; // Allocate space for the alpha channel.
} Tile,*lpTile;
int LoadTileTGA(char *lpFileName,Tile &rTile)
{
FILE *lpFile;
int iCounter,iCounter2,iCounter3;
unsigned char buffer[32*32*4];
lpFile = fopen(lpFileName,"rb");
if(!lpFile)
return 0;
fseek(lpFile,-(4096),SEEK_END); // 32*32*4 = 4096
fread(buffer,sizeof(unsigned char),4096,lpFile);
for(iCounter=0,iCounter2=0,iCounter3=0;iCounter<1024;
iCounter+=4,iCounter2+=3,iCounter3++) {
rTile.ucImageData[iCounter2] = buffer[iCounter+2];
rTile.ucImageData[iCounter2+1] = buffer[iCounter+1];
rTile.ucImageData[iCounter2+2] = buffer[iCounter];
rTile.ucCollisionMask[iCounter3] = buffer[iCounter+3];
} // End for iCounter.
return 1;
} // End LoadTileTGA().
Thanks in advance for your help!!
--Ignacio Liverotti
iliverotti@hotmail.com