SBSPSS/Utils/MapEdit/TexCache.cpp

164 lines
4.0 KiB
C++
Raw Normal View History

2000-10-27 20:18:30 +02:00
/*********************/
/*** Texture Cache ***/
/*********************/
#include "stdafx.h"
#include "gl3d.h"
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>
2000-11-22 23:08:47 +01:00
#include <gl\glaux.h> // Header File For The Glaux Library
2000-10-27 20:18:30 +02:00
#include <Vector>
#include "TexCache.h"
#include "utils.h"
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2000-11-22 23:08:47 +01:00
int CTexCache::GetTexIdx(char *Name,int Flags)
2000-10-27 20:18:30 +02:00
{
int ListSize=TexList.size();
2000-11-22 23:08:47 +01:00
2000-10-27 20:18:30 +02:00
for (int Count=0;Count<ListSize;Count++)
{
2000-11-22 23:08:47 +01:00
if (strcmp(Name,TexList[Count].Name)==0 && TexList[Count].Flags==Flags)
2000-10-27 20:18:30 +02:00
{
2000-10-27 22:21:39 +02:00
return(Count);
2000-10-27 20:18:30 +02:00
}
}
2000-11-22 23:08:47 +01:00
return(-1);
}
/*****************************************************************************/
int CTexCache::ProcessTexture(char *TexName,char *Path,int Flags,sRGBData *RGBData)
{
int ListSize=TexList.size();
int Idx;
char Name[_MAX_FNAME];
_splitpath(TexName,0,0,Name,0);
// Check if Tex exists
/* for (int Count=0;Count<ListSize;Count++)
{
if (strcmp(Name,TexList[Count].Name)==0 && TexList[Count].Flags==Flags)
{
return(Count);
}
}
*/
Idx=GetTexIdx(Name,Flags);
if (Idx!=-1) return(Idx);
sTex NewTex;
sRGBData ThisRGB;
strcpy(NewTex.Name,Name);
2000-10-27 22:21:39 +02:00
strcpy(NewTex.Path,Path);
NewTex.Flags=Flags;
2000-11-22 23:08:47 +01:00
if (!RGBData) // Need to load file
{
char Filename[_MAX_PATH];
sprintf(Filename,"%s%s.Bmp",Path,Name);
TRACE1("Loading Texture %s\n",Filename);
LoadBMP(Filename,ThisRGB);
RGBData=&ThisRGB;
LoadTex(NewTex,RGBData);
FreeBMP(ThisRGB);
}
else
{
LoadTex(NewTex,RGBData);
}
2000-10-27 20:18:30 +02:00
TexList.push_back(NewTex);
2000-11-22 23:08:47 +01:00
return(ListSize);
2000-10-27 20:18:30 +02:00
}
2000-11-22 23:08:47 +01:00
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
void CTexCache::LoadBMP(char *Filename,sRGBData &RGBData)
{
FILE *File=NULL;
AUX_RGBImageRec *Aux;
Aux=auxDIBImageLoad(Filename);
RGBData.Width=Aux->sizeX;
RGBData.Height=Aux->sizeY;
2000-11-24 23:34:20 +01:00
RGBData.RGB=Aux->data;
2000-11-22 23:08:47 +01:00
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)
{
2000-11-28 00:07:07 +01:00
std::vector<u8> Buffer;
int Width=TexData->Width;
int Height=TexData->Height;
int Size=Width*Height;
// create RGB & alpha texture
Buffer.resize(Size*4);
u8 *RgbPtr=TexData->RGB;
for (int i=0;i<Size;i++)
{
u8 R=*RgbPtr++;
u8 G=*RgbPtr++;
u8 B=*RgbPtr++;
u8 A=255;
if ((R==BlankRGB.rgbRed && G==BlankRGB.rgbGreen && B==BlankRGB.rgbBlue)) // Create alpha for transparent pixels (flagged with PINK!!)
{
A=0;
}
Buffer[(i*4)+0]=R;
Buffer[(i*4)+1]=G;
Buffer[(i*4)+2]=B;
Buffer[(i*4)+3]=A;
}
ThisTex.Width=Width;
ThisTex.Height=Height;
2000-11-22 23:08:47 +01:00
glGenTextures(1, &ThisTex.TexID);
glBindTexture(GL_TEXTURE_2D, ThisTex.TexID);
2000-11-28 00:07:07 +01:00
glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &Buffer[0]);
2000-11-22 23:08:47 +01:00
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
2000-11-14 16:03:04 +01:00
void CTexCache::Purge()
{
int ListSize=TexList.size();
TRACE1("Purging %i textures\n",ListSize);
for (int i=0; i<ListSize; i++)
{
glDeleteTextures(1,&TexList[i].TexID);
}
TexList.clear();
}