C语言图形编程代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
自己以前编写的C语言图形编程代码
自己以前编写的图形编程代码实现DOS下256BMP图片显示,中文注释,中文汉字显示
写的不好,给大家交流与学习,TC3.0与TC2.0下编译通过,实DOS下才能够运行
VGA256.C文件
/*.................................................*/
#include<stdio.h>
#include<bios.h>
#define maxx 800 /*这里决定了分辨率为800*600,方便屏幕绘图*/
#define maxy 600
unsigned char far *video_buffer=(unsigned char far *)0xA0000000L;
char color=2;
void vga256()
{
union REGS r;
r.x.bx=0x103; /*这里修改成适合你显卡驱动800*600的中断就可以了,我的是英特尔810的集成显卡驱动*/
r.x.ax=0x4f02;
int86(0x10,&r,&r);
}
void fy(int xx)
{ /*分页*/
union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=xx;
int86(0x10,&r,&r);
}
void fast(int x,int y)
{ /*写点程序代码*/
static long i=0;
int xx=0;
static int t=0;
if(x<maxx){
i=y;
i*=maxx;
i+=x;
xx=i>>16;
i&=0xffff;
if(t!=xx) fy(xx);
*(video_buffer+i)=color;
t=xx;
}
}
Hz.C文件
/*.................................................*/
#include<stdio.h>
char bit[8]={128,64,32,16,8,4,2,1};
void openhz(char hz0,char hz1,char *bitdata) { /* 打开字庫*/
FILE *st;
long fpos;
fpos=32L*(((unsigned char)hz0-161)*94+((unsigned char)hz1-161));
if((st=fopen("hzk16","rb"))==NULL) /*打开字库文件hzk16,该文件可以在UCDOS里面找到*/
{
printf("open File error!\n");
exit(0);
}
fseek(st,fpos,SEEK_SET);
fread(bitdata,32,1,st);
fclose(st);
}
void writehz(char hz0,char hz1,int x,int y) /*显示汉字*/
{
register int i,j;
unsigned vpos;
char bitdata[32];
openhz(hz0,hz1,bitdata);
for(i=0;i<16;i++)
for(j=0;j<8;j++)
{
if(bitdata[2*i]&bit[j])
fast(x+j,i+y);
if(bitdata[2*i+1]&bit[j])
fast(x+8+j,i+y);
}
}
int printfhz(char *str,int x,int y) /*显示汉字串*/
{
int num,i,xx;
num=strlen(str);
xx=x;
for(i=0;i<num;i+=2)
{
writehz(str,str[i+1],xx,y);
xx+=16;
}
}
BMP.C文件
/*.................................................*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxx 800
#define maxy 600
#define PALETTE_MASK 0x3c6
#define PALETTE_REGISTER_RD 0x3c7
#define PALETTE_REGISTER_WR 0x3c8
#define PALETTE_DATA 0x3c9
unsigned char bit[8]={128,64,32,16,8,4,2,1};
unsigned char far *video=(char far
*)0xA0000000L;
int color=7,bkcolor=7;
typedef struct BMP_file
{
unsigned int bftype;
unsigned long bfsize;
unsigned int reserved1;
unsigned int reserved2;
unsigned long bfOffset;
}
bitmapfile;
typedef struct BMP_info
{
unsigned long bisize;
unsigned long biwidth;
unsigned long biheight;
unsigned int biplanes;
unsigned int bibitcount;
unsigned long bicompression;
unsigned long bisizeImage;
unsigned long bixpolspermeter;
unsigned long biypelsPermeter;
unsigned long biclrused;
unsigned long biclrImportant;
}
bitmapinfo;
typedef struct RGB_BMP_TYP{
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char reserved;
}RGB_BMP,*RGB_BMP_PRT;
typedef struct bmp_picture_typ{
bitmapfile file;
bitmapinfo info;
RGB_BMP palette[256];
char far *buffer;
} bmp_picture,*bmp_picture_ptr;
void set_bmp_palette_register(int index,RGB_BMP_PRT 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 check_bmp(bmp_picture_ptr bmp){
if(bmp->file.bftype!=0x4d42)
{ printfhz("不是BMP文件",100,100);
exit(1);}
if(bmp->info.bicompression!=0){
printfhz("BMP文件被压缩过的无法打开",100,120);
exit(1);}
if(bmp->info.bibitcount!=8){
printfhz("NO这不是256色图片",100,120);
exit(1); }
}
示例演示代码
/*.......................................................*/
#include"bmp.c"
#include"vga256.c"
#include"hz.c"
void fastbmp(int y,char far *video_b){ /*BMP文件显示分页显示涵数 */
int xx=0,jj;
static int t=0;
long i;
for(jj=0;jj
i*=maxx;
i+=jj;
xx=i>>16;
i&=0xffff;
if(t!=xx) fy(xx);
*(video_buffer+i)=video_b[jj];
t=xx; } }
void bmpopen(char *bmp){ /*打开BMP文件 */
int i,fp,ii;
bmp_picture bmp256;
char *file_name;
if((bmp256.buffer=(char *)malloc(maxx))==NULL)
{
getch(); exit(1);
}
if((fp=open(bmp,O_RDONLY))==1)
return;
printfhz("打开文件成功",200,100);
getch();
read(fp,&bmp256.file,sizeof(bitmapfile));
read(fp,&,sizeof(bitmapinfo));
check_bmp((bmp_picture_ptr)&bmp256);
lseek(fp,54,0);
for(i=0;i<256;i++){
read(fp,&bmp256.palette.blue,1);
read(fp,&bmp256.palette.green,1);
read(fp,&bmp256.palette.red,1);
read(fp,&bmp256.palette.reserved,1);
bmp256.palette.blue=bmp256.palette.blue>>2;
bmp256.palette.green=bmp256.palette.green>>2;
bmp256.palette.red=bmp256.palette.red>>2;
}
for(i=0;i<256;i++)
set_bmp_palette_register(i,(RGB_BMP_PRT)&bmp256.palette);
for(i=maxy-1;i>=0;i)
{
lseek(fp,1078+(long)(maxy1)*maxx,0);
read(fp,bmp2
56.buffer,maxx);
fastbmp(i,bmp256.buffer);
}
close(fp);
}
void bmpopenx(char *bmp,char far *buffer){ /*打开80*70的BMP文件*/
int i,fp,ii;
bmp_picture bmp256;
char *file_name;
if((fp=open(bmp,O_RDONLY))==1)
return;
read(fp,&bmp256.file,sizeof(bitmapfile));
read(fp,&,sizeof(bitmapinfo));
lseek(fp,54,0);
for(i=0;i<256;i++)
{
read(fp,&bmp256.palette.blue,1);
read(fp,&bmp256.palette.green,1);
read(fp,&bmp256.palette.red,1);
read(fp,&bmp256.palette.reserved,1);
bmp256.palette.blue=bmp256.palette.blue>>2;
bmp256.palette.green=bmp256.palette.green>>2;
bmp256.palette.red=bmp256.palette.red>>2;
}
for(i=0;i<256;i++)
set_bmp_palette_register(i,(RGB_BMP_PRT)&bmp256.palette);
lseek(fp,1078,0);
read(fp,buffer,5600);
close(fp);
}
void fastbmpx(int x,int y,int color,unsigned char *video_b,unsigned char*buffer) /*显示80*70的BMP文件函数*/
{
int xx=0;
static int t=0,jj,kk;
long i;
for(jj=0;jj<70;jj++)
{
for(kk=0;kk<80;kk++)
{
i=y+jj;
i*=maxx;
i+=x+kk;
xx=i>>16;
i&=0xffff;
*(buffer+jj*80+kk)=video_buffer;
if(t!=xx) fy(xx);
if(video_b[jj*80+kk]!=color)
{
*(video_buffer+i)=video_b[jj*80+kk];
}
t=xx;
}
}
}
/*以下是简单的不能再简单的一个游戏,大家可以根据自己的创造来扩充和修改*/
void main()
{
int i=0,iii,key;
unsigned long xxx,x=400,y=300;
unsigned char far *buffer[8];
vga256();
printfhz("HELLO现在是256色800*600",200,200);
printfhz("王华胜二零零四年",350,300);
getch();
for(iii=0;iii<8;iii++)
{
if((buffer[iii]=(char far*)malloc(5600))==NULL)
{
printfhz("内存出错,对不起王华胜",250,350);
getch();
exit(1);
}
}
bmpopen("256.da"); /*该文件就是256色的BMP文件把后缀修改成DA了*/
bmpopenx("00.bmp",buffer[0]);
bmpopenx("01.bmp",buffer[1]);
bmpopenx("02.bmp",buffer[2]);
bmpopenx("03.bmp",buffer[3]);
bmpopenx("04.bmp",buffer[4]);
bmpopenx("05.bmp",buffer[5]);
while(1)
{
while(kbhit()==0)/*判断是否有按键,如果没有则继续循环*/
{
fastbmpx(x,y,0,buffer,buffer[6]);
delay(150);
fastbmpx(x,y,1000,buffer[6],buffer[7]);
i++;
if(i>5) i=0;
}
key=getch(); /*读取该键*/
if(!key) /*如果低位是0 则读取高位*/
{
key=getch(); /*读取高位*/
if(key==72||key==73)
{
if(y>0) y=y-4;
}
if(key==80)
{
if(y<600) y=y+4;
}
if(key==75)
{
if(x>0) x=x-5;
}
if(key==77)
{
if(x<720) x=x+5;
}
}
else{ /*如果低位是0 那就是普通按键*/
if(key=='w')
{
if(y>0) y=y-4;
}
if(key=='x')
{
if(y<600) y=y+4;
}
if(key=='a')
{
if(x>0) x=x-5;
}
if(key=='d')
{
if(x<720) x=x+5;
}
if(key=='q') exit(1);
}
}
}