home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CGAZV5N3.ZIP / PALETTE.C < prev    next >
C/C++ Source or Header  |  1991-03-01  |  9KB  |  271 lines

  1. /********* Listing 1 *************************** PALETTE.C ********** 
  2.  *                                                                  * 
  3.  * DESCRIPTION:  Functions for manipulating EGA/VGA color palettes  * 
  4.  * COMPILER(s):  Borland 2.0+, MS 5.0+, Zortech C/C++ 2.0+          * 
  5.  * AUTHOR:       Marv Luse, Autumn Hill Software, Inc.              * 
  6.  * COPYRIGHT:    Use freely but acknowledge source.                 * 
  7.  *******************************************************************/
  8.  
  9. #include "stdlib.h"
  10. #include "string.h"
  11. #include "dos.h"
  12.  
  13. #include "palette.h"
  14.  
  15. /*------------------------------------------------------------------*/
  16. /* Local variables...                                               */
  17. /*------------------------------------------------------------------*/
  18.  
  19. /* address of ROM save table */
  20. static unsigned long old_save_ptr;
  21.  
  22. /* area in RAM to establish a new table */
  23. static unsigned long save_ptr_tbl[7];
  24.  
  25. /* palette register values for the default EGA palette */
  26. static unsigned char ega_default_pal[16] =
  27. {
  28.      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
  29.      0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
  30. };
  31.  
  32. /* area in RAM for parameter save area - first 17 bytes contains */
  33. /* a copy of the 16 palette registers plus the overscan register */
  34. static unsigned char ega_save_area[256];
  35.  
  36. /*------------------------------------------------------------------*/
  37. /* Get or Set address of Video Save Pointer Table...                */
  38. /*------------------------------------------------------------------*/
  39.  
  40. /* save pointer is at 0x40:0xA8 */
  41. #define SAVE_POINTER ((void far *) (0x004000A8))
  42.  
  43. void far * get_save_ptr( void )
  44. {
  45.     return( SAVE_POINTER );
  46. }
  47.  
  48. void set_save_ptr( void far * ptbl )
  49. {
  50.     *((unsigned long far *) SAVE_POINTER) = (unsigned long) ptbl;
  51. }
  52.  
  53. /*------------------------------------------------------------------*/
  54. /* Setup our own Save Table and Parameter Save Area...              */
  55. /*------------------------------------------------------------------*/
  56.  
  57. void init_save_area( int init_palette )
  58. {
  59.     int i;
  60.     char *s;
  61.     char far *fs;
  62.  
  63.     /* save address of ROM table */
  64.     old_save_ptr = (unsigned long) get_save_ptr();
  65.  
  66.     /* copy save ptr table from ROM to RAM */
  67.     for (s = (char *)save_ptr_tbl, fs = get_save_ptr(), i = 0; i < 28; i++)
  68.         *s++ = *fs++;
  69.  
  70.     /* update save pointer to point to our copy */
  71.     set_save_ptr( save_ptr_tbl );
  72.  
  73.     /* the 2nd table entry points to parameter save area */
  74.     save_ptr_tbl[1] =
  75.         (unsigned long) ((unsigned char far *) ega_save_area);
  76.  
  77.     /* zero out the new parameter save area */
  78.     memset( ega_save_area, 0, 256 );
  79.  
  80.     /* if requested, initialize the default palette */
  81.     if( init_palette )
  82.         for( i=0; i<16; i++ )
  83.             set_ega_pal_register( i, ega_default_pal[i] );
  84. }
  85.  
  86. /*------------------------------------------------------------------*/
  87. /* Restore address of EGA Save Table to default ROM table...        */
  88. /*------------------------------------------------------------------*/
  89.  
  90. void restore_save_area( int reinit_palette )
  91. {
  92.     int i;
  93.  
  94.     /* restore address of default ROM table */
  95.     set_save_ptr( (void far *) old_save_ptr );
  96.  
  97.     /* if requested, reinitialize the default palette */
  98.     if( reinit_palette )
  99.         for( i=0; i<16; i++ )
  100.             set_ega_pal_register( i, ega_default_pal[i] );
  101. }
  102.  
  103. /*------------------------------------------------------------------*/
  104. /* Returns nonzero if active display is VGA color...                */
  105. /*------------------------------------------------------------------*/
  106.  
  107. int is_vga_color( void )
  108. {
  109.     union REGS r;
  110.     static int already_called = 0;
  111.     static int is_vga = 0;
  112.  
  113.     /* Have we been called before?                             */
  114.     if ( already_called )
  115.         return( is_vga );
  116.  
  117.     /*  Int 10h function 1Ah is supported on the VGA, but not  */
  118.     /*  the EGA.  If supported, a known value will be returned */
  119.     /*  in the BL register, which will be 08h for VGA color.   */
  120.     /*  To determine that the function is truly supported,     */
  121.     /*  BL must be initialized to a known invalid value.       */
  122.  
  123.     r.h.ah = 0x1A; /* function 1Ah */
  124.     r.h.al = 0x00; /* subfunction 00h */
  125.     r.h.bl = 0x99; /* invalid return value */
  126.  
  127.     int86( 0x10, &r, &r );
  128.  
  129.     already_called = 1;
  130.     is_vga = (r.h.bl == 0x08) ? 1 : 0;
  131.  
  132.     return( is_vga );
  133. }
  134.  
  135. /*------------------------------------------------------------------*/
  136. /* Returns nonzero if active display is EGA color...                */
  137. /*------------------------------------------------------------------*/
  138.  
  139. int is_ega_color( void )
  140. {
  141.     union REGS r;
  142.     static int already_called = 0;
  143.     static int is_ega = 0;
  144.  
  145.     /*  Since the VGA BIOS is a superset of the EGA BIOS, we   */
  146.     /*  must first rule out the possibility of a VGA display.  */
  147.  
  148.     if( is_vga_color() )
  149.         return( 0 );
  150.  
  151.     /* Have we been called before?                             */
  152.     if ( already_called )
  153.         return( is_ega );
  154.  
  155.     /* We now attempt to invoke an EGA-only BIOS function to   */
  156.     /* rule out the CGA, MDA, and other display hardware.      */
  157.  
  158.     r.h.ah = 0x12; /* function 12h */
  159.     r.h.bl = 0x10; /* sub function 10h */
  160.  
  161.     int86( 0x10, &r, &r );
  162.  
  163.     /* If the function is unsupported, BL remains unchanged    */
  164.  
  165.     if( r.h.bl == 0x10 )
  166.         return( 0 );
  167.  
  168.     /* The function is supported, so this is an EGA display.   */
  169.     /* If the value returned in BH is zero, then it's color.   */
  170.  
  171.     already_called = 1;
  172.     is_ega = (r.h.bh == 0) ? 1 : 0 ;
  173.  
  174.     return( is_ega );
  175. }
  176.  
  177. /*------------------------------------------------------------------*/
  178. /* Get contents of specified EGA palette register...                */
  179. /*------------------------------------------------------------------*/
  180.  
  181. int get_ega_pal_register( int reg_no )
  182. {
  183.     /* get copy of register maintained by the BIOS */
  184.     return( (int) ega_save_area[reg_no] );
  185. }
  186.  
  187. /*------------------------------------------------------------------*/
  188. /* Set contents of specified EGA palette register...                */
  189. /*------------------------------------------------------------------*/
  190.  
  191. void set_ega_pal_register( int reg_no, int reg_val )
  192. {
  193.     union REGS r;
  194.  
  195.     r.h.ah = 0x10;                    /* function 10h */
  196.     r.h.al = 0x00;                    /* subfunction 00h */
  197.     r.h.bl = (unsigned char) reg_no;  /* register number */
  198.     r.h.bh = (unsigned char) reg_val; /* color value */
  199.  
  200.     int86( 0x10, &r, &r );
  201. }
  202.  
  203. /*------------------------------------------------------------------*/
  204. /* Get contents of specified VGA palette register...                */
  205. /*------------------------------------------------------------------*/
  206.  
  207. int get_vga_pal_register( int reg_no )
  208. {
  209.     union REGS r;
  210.  
  211.     r.h.ah = 0x10;                   /* function 10h */
  212.     r.h.al = 0x07;                   /* subfunction 07h */
  213.     r.h.bl = (unsigned char) reg_no; /* register number */
  214.  
  215.     int86( 0x10, &r, &r );
  216.  
  217.     return( (int) r.h.bh );
  218. }
  219.  
  220. /*------------------------------------------------------------------*/
  221. /* Set contents of specified VGA palette register...                */
  222. /*------------------------------------------------------------------*/
  223.  
  224. void set_vga_pal_register( int reg_no, int reg_val )
  225. {
  226.     union REGS r;
  227.  
  228.     r.h.ah = 0x10;                    /* function 10h */
  229.     r.h.al = 0x00;                    /* subfunction 00h */
  230.     r.h.bl = (unsigned char) reg_no;  /* register number */
  231.     r.h.bh = (unsigned char) reg_val; /* color value */
  232.  
  233.     int86( 0x10, &r, &r );
  234. }
  235.  
  236. /*------------------------------------------------------------------*/
  237. /* Get contents of specified VGA DAC register...                    */
  238. /*------------------------------------------------------------------*/
  239.  
  240. void get_vga_dac_register( int reg_no, unsigned char rgb[] )
  241. {
  242.     union REGS r;
  243.  
  244.     r.h.ah = 0x10;   /* function 10h */
  245.     r.h.al = 0x15;   /* subfunction 15h */
  246.     r.x.bx = reg_no; /* register number */
  247.  
  248.     int86( 0x10, &r, &r );
  249.  
  250.     rgb[0] = r.h.dh; /* 6-bit red component */
  251.     rgb[1] = r.h.ch; /* 6-bit grn component */
  252.     rgb[2] = r.h.cl; /* 6-bit blu component */
  253. }
  254.  
  255. /*------------------------------------------------------------------*/
  256. /* Set contents of specified VGA DAC register...                    */
  257. /*------------------------------------------------------------------*/
  258.  
  259. void set_vga_dac_register( int reg_no, unsigned char rgb[] )
  260. {
  261.     union REGS r;
  262.  
  263.     r.h.ah = 0x10;   /* function 10h */
  264.     r.h.al = 0x10;   /* subfunction 10h */
  265.     r.x.bx = reg_no; /* register number */
  266.     r.h.dh = rgb[0]; /* 6-bit red component */
  267.     r.h.ch = rgb[1]; /* 6-bit grn component */
  268.     r.h.cl = rgb[2]; /* 6-bit blu component */
  269.  
  270.     int86( 0x10, &r, &r );
  271. }