home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / vesa24_2.zip / VGA13.CPP < prev    next >
C/C++ Source or Header  |  1993-04-09  |  11KB  |  275 lines

  1. /******************************* vga13.cpp **********************************/
  2. /*    Author: Don Lewis                                                       */
  3. /*  Date:    February 25, 1993                                               */
  4. /*  Description:                                                            */
  5. /*                 This Program is for demonstrating 15, 16 and 24bit colour   */
  6. /* VESA boards capabilities. This program is for purposes of demonstration  */
  7. /* only. It is not shareware. It is freeware. While Randy Buckland wrote a  */
  8. /* 256 color demo in 'C' this program only maintains Randy's original 'C'   */
  9. /* functions for your exercise. Much credit goes to Randy Buckland for his  */
  10. /* efforts. The assembly code vga24.asm is originally Randy Bucklands code. */
  11. /* I have modified it heavily and am redistributing it as vga24.asm. The    */
  12. /* 'VESA24.EXE' distribution is a compilation of 'vesa24.cpp', 'vheader.h'  */
  13. /* and 'vga24.asm'. I hope you enjoy and further develop this code and      */
  14. /* continue to offer it as freeware so that others might benefit from our   */
  15. /* efforts. Borlandc 3.1 assembler and compiler were used for development.  */
  16. /****************************************************************************/
  17. #pragma inline
  18.  
  19. #include "conio.h"
  20. #include "dos.h"
  21. #include "stdlib.h"
  22. #include "stdio.h"
  23. #include "dos.h"
  24. #include "process.h"
  25. #include "string.h"
  26. #include <e:\borlandc\cprogs\veinfo.cpp>
  27.  
  28. struct info;
  29. struct modeinfo;
  30.  
  31. ReturnCurrentMode(int curmode);
  32. void SetSvgaMode(int);
  33. void GetSvgaInfo(void);
  34. void ReturnSvgaMode(int);
  35. void tone(void);
  36. void PutPixelDemo(unsigned char);
  37. void FillAreaDemo(unsigned char, int);
  38.  
  39. void VesaInformation(void);
  40. void GetSvgaInfo(void);
  41. void ReturnSvgaMode(int nmode);
  42. ReturnCurrentMode(int curmode);
  43. void SetSvgaMode(int nmode);
  44. void writeText(int x,int y,unsigned char *str);
  45.  
  46. /* These r,g,b functions will work with std 256 color SVGA
  47.    just use the 0-255 byte color value in 'r' and '0's in 'g','b'.
  48.    Green and blue are ignored. Don Lewis */
  49. extern "C" {void vgapoint_rgb(int row, int column,
  50.          unsigned char r, unsigned char g, unsigned char b);}
  51. extern "C" {void vgaline (short row1, short col1, short row2, short col2,
  52.          unsigned char r, unsigned char g, unsigned char b);}
  53. extern "C" {void vgafill (short row, short column, short width,short height,
  54.          unsigned char r, unsigned char g, unsigned char b);}
  55.  
  56. /* These std 256 color (only) VGA functions are still supported. */
  57. /* Still SVGA 8bit mode functions */
  58. extern "C" {void vgainit (short vesamode);} /* Works for any VESA mode */
  59. extern "C" {void vgasetcolor(unsigned char[256][3], int, int);}
  60. extern "C" {void vgapoint (short row, short column, unsigned int pixel);}
  61. extern "C" {void vgarect (unsigned char *data, short linewidth,
  62.           short row, short col, short width, short height);}
  63. /* I have'nt modified or tested these functions. Don Lewis */
  64. extern "C" {void vgarect2 (unsigned char *data, short linewidth,
  65.            short row, short col, short width, short height);}
  66. extern "C" {void vgasync ();}
  67. extern "C" {void vgatext (short row, short col, char *text,
  68.           unsigned char fg, unsigned char bg);}
  69. extern "C" {short vgagetbutton();}
  70.  
  71. long    maxcolor = 0xFFFFFF,defcolor = 0x9f9f00;
  72. /****************************************************************************
  73. *
  74. *    SEG(p)        Evaluates to the segment portion of an 8086 address.
  75. *    OFF(p)        Evaluates to the offset portion of an 8086 address.
  76. *    FP(s,o)        Creates a far pointer given a segment offset pair.
  77. *    PHYS(p)        Evaluates to a long holding a physical address
  78. *
  79. ****************************************************************************/
  80. #        define SEG(p)    ( ((unsigned *)&(void far *)(p))[1] )
  81. #        define OFF(p)    ( (unsigned)(p) )
  82. #        define FP(s,o)    ( (void far *)( ((unsigned long)s << 16) +    \
  83.                           (unsigned long)o ))
  84. #        define PHYS(p)    ( (unsigned long)OFF(p) +                        \
  85.                           ((unsigned long)SEG(p) << 4))
  86.  
  87.  
  88. void main()
  89. {
  90.    unsigned char buf[80];
  91.    unsigned char inc;
  92.    unsigned int i,j,x,r,g,b;
  93.    int curmode, OldMode, NewMode;
  94.    OldMode = ReturnCurrentMode(curmode);
  95.    clrscr();
  96.    VesaInformation();
  97.    printf("Enter a 15, 16 or 24bit VESA mode in (hex) ei; 110<ret>\n");
  98.    scanf("%x", &NewMode);
  99.    printf("At the tone. . .");
  100.    delay(1000);
  101.    tone();
  102.    printf(" the present routine is finished. \n");
  103.    printf("Hit any key (which one is that) to continue.\n");
  104.    getch();
  105.    vgainit (NewMode); /* use vga24.asm init function */
  106.    GetSvgaInfo();     /* retrieves all kinda good info */
  107.    ReturnSvgaMode(NewMode); /* retrieves even more great stuff */
  108.    if ( modeinfo.BitsPerPixel < 15 )
  109.    {
  110.        SetSvgaMode(OldMode);
  111.        printf(" Sorry, This program does not use 4 or 8 bit per pixel modes.\n");
  112.        exit(1);
  113.    }
  114. /* 15 bit per pixel example:
  115.    The color range per pixel is 0-31 decimal which is equivalent
  116.    to 0 - 1F hex.                                                */
  117.    if (modeinfo.BitsPerPixel == 15)
  118.    {
  119.     inc = 31;
  120.     PutPixelDemo(inc); /* plot some random pixels */
  121.     vgafill(0,0,modeinfo.XResolution,modeinfo.YResolution,0,20,20);
  122.     vgaline(0,0,modeinfo.YResolution,modeinfo.XResolution,31,20,31);
  123.     vgaline(0,modeinfo.XResolution,modeinfo.YResolution,0,20,31,20);
  124.     FillAreaDemo(inc, NewMode);
  125.    }
  126. /* 16 bit per pixel example:
  127.    The color range per pixel is 0-31 decimal for red and blue, 0 - 62 decimal
  128.    for green which is equivalent to 0 - 1F hex for red and blue, 0 - 3F hex
  129.    for green. A handy way to approximate green level the same as blue or red
  130.    is to double the red or blue value for green. Example, BRIGHT YELLOW
  131.    is red = 31 decimal and green = 62 decimal.        */
  132.    if (modeinfo.BitsPerPixel == 16)
  133.    {
  134.        inc = 31;
  135.        PutPixelDemo(inc);
  136.        vgafill(0,0,modeinfo.XResolution,modeinfo.YResolution,25,0,20);
  137.        vgaline(0,0,modeinfo.YResolution,modeinfo.XResolution,10,50,10);
  138.        vgaline(0,modeinfo.XResolution,modeinfo.YResolution,0,0,0,0);
  139.        FillAreaDemo(inc, NewMode);
  140.    }
  141. /* 24 bits per pixel example:
  142.    Here all three colors range from 0 - 255 decimal or 0 - FF hex offering
  143.    color choices out of a 16,777,216 million color pallette.     */
  144.    if (modeinfo.BitsPerPixel == 24)
  145.    {
  146.        inc = 255;
  147.        PutPixelDemo(inc);
  148.        vgafill(0,0,modeinfo.XResolution,modeinfo.YResolution,50,100,70);
  149.        vgaline(0,0,modeinfo.YResolution,modeinfo.XResolution,150,200,150);
  150.        vgaline(0,modeinfo.XResolution,modeinfo.YResolution,0,200,150,200);
  151.        FillAreaDemo(inc, NewMode);
  152.     }
  153.  
  154. /*     Restore original video mode before program exit.  */
  155.     SetSvgaMode(OldMode);
  156. }
  157.  
  158. /*                                    */
  159. /*    PUTPIXELDEMO: Display a pattern of random dots on the screen    */
  160. /*    and pick them back up again. From Borland's BGIDEMO.C        */
  161. /*    Modified by Don Lewis for Hi-color and 24 bit modes.        */
  162.  
  163. void PutPixelDemo(unsigned char cinc)
  164. {
  165.   int seed = 1953;            /* use the year I was born */
  166.   int i, ginc, x, y, h, w, color[3];
  167.  
  168.   w = modeinfo.YResolution;
  169.   h = modeinfo.XResolution;
  170.  
  171.   srand( seed );            /* Restart random # function    */
  172.   if ( modeinfo.BitsPerPixel == 16)
  173.      ginc = 31;
  174.   else
  175.      ginc = 0;
  176.   for( i=0 ; i<3000 ; ++i ){        /* Put 3000 pixels on screen    */
  177.     x = 1 + random( h - 1 );            /* Generate a random location    */
  178.     y = 1 + random( w - 1 );
  179.     color[0] = random( cinc );         /* Generate random colors       */
  180.     color[1] = random( cinc + ginc );
  181.     color[2] = random( cinc );
  182.     /* Write pixel to BLACK  background    */
  183.     vgapoint_rgb( y, x, color[0], color[1], color[2] );
  184.   }
  185.   tone();
  186.   getch();                /* Wait for user's response     */
  187.  
  188. }
  189. void FillAreaDemo(unsigned char cinc, int mode)
  190. {
  191.     int i, j, ginc, xcnt, ycnt, width, height;
  192.     ycnt = modeinfo.YResolution/2;
  193.     xcnt = modeinfo.XResolution/2;
  194.     width = modeinfo.XResolution/(cinc+1);
  195.     /* adjust for more color and fewer lines */
  196.     if (modeinfo.BitsPerPixel == 16) ginc = 2; /* make green hotter */
  197.     else ginc = 1;
  198.     if ( mode == 0x010F || mode == 0x112)
  199.         height = (ycnt-(cinc/8)) / ((cinc/8)/2);
  200.     else
  201.         height = (ycnt-(cinc+1)) / ((cinc+1)/2);
  202.     for (i=0; i<= cinc; i++)
  203.     {
  204.        vgafill( 0*height,i*width,width,height,i,0,0);
  205.        vgafill( 1*height,i*width,width,height,0,i*ginc,0);
  206.        vgafill( 2*height,i*width,width,height,0,0,i);
  207.        vgafill( 3*height,i*width,width,height,cinc,i*ginc,0);
  208.        vgafill( 4*height,i*width,width,height,cinc,0,i);
  209.        vgafill( 5*height,i*width,width,height,0,cinc*ginc,i);
  210.        vgafill( 6*height,i*width,width,height,i,i*ginc,0);
  211.        vgafill( 7*height,i*width,width,height,0,i*ginc,i);
  212.        vgafill( 8*height,i*width,width,height,i,0,i);
  213.        vgafill( 9*height,i*width,width,height,i,cinc*ginc,cinc);
  214.        vgafill(10*height,i*width,width,height,cinc,i*ginc,cinc);
  215.        vgafill(11*height,i*width,width,height,cinc,cinc*ginc,i);
  216.        vgafill(12*height,i*width,width,height,i,i*ginc,cinc);
  217.        vgafill(13*height,i*width,width,height,cinc,i*ginc,i);
  218.        vgafill(14*height,i*width,width,height,i,cinc*ginc,i);
  219.        vgafill(15*height,i*width,width,height,i,i*ginc,i);
  220.        vgafill(30*height,i*width,width,height,i,0,cinc);
  221.        vgafill(31*height,i*width,width,height,0,i*ginc,cinc);
  222.        vgafill(32*height,i*width,width,height,i,cinc,0);
  223.     }
  224.     tone();
  225.     getch();
  226. /* put as many colors as we can on the screen. . . Not finished. */
  227. /* 32k and 64k colors show too little, 16mil color mode shows too much */
  228.     for (j=0; j<=cinc; j++)
  229.         for (i=0; i<=cinc; i++)
  230.         {
  231.         vgapoint_rgb(ycnt-i,xcnt-j,0,j*ginc,i);   /* top left     */
  232.         vgapoint_rgb(ycnt+i,xcnt-j,i,j*ginc,i);   /* bottom left  */
  233.         vgapoint_rgb(ycnt+i,xcnt+j,j,i*ginc,i);   /* bottom right */
  234.         vgapoint_rgb(ycnt-i,xcnt+j,j,0,i);        /* top right    */
  235.         }
  236.     writeText(20,100,"******** END OF TEST ********");
  237.     tone();
  238.     getch();
  239. }
  240.  
  241. void writeText(int x,int y,unsigned char *str)
  242. {
  243.     unsigned char    byte;
  244.     int        i,j,k,length,ch;
  245.     unsigned char    far *_font;
  246.  
  247.     asm {                /* Bloody inline assembly - we need it here tho    */
  248.         push    bp
  249.         mov        bh,0x06;
  250.         mov        ax,0x1130;
  251.         int        0x10;
  252.         mov        ax,es;
  253.         mov        bx,bp;
  254.         pop        bp;
  255.         mov        i,ax;
  256.         mov        j,bx;
  257.         }
  258.     _font = (unsigned char *)MK_FP(i,j);
  259.  
  260.     length = strlen(str);
  261.     x = ( modeinfo.XResolution - (length * modeinfo.XCharSize)) / 2;
  262.     for (k = 0; k < length; k++) {
  263.         ch =  str[k];
  264.         for (j = 0; j < 16; j++) {
  265.             byte = *(_font + ch * 16 + j);
  266.             for (i = 0; i < modeinfo.XCharSize; i++) {
  267.                 if ((byte & 0x80) != 0)
  268.                     vgapoint_rgb(y+j,x+i,2*j,0,0);
  269.                 byte <<= 1;
  270.                 }
  271.             }
  272.         x += modeinfo.XCharSize;
  273.         }
  274. }
  275.