home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atltangram / atlglworld / cgl-pal.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.0 KB  |  166 lines

  1. //  CGL-Pal.cpp : OpenGL palette support
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include <windows.h>
  14. #include "util.h"
  15.  
  16. #include "cgl.h"
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. //
  20. // RGBA Palette support code
  21. //
  22. unsigned char CGL::m_threeto8[8] = {
  23.     0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
  24. };
  25. unsigned char CGL::m_twoto8[4] = {
  26.    0, 0x55, 0xaa, 0xff
  27. };
  28. unsigned char CGL::m_oneto8[2] = {
  29.     0, 255
  30. };
  31.  
  32. int CGL::m_defaultOverride[13] = {
  33.     0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
  34. };
  35.  
  36. PALETTEENTRY CGL::m_defaultPalEntry[20] = {
  37.     { 0,   0,   0,    0 }, //0
  38.     { 0x80,0,   0,    0 },
  39.     { 0,   0x80,0,    0 },
  40.     { 0x80,0x80,0,    0 },
  41.     { 0,   0,   0x80, 0 },
  42.     { 0x80,0,   0x80, 0 },
  43.     { 0,   0x80,0x80, 0 },
  44.     { 0xC0,0xC0,0xC0, 0 }, //7
  45.  
  46.     { 192, 220, 192,  0 }, //8
  47.     { 166, 202, 240,  0 },
  48.     { 255, 251, 240,  0 },
  49.     { 160, 160, 164,  0 }, //11
  50.  
  51.     { 0x80,0x80,0x80, 0 }, //12
  52.     { 0xFF,0,   0,    0 },
  53.     { 0,   0xFF,0,    0 },
  54.     { 0xFF,0xFF,0,    0 },
  55.     { 0,   0,   0xFF, 0 },
  56.     { 0xFF,0,   0xFF, 0 },
  57.     { 0,   0xFF,0xFF, 0 },
  58.     { 0xFF,0xFF,0xFF, 0 }  //19
  59.   };
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Color Component From Index
  63.  
  64. unsigned char CGL::ComponentFromIndex(int i, UINT nbits, UINT shift)
  65. {
  66.     unsigned char val = (unsigned char) (i >> shift);
  67.     switch (nbits) {
  68.  
  69.     case 1:
  70.         val &= 0x1;
  71.         return m_oneto8[val];
  72.  
  73.     case 2:
  74.         val &= 0x3;
  75.         return m_twoto8[val];
  76.  
  77.     case 3:
  78.         val &= 0x7;
  79.         return m_threeto8[val];
  80.  
  81.     default:
  82.         return 0;
  83.     }
  84. }
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CreateRGBPalette
  88.  
  89. BOOL CGL::CreatePalette()
  90. {
  91.     // Check to see if we need a palette
  92.  
  93.     PIXELFORMATDESCRIPTOR pfd;
  94.     int n = ::GetPixelFormat(m_hdc);
  95.    ::DescribePixelFormat(m_hdc, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  96.  
  97.     // Palette needed for color index mode.
  98.     BOOL bColorIndex = (pfd.iPixelType & PFD_TYPE_COLORINDEX);
  99.     if (bColorIndex)
  100.     {
  101.         // Color index mode not supported.
  102.         return FALSE;
  103.     }
  104.  
  105.     // Palette needed for 8bpp DIBs. PFD_NEED_PALETTE is not set.
  106.     BOOL b8bppDIB = (pfd.cColorBits == 8);
  107.     if ( (pfd.dwFlags & PFD_NEED_PALETTE) ||
  108.          (b8bppDIB) ||
  109.          (bColorIndex))
  110.     {
  111.         TRACE0("Creating palette\r\n");
  112.         // allocate a log pal and fill it with the color table info
  113.         int iPalSize = sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY);
  114.         LOGPALETTE* pPal = (LOGPALETTE*) malloc(iPalSize);
  115.         memset(pPal, 0, iPalSize);
  116.  
  117.         if (!pPal)
  118.             return FALSE;
  119.  
  120.         pPal->palVersion = 0x300; // Windows 3.0
  121.         pPal->palNumEntries = 256; // table size
  122.  
  123.         // Create RGB Palette
  124.         ASSERT( pfd.cColorBits == 8);
  125.         n = 1 << pfd.cColorBits;
  126.         for (int i=0; i<n; i++)
  127.         {
  128.             pPal->palPalEntry[i].peRed =
  129.                 ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
  130.             pPal->palPalEntry[i].peGreen =
  131.                 ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
  132.             pPal->palPalEntry[i].peBlue =
  133.                 ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
  134.             pPal->palPalEntry[i].peFlags = 0;
  135.         }
  136.  
  137.         // Fix up color table with system colors.
  138.         if ((pfd.cColorBits == 8)                           &&
  139.             (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) &&
  140.             (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
  141.             (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6)
  142.             )
  143.         {
  144.             for (int j = 1; j <= 12; j++)
  145.             {
  146.                 pPal->palPalEntry[m_defaultOverride[j]] = m_defaultPalEntry[j];
  147.             }
  148.         }
  149.  
  150.         // Delete any existing GDI palette
  151.         if (m_hPal)
  152.             ::DeleteObject(m_hPal);
  153.  
  154.         m_hPal = ::CreatePalette(pPal);
  155.         free (pPal);
  156.  
  157.         if (m_hPal == NULL)
  158.             return FALSE;
  159.  
  160.         // Select and Realize Palette
  161.         return TRUE;
  162.     }
  163.     else
  164.         return FALSE;
  165. }
  166.