bmp256.h

来自「一个在tc3.0下面写的棋类游戏 很经典 值得好好研究」· C头文件 代码 · 共 138 行

H
138
字号
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#ifndef __BITMAP256_H
#define __BITMAP256_H


unsigned int Failed=0;

struct BITMAPFILE
{
	unsigned int bfType;
	unsigned long bfSize;
	unsigned int Reserved1;
	unsigned int Reserved2;
	unsigned long bfOffset;
};

struct BITMAPINFO
{
	unsigned long biSize;
	unsigned long biWidth;
	unsigned long biHeight;
	unsigned int biPlanes;
	unsigned int biBitCount;
	unsigned long biCompression;
	unsigned long biSizeImage;
	unsigned long biXpelsPerMeter;
	unsigned long biYpelsPerMeter;
	unsigned long biClrUsed;
	unsigned long biClrImportant;
};

struct BitmapHeader
{
	struct BITMAPFILE BitmapFile;
	struct BITMAPINFO BitmapInfo;
} BmpHeader;

void PalOut(int num,char r,char g,char b)
{
   outp(0x3c8,num);
   outp(0x3c9,r);
   outp(0x3c9,g);
   outp(0x3c9,b);
}

void pset(long x,long y,unsigned int cc)
{
   unsigned long Addr,off;
   int page;
   static oldpage=0;
   Addr=x+(y<<9)+(y<<7);
   page=Addr>>16;
   off=Addr&0xffff;
   if(page!=oldpage)
   {
      asm{
	  mov ax,0x4f05
	  mov bx,0
	  mov dx,page
	  int 0x10
   };
   oldpage=page;
   }
   *(unsigned char far *)(0xa0000000+off)=cc;
}

void ShowBmp256(const char* BitmapFileName,int x0,int y0)
{
   unsigned int Width,Height;
   unsigned char color1,color2,*data;
   unsigned int i, j;
   char ColorTable[256][4];
   FILE* fp;
   fp=fopen(BitmapFileName,"rb");
   if(fp==NULL){Failed=1;return;}
   fread(&BmpHeader,sizeof(BmpHeader),1,fp);
   Width=BmpHeader.BitmapInfo.biWidth;Height=BmpHeader.BitmapInfo.biHeight;
   //fseek(fp,BmpHeader.BitmapFile.bfOffset,SEEK_SET);
   fread(ColorTable,1,256*4,fp);
   for(i=0;i<256;i++)
      PalOut(i,ColorTable[i][2]>>2,ColorTable[i][1]>>2,ColorTable[i][0]>>2);
   data=(char*)malloc(Width*sizeof(char));
   for(i=Height;i>0;i--)
   {
      fread(data,Width,1,fp);
      for(j=0;j<Width;j++)
     putpixel(x0+j,y0+i-1,*(data+j));
   //	pset(x0+j,y0+i-1,*(data+j));
   }
   fclose(fp);
   Failed=0;
   return;
}

void DisplayBmp16(const char* BitmapFileName,int x0,int y0)
{
   int linebytes;
   unsigned char buf[2000], buftemp;
   unsigned int Width,Height;
   unsigned int i, j, k, m;
   FILE* fp;

   fp=fopen(BitmapFileName,"rb");
   if(fp==NULL){
   printf("not\n ");
   Failed=1;return;}
   else{
   printf("can");  }
   fread(&BmpHeader,sizeof(BmpHeader),1,fp);
   Width=BmpHeader.BitmapInfo.biWidth;
   Height=BmpHeader.BitmapInfo.biHeight;
   linebytes=(Width+7)/8;

   ((int*)buf)[0]=Width-1;
   ((int*)buf)[1]=0;
   for(i=0;i<Height;i++){
	for(j=0;j<linebytes*4;j++) buf[4+j]=0;
	fseek(fp,-linebytes*4L*(i+1),SEEK_END);
	for(j=0;j<(Width+1)/2;j++){
		fread(&buftemp,1,1,fp);
		for(k=0;k<4;k++){
			if(k==1) m=3;
			else if(k==3) m=1;
			else m=k;
			if(buftemp&(0x80>>k)) buf[4+linebytes*m+j/4]|=(0x80>>(j%4*2));
			if(buftemp&(0x08>>k)) buf[4+linebytes*m+j/4]|=(0x40>>(j%4*2));
		}
	}
	putimage(x0,y0+i,buf,COPY_PUT);
   }
   fclose(fp);
   return;
}

#endif

⌨️ 快捷键说明

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