#include "StdAfx.h"
#include "GLHelp.h"

BOOL bSetupPixelFormat(HDC hdc, int mode)
{
    PIXELFORMATDESCRIPTOR pfd, *ppfd;
    int pixelformat;

    ppfd = &pfd;

    ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
    ppfd->nVersion = 1;
	if (mode)
	{
		ppfd->dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
		ppfd->iPixelType = PFD_TYPE_RGBA;
		//ppfd->cColorBits = 24;
		//ppfd->cDepthBits = 32;
		//========================
		//ppfd->cColorBits = 8;
		//ppfd->cRedBits = 8;
		//ppfd->cGreenBits = 8;
		//ppfd->cBlueBits = 8;

		//========================
		ppfd->iLayerType = PFD_MAIN_PLANE;
		if (mode == 2)
		{
			ppfd->dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_NEED_PALETTE;
			LOGPALETTE * pPal;
			ppfd->iPixelType = PFD_TYPE_COLORINDEX;
			LPLOGPALETTE lpLogPal;
			int     nPalEntries = 129;   // Number of entries in our
                                         // palette.
                                         // 256 are possible, but the
                                         // system reserves 20 of them.

			lpLogPal = (LPLOGPALETTE) new char[
                   sizeof (LOGPALETTE) + nPalEntries * sizeof
                   (PALETTEENTRY)];

			lpLogPal->palVersion    = 0x300;
			lpLogPal->palNumEntries = nPalEntries;

			for (int i = 0; i < nPalEntries; i++)
			{

				// Fill in the red, green, and blue values for our palette.
				// This particular palette is a wash from green to black.
				lpLogPal->palPalEntry[i].peRed   = (i & 0x7) * 30;
				lpLogPal->palPalEntry[i].peGreen = ((i >> 3) & 0x7) * 30;
				lpLogPal->palPalEntry[i].peBlue  = ((i >> 6) & 0x1) * 60;

				// Create unique palette entries. This flag may change depending
				// on your purposes. See the Windows API documentation
				// about the PALETTEENTRY structure for more information.
				lpLogPal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
			}

       // Create the logical palette.
			HPALETTE g_hPal = CreatePalette (lpLogPal);
			SelectPalette(hdc, g_hPal, false);

		
		}

	}
	else
	{
		ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
                        PFD_DOUBLEBUFFER;
		ppfd->dwLayerMask = PFD_MAIN_PLANE;
		ppfd->iPixelType = PFD_TYPE_COLORINDEX;
		ppfd->cColorBits = 8;
		ppfd->cAlphaBits = 64;
		ppfd->cAccumBits = 0;
		
		ppfd->cDepthBits = 16;

		ppfd->cStencilBits = 0;
	}


    if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
    {
        //MessageBox(NULL, L"ChoosePixelFormat failed", L"Error", MB_OK);
        return FALSE;
    }

    if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
    {
        //MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
        return FALSE;
    }

    return TRUE;

}
