📄 base.h
字号:
/****************************************
*SOURCEFILE OF *
* ------<BASE.H> *
* COPYRIGHT(R) M.CHEN 12.3.99 *
****************************************/
#if !defined(_BASE_H)
#define _BASE_H
#include<dos.h>
#include<alloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#if !defined(__PRIVATE_VAR)
#if !defined NO_FONT_FILE
unsigned fontarray[100]; //the number to be decided
#endif
#define __PRIVATE_VAR
unsigned char unsigned_char_data;
unsigned unsigned_int_data;
union REGS reg;
struct SREGS sreg;
enum COLORS{BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,LIGHTGRAY,DARKGRAY,LIGHTBLUE,LIGHTGREEN,LIGHTCYAN,LIGHTRED,LIGHTMAGENTA,YELLOW,WHITE};
#endif
int lastbutton[6],mx,my,pressedkey=0,mousepresent=0,mousevisible=0,lastpage=100,MAXX=80,MAXY=25,writemode=0,screenmode=0x3,palnum;
/***************************************************************************/
/*<mouse>*/
unsigned mask[34]={
0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
0x1800,0x1400,0x1400,0x1400,0x1400,0x1400,0x33e0,0x52b0,
0x52a8,0x52a8,0x4008,0x4008,0x4008,0x2010,0x0020,0x0000,0x4,0x0};
int resetmouse();
void setmctype_crt(unsigned char form,unsigned char color);
void setmctype_pic(unsigned *mask);
void showmc();
void hidemc();
int mcmoved();
void adjustmouse();
void limitmouse(int x1,int y1,int x2,int y2);
void movemcto(int x,int y);
void getmcoords();
void getmbs(unsigned button);
unsigned char getkey();
void waitforkey();
/*<crt>*/
typedef unsigned char crt_screen_array[25][80][2];
crt_screen_array far *crt_screen=(crt_screen_array far *)0xB8000000;
void setcursize(int low,int high);
void hidekc();
void showkc();
int wherex();
int wherey();
void gotoxy(int x,int y);
void put_textbg(int x,int y,char backcolor); /*print with backcolor (fix the backcolor of a char in the screen)*/
void put_ch(int x,int y,char ch,char color); /*print with char,color*/
void put_char(int x,int y,char ch,char attr); /*print with char,color,backcolor*/
void get_char(int x,int y,char *ch, char *attr);
void putstring(int x,int y,char *s,int attr);
void putstr(int x,int y,char *s,int attr);
void textrec(int x1,int y1,int x2,int y2,int attr,int mode);
void textbar(int x1,int y1,int x2,int y2,char attr,char mode);
void clrscr();
void setscreen(int mode);
//<draw>
FILE *fontfile;
void setpalette(unsigned char i,unsigned char r,unsigned char g,unsigned char b);
void getpalette(unsigned char i,unsigned char *r,unsigned char *g,unsigned char *b);
void line(int x1,int y1,int x2,int y2,int color);
void lines(int x1,int y1,int x2,int y2,int color,int k);
void polyline(int N,int *BUFF,int color,int k);
void rectangle(int x1,int y1,int x2,int y2,int color,int k);
void rec3D(int x1,int y1,int x2,int y2,int color,int colour,int k);
void bar(int x1,int y1,int x2,int y2,int color);
void drawbutton(int x1,int y1,int x2,int y2,int OUT,int k);
void ellipse(int x,int y,int A,int B,int color,int k);
void circle(int x,int y,int A,int color,int k);
void openfontfile(char *filename);
void closefontfile();
void putword(int x,int y,unsigned long number,unsigned char color);
void text(int x,int y,unsigned char *s,unsigned char color);
void putword24(int x,int y,unsigned long number,unsigned char color);
void text24(int x,int y,unsigned char *s,unsigned char color);
void getfontarray (unsigned number);
void writefontarray(char *filename);
/*********************<MOUSE>***********************/
int strlength(char *str)
{ int length=0;
while (str[length]!='\0')length++;
return(length);
}
char *strcopy(char *str)
{ int i,length=strlength(str);
char *mystr=(char *)malloc(length+1);
for(i=0;i<length;i++) mystr[i]=str[i];
mystr[i]='\0';
return(mystr);
}
char *strcombine(char *str1,char *str2)
{ int i,length1=strlength(str1),length2=strlength(str2);
char *mystr=(char *)malloc(length1+length2+1);
for(i=0;i<length1;i++) mystr[i]=str1[i];
length1+=length2;
for(;i<length1;i++) mystr[i]=*str2++;
mystr[i]='\0';
return(mystr);
}
int strcompare(unsigned char *str1,unsigned char *str2)
{ for(;*str1!='\0'&&*str2!='\0'&&*str1==*str2;str1++,str2++);
if (*str1=='\0'&&*str2=='\0') return(1);
else if(*str2=='\0') return(2);
else if(*str1=='\0') return(-1);
else return(0);
}
char *inttostr(unsigned long data)
{ unsigned char str[11],*mystr,i,j;
if (data==0) {str[0]=48;i=1;}
else for(i=0;data>0;i++)
{ str[i]=data%10+48;
data/=10;
}
mystr=(char *)malloc(i+1);
for(j=0;j<i;j++)mystr[j]=str[i-j-1];
mystr[i]='\0';
return(mystr);
}
unsigned long strtoint(char *str)
{int i,length=strlength(str);
unsigned long data=0;
for(i=0;i<length;i++) data=data*10+str[i]-48;
return(data);
}
int inside(int x,int y,int x1,int y1,int x2,int y2)
{ if (x>=x1 && x<=x2 && y>=y1 && y<=y2) return(1);
else return(0);
}
int resetmouse()
{ reg.x.ax=0;
int86(0x33,®,®);
mousepresent=reg.x.ax>0;
mx=my=pressedkey=mousevisible=0;
return(mousepresent);
}
void setmctype_crt(unsigned char form,unsigned char color)
{ reg.x.ax=0x0a;
reg.x.bx=0x00;
reg.x.cx=0xFF00;
reg.h.dh=color;
reg.h.dl=form;
int86(0x33,®,®);
}
void setmctype_pic(unsigned *mask)
{ union REGS reg;
struct SREGS sreg;
reg.x.ax=0x09;
reg.x.bx=mask[32];
reg.x.cx=mask[33];
reg.x.dx=FP_OFF(mask);
sreg.es=FP_SEG(mask);
int86x(0x33,®,®,&sreg);
}
void showmc()
{ if ((mousepresent) && (!mousevisible))
{ reg.x.ax=1;
int86(0x33,®,®);
mousevisible=1;
}
}
void hidemc()
{ union REGS reg;
if ((mousepresent) && (mousevisible))
{ reg.x.ax=2;
int86(0x33,®,®);
mousevisible=0;
}
}
void adjustmouse(int mode,int *x,int *y)
{ if (mode==0)
{ if (screenmode<=0x4) {*x>>=3;*y>>=3;}
if (screenmode==0x13) *x>>=1;
}
else
{ if (screenmode<=0x4) {*x<<=3;*y<<=3;}
if (screenmode==0x13) *x<<=1;
}
}
void limitmouse(int x1,int y1,int x2,int y2)
{ adjustmouse(1,&x1,&y1);
adjustmouse(1,&x2,&y2);
reg.x.ax=7;
reg.x.cx=x1;
reg.x.dx=x2;
int86(0x33,®,®);
reg.x.ax=8;
reg.x.cx=y1;
reg.x.dx=y2;
int86(0x33,®,®);
}
void movemcto(int x,int y)
{ mx=x;my=y;
adjustmouse(1,&x,&y);
reg.x.ax=4;
reg.x.cx=x;
reg.x.dx=y;
int86(0x33,®,®);
}
void getmcoords()
{ reg.x.ax=0x03;
int86(0x33,®,®);
// pressedkey=reg.x.bx;
mx=reg.x.cx;
my=reg.x.dx;
adjustmouse(0,&mx,&my);
}
void getmbs(unsigned button)
{ if (!mousepresent) {lastbutton[0]=lastbutton[3]=0;exit(0);}
reg.x.ax=0x5;
reg.x.bx=button-1;
int86(0x33,®,®);
lastbutton[0]=reg.x.bx; //times that the key was pressed lasttime =>pnum
lastbutton[1]=reg.x.cx; //abscissa that the key was pressed lasttime=>px
lastbutton[2]=reg.x.dx; //ordinates that the key was pressed lasttime=>py
reg.x.ax=0x6;
reg.x.bx=button-1;
int86(0x33,®,®);
lastbutton[3]=reg.x.bx; //times that the key was open lasttime =>onum
lastbutton[4] =reg.x.cx;//abscissa that the key was open lasttime=>px
lastbutton[5]=reg.x.dx; //ordinates that the key was open lasttime=>py
adjustmouse(0,&lastbutton[1],&lastbutton[2]);
adjustmouse(0,&lastbutton[4],&lastbutton[5]);
}
unsigned char getkey()
{ if (!mousepresent) pressedkey=0;
else{ reg.x.ax=0x03;int86(0x33,®,®);pressedkey=reg.x.bx;}
if (pressedkey==0)
{ reg.x.ax=3;
reg.h.ah=0x0b;
int86(0x21,®,®);
if (reg.h.al==0xff) //if keypressed
{ reg.h.ah=0x07;
int86(0x21,®,®);
pressedkey=reg.h.al;
if (pressedkey==0) {int86(0x21,®,®);pressedkey=reg.h.al+100;}
}
}
return(pressedkey);
}
void waitforkey()
{ do getkey();while (pressedkey==0);
if (pressedkey<4) delay(500);
}
/*****************<CRT>********************/
void setcursize(int low,int high)
{ reg.h.ah=1;
reg.h.ch=low;
reg.h.cl=high;
int86(0x10,®,®);
}
void hidekc()
{ reg.h.ah=0x01;
reg.h.ch=0xff;
reg.h.cl=0xff;
int86(0x10,®,®);
}
void showkc()
{ reg.h.ah=0x01;
reg.h.ch=0x0c;
reg.h.cl=0x0d;
int86(0x10,®,®);
}
int mcmoved()
{static lastmcx,lastmcy;
getmcoords();
if (mx!=lastmcx||my!=lastmcy)
{lastmcx=mx;
lastmcy=my;
return(1);
} else return(0);
}
int wherex()
{ reg.h.ah=0x03;
reg.h.bh=0;
int86(0x10,®,®);
return(reg.h.dl);
}
int wherey()
{ reg.h.ah=0x03;
reg.h.bh=0;
int86(0x10,®,®);
return(reg.h.dh);
}
void gotoxy(int x,int y)
{ reg.h.ah=0x02;
reg.h.bh=0;
reg.h.dh=y;
reg.h.dl=x;
int86(0x10,®,®);
}
void put_char(int x,int y,char ch,char attr)
{ if (x>79 || y>24) return;
(*crt_screen)[y][x][0]=ch;
(*crt_screen)[y][x][1]=attr;
}
void put_textbg(int x,int y,char backcolor)
{ if (x>79 || y>24) return;
(*crt_screen)[y][x][1]=((*crt_screen)[y][x][1]&0x0f)+(backcolor&0xf0);
}
void put_ch(int x,int y,char ch,char color)
{ if (x>79 || y>24) return;
(*crt_screen)[y][x][0]=ch;
(*crt_screen)[y][x][1]=((*crt_screen)[y][x][1]&0xf0)+(color&0x0f);
}
void get_char(int x,int y,char *ch,char *attr)
{ if (x>79 || y>24) return;
*ch=(*crt_screen)[y][x][0];
*attr=(*crt_screen)[y][x][1];
}
void putstring(int x,int y,char *s,int attr)
{ while (*s!='\0') put_char(x++,y,*s++,attr);
}
void putstr(int x,int y,char *s,int attr)
{ while (*s!='\0') put_ch(x++,y,*s++,attr);
}
void textrec(int x1,int y1,int x2,int y2,int attr,int mode)
{ int x,y;
struct {int line,row,a,b,c,d;}ct={196,179,218,191,192,217};
if (mode!=0)
{ct.line=205;
ct.row=186;
ct.a=201;
ct.b=187;
ct.c=200;
ct.d=188;
}
put_char(x1,y1,ct.a,attr);
put_char(x2,y1,ct.b,attr);
put_char(x1,y2,ct.c,attr);
put_char(x2,y2,ct.d,attr);
for (x=x1+1;x<x2;x++)
{ put_char(x,y1,ct.line,attr);
put_char(x,y2,ct.line,attr);
}
for (y=y1+1;y<y2;y++)
{ put_char(x1,y,ct.row,attr);
put_char(x2,y,ct.row,attr);
}
}
void textbar(int x1,int y1,int x2,int y2,char attr,char mode)
{ int yi;
if (mode==0)
for(;x1<=x2;x1++)
for(yi=y1;yi<=y2;yi++) put_char(x1,yi,32,attr);
else
for(;x1<=x2;x1++)
for(yi=y1;yi<=y2;yi++) put_textbg(x1,yi,attr);
}
void clrscr()
{ textbar(0,0,79,24,7,0);gotoxy(0,0);}
void setscreen(int mode)
{static visible=1;
if (mode!=visible)
{ if (mode==0) {visible=0;reg.h.al=0x01;}
else{visible=1;reg.h.al=0x00;}
reg.h.ah=0x12;
reg.h.bl=0x36;
int86(0x10,®,®);
}
}
/*******************<DRAW>************************************/
/*###########################################################*/
/*##############setmode,getpixel,putpixel####################*/
void (*putpixel)(unsigned,unsigned,unsigned char);
unsigned char (*getpixel)(unsigned,unsigned);
void getvga()
{ char buff[0x40];
reg.h.ah=0x1b;
reg.h.bl=0;
reg.h.bh=0;
sreg.es=FP_SEG(buff);
reg.x.di=FP_OFF(buff);
int86x(0x10,®,®,&sreg);
MAXX=8*buff[5]; //横坐标分辩率
MAXY=8*buff[0X22]; //纵坐标分辩率
if (screenmode==0x12) {MAXX=640; MAXY=480; }
else if(screenmode==0x3){MAXX=80 ; MAXY=20 ; }
}
void getvesa(int x)
{ struct modeinfo
{ unsigned attr; //窗口属性
unsigned char waa,wab;//窗口AB属性
unsigned winkb; //粒度
unsigned winsize;//窗口大小
unsigned winadda;//窗口A地址
unsigned winaddb;//窗口B地址
char far *pf; //功能指针
unsigned bps; //每条扫描线字节数
unsigned xr,yr; //水平垂直分辩率
unsigned char xs,ys;//字符宽高
unsigned char nop; //位平面数
unsigned char bpp; //每像素位数
unsigned char nopage;//页面数
unsigned char mm; //页面模式
unsigned char bz; //页面大小
};
struct modeinfo *mp=(struct modeinfo *)malloc(sizeof(struct modeinfo));
if(mp==NULL) { printf("Possibly insufficient memory!\n");exit(0);}
reg.h.ah=0x4f;
reg.h.al=1;
reg.x.cx=x;
sreg.es=FP_SEG(mp);
reg.x.di=FP_OFF(mp);
int86x(0X10,®,®,&sreg);
if(reg.x.ax!=0x004f){ printf("Possibly insufficient video memory!\n");exit(0);}
MAXX=mp->xr; //保存水平分辩率
MAXY=mp->yr; //保存垂直分辩率
free(mp);
}
void setvga(int x)
{ reg.h.al=x;
reg.h.ah=0;
int86(0X10,®,®);
}
void setvesa(int x)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -