home *** CD-ROM | disk | FTP | other *** search
/ Mega ROM 1 / MEGA_ROM_1.cdr / vga / vga.c < prev    next >
Text File  |  1992-01-09  |  7KB  |  336 lines

  1. Path: accuvax.nwu.edu!tank!shamash!com50!pwcs!minnow!lee
  2. From: lee@minnow.sp.unisys.com (Gene Lee)
  3. Newsgroups: comp.graphics,comp.sys.ibm.pc
  4. Subject: How to use VGA Cards (source included)
  5. Date: 12 Apr 89 11:55:15 GMT
  6. Keywords: VGA
  7.  
  8. /* I have had several requests for information on how to write software
  9.     which can make use of the 256 color modes of VGA.  Rather than mail
  10.     everyone a copy I am posting these routines as an example.  They 
  11.     should at least give the concepts behind programming the card.
  12.  
  13. /* ----------------------------------------------------------------------- */
  14.  
  15. /*
  16.  
  17.                 VGA.C
  18.  
  19.      The following are a few routines written in Turbo-C which plot
  20.      pixels and set palette in VGA mode for Paradise VGA cards.
  21.      
  22.      Hack them as you wish
  23.      
  24. */     
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <dos.h>
  29. #include <math.h>
  30.  
  31. #include "\drvd\progs\vga\vga.h"
  32.  
  33.  
  34. /* global variables */
  35.  
  36. /* values used for the rainbow palette routine */
  37. int W=75;
  38. int RC=64;
  39. int GC=128;
  40. int BC=192;
  41. int H=63;
  42.  
  43. int XSIZE,YSIZE;       /* number of pixels in x and y direction */
  44. int MAX_COLOR,NUM_COLORS;   /* max color value and maxcolor value +1 */
  45.  
  46. int __current_mode__=TEXT_MODE;
  47. static unsigned int lasthio=-1;
  48. p palette[256];
  49.  
  50. /* _______________________________________________________________________ */
  51.  
  52.  
  53. void vga_dot(x,y, color)
  54. int x,y,color;
  55. {
  56.    union REGS in, out;
  57.  
  58.    static char *p=MK_FP(0xA000,0);
  59.    static unsigned long offset;   /* offset into VGA memory */
  60.    static unsigned int hio,lowo;  /* hi and low portions of offset */
  61.  
  62. #ifndef USE_BIOS
  63.  
  64.     switch (__current_mode__) {
  65.  
  66.     case VGA320200:
  67.             p[XSIZE*y+x] = color;
  68.  
  69.             break;
  70.  
  71.     case VGA640400:
  72.        offset =((long)XSIZE*y)+x;
  73.  
  74.            /* in order to access all pixels in a 640x400 display, the offset */
  75.        /* generated can be greater than 64K.  To get around this problem */
  76.        /* they have chosen to break up the offset as follows. */
  77.        /* Break into hi and low parts, ie. 0x1A2B3 give hi:0x1A low:0x2B3 */
  78.        /* hi will be used put into the VGA register PR0A and */ 
  79.        /* and low will be the offset */
  80.  
  81.        hio = (unsigned int)(offset >>12);
  82.        lowo = (unsigned int)(offset & 0xFFF);
  83.  
  84.  
  85.        /* only set PR0A if its changed from last time */
  86.        if (hio != lasthio) {
  87.            lasthio = hio;
  88.  
  89.            /* unlock PR0A*/
  90.            outport (0x3CE, 0x50F);
  91.  
  92.            /* set PR0A */
  93.            hio = (hio<<8) | 9;
  94.            outport(0x3CE,hio);
  95.  
  96.            /* lock PR0A again*/
  97.            outport (0x3CE, 0x00F);
  98.  
  99.        }
  100.  
  101.        p[lowo] = color;
  102.  
  103.        break;
  104.  
  105.     default:         /* any other mode */
  106.        in.h.ah =0xc;
  107.        in.h.al =color;
  108.        in.x.dx =y;
  109.        in.x.cx =x;
  110.        in.h.bh =0;     /* page zero */
  111.  
  112.        int86(VIDEO_INT, &in, &out);
  113.  
  114.        break;
  115.     }
  116.  
  117.  
  118. #else
  119.        in.h.ah =0xc;
  120.        in.h.al =color;
  121.        in.x.dx =y;
  122.        in.x.cx =x;
  123.        in.h.bh =0;     /* page zero */
  124.        int86(VIDEO_INT, &in, &out);
  125. #endif
  126. }
  127.  
  128. /* _____________________________________________________________________ */
  129.  
  130.  
  131. void vga_mode(mode)
  132. int mode;
  133. {
  134.    union REGS in, out;
  135.  
  136.    lasthio = -1;    /* initialize variables for plotting */
  137.  
  138.    __current_mode__ = mode;   /* know the current mode */
  139.  
  140.    switch (mode) {
  141.  
  142.    case VGA640400:
  143.        XSIZE = 640;
  144.        YSIZE = 400;
  145.  
  146.        MAX_COLOR = 255;
  147.        NUM_COLORS = MAX_COLOR +1;
  148.  
  149.        break;
  150.  
  151.    case VGA320200:
  152.        XSIZE = 320;
  153.        YSIZE = 200;
  154.  
  155.        MAX_COLOR = 255;
  156.        NUM_COLORS = MAX_COLOR +1;
  157.  
  158.        break;
  159.  
  160.    case VGA800600:
  161.        XSIZE = 800;
  162.        YSIZE = 600;
  163.  
  164.        MAX_COLOR = 15;
  165.        NUM_COLORS = MAX_COLOR +1;
  166.  
  167.        break;
  168.  
  169.  
  170.    default:
  171.        printf("vga_mode: Invalid graphics mode\n");
  172.        exit(-1);
  173.  
  174.        break;
  175.    }
  176.  
  177.    in.x.ax = mode;
  178.  
  179.    int86(VIDEO_INT, &in, &out);
  180. }
  181. /* _____________________________________________________________________ */
  182.  
  183.  
  184. void text_mode()
  185. {
  186.    union REGS in, out;
  187.  
  188.    __current_mode__ = TEXT_MODE;   /* know the current mode */
  189.  
  190.  
  191.    in.x.ax = TEXT_MODE;
  192.    int86(VIDEO_INT, &in, &out);
  193. }
  194.  
  195. /* _______________________________________________________________________ */
  196.  
  197. /*
  198.         set the whole vga palette with one call
  199. */
  200.  
  201. void set_vga_palette(palette)
  202. p *palette;
  203. {
  204.    union REGS in, out;
  205.    struct SREGS sregs;
  206.  
  207.    in.x.ax =0x1012;
  208.    in.x.bx =0;
  209.    in.x.cx =256;
  210.  
  211.    sregs.es = FP_SEG(palette);
  212.    in.x.dx = FP_OFF(palette);
  213.  
  214.  
  215.    int86x(VIDEO_INT, &in, &out, &sregs);
  216. }
  217.  
  218.  
  219. /* _______________________________________________________________________ */
  220.  
  221. /*
  222.         set one index in the vga palette
  223. */
  224.  
  225. void set_vga_pal(index,r,g,b)
  226. int index;
  227. int r,g,b;
  228. {
  229.    union REGS in, out;
  230.  
  231.    in.x.ax =0x1010;
  232.  
  233.    in.x.bx =index;
  234.    in.h.dh =r;
  235.    in.h.ch =g;
  236.    in.h.cl =b;
  237.  
  238.    int86(VIDEO_INT, &in, &out);
  239. }
  240.  
  241.  
  242. /* --------------------------------------------------------------------- */
  243.  
  244. /* just for fun, this routine will setup the VGA palette with the colors
  245.    of the rainbow.  The basic idea is to slightly overlap three (red,green,
  246.    blue) normal shaped curves.  The height of each curve is the intensity
  247.    of the red,green or blue color for that palette index.
  248.    
  249.           ^  |
  250.           |  |    ..  ..   ..
  251.     Intensity|   .  ..  . .  .
  252.              |  .   ..   .    .
  253.              | .   .  . . .    .
  254.              |__________________
  255.                 Palette Index ->
  256.                 
  257. */
  258.                 
  259. void rainbow_palette()
  260. {
  261.    union REGS in, out;
  262.    struct SREGS sregs;
  263.  
  264.     int i;
  265.  
  266.     for (i=1 ; i < 256 ; i++) {
  267.         palette[i].red = H * pow(M_E,-1.0*pow(((double)i-RC)/W,2.0));
  268.         palette[i].green = H * pow(M_E,-1.0*pow(((double)i-GC)/(W),2.0));
  269.         palette[i].blue = H * pow(M_E,-1.0*pow(((double)i-BC)/W,2.0));
  270.  
  271.     }
  272.  
  273.    in.x.ax =0x1012;
  274.    in.x.bx =0;
  275.    in.x.cx =256;
  276.  
  277.    sregs.es = FP_SEG(palette);
  278.    in.x.dx = FP_OFF(palette);
  279.  
  280.  
  281.    int86x(VIDEO_INT, &in, &out, &sregs);
  282. }
  283.  
  284. --------------------- CUT HERE -----------------------------------------
  285.  
  286. /*
  287.                             VGA.H
  288. */                            
  289.  
  290. typedef struct {
  291.         unsigned char red,green,blue;
  292.         } p;
  293.  
  294.  
  295. #define ESC 0x1B
  296. #define VIDEO_INT 0x10
  297. #define EGA_MODE  0x13
  298. #define TEXT_MODE 0x3
  299. #define VGA640400 0x5E
  300. #define VGA320200 0x13
  301. #define VGA800600 0x58
  302.  
  303.  
  304. #ifndef W
  305. extern int W;
  306. extern int RC;
  307. extern int GC;
  308. extern int BC;
  309. extern int H;
  310.  
  311. extern int XSIZE,YSIZE;       /* number of pixels in x and y direction */
  312. extern int MAX_COLOR,NUM_COLORS;   /* max color value and maxcolor value +1 */
  313.  
  314. extern int __current_mode__;
  315. extern p palette[256];
  316.  
  317. #endif
  318.  
  319.  
  320. /* proto types */
  321.  
  322. void vga_dot(int x, int y, int color);
  323. void vga_mode(int mode);
  324. void text_mode(void);
  325. void set_vga_palette(p *palette);
  326. void set_vga_pal(int index, int red, int green, int blue);
  327. void rainbow_palette(void);
  328.  
  329.  
  330.  
  331. -- 
  332. Gene Lee               UUCP: ...!amdahl!ems!minnow!lee
  333. Unisys Corporation             
  334. Phone: +1 612 635 3993    CSNET: lee@minnow.SP.Unisys.Com
  335. If not for the courage of the fearless manager, the paycheck would be lost.
  336.