📄 texture.cpp
字号:
#include "..\System\simplememory.h"
#include "..\System\resfile.h"
#include "Texture.h"
extern CSimpleMemory *SMemory;
extern CResFile *pResFile;
CTexture::CTexture()
{
m_w = -1;
m_h = -1;
m_bpp = 0;
m_flag = 0;
m_npal = -1;
m_ppalData = NULL;
m_pData = NULL;
m_expp = NULL;
}
CTexture::~CTexture()
{
m_w = -1;
m_h = -1;
m_bpp = 0;
m_flag = 0;
m_npal = -1;
m_ppalData = NULL;
m_pData = NULL;
m_expp = NULL;
}
bool CTexture::LoadTexData(const char *filename, int bIndex)
{
CResFile *pRes = pResFile;
CBufReader bufReader;
int rest_size = pRes->LoadFileToStack(filename,&m_pFile);
if( m_pFile )
{
bufReader.CBufReader_init(m_pFile,rest_size);
}
else
return false;
bool re = LoadTga(&bufReader, bIndex);
SMemory->StackFree(0);
return re;
}
bool CTexture::LoadTga(CBufReader *r, int bIndex)
{
int taginfo;
taginfo = r->ReadChar();
if (taginfo) return false;//if has the information of the tga ,return error.
int pal_typ;
pal_typ = r->ReadChar();//if the pal type is not 1,return error.
if (pal_typ !=1) return false;
int tga_typ;
tga_typ = r->ReadChar();// the tga type is 1.
int pal_start;
pal_start = r->ReadUShort();
int pal_size;
pal_size = r->ReadUShort();
int pal_bit;
pal_bit = r->ReadChar();
int start_x = r->ReadUShort();
int start_y = r->ReadUShort();
int tex_w = r->ReadUShort();
int tex_h = r->ReadUShort();
if (tex_w != tex_h) return false;
int pixel_size = r->ReadChar();
if (pixel_size != 8) return false ;// not use the palette
int tga_attri = r->ReadChar();
byte *temp;
int i, j;
if(pal_bit == 16) {
m_ppalData = (unsigned short*)SMemory->HeapMalloc(pal_size * 2);
m_bpp = 16;
r->ReadBlock(m_ppalData,pal_size * 2);
}
if(pal_bit == 24) {
m_bpp = 24;
m_ppalData = (unsigned short*)SMemory->HeapMalloc(pal_size * 2);
temp = (byte*)SMemory->StackMalloc(pal_size * 3);
r->ReadBlock(temp,pal_size * 3);
i=j=0;
while (i<pal_size<<1) {
m_ppalData[i++] = ((temp[j+2]&0xF8)<<8)|((temp[j+1]&0xFC)<<3)|((temp[j]&0xF8)>>3);
j+=3;
}
SMemory->StackFree(0);
}
if(pal_bit == 32) {
m_bpp = 32;
// m_32pal = (unsigned int*)SMemory->HeapMalloc(pal_size * 4);
// r->ReadBlock(m_32pal, pal_size * 4);
}
if (bIndex)
m_pData = (byte*)SMemory->HeapMalloc( tex_w * tex_h );
else
m_pData = (byte*)SMemory->HeapMalloc( tex_w * tex_h *2);
r->ReadBlock(m_pData, tex_w*tex_h);
if (!bIndex) {
unsigned short *p = (unsigned short *)m_pData;
for (i=tex_w*tex_h-1; i>=0; i--)
p[i] = m_ppalData[m_pData[i]];
}
m_npal = pal_size;
m_w = tex_w;
m_h = tex_h;
switch(m_w) {
case 512:
m_shift = 9;
break;
case 256:
m_shift = 8;
break;
case 128:
m_shift = 7;
break;
case 64:
m_shift = 6;
break;
case 32:
m_shift = 5;
break;
case 16:
m_shift = 4;
break;
case 8:
m_shift = 3;
break;
}
m_mask = m_w-1;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -