home *** CD-ROM | disk | FTP | other *** search
/ World of Graphics / WOGRAPH.BIN / 443.SVGA16C.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  6KB  |  213 lines

  1. /****************************************************************************
  2. *
  3. *                           SuperVGA Test Library
  4. *
  5. *                   Copyright (C) 1993 Kendall Bennett.
  6. *                            All rights reserved.
  7. *
  8. * Filename:        $RCSfile: svga16c.c $
  9. * Version:        $Revision: 1.2 $
  10. *
  11. * Language:        ANSI C
  12. * Environment:    IBM PC (MSDOS)
  13. *
  14. * Description:    Simple library to collect together the functions in the
  15. *                SuperVGA test library for use in other C programs. The
  16. *                support is reasonably low level, so you can do what you
  17. *                want.
  18. *
  19. *                MUST be compiled in the large model.
  20. *
  21. * $Id: svga16c.c 1.2 1993/03/07 04:06:11 kjb Exp $
  22. *
  23. * Revision History:
  24. * -----------------
  25. *
  26. * $Log: svga16c.c $
  27. * Revision 1.2  1993/03/07  04:06:11  kjb
  28. * Bug fixes.
  29. *
  30. * Revision 1.1  1993/03/03  10:28:57  kjb
  31. * Initial revision
  32. *
  33. ****************************************************************************/
  34.  
  35. #include "svga16.h"
  36.  
  37. /*-------------------------- Internal Functions --------------------------*/
  38.  
  39. /* In svga16.asm */
  40.  
  41. int _initSuperVGA(int driver,int chipID,int mode,int memory);
  42. int _setSuperVGAMode(void);
  43. void _getVideoInfo(int *xres,int *yres,int *bytesperline,int *maxpage);
  44.  
  45. /* Global variables */
  46.  
  47. PUBLIC    int        _grResult;
  48. PUBLIC    int        maxx,maxy;
  49. PUBLIC    int        maxpage,bytesperline;
  50. PUBLIC    bool    twobanks,extendedflipping;
  51. PUBLIC    int        far *modeList;
  52. PUBLIC    int        _VESAFirst = false;
  53. PUBLIC    int        _ignoreSVGA = false;
  54. PRIVATE    int        _driver,_chipID,_memory;
  55.  
  56. /*----------------------------- Implementation ----------------------------*/
  57.  
  58. PUBLIC void initSuperVGA(int *driver,int *chipID,int *memory,int *dac)
  59. /****************************************************************************
  60. *
  61. * Function:        initSuperVGA
  62. * Parameters:    driver    - Place to store the SuperVGA driver ID
  63. *                chipID    - Place to store the SuperVGA chip ID
  64. *                memory    - Place to store the amount of memory
  65. *                dac        - Place to store the Video DAC type
  66. *
  67. * Description:    Detects the installed SuperVGA and initialises the library,
  68. *                if the value in 'driver' is grDETECT. You can force the
  69. *                library to work with any values you like, by simply
  70. *                passing valid values as parameters.
  71. *
  72. ****************************************************************************/
  73. {
  74.     int        mode;
  75.  
  76.     if (*driver == grDETECT) {
  77.         /* Auto detect the installed graphics adapter */
  78.  
  79.         MGL_detectGraph(driver,chipID,memory,dac,&mode);
  80.         }
  81.     modeList = MGL_availableModes(*driver,*memory);
  82.     mode = _initSuperVGA(*driver,*chipID,grEGA_320x200x16,*memory);
  83.     extendedflipping = (mode & 1);
  84.     twobanks = (mode & 2);
  85.     _driver = *driver;
  86.     _chipID = *chipID;
  87.     _memory = *memory;
  88. }
  89.  
  90. PUBLIC bool setSuperVGAMode(int mode)
  91. /****************************************************************************
  92. *
  93. * Function:        setSuperVGAMode
  94. * Parameters:    mode    - SuperVGA video mode to set.
  95. * Returns:        True if the mode was set, false if not.
  96. *
  97. * Description:    Attempts to set the specified video mode. This routine
  98. *                assumes that the library and SuperVGA have been initialised
  99. *                with the initSuperVGA() routine first.
  100. *
  101. ****************************************************************************/
  102. {
  103.     if (_initSuperVGA(_driver,_chipID,mode,_memory) == -1)
  104.         return -1;
  105.     if (_setSuperVGAMode()) {
  106.         _getVideoInfo(&maxx,&maxy,&bytesperline,&maxpage);
  107.         return true;
  108.         }
  109.     return false;
  110. }
  111.  
  112. PUBLIC void line(int x1,int y1,int x2,int y2,int color)
  113. /****************************************************************************
  114. *
  115. * Function:        line
  116. * Parameters:    x1,y1        - First endpoint of line
  117. *                x2,y2        - Second endpoint of line
  118. *
  119. * Description:    Scan convert a line segment using the MidPoint Digital
  120. *                Differential Analyser algorithm.
  121. *
  122. ****************************************************************************/
  123. {
  124.     int        d;                        /* Decision variable                */
  125.     int        dx,dy;                    /* Dx and Dy values for the line    */
  126.     int        Eincr,NEincr;            /* Decision variable increments        */
  127.     int        yincr;                    /* Increment for y values            */
  128.     int        t;                        /* Counters etc.                    */
  129.  
  130.     dx = ABS(x2 - x1);
  131.     dy = ABS(y2 - y1);
  132.  
  133.     if (dy <= dx) {
  134.  
  135.         /* We have a line with a slope between -1 and 1
  136.          *
  137.          * Ensure that we are always scan converting the line from left to
  138.          * right to ensure that we produce the same line from P1 to P0 as the
  139.          * line from P0 to P1.
  140.          */
  141.  
  142.         if (x2 < x1) {
  143.             t = x2; x2 = x1; x1 = t;    /* Swap X coordinates            */
  144.             t = y2; y2 = y1; y1 = t;    /* Swap Y coordinates            */
  145.             }
  146.  
  147.         if (y2 > y1)
  148.             yincr = 1;
  149.         else
  150.             yincr = -1;
  151.  
  152.         d = 2*dy - dx;                /* Initial decision variable value    */
  153.         Eincr = 2*dy;                /* Increment to move to E pixel        */
  154.         NEincr = 2*(dy - dx);        /* Increment to move to NE pixel    */
  155.  
  156.         putPixel(x1,y1,color);        /* Draw the first point at (x1,y1)    */
  157.  
  158.         /* Incrementally determine the positions of the remaining pixels
  159.          */
  160.  
  161.         for (x1++; x1 <= x2; x1++) {
  162.             if (d < 0) {
  163.                 d += Eincr;            /* Choose the Eastern Pixel            */
  164.                 }
  165.             else {
  166.                 d += NEincr;        /* Choose the North Eastern Pixel    */
  167.                 y1 += yincr;        /* (or SE pixel for dx/dy < 0!)        */
  168.                 }
  169.             putPixel(x1,y1,color);    /* Draw the point                    */
  170.             }
  171.         }
  172.     else {
  173.  
  174.         /* We have a line with a slope between -1 and 1 (ie: includes
  175.          * vertical lines). We must swap our x and y coordinates for this.
  176.          *
  177.          * Ensure that we are always scan converting the line from left to
  178.          * right to ensure that we produce the same line from P1 to P0 as the
  179.          * line from P0 to P1.
  180.          */
  181.  
  182.         if (y2 < y1) {
  183.             t = x2; x2 = x1; x1 = t;    /* Swap X coordinates            */
  184.             t = y2; y2 = y1; y1 = t;    /* Swap Y coordinates            */
  185.             }
  186.  
  187.         if (x2 > x1)
  188.             yincr = 1;
  189.         else
  190.             yincr = -1;
  191.  
  192.         d = 2*dx - dy;                /* Initial decision variable value    */
  193.         Eincr = 2*dx;                /* Increment to move to E pixel        */
  194.         NEincr = 2*(dx - dy);        /* Increment to move to NE pixel    */
  195.  
  196.         putPixel(x1,y1,color);        /* Draw the first point at (x1,y1)    */
  197.  
  198.         /* Incrementally determine the positions of the remaining pixels
  199.          */
  200.  
  201.         for (y1++; y1 <= y2; y1++) {
  202.             if (d < 0) {
  203.                 d += Eincr;            /* Choose the Eastern Pixel            */
  204.                 }
  205.             else {
  206.                 d += NEincr;        /* Choose the North Eastern Pixel    */
  207.                 x1 += yincr;        /* (or SE pixel for dx/dy < 0!)        */
  208.                 }
  209.             putPixel(x1,y1,color);    /* Draw the point                    */
  210.             }
  211.         }
  212. }
  213.