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

📄 vga.c

📁 This software was done in part for a textbook on AI I ve written called _The Basis of AI_ (tentative
💻 C
📖 第 1 页 / 共 2 页
字号:
        set_regs(g640x480x16_regs);        /* set default palette */        for(i = 0; i < 16; i++)          vga_setpal(i, _R_[i], _G_[i], _B_[i]);        //vga_clear(root_col);        LINE_BYTES = 80;        VGA_WIDTH = 640;        VGA_HEIGHT= 480;        VGA_DEPTH = 4;        VGA_MAX_X = VGA_WIDTH - 1;        VGA_MAX_Y = VGA_HEIGHT - 1;      }    /* enable video */    port_in(IS1_R);    port_out(0x20, ATT_IW);}int vga_width()  { return VGA_WIDTH; }int vga_height() { return VGA_HEIGHT; }int vga_depth()  { return VGA_DEPTH; }void vga_set_mode(int m){ port_out(0x03, GRA_I );  port_out(m<<3, GRA_D ); }void vga_set_color(int col){  port_out(0x00, GRA_I );   port_out(col, GRA_D ); }void vga_pixel(int x, int y){ // clip to screen  if (x < 0 || x > VGA_MAX_X || y < 0 || y > VGA_MAX_Y) return;  VIDEO_PTR p = VIDEO + y*LINE_BYTES + (x>>3);  port_out(8, GRA_I);  port_out((0x80 >> (x & 7)), GRA_D);  *p = *p; }int vga_getpix(int x, int y){  register int bit = 0x80 >> (x&7);  register VIDEO_PTR byte = VIDEO + LINE_BYTES*y+(x>>3);  register int c;  /* set read mode 1 */  port_out(5, GRA_I);  port_out(8, GRA_D);  for(c=0; c<16; c++)  { port_out(2, GRA_I);    port_out(c, GRA_D);    if (*byte & bit)  break;   }  return c;}void vga_hline(int x0, int x1, int y){  // clip into screen  if (y < 0 || y > VGA_MAX_Y || x1 < 0 || x0 > VGA_MAX_X) return;  if (x0 < 0) x0 = 0;  if (x1 > VGA_MAX_X) x1 = VGA_MAX_X;    VIDEO_PTR first = VIDEO + LINE_BYTES*y + (x0>>3);  VIDEO_PTR last  = VIDEO + LINE_BYTES*y + (x1>>3);  port_out(8, GRA_I);  if (first == last)  { char byte  = 0xFF>>(x0&7);    byte &= 0xFF<<((~x1)&7);    port_out(byte, GRA_D);    *first = *first;    return;   }  port_out(0xFF>>(x0&7), GRA_D);  *first = *first;  port_out(0xFF<<((~x1)&7), GRA_D);  *last = *last;  port_out(0xFF, GRA_D);  for(VIDEO_PTR p=first+1; p<last; p++) *p = *p; }void vga_vline(int x, int y0, int y1){// clip into screen  if (x < 0 || x > VGA_MAX_X || y1 < 0 || y0 > VGA_MAX_Y) return;  if (y0 < 0) y0 = 0;  if (y1 > VGA_MAX_Y) y1 = VGA_MAX_Y;  port_out(8, GRA_I);  port_out(128 >> (x&7), GRA_D);  VIDEO_PTR last  = VIDEO + LINE_BYTES*y1 + (x>>3);  VIDEO_PTR p;  for(p = VIDEO + LINE_BYTES*y0 + (x>>3); p <= last; p+=LINE_BYTES)  *p = *p;}void vga_box(int x0, int y0, int x1, int y1){   for(int y=y0; y<=y1; y++) vga_hline(x0,x1,y); }static void write_byte(VIDEO_PTR q, unsigned char byte, int D){  if (D==0)    { port_out(byte, GRA_D);      *q = *q;     }  else     { port_out(0x00, GRA_I );  // set_color(white)       port_out(0, GRA_D );       port_out(0x03, GRA_I ); // set_mode(src)       port_out(0, GRA_D );       port_out(0xFF, GRA_D);  // clear       *q = *q;       port_out(0x03, GRA_I ); // set_mode(or)       port_out(16, GRA_D );              for(int i = 0; i < D; i++)       { port_out(0x00, GRA_I );  // set_color(1<<i)         port_out(1<<i, GRA_D );         port_out(byte, GRA_D);         *q = *q;        }     }}static void vga_bytes(int x, int y, unsigned char* ptr, int len, int D=0){   if (len < 1) return;  // clip into display  int x1 = x + 8*len - 1;  if (y < 0 || y > VGA_MAX_Y || x1 < 0 || x > VGA_MAX_X) return;  if (x < 0)   { ptr -= x/8;    x = 0;   }  if (x1 > VGA_MAX_X) x1 = VGA_MAX_X;  len = (x1 - x + 1)/8;  // shift to byte boundary   int s1 = x % 8;  int s2 = 8 - s1;  unsigned char  last_c = 0;  unsigned char* stop = ptr + len;  VIDEO_PTR q = VIDEO + LINE_BYTES*y + x/8;  port_out(8, GRA_I);  while (ptr < stop)  { unsigned char c = *ptr++;    unsigned char byte = (c >> s1) | (last_c << s2);    port_out(byte, GRA_D);    *q = *q;    last_c = c;    q++;   }  port_out(last_c<<s2, GRA_D);  *q = *q;}void vga_bitmap(int x, int y, unsigned char* pm, int width, int height){ while (height--)  { vga_bytes(x,y++,pm,width);    pm += width;   } }//------------------------------------------------------------------------------// pixrects and images//------------------------------------------------------------------------------struct pixrect{ unsigned char** plane;  int width;  int bwidth;  int height;  int depth;  int offset;  pixrect(int, int, int, int); ~pixrect();};pixrect::pixrect(int left, int top, int right, int bottom){ offset = left % 8;  width  = right - left + 1;  height = bottom - top + 1;  depth  = VGA_DEPTH;  bwidth = width/8;  if (width % 8) bwidth++;  if (offset) bwidth++;  plane = new unsigned char*[depth];  for(int i=0; i<depth; i++)       plane[i] =  new unsigned char[height*bwidth]; }pixrect::~pixrect(){ for(int i=0; i<depth; i++) delete[] plane[i];  delete[] plane; }void vga_delimage(char* image) { delete (pixrect*)image; }char* vga_getimage(int left, int top, int right, int bottom){   if (left < 0) left = 0;  if (top  < 0) top = 0;  if (right  > VGA_MAX_X) right = VGA_MAX_X;  if (bottom > VGA_MAX_Y) bottom = VGA_MAX_Y;  pixrect* pr = new pixrect(left,top,right,bottom);  unsigned char mask1 = 0xFF >> pr->offset;  unsigned char mask2 = 0xFF << ((8-pr->offset) % 8);  /* set read mode 0 */  port_out(5, GRA_I);  port_out(0, GRA_D);  /* read planes 0 to 3 */  for(int i = 0; i < pr->depth; i++)  { unsigned char* p = pr->plane[i];    VIDEO_PTR first = VIDEO + LINE_BYTES*top + left/8;    VIDEO_PTR last  = first + pr->bwidth - 1;    port_out(4, GRA_I);    port_out(i, GRA_D);      for(int y=top; y<=bottom; y++)    { VIDEO_PTR q = first;       *p++ = (*q++) & mask1;      while (q < last) *p++ = *q++;      *p++ = (*q++) & mask2;      first += LINE_BYTES;      last  += LINE_BYTES;     }   }  return (char*)pr;  }void vga_putimage(int left, int top, char* image){   pixrect* pr = (pixrect*)image;  int right  = left + pr->width - 1;   int bottom = top  + pr->height - 1;   int left0  = left - pr->offset;  int bw = pr->bwidth;  unsigned char** pfirst = new unsigned char*[pr->depth];  for(int i = 0; i < pr->depth; i++) pfirst[i] = pr->plane[i];  unsigned char* p0 = pr->plane[0];  unsigned char* p1 = pr->plane[1];  unsigned char* p2 = pr->plane[2];  unsigned char* p3 = pr->plane[3];  for(int y=top; y <= bottom; y++)   { vga_set_color(0);    vga_set_mode(0);    vga_hline(left,right,y); // clear line    vga_set_mode(2); // or    for(int i = 0; i < pr->depth; i++)    { vga_set_color(1<<i);      vga_bytes(left0,y,pfirst[i],bw);      pfirst[i] += bw;     }   }  }void vga_copyimage(int left,int top,int right,int bottom,int x,int y){    int width  = right-left+1;  int height = bottom-top+1;  if(x < 0)   { left += x;    width -= x;    x = 0;   }  if(y < 0)   { top += y;    height -= y;    y = 0;   }  if(x+width >= vga_width()) width = vga_width() - x - 1;  if(y+height >= vga_height()) height = vga_height() - y -1;  int  d1  = LINE_BYTES;  int  d2  = d1 * (height-1);  VIDEO_PTR first1 = VIDEO + LINE_BYTES*y + x/8;  VIDEO_PTR last1  = VIDEO + LINE_BYTES*y + (x+width-1)/8;  VIDEO_PTR first  = VIDEO + LINE_BYTES*top + left/8;  VIDEO_PTR last   = VIDEO + LINE_BYTES*top + (left+width-1)/8;  VIDEO_PTR p;  VIDEO_PTR q;  if (y > top)  { first1 +=  d2;    last1  +=  d2;    first  +=  d2;    last   +=  d2;    d1  = -d1;   }  // set write mode 1   port_out(5, GRA_I);  port_out(1, GRA_D);  for(int i=0; i<height; i++)  {     if (x <= left)      for(q=first,p=first1; q <= last;  q++,p++) *p = *q;    else      for(q=last,p=last1;   q >= first; q--,p--) *p = *q;    first1 += d1;    first  += d1;    last1  += d1;    last   += d1;   }  /* set write mode 0 */  port_out(5, GRA_I);  port_out(0, GRA_D); }

⌨️ 快捷键说明

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