home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / dflt14.zip / VIDEO.C < prev    next >
Text File  |  1992-06-29  |  7KB  |  290 lines

  1. /* --------------------- video.c -------------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. BOOL ClipString;
  6. static BOOL snowy;
  7.  
  8. static unsigned video_address;
  9. static int near vpeek(int far *vp);
  10. static void near vpoke(int far *vp, int c);
  11. void movefromscreen(void *bf, int offset, int len);
  12. void movetoscreen(void *bf, int offset, int len);
  13.  
  14. /* -- read a rectangle of video memory into a save buffer -- */
  15. void getvideo(RECT rc, void far *bf)
  16. {
  17.     int ht = RectBottom(rc)-RectTop(rc)+1;
  18.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  19.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  20.     hide_mousecursor();
  21.     while (ht--)    {
  22.         movefromscreen(bf, vadr, bytes_row);
  23.         vadr += SCREENWIDTH*2;
  24.         bf = (char far *)bf + bytes_row;
  25.     }
  26.     show_mousecursor();
  27. }
  28.  
  29. /* -- write a rectangle of video memory from a save buffer -- */
  30. void storevideo(RECT rc, void far *bf)
  31. {
  32.     int ht = RectBottom(rc)-RectTop(rc)+1;
  33.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  34.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  35.     hide_mousecursor();
  36.     while (ht--)    {
  37.         movetoscreen(bf, vadr, bytes_row);
  38.         vadr += SCREENWIDTH*2;
  39.         bf = (char far *)bf + bytes_row;
  40.     }
  41.     show_mousecursor();
  42. }
  43.  
  44. /* -------- read a character of video memory ------- */
  45. unsigned int GetVideoChar(int x, int y)
  46. {
  47.     int c;
  48.     hide_mousecursor();
  49.     if (snowy)
  50.         c = vpeek(MK_FP(video_address, vad(x,y)));
  51.     else
  52.         c = peek(video_address, vad(x,y));
  53.     show_mousecursor();
  54.     return c;
  55. }
  56.  
  57. /* -------- write a character of video memory ------- */
  58. void PutVideoChar(int x, int y, int c)
  59. {
  60.     if (x < SCREENWIDTH && y < SCREENHEIGHT)    {
  61.         hide_mousecursor();
  62.         if (snowy)
  63.             vpoke(MK_FP(video_address, vad(x,y)), c);
  64.         else
  65.             poke(video_address, vad(x,y), c);
  66.         show_mousecursor();
  67.     }
  68. }
  69.  
  70. BOOL CharInView(WINDOW wnd, int x, int y)
  71. {
  72.     WINDOW nwnd = NextWindow(wnd);
  73.     WINDOW pwnd;
  74.     RECT rc;
  75.     int x1 = GetLeft(wnd)+x;
  76.     int y1 = GetTop(wnd)+y;
  77.  
  78.     if (!TestAttribute(wnd, VISIBLE))
  79.         return FALSE;
  80.     if (!TestAttribute(wnd, NOCLIP))    {
  81.         WINDOW wnd1 = GetParent(wnd);
  82.         while (wnd1 != NULL)    {
  83.             /* --- clip character to parent's borders -- */
  84.             if (!TestAttribute(wnd1, VISIBLE))
  85.                 return FALSE;
  86.             if (!InsideRect(x1, y1, ClientRect(wnd1)))
  87.                 return FALSE;
  88.             wnd1 = GetParent(wnd1);
  89.         }
  90.     }
  91.     while (nwnd != NULL)    {
  92.         if (!isHidden(nwnd) && !isAncestor(wnd, nwnd))    {
  93.             rc = WindowRect(nwnd);
  94.             if (TestAttribute(nwnd, SHADOW))    {
  95.                 RectBottom(rc)++;
  96.                 RectRight(rc)++;
  97.             }
  98.             if (!TestAttribute(nwnd, NOCLIP))    {
  99.                 pwnd = nwnd;
  100.                 while (GetParent(pwnd))    {
  101.                     pwnd = GetParent(pwnd);
  102.                     rc = subRectangle(rc, ClientRect(pwnd));
  103.                 }
  104.             }
  105.             if (InsideRect(x1,y1,rc))
  106.                 return FALSE;
  107.         }
  108.         nwnd = NextWindow(nwnd);
  109.     }
  110.     return (x1 < SCREENWIDTH && y1 < SCREENHEIGHT);
  111. }
  112.  
  113. /* -------- write a character to a window ------- */
  114. void wputch(WINDOW wnd, int c, int x, int y)
  115. {
  116.     if (CharInView(wnd, x, y))    {
  117.         int ch = (c & 255) | (clr(foreground, background) << 8);
  118.         int xc = GetLeft(wnd)+x;
  119.         int yc = GetTop(wnd)+y;
  120.         hide_mousecursor();
  121.         if (snowy)
  122.             vpoke(MK_FP(video_address, vad(xc, yc)), ch);
  123.         else
  124.             poke(video_address, vad(xc, yc), ch);
  125.         show_mousecursor();
  126.     }
  127. }
  128.  
  129. /* ------- write a string to a window ---------- */
  130. void wputs(WINDOW wnd, void *s, int x, int y)
  131. {
  132.     int x1 = GetLeft(wnd)+x;
  133.     int x2 = x1;
  134.     int y1 = GetTop(wnd)+y;
  135.     if (x1 < SCREENWIDTH && y1 < SCREENHEIGHT && isVisible(wnd))    {
  136.         int ln[200];
  137.         int *cp1 = ln;
  138.         unsigned char *str = s;
  139.         int fg = foreground;
  140.         int bg = background;
  141.         int len;
  142.         int off = 0;
  143.         while (*str)    {
  144.             if (*str == CHANGECOLOR)    {
  145.                 str++;
  146.                 foreground = (*str++) & 0x7f;
  147.                 background = (*str++) & 0x7f;
  148.                 continue;
  149.             }
  150.             if (*str == RESETCOLOR)    {
  151.                 foreground = fg & 0x7f;
  152.                 background = bg & 0x7f;
  153.                 str++;
  154.                 continue;
  155.             }
  156.                *cp1 = (*str & 255) | (clr(foreground, background) << 8);
  157.             if (ClipString)
  158.                 if (!CharInView(wnd, x, y))
  159.                     *cp1 = peek(video_address, vad(x2,y1));
  160.             cp1++;
  161.             str++;
  162.             x++;
  163.             x2++;
  164.         }
  165.         foreground = fg;
  166.         background = bg;
  167.            len = (int)(cp1-ln);
  168.            if (x1+len > SCREENWIDTH)
  169.                len = SCREENWIDTH-x1;
  170.  
  171.         if (!ClipString && !TestAttribute(wnd, NOCLIP))    {
  172.             /* -- clip the line to within ancestor windows -- */
  173.             RECT rc = WindowRect(wnd);
  174.             WINDOW nwnd = GetParent(wnd);
  175.             while (len > 0 && nwnd != NULL)    {
  176.                 if (!isVisible(nwnd))    {
  177.                     len = 0;
  178.                     break;
  179.                 }
  180.                 rc = subRectangle(rc, ClientRect(nwnd));
  181.                 nwnd = GetParent(nwnd);
  182.             }
  183.             while (len > 0 && !InsideRect(x1+off,y1,rc))    {
  184.                 off++;
  185.                 --len;
  186.             }
  187.             if (len > 0)    {
  188.                 x2 = x1+len-1;
  189.                 while (len && !InsideRect(x2,y1,rc))    {
  190.                     --x2;
  191.                     --len;
  192.                 }
  193.             }
  194.         }
  195.         if (len > 0)    {
  196.             hide_mousecursor();
  197.             movetoscreen(ln+off, vad(x1+off,y1), len*2);
  198.             show_mousecursor();
  199.         }
  200.     }
  201. }
  202.  
  203. /* --------- get the current video mode -------- */
  204. void get_videomode(void)
  205. {
  206.     videomode();
  207.     /* ---- Monochrome Display Adaptor or text mode ---- */
  208.     snowy = FALSE;
  209.     if (ismono())
  210.         video_address = 0xb000;
  211.     else    {
  212.         /* ------ Text mode -------- */
  213.         video_address = 0xb800 + video_page;
  214.         if (!isEGA() && !isVGA())
  215.             /* -------- CGA --------- */
  216.             snowy = cfg.snowy;
  217.     }
  218. }
  219.  
  220. /* --------- scroll the window. d: 1 = up, 0 = dn ---------- */
  221. void scroll_window(WINDOW wnd, RECT rc, int d)
  222. {
  223.     if (RectTop(rc) != RectBottom(rc))    {
  224.         union REGS regs;
  225.         regs.h.cl = RectLeft(rc);
  226.         regs.h.ch = RectTop(rc);
  227.         regs.h.dl = RectRight(rc);
  228.         regs.h.dh = RectBottom(rc);
  229.         regs.h.bh = clr(WndForeground(wnd),WndBackground(wnd));
  230.         regs.h.ah = 7 - d;
  231.         regs.h.al = 1;
  232.         hide_mousecursor();
  233.         int86(VIDEO, ®s, ®s);
  234.         show_mousecursor();
  235.     }
  236. }
  237.  
  238.  
  239. static void near waitforretrace(void)
  240. {
  241. #ifndef WATCOM
  242. asm        mov        dx,3dah
  243. loop1:
  244. asm        mov        cx,6
  245. loop2:
  246. asm        in        al,dx
  247. asm        test    al,8
  248. asm        jnz        loop2
  249. asm        test    al,1
  250. asm        jz        loop2
  251. asm        cli
  252. loop3:
  253. asm        in        al,dx
  254. asm        test    al,1
  255. asm        loopnz    loop3
  256. asm        sti
  257. asm        jz        loop1
  258. #endif
  259. }
  260.  
  261. void movetoscreen(void *bf, int offset, int len)
  262. {
  263.     if (snowy)
  264.         waitforretrace();
  265.     movedata(FP_SEG(bf), FP_OFF(bf), video_address, offset, len);
  266. }
  267.  
  268. void movefromscreen(void *bf, int offset, int len)
  269. {
  270.     if (snowy)
  271.         waitforretrace();
  272.     movedata(video_address, offset,    FP_SEG(bf), FP_OFF(bf),    len);
  273. }
  274.  
  275.  
  276. static int near vpeek(int far *vp)
  277. {
  278.     int c;
  279.     waitforretrace();
  280.     c = *vp;
  281.     return c;
  282. }
  283.  
  284. static void near vpoke(int far *vp, int c)
  285. {
  286.     waitforretrace();
  287.     *vp = c;
  288. }
  289.  
  290.