⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lkeytext.cpp

📁 ldraw_DOS游戏开发包
💻 CPP
字号:
#include <lkey.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <i86.h>
#include <stdarg.h>
#include <lsys.h>

static char *TextBuf=VideoTextBuf;

static short screen_len=80, screen_wid=25, 
             win_left=0, win_top=0,  win_right=79, win_bottom=24,
             win_len=80, win_wid=25, curse_x=0, curse_y=0, curse_mode=1,
             color_text=7, color_backgrnd=0, color_byte=0x07;

/////////////////////////
char  lk_text_init(short mode,short scr_len,short scr_wid);
void  lk_write_data(short x,short y,char data,uchar mode);
char  lk_read_data(short x,short y,uchar mode);
char  lk_copy_data(short left,short top,short right,short bottom,char *buf,char mode); // 0 screen->mem  1 mem->screen
char  lk_fill_data(short left,short top,short right,short bottom,char data,char mode); // 0 text  1 color
char  lk_write_str(short x,short y,char *s);
char  lk_scroll(short left,short top,short right,short bottom,char direction);

void  lk_gotoxy(short x,short y);
void  lk_getxy(short *x,short *y);
void  lk_putch(char ch);
void  lk_setwindow(short left,short top,short right,short bottom);
void  lk_setcolor(short textcolor,short backgrnd);
void  lk_clear(char mode); // 0 window   1 full screen
void  lk_puts(char *s);
void  lk_printf(char *fmt,...);
void  lk_set_cursor(short mode);

void  lk_draw_window(short left,short top,short right,short bottom,char *name,short type);
///////////////////////////////////////////////////////////////////////////
static void lk_text_cursorxy(short x,short y);

void lk_set_cursor(short mode)
{ union REGS r;
  r.h.ah=1; r.h.bh=0;
  switch (mode)
   { case 1: r.h.ch=6; r.h.cl=7; break;
     case 2: r.h.ch=0; r.h.cl=7; break;
     default: mode=0;
              r.h.ch=7; r.h.cl=0; break;
   }
  int386(0x10,&r,&r); curse_mode=mode;
  lk_gotoxy(curse_x,curse_y);
}
static void lk_text_cursorxy(short x,short y)
{ REGS r;
  r.h.ah=2;   r.h.bh=0;
  r.h.dl=x;   r.h.dh=y;
  if (x>=0&&y>=0&&x<screen_len&&y<screen_wid) int386(0x10,&r,&r);
}
char  lk_text_init(short mode,short scr_len,short scr_wid)
{ union REGS r;
  r.h.ah=0x0f;
  int386(0x10,&r,&r);
  if (r.h.al!=mode) {
     r.w.ax=(mode&255);
     int386(0x10,&r,&r);
  }
  r.h.ah=0x0f;
  int386(0x10,&r,&r);
  screen_len=scr_len; screen_wid=scr_wid;
  return 1;
}
void  lk_write_data(short x,short y,char data,uchar mode)
{ if (x<0||y<0||x>=screen_len||y>=screen_wid) return;
  TextBuf[ (( y * screen_len + x ) << 1 ) + mode ]=data;
}
char  lk_read_data(short x,short y,uchar mode)
{ if (x<0||y<0||x>=screen_len||y>=screen_wid) return 0;
  return TextBuf[ (( y * screen_len + x ) << 1 ) + mode ];
}
char  lk_copy_data(short left,short top,short right,short bottom,char *buf,char mode)
{ short i,line_count,bytes;
  if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;
  line_count=bottom-top+1; bytes=(right-left+1)*2;
  for (i=0;i<line_count;i++,buf+=bytes)
    if (mode==0) memcpy(buf,TextBuf+((top+i)*screen_len+left)*2,bytes);
       else memcpy(TextBuf+((top+i)*screen_len+left)*2,buf,bytes);
  return 0;
}
char  lk_fill_data(short left,short top,short right,short bottom,char data,char mode)
{ short x,y,pos;
  if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;
  pos=top*screen_len*2;
  for (y=top;y<=bottom;y++,pos+=(screen_len*2)) for (x=left;x<=right;x++) 
      TextBuf[pos+(x<<1)+mode]=data;
  return 0;
}
char  lk_scroll(short left,short top,short right,short bottom,char direction)
{ static char lines[100][200];
  short nx1,ny1,nx2,ny2,tx1,ty1,tx2,ty2,y,line_count;
  short rx1,rx2,ry1,ry2;

  if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;

  nx1=left,ny1=top,nx2=right,ny2=bottom,
  tx1=left,ty1=top,tx2=right,ty2=bottom;
  switch(direction)
   { case 1: if (screen_wid>1)  ny1++,ty2--; 
             rx1=nx1, ry1=ny2, rx2=nx2, ry2=ny2; break;
     case 2: if (screen_len>1)  nx2--,tx1++; 
             rx1=nx1, ry1=ny1, rx2=nx1, ry2=ny2; break;
     case 3: if (screen_wid>1)  ny2--,ty1++;
             rx1=nx1, ry1=ny1, rx2=nx2, ry2=ny1; break;
     case 4: if (screen_len>1)  nx1++,tx2--; 
             rx1=nx2, ry1=ny1, rx2=nx2, ry2=ny2; break;
   }
  line_count=ny2-ny1+1;
  for (y=0;y<line_count;y++) lk_copy_data(nx1,ny1+y,nx2,ny1+y,lines[y],0);
  for (y=0;y<line_count;y++) lk_copy_data(tx1,ty1+y,tx2,ty1+y,lines[y],1);
  lk_fill_data(rx1,ry1,rx2,ry2,' ',0);
  return 0;
}
char  lk_write_str(short x,short y,char *s)
{ for (;x<screen_len&&(*s);x++,s++) lk_write_data(x,y,*s,0);
  return 0;
}
void  lk_gotoxy(short x,short y)
{ if (x<0||y<0||x>=win_len||y>=win_wid) return;
  curse_x=x, curse_y=y;
  if (curse_mode) lk_text_cursorxy(x+win_left,y+win_top);
}
void  lk_getxy(short *x,short *y)
{ *x=curse_x; *y=curse_y;
}
static void lk_move_curse(char mode)
{ switch (mode)
   { case 0: curse_x=0; break;
     case 1:
     case 2: if (++curse_y>=win_wid) { 
                  curse_y=win_wid-1; 
                  lk_scroll(win_left,win_top,win_right,win_bottom,1);
               } if (mode==2) curse_x=0; break;
     case 3: if (++curse_x>=win_len) {
                curse_x=0;
                if (++curse_y>=win_wid) {
                   curse_y=win_wid-1;
                   lk_scroll(win_left,win_top,win_right,win_bottom,1);
                }
             }  break;
   }
 lk_gotoxy(curse_x,curse_y);
}
void  lk_putch(char ch)
{ if (ch=='\r') { lk_move_curse(0); return; }
  if (ch=='\n') { lk_move_curse(2); return; }
  lk_write_data(win_left+curse_x,win_top+curse_y,ch,0);
  lk_write_data(win_left+curse_x,win_top+curse_y,color_byte,1);
  lk_move_curse(3);
}
void  lk_setwindow(short left,short top,short right,short bottom)
{ if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;
  win_left=left, win_top=top, win_right=right, win_bottom=bottom;
  win_len=win_right-win_left+1, win_wid=win_bottom-win_top+1;
  lk_gotoxy(0,0);
}
void  lk_setcolor(short textcolor,short backgrnd)
{ if (textcolor>=0) color_text=textcolor;
  if (backgrnd>=0) color_backgrnd=backgrnd;
  color_text&=0x0f; color_backgrnd&=0x0f;
  color_byte=(color_backgrnd<<4)+color_text; 
}
void  lk_clear(char mode)
{ short x1,y1,x2,y2;
  switch (mode)
   { case 0: x1=win_left, y1=win_top, x2=win_right, y2=win_bottom; break;
     case 1: x1=0, y1=0, x2=screen_len-1, y2=screen_wid-1; break;
   }
  lk_fill_data(x1,y1,x2,y2,' ',0);
  lk_fill_data(x1,y1,x2,y2,color_byte,1);
}
void  lk_puts(char *s)
{ for (;*s;s++) lk_putch(*s);
}
void  lk_printf(char *fmt,...)
{ static char string[8192];
  va_list argptr;
  va_start(argptr,fmt);
  vsprintf(string,fmt,argptr);
  va_end(argptr);
  lk_puts(string);
}
///////////////////////////////////////////////////////////////////////////
static char win_template[5][10]={
 { 0xda, 0xbf, 0xc0, 0xd9,  0xc4, 0xc4, 0xb3, 0xb3, 0, 0 },
 { 0xc9, 0xbb, 0xc8, 0xbc,  0xcd, 0xcd, 0xba, 0xba, 0, 0 }
};
static void lk_draw_plate(short left,short top,short right,short bottom,char *plate)
{ short x,y;
  if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;

  lk_fill_data(left,top,right,bottom,color_byte,1);

  lk_write_data(left,top,plate[0],0); lk_write_data(right,top,plate[1],0);
  lk_write_data(left,bottom,plate[2],0); lk_write_data(right,bottom,plate[3],0);

  for (x=left+1;x<right;x++) {
    lk_write_data(x,top,plate[4],0); 
    lk_write_data(x,bottom,plate[5],0);
  }
  for (y=top+1;y<bottom;y++) {
    lk_write_data(left,y,plate[6],0); 
    lk_write_data(right,y,plate[7],0);    
  }

  for (x=left+1;x<=right+1;x++) lk_write_data(x,bottom+1,0x08,1);
  for (y=top+1;y<=bottom+1;y++) lk_write_data(right+1,y,0x08,1);
}
void  lk_draw_window(short left,short top,short right,short bottom,char *name,short type)
{ short x, y, len=strlen(name);

  if (left<0) left=0; if (top<0)  top=0;
  if (right>=screen_len) right=screen_len-1;
  if (bottom>=screen_wid) bottom=screen_wid-1;
  if (right-left<3) right=left+3;

  lk_draw_plate(left,top,right,bottom,win_template[type]);

  if (len==0) return;
  if (len>=right-left+1-3) len=right-left+1-3;
  x=left+2+(right-left-2-len)/2;
  for (y=0;y<len;y++) lk_write_data(x+y,top,name[y],0);
  if (len<right-len-5) { 
    lk_write_data(x-1,top,' ',0); 
    lk_write_data(x+len,top,' ',0);
  }
}

⌨️ 快捷键说明

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