home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / WINPAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-15  |  6.1 KB  |  242 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. // WINPAL.CPP
  3. //
  4. // Hacked from WinG sample PALANIM sample.c.  Only changes were replacing
  5. // WinG calls with the WinG interface class, and removing the two routines
  6. // at the end of sample.c, as they were of no use here.
  7. //
  8. // Basically pure Microsoft WinG sample code.
  9. // 7/12/94 Chris Thomas (GCT)
  10. //
  11. // Modified and adapted by Matt Howard
  12. ///////////////////////////////////////////////////////////////////////////
  13. #include "os.h"
  14. #ifdef OS_WINDOWS
  15.  
  16. // *** Creating an identity palette code here
  17.  
  18. HPALETTE CreateIdentityPalette(RGBQUAD * aRGB, int nColors)
  19. {
  20.     int i;
  21.     struct
  22.     {
  23.         WORD Version;
  24.         WORD NumberOfEntries;
  25.         PALETTEENTRY aEntries[256];
  26.     } Palette =
  27.     {
  28.         0x300,
  29.         256
  30.     };
  31.     HDC hdc = GetDC(NULL);
  32.  
  33.     // *** For SYSPAL_NOSTATIC, just copy the color table into
  34.     // *** a PALETTEENTRY array and replace the first and last entries
  35.     // *** with black and white
  36.     if (GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)
  37.     {
  38.         // *** Fill in the palette with the given values, marking each
  39.         // *** as PC_RESERVED
  40.         for(i = 0; i < nColors; i++)
  41.         {
  42.             Palette.aEntries[i].peRed = aRGB[i].rgbRed;
  43.             Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;
  44.             Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;
  45.             Palette.aEntries[i].peFlags = PC_RESERVED;
  46.         }
  47.  
  48.         // *** Mark any remaining entries PC_RESERVED
  49.         for (; i < 256; ++i)
  50.         {
  51.             Palette.aEntries[i].peFlags = PC_RESERVED;
  52.         }
  53.  
  54.         // *** Make sure the last entry is white
  55.         // *** This may replace an entry in the array!
  56.         Palette.aEntries[255].peRed = 255;
  57.         Palette.aEntries[255].peGreen = 255;
  58.         Palette.aEntries[255].peBlue = 255;
  59.         Palette.aEntries[255].peFlags = 0;
  60.  
  61.         // *** And the first is black
  62.         // *** This may replace an entry in the array!
  63.         Palette.aEntries[0].peRed = 0;
  64.         Palette.aEntries[0].peGreen = 0;
  65.         Palette.aEntries[0].peBlue = 0;
  66.         Palette.aEntries[0].peFlags = 0;
  67.     }
  68.     else
  69.     // *** For SYSPAL_STATIC, get the twenty static colors into
  70.     // *** the array, then fill in the empty spaces with the
  71.     // *** given color table
  72.     {
  73.         int nStaticColors;
  74.         int nUsableColors;
  75.  
  76.         // *** Get the static colors
  77.         nStaticColors = GetDeviceCaps(hdc, NUMCOLORS);
  78.         GetSystemPaletteEntries(hdc, 0, 256, Palette.aEntries);
  79.  
  80.         // *** Set the peFlags of the lower static colors to zero
  81.         nStaticColors = nStaticColors / 2;
  82.         for (i=0; i<nStaticColors; i++)
  83.             Palette.aEntries[i].peFlags = 0;
  84.  
  85.         // *** Fill in the entries from the given color table
  86.         nUsableColors = nColors - nStaticColors;
  87.         for (; i<nUsableColors; i++)
  88.         {
  89.             Palette.aEntries[i].peRed = aRGB[i].rgbRed;
  90.             Palette.aEntries[i].peGreen = aRGB[i].rgbGreen;
  91.             Palette.aEntries[i].peBlue = aRGB[i].rgbBlue;
  92.             Palette.aEntries[i].peFlags = PC_RESERVED;
  93.         }
  94.  
  95.         // *** Mark any empty entries as PC_RESERVED
  96.         for (; i<256 - nStaticColors; i++)
  97.             Palette.aEntries[i].peFlags = PC_RESERVED;
  98.  
  99.         // *** Set the peFlags of the upper static colors to zero
  100.         for (i = 256 - nStaticColors; i<256; i++)
  101.             Palette.aEntries[i].peFlags = 0;
  102.     }
  103.  
  104.     ReleaseDC(NULL, hdc);
  105.  
  106.     // *** Create the palette
  107.     return CreatePalette((LOGPALETTE *)&Palette);
  108. }
  109.  
  110. // *** Resetting the system palette code here
  111.  
  112. void ClearSystemPalette(void)
  113. {
  114.     // *** A dummy palette setup
  115.     struct
  116.     {
  117.         WORD Version;
  118.         WORD NumberOfEntries;
  119.         PALETTEENTRY aEntries[256];
  120.     } Palette =
  121.     {
  122.         0x300,
  123.         256
  124.     };
  125.  
  126.     HPALETTE ScreenPalette = 0;
  127.     HDC ScreenDC;
  128.     int Counter;
  129.  
  130.     // *** Reset everything in the system palette to black
  131.     for(Counter = 0; Counter < 256; Counter++)
  132.     {
  133.         Palette.aEntries[Counter].peRed = 0;
  134.         Palette.aEntries[Counter].peGreen = 0;
  135.         Palette.aEntries[Counter].peBlue = 0;
  136.         Palette.aEntries[Counter].peFlags = PC_NOCOLLAPSE;
  137.     }
  138.  
  139.     // *** Create, select, realize, deselect, and delete the palette
  140.     ScreenDC = GetDC(NULL);
  141.     ScreenPalette = CreatePalette((LOGPALETTE *)&Palette);
  142.     ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
  143.     RealizePalette(ScreenDC);
  144.     ScreenPalette = SelectPalette(ScreenDC,ScreenPalette,FALSE);
  145.     DeleteObject(ScreenPalette);
  146.     ReleaseDC(NULL, ScreenDC);
  147. }
  148.  
  149. // *** Setting up SYSPAL_NOSTATIC
  150.  
  151. #define NumSysColors (sizeof(SysPalIndex)/sizeof(SysPalIndex[1]))
  152. #define rgbBlack RGB(0,0,0)
  153. #define rgbWhite RGB(255,255,255)
  154.  
  155. // *** These are the GetSysColor display element identifiers
  156. static int SysPalIndex[] = {
  157.     COLOR_ACTIVEBORDER,
  158.     COLOR_ACTIVECAPTION,
  159.     COLOR_APPWORKSPACE,
  160.     COLOR_BACKGROUND,
  161.     COLOR_BTNFACE,
  162.     COLOR_BTNSHADOW,
  163.     COLOR_BTNTEXT,
  164.     COLOR_CAPTIONTEXT,
  165.     COLOR_GRAYTEXT,
  166.     COLOR_HIGHLIGHT,
  167.     COLOR_HIGHLIGHTTEXT,
  168.     COLOR_INACTIVEBORDER,
  169.  
  170.     COLOR_INACTIVECAPTION,
  171.     COLOR_MENU,
  172.     COLOR_MENUTEXT,
  173.     COLOR_SCROLLBAR,
  174.     COLOR_WINDOW,
  175.     COLOR_WINDOWFRAME,
  176.     COLOR_WINDOWTEXT
  177. };
  178.  
  179. // *** This array translates the display elements to black and white
  180. static COLORREF MonoColors[] = {
  181.     rgbBlack,
  182.     rgbWhite,
  183.     rgbWhite,
  184.     rgbWhite,
  185.     rgbWhite,
  186.     rgbBlack,
  187.     rgbBlack,
  188.     rgbBlack,
  189.     rgbBlack,
  190.     rgbBlack,
  191.     rgbWhite,
  192.     rgbWhite,
  193.     rgbWhite,
  194.     rgbWhite,
  195.     rgbBlack,
  196.     rgbWhite,
  197.     rgbWhite,
  198.     rgbBlack,
  199.  
  200.     rgbBlack
  201. };
  202.  
  203. // *** This array holds the old color mapping so we can restore them
  204. static COLORREF OldColors[NumSysColors];
  205.  
  206. // *** AppActivate sets the system palette use and
  207. // *** remaps the system colors accordingly.
  208. void AppActivate(BOOL fActive)
  209. {
  210.     HDC hdc;
  211.     int i;
  212.  
  213.     // *** Just use the screen DC
  214.     hdc = GetDC(NULL);
  215.  
  216.     // *** If the app is activating, save the current color mapping
  217.     // *** and switch to SYSPAL_NOSTATIC
  218.     if (fActive && GetSystemPaletteUse(hdc) == SYSPAL_STATIC)
  219.  
  220.     {
  221.         // *** Store the current mapping
  222.         for (i=0; i<NumSysColors; i++)
  223.             OldColors[i] = GetSysColor(SysPalIndex[i]);
  224.  
  225.         // *** Switch to SYSPAL_NOSTATIC and remap the colors
  226.         SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
  227.         SetSysColors(NumSysColors, SysPalIndex, MonoColors);
  228.     }
  229.     else if (!fActive && GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)
  230.     {
  231.         // *** Switch back to SYSPAL_STATIC and the old mapping
  232.         SetSystemPaletteUse(hdc, SYSPAL_STATIC);
  233.  
  234.         SetSysColors(NumSysColors, SysPalIndex, OldColors);
  235.     }
  236.  
  237.     // *** Be sure to release the DC!
  238.     ReleaseDC(NULL,hdc);
  239. }
  240.  
  241. #endif
  242.