InitOpenGL.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:3k
- #include "stdafx.h"
- #include "InitOpenGL.h"
- static HPALETTE ghpalOld, ghPalette;
- static unsigned char threeto8[8] = {
- 0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
- };
- static unsigned char twoto8[4] = {
- 0, 0x55, 0xaa, 0xff
- };
- static unsigned char oneto8[2] = {
- 0, 255
- };
- static int defaultOverride[13] = {
- 0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
- };
- static PALETTEENTRY defaultPalEntry[20] = {
- { 0, 0, 0, 0 },
- { 0x80,0, 0, 0 },
- { 0, 0x80,0, 0 },
- { 0x80,0x80,0, 0 },
- { 0, 0, 0x80, 0 },
- { 0x80,0, 0x80, 0 },
- { 0, 0x80,0x80, 0 },
- { 0xC0,0xC0,0xC0, 0 },
- { 192, 220, 192, 0 },
- { 166, 202, 240, 0 },
- { 255, 251, 240, 0 },
- { 160, 160, 164, 0 },
- { 0x80,0x80,0x80, 0 },
- { 0xFF,0, 0, 0 },
- { 0, 0xFF,0, 0 },
- { 0xFF,0xFF,0, 0 },
- { 0, 0, 0xFF, 0 },
- { 0xFF,0, 0xFF, 0 },
- { 0, 0xFF,0xFF, 0 },
- { 0xFF,0xFF,0xFF, 0 }
- };
- char ComponentFromIndex(int i, UINT nbits, UINT shift)
- {
- unsigned char val;
- val = (unsigned char) (i >> shift);
- switch (nbits) {
- case 1:
- val &= 0x1;
- return oneto8[val];
- case 2:
- val &= 0x3;
- return twoto8[val];
- case 3:
- val &= 0x7;
- return threeto8[val];
- default:
- return 0;
- }
- }
- void CreateRGBPalette(HDC hDC)
- {
- PIXELFORMATDESCRIPTOR pfd;
- LOGPALETTE *pPal;
- int n, i;
- n = GetPixelFormat(hDC);
- DescribePixelFormat(hDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- if (pfd.dwFlags & PFD_NEED_PALETTE) {
- n = 1 << pfd.cColorBits;
- pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
- n * sizeof(PALETTEENTRY));
- pPal->palVersion = 0x300;
- pPal->palNumEntries = n;
- for(i=0; i<n; i++) {
- pPal->palPalEntry[i].peRed =
- ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
- pPal->palPalEntry[i].peGreen =
- ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
- pPal->palPalEntry[i].peBlue =
- ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
- pPal->palPalEntry[i].peFlags = 0;
- }
- // fix up the palette to include the default GDI palette
- if ((pfd.cColorBits == 8) &&
- (pfd.cRedBits == 3) && (pfd.cRedShift == 0) &&
- (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
- (pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)
- ) {
- for (i = 1 ; i <= 12 ; i++)
- pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
- }
- ghPalette = CreatePalette(pPal);
- LocalFree(pPal);
- ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
- n = RealizePalette(hDC);
- }
- }