home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l352 / 1.img / EXAMPLES / FULLSCRN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-04  |  4.8 KB  |  217 lines

  1. /* 
  2. FULLSCRN.C -- package of routines for direct screen writes
  3.  
  4. Borland C:
  5.     uses inline assembler
  6.         
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. #include <dos.h>
  14.  
  15. #include <phapi.h>
  16.  
  17. #include "fullscrn.h"
  18.  
  19. typedef void far *FP;
  20. typedef unsigned long ULONG;
  21.  
  22. #define VIDEO               0x10
  23. #define SCROLL_UP           0x06
  24. #define GET_VIDEO_MODE      0x0F
  25.  
  26. static BYTE far *vid_mem;
  27.  
  28. #define SCR(y,x)            (((y) * 160) + ((x) << 1))
  29.  
  30. int video_mode(void)
  31. {
  32.     int mode;
  33.     _asm mov ah, GET_VIDEO_MODE
  34.     _asm int 10h
  35.     _asm xor ah, ah
  36.     _asm mov mode,ax
  37.     return mode;
  38. }
  39.  
  40. unsigned get_vid_mem(void)
  41. {
  42.     int vmode = video_mode();
  43.     unsigned short vid_seg;
  44.  
  45.     if (vmode == 7)
  46.         vid_seg = 0xB000;
  47.     else if ((vmode == 2) || (vmode == 3))
  48.         vid_seg = 0xB800;
  49.     else
  50.         return 0;
  51.  
  52. #ifdef DOSX286
  53.     {
  54.         unsigned short sel;
  55.         /*
  56.             DosMapRealSeg() takes a real-mode paragraph address and
  57.             a count of bytes, and gives back a selector that can be used
  58.             to address the memory from protected mode. Like all PHAPI
  59.             functions, DosMapRealSeg() returns 0 for success, or a
  60.             non-zero error code. Any returned information (such as the
  61.             selector we're interested in) is returned via parameters.
  62.         */
  63.         if (DosMapRealSeg(vid_seg, (long) 25*80*2, &sel) == 0)
  64.             return sel;
  65.         else
  66.             return 0;
  67.     }
  68. #else
  69.         return vid_seg;
  70. #endif
  71.  
  72. }
  73.  
  74. int video_init(void)
  75. {
  76.     return !! (vid_mem = MAKEP(get_vid_mem(), 0));
  77. }
  78.  
  79. #define MAKEWORD(l, h)  (((unsigned short) (l)) | ((unsigned short) (h)) << 8)
  80.  
  81. /* faster and safer version */
  82. void wrt_str(int y, int x, ATTRIB attr, unsigned char *p)
  83. {
  84.     unsigned short far *v = (unsigned short far *)(vid_mem + SCR(y, x));
  85.     int ok = 80 - x;
  86.     while (ok && *p)
  87.     {
  88.         *v++ = MAKEWORD(*p++, attr);
  89.         ok--;
  90.     }
  91. }
  92.  
  93. static char buf[128] = {0};
  94.  
  95. void wrt_printf(int y, int x, ATTRIB attr, char *fmt, ...)
  96. {
  97.     va_list marker;
  98.     va_start(marker, fmt);
  99.     vsprintf(buf, fmt, marker); 
  100.     va_end(marker);
  101.     wrt_str(y, x, attr, buf);
  102. }
  103.  
  104. void wrt_chr(int y, int x, ATTRIB attr, unsigned char c)
  105. {
  106.     BYTE far *v = vid_mem + SCR(y, x);
  107.     *v++ = c;
  108.     *v = attr;
  109. }
  110.  
  111. void set_attr(int starty, int startx, int endy, int endx, ATTRIB attr)
  112. {
  113.     int x, y;
  114.     for (y=starty; y<=endy; y++)
  115.     {
  116.         BYTE far *v = vid_mem + SCR(y, startx);
  117.         for (x=startx; x<=endx; x++)
  118.         {
  119.             v++;
  120.             *v++ = attr;
  121.         }
  122.     }
  123. }
  124.  
  125. void fill(int starty, int startx, int endy, int endx, unsigned char c,
  126.     ATTRIB attr)
  127. {
  128.     BYTE far *v = vid_mem;
  129.     int x, y;
  130.     for (y=starty; y<=endy; y++)
  131.         for (x=startx, v=vid_mem+SCR(y, startx); x<=endx; x++)
  132.         {
  133.             *v++ = c;
  134.             *v++ = attr;
  135.         }
  136. }
  137.  
  138. void clear(int starty, int startx, int endy, int endx, ATTRIB attr)
  139. {
  140.     _asm mov ax, 0600h
  141.     _asm mov bh, byte ptr attr
  142.     _asm mov ch, byte ptr starty
  143.     _asm mov cl, byte ptr startx
  144.     _asm mov dh, byte ptr endy
  145.     _asm mov dl, byte ptr endx
  146.     _asm int 10h
  147. }
  148.  
  149. void cls(void)
  150. {
  151.     clear(0, 0, 24, 79, NORMAL);
  152. }
  153.  
  154. static char brd[2][6] = {
  155.     179, 196, 218, 191, 192, 217,       /* single box chars */
  156.     186, 205, 201, 187, 200, 188        /* double box chars */
  157.     } ;
  158.  
  159. void border(int starty, int startx, int endy, int endx, ATTRIB attr, int dbl)
  160. {
  161.     BYTE far *v;
  162.     char *b;
  163.     register int i;
  164.     
  165.     b = brd[(dbl-1) & 1];
  166.     
  167.     for (i=starty+1; i<endy; i++)
  168.     {
  169.         wrt_chr(i, startx, attr, *b);
  170.         wrt_chr(i, endx, attr, *b);
  171.     }
  172.     b++;
  173.     for (i=startx+1, v=vid_mem+SCR(starty, startx+1); i<endx; i++)
  174.     {
  175.         *v++ = *b;
  176.         *v++ = attr;
  177.         // equivalent to wrt_chr(starty, i, attr, *b);
  178.     }
  179.     for (i=startx+1, v=vid_mem+SCR(endy, startx+1); i<endx; i++)
  180.     {
  181.         *v++ = *b;
  182.         *v++ = attr;
  183.         // equivalent to wrt_chr(endy, i, attr, *b);
  184.     }
  185.     b++;
  186.     wrt_chr(starty, startx, attr, *b++);
  187.     wrt_chr(starty, endx, attr, *b++);
  188.     wrt_chr(endy, startx, attr, *b++);
  189.     wrt_chr(endy, endx, attr, *b);
  190. }
  191.  
  192. void cursor(int on)
  193. {
  194.     static int old_curs = 0;
  195.     if (on && old_curs)
  196.     {
  197.         _asm mov cx, old_curs
  198.         _asm mov ah, 1
  199.         _asm int 10h
  200.     }
  201.     else if (! (on || old_curs))
  202.     {
  203.         _asm mov ah, 3
  204.         _asm mov bh, 0
  205.         _asm int 10h
  206.         _asm mov old_curs, cx
  207.         _asm mov cx, 2000h
  208.         _asm mov ah, 1
  209.         _asm int 10h
  210.     }
  211. }
  212.  
  213. void center(int y, ATTRIB attr, char *s)
  214. {
  215.     wrt_str(y, 40 - (strlen(s) >> 1), attr, s);
  216. }
  217.