home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_40.arc / KBDTEST.ARC / VIDEO.C < prev    next >
Text File  |  1988-01-11  |  5KB  |  159 lines

  1. /* Code from 86World column in Micro Cornucopia Issue #40 */
  2.  
  3. /* VIDEO.C - a few video subroutines to make writing C programs a bit simpler
  4.  
  5.         a line of code for printing something will look like this:
  6.  
  7.             at(2,20); in(RED,WHITE); cprintf("Just Like English!");
  8.  
  9.         Of course, you can omit the at() or in() (or both) if you like.
  10.  
  11.     NOTE: In order for this package to work, you must link in the file
  12.           MCMVSMEM.OBJ from the Turbo C distribution disk.
  13.  
  14.           Also remember that you must somehow force VIDEO.C to recompile
  15.           whenever you switch memory models. This is because I haven't
  16.           specifically declared all the routines and variables as 'far'
  17.           (I hate using extra space and code unless I have to). If you
  18.           like, you can do so yourself, then you can link the same OBJ
  19.           with any memory model.
  20.  
  21.     To use the following routines in your own program, just put the
  22.     files video.c, video.h, and mcmvsmem.obj in your working directory,
  23.     add the line '#include "video.h" to the top of your program, call
  24.     initvideo() at the beginning of main(), and make a project file that
  25.     makes reference to all three files. See kbdtest.c and kbdtest.prj
  26.     for examples
  27.  
  28.     As usual: permission granted to use this for whatever you want to,
  29.     commercial, private, obscene, or otherwise.
  30.  
  31.                                     Laine Stump, 12/13/87
  32.                                                                         */
  33. #include <stdarg.h>
  34. #include <stdio.h>
  35. #include <dos.h>
  36. #include <mem.h>
  37. #include <string.h>
  38. #include "video.h"
  39.  
  40. char snow, curcolor;
  41. int  currow, curcol;
  42. char far *screen;
  43.  
  44. struct character
  45.     {
  46.     char ch;
  47.     char color;
  48.     };
  49.  
  50. void scroll(int lines, int x1, int y1, int x2, int y2)
  51. /* Scrolls the screen between x1,y1 and x2,y2. Scrolling is up if
  52.    lines is positive, down if it is negative */
  53.     {
  54.     union REGS reg;
  55.  
  56.     if (lines < 0)
  57.         {
  58.         reg.h.ah = 7;
  59.         lines = (-lines);
  60.         }
  61.     else
  62.         reg.h.ah = 6;
  63.     reg.h.al = lines;
  64.     reg.h.bh = curcolor;
  65.     reg.x.cx = (y1 << 8) + x1;
  66.     reg.x.dx = (y2 << 8) + x2;
  67.     int86(0X10, ®, ®);
  68.     } /* scroll */
  69.  
  70. int egainstalled(void)
  71. /* returns true if EGA is installed */
  72.     {
  73.     union REGS reg;
  74.  
  75.     reg.x.ax = 0x1200;
  76.     reg.x.bx = 0x0010;
  77.     reg.x.cx = 0xFFFF;
  78.     int86(0X10, ®, ®);
  79.     return((reg.x.cx == 0xFFFF) ? 0 : 1);
  80.     } /* egainstalled */
  81.  
  82. void cursorsize(int startline, int endline)
  83. /* Sets the size of the cursor */
  84.     {
  85.     union REGS reg;
  86.  
  87.     reg.h.ah = 1;
  88.     reg.x.cx = (startline << 8) + endline;
  89.     int86(0X10, ®, ®);
  90.     } /* cursorsize */
  91.  
  92. void clearscreen(void)
  93. /* clears the screen to curcolor and positions cursor at(0,0) */
  94.     {
  95.     scroll(0, 0, 0, 79, 24);
  96.     at(0,0);
  97.     } /* clearscreen */
  98.  
  99. void at(int row, int col)
  100. /* places the cursor at a specific row and column */
  101.     {
  102.     union REGS reg;
  103.  
  104.     currow = row;   /* for use by cprintf */
  105.     curcol = col;
  106.     reg.h.ah = 2;
  107.     reg.h.bh = 0;
  108.     reg.x.dx = (row << 8) + col;
  109.     int86(0X10, ®, ®);
  110.     } /* at */
  111.  
  112. void in(char forecolor, char backcolor)
  113. /* sets current color to 'color' */
  114.     {
  115.     curcolor = forecolor | (backcolor << 4);
  116.     } /* in */
  117.  
  118. void cprintf(va_list arg_list, ...)
  119. /* Prints a string in video memory at current location in current color
  120.    then advances cursor to end of string. This cprintf replaces the
  121.    standard cprintf in UNIX c. It is functionally identical, except
  122.    that it will use the color set by the function in(). */
  123.     {
  124.     va_list arg_ptr;
  125.     char *format;
  126.     char output[81];
  127.     struct character buffer[80];
  128.     int counter, len;
  129.     unsigned size;
  130.  
  131.     va_start(arg_ptr, arg_list);
  132.     format = arg_list;
  133.     vsprintf(output, format, arg_ptr);
  134.     len = strlen(output);
  135.     size = len << 1;
  136.     setmem(buffer, size, curcolor);
  137.     for (counter = 0; counter < len; counter++)
  138.     buffer[counter].ch = output[counter];
  139.     movescreenmem((char far *)(buffer),
  140.                   screen + (currow * 160) + (curcol << 1), size, snow);
  141.     at(currow,curcol+len);
  142.     } /* cprintf */
  143.  
  144. void initvideo(void)
  145. /* determines what type of video is in use and sets screen pointer,
  146.    then sets colors to lightgray on black and clears screen
  147.    Variable snow is needed by movescreenmem routine contained in
  148.    the file MCMVSMEM.C (.OBJ) that comes with Turbo C.
  149. */
  150.     {
  151.     union REGS reg;
  152.  
  153.     reg.h.ah = 15;
  154.     int86(0X10, ®, ®);
  155.     snow = (!egainstalled() && reg.h.al != 7);
  156.     screen = (char far *)((reg.h.al != 7) ? 0xB8000000L : 0xB0000000L);
  157.     in(LIGHTGRAY,BLACK); clearscreen();
  158.     } /* initvideo */
  159.