#include "..\..\..\OpenGL\include\WindowsViewer\WinGraphicContext.h"
oag::WinGraphicContext::WinGraphicContext(void)
:m_bIsDone(FALSE)
,m_pDC(NULL)
,m_hRC(NULL)
,X(0)
,Y(0)
,m_WindowWidth(0)
,m_WindowHeight(0)
{
}
oag::WinGraphicContext::WinGraphicContext(CWnd* pWindow)
:m_bIsDone(FALSE)
,m_pDC(NULL)
,m_hRC(NULL)
,X(0)
,Y(0)
,m_WindowWidth(0)
,m_WindowHeight(0)
{
m_pDC = new CClientDC(pWindow);
if( m_pDC )
{
if (!SetDCPixelFormat(m_pDC->GetSafeHdc(), PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER ))
return;
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
CRect rect;
pWindow->GetClientRect(rect);
m_WindowWidth = rect.Width();
m_WindowHeight = rect.Height();
}
}
oag::WinGraphicContext::~WinGraphicContext(void)
{
if (m_hRC != NULL)
{
::wglDeleteContext(m_hRC);
m_hRC = NULL;
}
if(m_pDC != NULL)
{
delete m_pDC;
m_pDC = NULL;
}
}
//Operations
bool oag::WinGraphicContext::SetDCPixelFormat(HDC hDC, DWORD dwFlags)
{
PIXELFORMATDESCRIPTOR pixelDescriptor;
pixelDescriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDescriptor.nVersion = 1;
pixelDescriptor.dwFlags = dwFlags;
pixelDescriptor.iPixelType = PFD_TYPE_RGBA;
pixelDescriptor.cColorBits = 16;
pixelDescriptor.cRedBits = 8;
pixelDescriptor.cRedShift = 16;
pixelDescriptor.cGreenBits = 8;
pixelDescriptor.cGreenShift = 8;
pixelDescriptor.cBlueBits = 8;
pixelDescriptor.cBlueShift = 0;
pixelDescriptor.cAlphaBits = 0;
pixelDescriptor.cAlphaShift = 0;
pixelDescriptor.cAccumBits = 64;
pixelDescriptor.cAccumRedBits = 16;
pixelDescriptor.cAccumGreenBits = 16;
pixelDescriptor.cAccumBlueBits = 16;
pixelDescriptor.cAccumAlphaBits = 0;
pixelDescriptor.cDepthBits = 32;
pixelDescriptor.cStencilBits = 8;
pixelDescriptor.cAuxBuffers = 0;
pixelDescriptor.iLayerType = PFD_MAIN_PLANE;
pixelDescriptor.bReserved = 0;
pixelDescriptor.dwLayerMask = 0;
pixelDescriptor.dwVisibleMask = 0;
pixelDescriptor.dwDamageMask = 0;
int nPixelIndex = ::ChoosePixelFormat(hDC, &pixelDescriptor);
if (nPixelIndex == 0) //*** Choose default
{
nPixelIndex = 1;
if (::DescribePixelFormat(hDC, nPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDescriptor) == 0) return FALSE;
}
if (!::SetPixelFormat(hDC, nPixelIndex, &pixelDescriptor)) return FALSE;
return true;
}
void oag::WinGraphicContext::OnSize(int width, int height)
{
m_WindowWidth = width; m_WindowHeight = height;
}
BOOL oag::WinGraphicContext::MakeCurrent()
{
BOOL bMake = false;
if( m_hRC )
bMake = ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
return bMake;
}
void oag::WinGraphicContext::DeleteCurrent()
{
::wglMakeCurrent(NULL, NULL);
}