|
|
发表于 2006-6-4 14:03:00
|
显示全部楼层
Re:在2D游戏中如何加载TGA图片
先LoadHead,再LoadFileImage.
int loadTGAhead(char *file) /*读TGA文件头*/
{
struct TGA_HEAD tgahead;
int i,j,flag=0;
FILE *fp;
BYTE *p;
fp=fopen(file,"rb"); /*打开图像文件*/
if (fp != NULL) {
flag=1;
fseek(fp,0L,SEEK_SET);
fread(&tgahead,18,1,fp); /*读入图像文件头*/
/*以下取出图像参数*/
p=(BYTE*) &tgahead;
Width = p[12]+256*p[13]; // tgahead.Width;
Height = p[14]+256*p[15]; // tgahead.Height;
BitCount = p[16]; // tgahead.Bits;
BytesPerLine = Width * BitCount / 8;
Compression = p[2] & 0x8; // tgahead.ImageType;
ImageType = p[2] & 0x3;
Planes=1;
ColorNum = p[5]+256*p[6];
OffBits=18;
if (ImageType!=2) OffBits += 3*ColorNum;
p=(BYTE *) T_pal;
if (ImageType==1) { /*文件中有调色板*/
fread(p,1,768,fp); /*读入调色板数据*/
for (i=0;i<768;i+=3) { /*红蓝分量交换*/
j=p; p=p[i+2];
p[i+2]=j;
}
}
else if (ImageType==3) { /*黑白图像补上调色板*/
for (i=0;i<256;i++) {
for (j=0;j<3;j++) p[3*i+j]=i;
}
ColorNum = 256;
}
fclose(fp); /*关闭图像文件*/
}
return(flag);
}
;
void loadTGAimage(char *file,int px,int py,int Dx,int Dy,int sx,int sy)
{ /*读入并显示彩色图像*/
int i,dw,dh;
FILE *fp;
BYTE *sc;
i=loadTGAhead(file); /*读TGA文件头*/
if (i==0) return;
dw=min(Width -px,Dx); dw=min(dw,swide-sx); /*确定视口尺寸*/
dh=min(Height-py,Dy); dh=min(dh,sdeep-sy);
if (T_bitpp<=8)
set_allpalette(T_pal); /*设置VGA调色板寄存器*/
else {
px *= T_bpp;
if (BitCount<=8)
set_Vpalette((BYTE *)T_pal,(union COLOR_V *)bgr);
}
sc=malloc(4*Width); /*申请存储单元*/
fp=fopen(file,"rb"); /*打开图像文件*/
fseek(fp,OffBits,SEEK_SET); /*设置取数指针*/
for (i=Height-1;i>=0;i--) {
fread(sc,BytesPerLine,1,fp); /*读一行图像数据*/
if (T_bitpp==32) Trans_24to32(sc,3*Width); /*32Bit显示模式*/
if ((BitCount<=8)&&(T_bitpp>=24))
trans_8toColor(sc,Width);
if ((i>=py)&&(i<py+dh))
put_image(sx,i+sy-py,dw,1,&sc[px]); /*显示一行*/
}
fclose(fp); /*关闭图像文件*/
free(sc); /*释放存储单元*/
}
主要的就是这2个函数了,参数命名都应该能看明白吧
put_image采用bitblt函数 |
|