home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / IMGPROC.ZIP / C1.ZIP / VGA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-11  |  5.5 KB  |  217 lines

  1. /*  
  2. Copyright 1990 by John Wiley & Sons, Inc.
  3.           All Rights Reserved.
  4. */
  5.  
  6. /****************************************/
  7. /*    VGA Graphic Adapter Functions     */
  8. /*       written in Turbo C 2.0         */
  9. /*                by                    */
  10. /*         Craig A. Lindley             */
  11. /*                                      */
  12. /*   Vers: 1.0  Last Update: 12/04/89   */
  13. /****************************************/
  14.  
  15. #include <stdio.h>
  16. #include <process.h>
  17. #include <dos.h>
  18. #include <graphics.h>
  19. #include "misc.h"
  20. #include "pcx.h"
  21. #include "vga.h"
  22.  
  23. /* equally tempered 16 level gray scale */
  24. ColorRegister Gray16ColorPalette[MAXPALETTECOLORS] =
  25.  
  26. { 0, 0, 0, 5, 5, 5, 8, 8, 8,11,11,11,
  27.  14,14,14,17,17,17,20,20,20,24,24,24,
  28.  28,28,28,32,32,32,36,36,36,40,40,40,
  29.  45,45,45,50,50,50,56,56,56,63,63,63 };
  30.  
  31.  
  32.  
  33. /* equally tempered 64 level gray scale */
  34. ColorRegister Gray64ColorPalette[MAX256PALETTECOLORS] =
  35.  
  36. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37.   0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  38.   3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
  39.   6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9,
  40.  10,10,10,10,10,10,11,11,11,12,12,12,
  41.  13,13,13,14,14,14,15,15,15,16,16,16,
  42.  18,18,18,19,19,19,20,20,20,21,21,21,
  43.  22,22,22,23,23,23,24,24,24,25,25,25,
  44.  26,26,26,27,27,27,28,28,28,29,29,29,
  45.  31,31,31,32,32,32,33,33,33,34,34,34,
  46.  35,35,35,36,36,36,37,37,37,39,39,39,
  47.  40,40,40,41,41,41,42,42,42,43,43,43,
  48.  44,44,44,46,46,46,47,47,47,48,48,48,
  49.  49,49,49,50,50,50,52,52,52,53,53,53,
  50.  54,54,54,55,55,55,56,56,56,58,58,58,
  51.  59,59,59,60,60,60,61,61,61,63,63,63 };
  52.  
  53.  
  54.  
  55. /* Start of VGA functions */
  56.  
  57. /* Initialize the graphics subsystem */
  58. void InitGraphics( void )
  59. {
  60.    int g_driver, g_mode, g_error;
  61.  
  62.    /*
  63.    Make sure graphic system is not already open. Whether
  64.    it is or not, close it and open it again. Otherwise
  65.    memory for driver will be allocated each time this
  66.    function is called.
  67.    */
  68.  
  69.    closegraph();
  70.  
  71.    /* initialize graphics variables */
  72.    g_driver = g_mode = g_error = 0;
  73.  
  74.    /*
  75.    The call to registerbgidriver below links the display driver
  76.    to the application program. It assumes the driver was converted from
  77.    egavga.bgi to egavga.obj by the bgiobj converter program and
  78.    linked into the application program. The line egavga.obj must
  79.    be in the .prj file for the application program.
  80.    */
  81.    registerbgidriver(EGAVGA_driver);
  82.  
  83.    initgraph(&g_driver,&g_mode,"");
  84.    g_error = graphresult();
  85.    if (g_error < 0)
  86.    {
  87.       printf("Initgraph error: %s.\n",
  88.       grapherrormsg(g_error));
  89.       exit(EGraphics);
  90.    }
  91.    restorecrtmode();
  92. }
  93.  
  94. /*
  95. This function fetches the current video mode
  96. from the active video controller.
  97. */
  98.  
  99. unsigned GetVideoMode( void )
  100. {
  101.    union REGS regs;
  102.  
  103.    regs.h.ah = 0x0F;               /* request the current video mode */
  104.    int86(VIDEO,®s,®s);
  105.    return(regs.h.al);
  106. }
  107.  
  108. /* Set the VGA into the 256 320 by 200 display mode. */
  109. void Set256ColorMode( void )
  110. {
  111.    union REGS regs;
  112.  
  113.    setgraphmode(VGALO);            /* go into graphic mode */
  114.    regs.h.ah = 0;            /* now into mode 13H for 256 colors */
  115.    regs.h.al = 0x13;
  116.    int86(VIDEO,®s,®s);
  117. }
  118.  
  119. /* Set an individual VGA color register */
  120. void SetAColorReg(unsigned RegNum, unsigned Red,
  121.           unsigned Green,  unsigned Blue)
  122. {
  123.    union REGS regs;
  124.  
  125.    /*
  126.    With graphics mode set, we can load a color register
  127.    in the DAC.
  128.    */
  129.  
  130.    /* set a Color Register */
  131.    regs.h.ah = 0x10;
  132.    regs.h.al = 0x10;
  133.    regs.x.bx = RegNum;
  134.    regs.h.dh = Red;
  135.    regs.h.ch = Green;
  136.    regs.h.cl = Blue;
  137.    int86(VIDEO,®s,®s);
  138. }
  139.  
  140. /* Get the color components of a VGA color register */
  141. void GetAColorReg(unsigned RegNum, unsigned *Red,
  142.           unsigned *Green, unsigned *Blue)
  143. {
  144.    union REGS regs;
  145.  
  146.    /*
  147.    With graphics mode set, we can read a color register
  148.    from the DAC.
  149.    */
  150.  
  151.    /* get a Color Register's components */
  152.    regs.h.ah = 0x10;
  153.    regs.h.al = 0x15;
  154.    regs.x.bx = RegNum;
  155.    int86(VIDEO,®s,®s);
  156.    /* store the returned values at the pointers */
  157.    *Red   = regs.h.dh;
  158.    *Green = regs.h.ch;
  159.    *Blue  = regs.h.cl;
  160. }
  161.  
  162.  
  163. /* load the gray palette */
  164. void LoadGray16Palette(void)
  165. {
  166.    struct palettetype palette;
  167.    unsigned Index;
  168.    union REGS regs;
  169.  
  170.    /*
  171.    With a graphics mode set, we can proceed to load our palette and
  172.    color registers in the DAC. The palette is set up in sequential
  173.    order and the color register are set to gray scale values.
  174.    */
  175.  
  176.    palette.size = 16;
  177.  
  178.    for (Index = 0; Index < MAXPALETTECOLORS; Index++)
  179.       palette.colors[Index] = Index;
  180.  
  181.    /* set a block of Color Registers */
  182.    regs.h.ah = 0x10;
  183.    regs.h.al = 0x12;
  184.    regs.x.bx = 0;
  185.    regs.x.cx = MAXPALETTECOLORS;
  186.    _ES = FP_SEG(Gray16ColorPalette);
  187.    regs.x.dx =FP_OFF(Gray16ColorPalette);
  188.    int86(VIDEO,®s,®s);
  189.  
  190.    /* install the newly created palette */
  191.    setallpalette(&palette);
  192. }
  193.  
  194.  
  195. /* load the gray palette */
  196. void LoadGray64Palette(void)
  197. {
  198.    union REGS regs;
  199.  
  200.    /*
  201.    This 64 level gray scale can only be loaded when the VGA is in
  202.    the 256 color mode 13h. The actual palette mechanism is bypassed
  203.    in this mode. The color registers in the DAC are loaded and accessed
  204.    directly.
  205.    */
  206.  
  207.    /* set a block of Color Registers */
  208.    regs.h.ah = 0x10;
  209.    regs.h.al = 0x12;
  210.    regs.x.bx = 0;
  211.    regs.x.cx = 64;
  212.    _ES = FP_SEG(Gray64ColorPalette);
  213.    regs.x.dx =FP_OFF(Gray64ColorPalette);
  214.  
  215.    int86(VIDEO,®s,®s);
  216. }
  217.