📄 map.cpp
字号:
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <mem.h>
#include "map.h"
#include "global.h"
#include "string.h"
//#include "mda_vga.h"
// unchar 为unsigned char
MDAMAP::MDAMAP()
{
width = MAP_WIDE;
height = MAP_HIGH;
if((buf=new unchar far [width*(height+1)*sizeof(unchar)])==NULL)
{
printf("NO Enough Memory Alloc DispBuffer\n");
exit(1);
}
}
MDAMAP::~MDAMAP()
{
delete[]buf;
}
void MDAMAP::Realloc(int high)
{
delete[]buf;
height = high;
if((buf=new unchar far [width*(height+1)*sizeof(unchar)])==NULL)
{
printf("NO Enough Memory Alloc DispBuffer\n");
exit(1);
}
}
////////////////////////////////////////////////////////////////////
// 640*480(16色图形方式) //
// 水平方向为480行,每行640个象素。16色的图形方式,4个位平面. //
// 每个位平面的地址空间相同,其字节数为640*480/8=38400字节。 //
// 在显存中,其起始地址为A0000H。每行的640个像素在存储器中占 //
// 80个字节,物理屏幕上的一个像素的位置,相对于起始地址A0000H //
// 的偏移量可以这样计算: //
// 偏移量=行号*80+列号/8; //
// 在这个字节中,对应于像素点的位号: //
// 位号=7-列号 MOD 8 //
////////////////////////////////////////////////////////////////////
void MDAMAP::PutPixel(int x,int y,int ch_color)
{
if(_disp.disp_way==2)
y*=0.8;
unchar far *ptr=(unchar far *)(buf+(y<<6)+(y<<4)+(x>>3));
outportb(0x3c4,2);
// if(_marker[ch_sel].mark[i].flag==0)
// int ch_color=(ch_sel==0)?CHAWAVELINE:CHBWAVELINE;
// int ch_color=(chs==0)?CHAWAVELINE:CHBWAVELINE;
// int ch_color=(chs==0)?_system.colora:_system.colorb;
// ch_color = ;
outportb(0x3c5,ch_color/*CHAWAVELINE/CHBWAVELINE*/);
*ptr|=(0x80>>(x&0x07));
}
void MDAMAP::Line(int x1,int y1,int x2,int y2,int ch_color)
{
register int t,distance;
int x=0,y=0,dx,dy,incx,incy;
disable();
dx=x2-x1;
dy=y2-y1;
if(dx>0)incx=1;
else if(dx==0) incx=0;
else {dx=-dx;incx=-1;}
if(dy>0)incy=1;
else if(dy==0)incy=0;
else {dy=-dy;incy=-1;}
if(dx>dy)
distance=dx;
else distance=dy;
for(t=0;t<distance+1;t++)
{
PutPixel(x1,y1,ch_color);
x+=dx;
y+=dy;
if(x>distance){
x-=distance;
x1+=incx;
}
if(y>distance){
y-=distance;
y1+=incy;
}
}
}
void MDAMAP::ScreenMove(unchar color)
{
unchar far *ptr=(unchar far *)(0xa0000000L/*+35*80*/);
unchar far *temp=buf;
outportb(0x3c4,2);
outportb(0x3c5,color);
for(register int i=0;i<height+1;i++){
_fmemcpy(ptr,temp,width);
ptr+=width;
temp+=width;
}
}
void MDAMAP::ScreenMove()
{
unchar far *ptr=(unchar far *)(0xa0000000L+36*80+4);
// unchar far *temp=buf+4;
//lingyi
unchar far *temp=buf+4;
// outportb(0x3c4,2);
// outportb(0x3c5,CHAWAVELINE); //SET COLOR
for(register int i=0;i<height;i++){
_fmemcpy(ptr,temp,64);
ptr+=width;
temp+=width;
}
}
void MDAMAP::ScreenClear()
{
_fmemset(buf,0,width*(height+1));
}
void MDAMAP::ScanLine(int x1,int x2,int y,unsigned char style)
// 画横线 ,style 为线型
{
/* int x;
char far *ptr;
if(x1>x2)
{x=x1;x1=x2;x2=x;}
ptr=(char far *)(buf+y*MAP_WIDE+(x1>>3));
x=(x2-x1+7)>>3;
_fmemset(ptr,style,x);
*/
unchar far *pptr=buf;
asm{
mov ax,x1
mov bx,x2
cmp bx,ax
jge JEMP1 // if(x2 >= x1)
mov x1,bx // else { x=x1;x1=x2;x2=x1; }
mov x2,ax
}
JEMP1:
asm{
mov ax,y
mov bx,MAP_WIDE
mul bx
push ax // ax = y*80;
mov bx,x1
mov cl,3
shr bx,cl
add ax,bx
mov di,ax // di = y*80+x1/8
mov bx,x2
shr bx,cl
pop ax
add ax,bx
mov si,ax // si = y*80+x2/8
}
JEMP2:
asm{
les bx,Dword ptr pptr
cmp si,di
je INONEBYTE
mov cx,x1 // 画线头
and cx,7
mov al,style
shr al,cl
or byte ptr es:[bx+di],al
mov al,style
}
MIDDLE_BYTE_LOOP: // 画中间线
asm{
inc di
cmp di,si
je WRITE_END_BYTE
or byte ptr es:[bx+di],al
jmp MIDDLE_BYTE_LOOP
}
WRITE_END_BYTE: // 画线尾
asm{
mov cx,x2
not cx
and cx,7
mov al,style
shl al,cl
or byte ptr es:[bx+di],al
jmp END
}
INONEBYTE:
asm{ // 如果没有中间线
mov cx,x1
and cx,7
mov al,style
shr al,cl
mov cx,x2
not cx
and cx,7
mov ah,style
shl ah,cl
and al,ah
or byte ptr es:[bx+di],al
}
END:
}
void MDAMAP::VertLine(int y1,int y2,int x,unsigned char style)
// 画竖线 ,style 为线型
{
unchar far *pptr=buf;
asm{
mov di,y1
mov si,y2
cmp si,di
jge JEMP1
xchg di,si
}
JEMP1:
asm{
mov bx,MAP_WIDE;
mov ax,di
mul bx
les bx,dword ptr pptr
add bx,ax
mov ax,x
mov cl,3
shr ax,cl
add bx,ax
mov cl,byte ptr x
and cl,7
mov al,0x80
shr al,cl
mov cl,style
}
LINELOOP:
asm{
rol cl,1
jnc JEMP3
or byte ptr es:[bx],al
}
JEMP3:
asm{
add bx,MAP_WIDE
inc di
cmp di,si
jl LINELOOP
}
}
//void MDAMAP::Rectangle(int x1,int y1,int x2,int y2,unsigned char style)
//{
// VertLine(y1,y2,x1,style);
// VertLine(y1,y2,x2,style);
// ScanLine(x1,x2,y1,style);
// ScanLine(x1,x2,y2,style);
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -