/*********************/ /*** Texture Cache ***/ /*********************/ #include "stdafx.h" #include "gl3d.h" #include #include #include #include // Header File For The Glaux Library #include #include "TexCache.h" #include "utils.h" /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ int CTexCache::GetTexIdx(char *Filename,int Flags) { sTex Tex; strcpy(Tex.Filename,Filename); Tex.Flags=Flags; return(TexList.Find(Tex)); } /*****************************************************************************/ // Checks loaded files for dups, assumes all passed RGB is unique int CTexCache::ProcessTexture(char *Filename,int Flags,sRGBData *RGBData) { int ListSize=TexList.size(); sTex NewTex; sRGBData ThisRGB; strcpy(NewTex.Filename,Filename); NewTex.Flags=Flags; NewTex.Flags=Flags; if (!RGBData) // Need to load file { int Idx=GetTexIdx(NewTex); // Is already loaded? if (Idx!=-1) return(Idx); TRACE1("Loading Texture %s\n",NewTex.Filename); LoadBMP(NewTex.Filename,ThisRGB); RGBData=&ThisRGB; LoadTex(NewTex,RGBData); FreeBMP(ThisRGB); NewTex.Loaded=TRUE; } else { LoadTex(NewTex,RGBData); NewTex.Loaded=FALSE; } TexList.push_back(NewTex); return(ListSize); } /**************************************************************************************/ /**************************************************************************************/ /**************************************************************************************/ const int TexAlignTable[]={1,2,4,8,16,32,64,128,256}; const int TexAlignTableSize=sizeof(TexAlignTable)/sizeof(int); int CTexCache::AlignSize(int Size) { for (int i=0;iTexAlignTable[i] && SizesizeX; RGBData.Height=Aux->sizeY; RGBData.RGB=Aux->data; free(Aux); // Safe to free aux now, contents copied (I HATE AUX) } /**************************************************************************************/ void CTexCache::FreeBMP(sRGBData &RGBData) { if (RGBData.RGB) { free((unsigned char*)RGBData.RGB); } } /**************************************************************************************/ void CTexCache::LoadTex(sTex &ThisTex,sRGBData *TexData) { std::vector Buffer; int TexWidth=TexData->Width; int TexHeight=TexData->Height; int GLWidth=AlignSize(TexWidth); int GLHeight=AlignSize(TexHeight); u8 *Src,*Dst; // create RGB & alpha texture & ensuse texture is correct size for GL Buffer.resize(GLWidth*GLHeight*4); Src=TexData->RGB; Dst=&Buffer[0]; for (int Y=0; Y