📄 graph.h
字号:
/*
=============0x13h display mode version 1.1
=============CopyRight (C) LiGungcun
=============QQ:66855618
=============E-Mail: hoya-lee@sohu.com
*/
#ifndef _V13
#define _V13
/*===========head file include============*/
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <mem.h>
#include <math.h>
#include <stdarg.h>
#include <process.h>
#include <bios.h>
/*===========macro defined================*/
/*===========globe variable===============*/
unsigned char g_current_color;
unsigned char g_back_color;
int g_image_height;
int g_image_width;
void far *g_bitmap;
/*===========data structure===============*/
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} palette_type;
typedef struct{
unsigned long fsize;/*2文件大小4B*/
unsigned long offset;/*10图象数据在文件中的偏移地址4B*/
unsigned long width,height;/*18,22宽高(像素)4B+4B*/
unsigned int bit;/*28指明每个像素点需要的位数2B*/
unsigned long compres;/*30压缩标志0为不压1为256色压4B*/
unsigned long bsize;/*34图象数据所占用的字节数4B*/
}BMP;
/*===========function declare=============*/
void init_graph (void);
void close_graph (void);
void draw_pixel (unsigned x,unsigned y,unsigned char color);
void draw_line (unsigned x1,unsigned y1,unsigned x2,unsigned y2);
void draw_rectangle (unsigned left, unsigned top, unsigned right, unsigned bottom);
void draw_bar (unsigned left, unsigned top, unsigned right, unsigned bottom);
void draw_circle (unsigned x, unsigned y, unsigned r);
void draw_solid_circle (unsigned x, unsigned y, unsigned r);
void clear_device (unsigned left,unsigned top,unsigned right,unsigned bottom);
void set_back_color (unsigned char color);
unsigned char get_back_color (void);
void set_current_color (unsigned char color);
unsigned char get_current_color (void);
void far * get_image (unsigned left, unsigned top, unsigned right, unsigned bottom);
void put_image (unsigned left,unsigned top,void far *bitmap);
int get_max_x (void);
int get_max_y (void);
unsigned get_pixel (unsigned x,unsigned y);
void out_text_xy (unsigned x,unsigned y,const unsigned char *text);
void set_palette (unsigned color_index,palette_type rgb);
palette_type get_palette (unsigned color_index);
int show_bmp (int x,int y,char *path);
/*===========function implement===========*/
void init_graph(void){
union REGS r;
r.x.ax=0x13;
int86(0x10,&r,&r);
g_current_color=0;
g_image_height=0;
g_image_width=0;
g_bitmap=NULL;
}
void close_graph(void){
union REGS r;
r.x.ax=0x03;
int86(0x10,&r,&r);
}
void draw_pixel (unsigned x,unsigned y,unsigned char color){
unsigned char *video_buffer=NULL;
if(x<320&&y<200){
*(video_buffer=MK_FP(0xa000,((y<<8)+(y<<6))+x))=color;
}
else
return;
}
void draw_line (unsigned x1,unsigned y1,unsigned x2,unsigned y2){
int dx,dy,index,x_inc,y_inc;
int error=0;
if(x1>319||x2>319) return ;
if(y1>199||y2>199) return ;
dx=x2-x1; dy=y2-y1;
if(dx>=0){
x_inc=1;
}
else{
x_inc=-1;
dx=-dx;
}
if(dy>=0){
y_inc=1;
}
else{
y_inc=-1;
dy=-dy;
}
if(dx>dy){
for(index=0;index<=dx;index++){
draw_pixel(x1,y1,g_current_color);
error+=dy;
if(error>dx){
error-=dx;
y1+=y_inc;
}
x1+=x_inc;
}
}
else{
for(index=0;index<=dy;index++) {
draw_pixel(x1,y1,g_current_color);
error+=dx;
if(error>0){
error-=dy;
x1+=x_inc;
}
y1+=y_inc;
}
}
}
void draw_rectangle (unsigned left, unsigned top, unsigned right, unsigned bottom){
unsigned t;
if(left>319||right>319) return ;
if(top>199||bottom>199) return ;
if(left>right){
t=left; left=right; right=t;
}
if(top>bottom){
t=top; top=bottom; bottom=t;
}
draw_line(left,top,right,top);
draw_line(left,top,left,bottom);
draw_line(left,bottom,right,bottom);
draw_line(right,top,right,bottom);
}
void draw_bar (unsigned left, unsigned top, unsigned right, unsigned bottom){
int i;
unsigned x,y,t;
if(left>319||right>319) return ;
if(top>199||bottom>199) return ;
if(left>right){
t=left; left=right; right=t;
}
if(top>bottom){
t=top; top=bottom; bottom=t;
}
for(y=top;y<=bottom;y++){
/*draw_line(left,y,right,y);*/
for(i=left;i<=right;i++)
draw_pixel(i,y,g_current_color);
}
}
void draw_circle (unsigned x, unsigned y, unsigned r){
int t_x,t_y,i;
float pi_2=6.283;
if(x>319||y>199) return ;
if(r>319) return ;
for(i=1;i<720;i++){
t_x=x+r*cos(pi_2/720*i);
t_y=y+r*sin(pi_2/720*i);
draw_pixel(t_x,t_y,g_current_color);
}
}
void draw_solid_circle(unsigned x, unsigned y, unsigned r){
int i;
if(x>319||y>199) return ;
if(r>319) return ;
for(i=1;i<r;i++)
draw_circle(x,y,i);
}
void clear_device (unsigned left,unsigned top,unsigned right,unsigned bottom){
unsigned char t_color;
t_color=g_current_color;
set_current_color(get_back_color());
draw_bar(left,top,right,bottom);
set_current_color(t_color);
}
void set_back_color (unsigned char color){
unsigned char t_color;
g_back_color=color;
t_color=get_current_color();
set_current_color(color);
draw_bar(0,0,get_max_x(),get_max_y());
set_current_color(t_color);
}
unsigned char get_back_color (void){
return g_back_color;
}
void set_current_color (unsigned char color){
g_current_color=color;
}
unsigned char get_current_color (void){
return g_current_color;
}
void far *get_image (unsigned left, unsigned top, unsigned right, unsigned bottom){
unsigned char *t_video=NULL,*t_bitmap=NULL;
int i; unsigned t;
if(left>319||right>319) return NULL;
if(top>199||bottom>199) return NULL;
if(left>right){
t=left; left=right; right=t;
}
if(top>bottom){
t=top; top=bottom; bottom=t;
}
g_image_height=bottom-top+1; g_image_width=right-left+1;
if((g_bitmap=(unsigned char *)malloc(g_image_height*g_image_width))==NULL)
return NULL;
t_video=MK_FP(0xa000,((top<<8)+(top<<6))+left);
t_bitmap=g_bitmap;
for(i=0;i<g_image_height;i++){
memcpy(t_bitmap,t_video,g_image_width);
t_bitmap+=g_image_width;
t_video+=320;
}
return g_bitmap;
}
void put_image (unsigned left,unsigned top,void far *bitmap){
unsigned char *t_video=NULL,*t_bitmap=NULL;
int i;
if(bitmap==NULL) return ;
if(left>319||top>199) return ;
if(left+g_image_width>321||top+g_image_height>201) return ;
t_video=MK_FP(0xa000,((top<<8)+(top<<6))+left);
t_bitmap=bitmap;
for(i=0;i<g_image_height;i++){
memcpy(t_video,t_bitmap,g_image_width);
t_bitmap+=g_image_width;
t_video+=320;
}
free(bitmap); bitmap=NULL;
g_image_width=0;
g_image_height=0;
}
int get_max_x (void){
return 319;
}
int get_max_y (void){
return 199;
}
unsigned get_pixel (unsigned x,unsigned y){
unsigned char *video_buffer=NULL;
if(x<320){
video_buffer=MK_FP(0xa000,((y<<8)+(y<<6))+x);
return *(video_buffer);
}
else
return 300;
}
/*==========out_text function=============*/
void print_hz(char *zimo,int size,int x,int y){
int i,j,k,n;
if(x<0||x>303||y<0||y>183) return ;
n=(size-1)/8+1;
for(j=0;j<size;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(zimo[j*n+i]&(0x80>>k))
draw_pixel(x+i*8+k,y+j,g_current_color);
}
void print_hzk(char *hz,int x,int y,const char *path){
int c1,c2;
FILE *fp=NULL;
char buf[32];
unsigned long offset;
if(x<0||x>303||y<0||y>183) return ;
fp=fopen(path,"rb");
if(fp==NULL){ close_graph(); printf("Can't find file hzk16"); exit(0);}
c1=hz[0]-0xa0; c1=(c1<<8)>>8;
c2=hz[1]-0xa0; c2=(c2<<8)>>8;
offset=(94*(c1-1)+(c2-1))*32L;
fseek(fp,offset,0);
fread(buf,32,1,fp);
print_hz(buf,16,x,y);
fclose(fp);
}
void print_ascii(char c,int x,int y){
/*ascii 8*8 字模点阵地址=0xF000:0xFA6E*/
char m[8];
int i,j;
char far *p=(char far*)0xf000fa6e;
if(x<0||x>311||y<0||y>191) return ;
p=p+c*8;
memcpy(m,p,8);
if((x<311)&&(y<191)){
for(i=0;i<8;i++)
for(j=0;j<8;j++){
if(m[i]&(0x80>>j))
draw_pixel(x+j,y+i,g_current_color);
}
}
}
/*==========out_text function END=========*/
void out_text_xy (unsigned x,unsigned y,const unsigned char *text){
int t_x,t_y;
int i;
unsigned char *t_text=NULL;
if(x>311||y>191) return ;
t_x=x; t_y=y; t_text = (unsigned char *) text;
while(*t_text!='\0'){
/*用print_hzk print_ascii 函数输出连续的字符序列*/
if(*(t_text)>128&&*(t_text+1)>128){
print_hzk(t_text,t_x,t_y,"hzk16");
t_x+=17;
t_text+=2;
}
else if(*(t_text)<128){
t_y+=8;
print_ascii(*t_text,t_x,t_y);
t_x+=9;
t_y-=8;
t_text+=1;
}
}
}
void set_palette(unsigned color_index,palette_type rgb){
outportb(0x3c6,0xff); /*调色板屏蔽寄存器*/
outportb(0x3c8,color_index);/*写颜色寄存器*/
outportb(0x3c9,rgb.r);
outportb(0x3c9,rgb.g);
outportb(0x3c9,rgb.b);
}
palette_type get_palette(unsigned color_index){
palette_type rgb;
rgb.r=rgb.g=rgb.b=-1;
outportb(0x3c6,0xff);
outportb(0x3c7,color_index);/*读颜色寄存器*/
rgb.r=inportb(0x3c9);
rgb.g=inportb(0x3c9);
rgb.b=inportb(0x3c9);
return rgb;
}
int show_bmp(int x,int y,char *path){
FILE *fp=NULL;
char *str=NULL;
int n,i,c;
int w,h;/*实际能显示出来的宽和高*/
palette_type rgb;/*调色板信息*/
BMP bmp;
if(x<0||y<0||x>319||y>199) return 0;
fp=fopen(path,"rb");
if(!fp) return 0;
/**/
fseek(fp,10,SEEK_SET);fread(&bmp.offset,4,1,fp);
fseek(fp,18,SEEK_SET);fread(&bmp.width,4,1,fp);fread(&bmp.height,4,1,fp);
fseek(fp,28,SEEK_SET);fread(&bmp.bit,2,1,fp);
fseek(fp,34,SEEK_SET);fread(&bmp.bsize,4,1,fp);
/**/
fseek(fp,54,SEEK_SET);
for(i=0;i<256;i++) {
rgb.b=fgetc(fp)>>2;
rgb.g=fgetc(fp)>>2;
rgb.r=fgetc(fp)>>2;
fgetc(fp);
set_palette(i,rgb);
}
/*======纠正每行像素所占字节数n 算出实际显示的w,h======*/
n=bmp.width+(4-bmp.width%4);/*每行像素所占字节数n*/
if((199-y)<bmp.height) h=199-y;
else h=bmp.height; /*根据输入参数x,y求出实际能显示出的宽和高,h,w*/
if((319-x)<bmp.width) w=319-x;
else w=bmp.width;
/*=============*/
if(bmp.width%4==0){
for(i=0;i<=h;i++){
str=MK_FP(0xa000,(y+i-1)*320+x);
fseek(fp,bmp.offset+(bmp.height-i)*bmp.width,SEEK_SET);
fread(str,w,1,fp);
}
}
else{
for(i=0;i<=h;i++){
str=MK_FP(0xa000,(y+i-1)*320+x);
fseek(fp,bmp.offset+(bmp.height-i)*n,SEEK_SET);
fread(str,w,1,fp);
}
}
fclose(fp);
return 1;
}
/*===========END==========================*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -