⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcx.h

📁 c游戏编程从入门到精通_全部源代码和文档
💻 H
字号:
typedef struct pcx_header_typ
	{
	char manufacturer;
	char version;
	char encoding;
	char bits_per_pixel;
	int x,y;
	int width,height;
	int horz_res;
	int vert_res;
	char ega_palette[48];
	char reserved;
	char num_color_planes;
	int bytes_per_line;
	int palette_type;
	char padding[58];

	} pcx_header, *pcx_header_ptr;

typedef struct RGB_color_typ
{
unsigned char red;
unsigned char green;
unsigned char blue;
}RGB_color,*RGB_color_ptr;



typedef struct pcx_picture_typ
	{
	pcx_header header;
	RGB_color palette[256];
	char far *buffer;

	} pcx_picture, *pcx_picture_ptr;


void Set_Palette_Register(int index,RGB_color_ptr color)
{
outp(PALETTE_MASK,0xff);
outp(PALETTE_REGISTER_WR,index);
outp(PALETTE_DATA,color->red);
outp(PALETTE_DATA,color->green);
outp(PALETTE_DATA,color->blue);
}


void PCX_Load_Screen(char *filename,int enable_palette)
{
// this function loads a pcx file into a picture structure, the actual image
// data for the pcx file is decompressed and expanded into a secondary buffer
// within the picture structure, the separate images can be grabbed from this
// buffer later.  also the header and palette are loaded

FILE *fp;
int num_bytes,index;
unsigned int count;
unsigned char data;
RGB_color palette[256];
// open the file


fp = fopen(filename,"rb");


fseek(fp,128L,SEEK_SET);
// load the data and decompress into buffer
count=0;


while(count<=(unsigned int)SCREEN_WIDTH * SCREEN_HEIGHT)
	 {
	 // get the first piece of data

	 data = getc(fp);

	 // is this a rle?

     if (data>=192 && data<=255)
	{
	// how many bytes in run?

        num_bytes = data-192;

	// get the actual data for the run

	data  = getc(fp);

		// replicate data in buffer num_bytes times

        while(num_bytes-->0)
	     {
//             image->buffer[count++] = data;
  video_buffer[count++]=data;
	     } // end while

	} // end if rle
     else
	{
	// actual data, just copy it into buffer at next location

	//image->buffer[count++] = data;
	  video_buffer[count++]=data;
	} // end else not rle

     } // end while
for (index=0; index<256; index++)
    {
    // get the red component

    palette[index].red   = (getc(fp) >> 2);

    // get the green component

    palette[index].green = (getc(fp) >> 2);

    // get the blue component

    palette[index].blue  = (getc(fp) >> 2);

    } // end for index

fclose(fp);

// change the palette to newly loaded palette if commanded to do so

if (enable_palette)
   {

   for (index=0; index<256; index++)
       {

       Set_Palette_Register(index,(RGB_color_ptr)&palette[index]);

       } // end for index

   } // end if change palette

} // end PCX_Load

void PCX_Init(pcx_picture_ptr image)
//申请图像大小的内存空间
{
unsigned int a=(unsigned int)(SCREEN_WIDTH * SCREEN_HEIGHT + 1);
if((image->buffer = (char far *)malloc(a))==NULL)
{
   printf("\ncouldn't allocate screen buffer");
 exit(1);
 }
}


void PCX_Delete(pcx_picture_ptr image)
{
free(image->buffer);
}


void PCX_Load(char *filename, pcx_picture_ptr image,int enable_palette)
//将文件中的图像压缩数据解压到内存
{

FILE *fp;
int num_bytes,index;
unsigned int count;
unsigned char data;
char far *temp_buffer;

fp = fopen(filename,"rb");

temp_buffer = (char far *)image;

for (index=0; index<128; index++)
    {
    temp_buffer[index] = getc(fp);
    } 

count=0;


while(count<=(unsigned int)SCREEN_WIDTH * SCREEN_HEIGHT)
	 {

	 data = getc(fp);


     if (data>=192 && data<=255)
	{

	num_bytes = data-192;

	data  = getc(fp);

	while(num_bytes-->0)
	     {
	     image->buffer[count++] = data;

	     }
	 }
     else
	{

	image->buffer[count++] = data;

	}

     }

fseek(fp,-768L,SEEK_END);


for (index=0; index<256; index++)
    {

    image->palette[index].red   = (getc(fp) >> 2);


    image->palette[index].green = (getc(fp) >> 2);


    image->palette[index].blue  = (getc(fp) >> 2);

    }

fclose(fp);


if (enable_palette)
   {

   for (index=0; index<256; index++)
       {

       Set_Palette_Register(index,(RGB_color_ptr)&image->palette[index]);

       }

   }

}


void PCX_Show_Buffer(pcx_picture_ptr image)
//将内存中的图像显示
{

char far *data;
data=image->buffer;


asm	push ds;
asm	les di,video_buffer;
asm	lds si,data;
asm 	mov cx,SCREEN_HEIGHT*SCREEN_WIDTH/2;
asm 	cld;
asm	rep movsw;
asm	pop ds;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -