SBSPSS/Utils/MapEdit/TexCache.cpp

138 lines
3.5 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)
{
ThisTex.Width=TexData->Width;
ThisTex.Height=TexData->Height;
glGenTextures(1, &ThisTex.TexID);
glBindTexture(GL_TEXTURE_2D, ThisTex.TexID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TexData->Width, TexData->Height, 0, GL_RGB, GL_UNSIGNED_BYTE, TexData->RGB);
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();
}