// RC.cpp: implementation of the CRC class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RC.h"

#ifndef PFD_GENERIC_ACCELERATED
#define PFD_GENERIC_ACCELERATED		0x00001000
#endif

/*
#ifndef PFD_DEPTH_DONTCARE
#define PFD_DEPTH_DONTCARE			0x20000000
#endif
*/

#define INSTALLABLE_DRIVER_TYPE_MASK  (PFD_GENERIC_ACCELERATED|PFD_GENERIC_FORMAT)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRC::CRC()
{
	m_iPixelFormat = 0;
	m_pDC = NULL;
	m_hRC = 0;
}

CRC::~CRC()
{
	// clean up the extensions array
	if ( m_ExtensionPtr )
	{
		m_ExtensionArray.RemoveAll();
		delete[] m_ExtensionPtr;
	}
}

bool CRC::SetupPixelFormat( void )
{	
	static PIXELFORMATDESCRIPTOR pfd = 
	{
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW |            // support window
			PFD_SUPPORT_OPENGL |        // support OpenGL
			PFD_DOUBLEBUFFER,           // double buffered
        PFD_TYPE_RGBA,                  // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored
        0,                              // no alpha buffer
        0,                              // shift bit ignored
        0,                              // no accumulation buffer
        0, 0, 0, 0,                     // accum bits ignored
		24,
        //16,                             // 16-bit z-buffer		
		0,                              // no stencil buffer
        0,                              // no auxiliary buffer
        PFD_MAIN_PLANE,                 // main layer
        0,                              // reserved
        0, 0, 0                         // layer masks ignored
    };
    
    if ( ( m_iPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd ) ) == 0 )
	{
		TRACE( "ChoosePixelFormat Failed! %d\r\n", GetLastError() );
        return false;
	}

    if ( ::SetPixelFormat(m_pDC->GetSafeHdc(), m_iPixelFormat, &pfd) == false )
    {
      	TRACE( "SetPixelFormat Failed! %d\r\n", GetLastError() );
        return false;
	}

    return true;
}

bool CRC::Create( CWnd *pWnd )
{
    m_pDC = new CClientDC( pWnd );

    if ( m_pDC == NULL )
	{
		TRACE( "CClientDC construction Failed! %d\r\n", GetLastError() );
        return false;
	}

	if ( !SetupPixelFormat() )
        return false;

//	For Color-Index mode, you'd probably create your palette here, right
//	after you select the pixel format

    if ( ( m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() ) ) == 0)
	{
		TRACE( "wglCreateContext Failed! %d\r\n", GetLastError() );
        return false;
	}

    if ( ::wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) == false )
	{
		TRACE( "wglMakeCurrent Failed! %d\r\n", GetLastError() );
        return false;
	}	
	
	return TRUE;
}

void CRC::Initialize( void )
{
	// Initialize OpenGL parameters and setup display list;

	FetchExtendedInformation();

	// specify black as clear color
    ::glClearColor( CLEAR_COLOR[0], CLEAR_COLOR[1], CLEAR_COLOR[2], CLEAR_COLOR[3] );
	// specify the back of the buffer as clear depth
    ::glClearDepth( 1.0 );
	// enable depth testing
    ::glEnable( GL_DEPTH_TEST );
}

void CRC::Destroy( void )
{
	if ( ::wglDeleteContext( m_hRC ) == false )
		TRACE( "wglDeleteContex Failed! %d\r\n", GetLastError() );        
	
//	For Color-Index mode, you should reset the palette to the original here

    if ( m_pDC )
		delete m_pDC;
}

void CRC::FinishAndSwap( void )
{
	::glFinish();
	if ( ::SwapBuffers( m_pDC->GetSafeHdc() ) == false )
		TRACE( "SwapBuffers Failed! %d\r\n", GetLastError() );
}

void CRC::FetchExtendedInformation( void )
{
	// Get information about the particular pixel format that's running. From this we
// can tell if the OpenGL implementation is strictly software, partially in hardware with
// some of the routines being processed by the generic software driver (the Mini-Client driver)
// or if the entire implementation is in hardware (the Installable Client driver)

	PIXELFORMATDESCRIPTOR pfd;

	// Get information about the DC's current pixel format 
	::DescribePixelFormat( m_pDC->GetSafeHdc(), m_iPixelFormat,
		sizeof(PIXELFORMATDESCRIPTOR), &pfd ); 
 
	// Now that we've set up a pixel format, get information about the current
	// driver for this format. You can use this info to determine which pixel format
	// is the fastest. Note that the Mini-Client driver is only available in OpenGL 1.1
	// or later.

	if ( 0 == ( INSTALLABLE_DRIVER_TYPE_MASK & pfd.dwFlags ) )
		m_eCurrentDriver = eInstallableClient; // fully in hardware (fastest)
	else if ( INSTALLABLE_DRIVER_TYPE_MASK == ( INSTALLABLE_DRIVER_TYPE_MASK & pfd.dwFlags ) )
		m_eCurrentDriver = eMiniClient;	// partially in hardware (pretty fast, maybe..)
	else  // plain old generic
		m_eCurrentDriver = eGeneric;	// software

// end of the current driver specific area

// Get general information about the OpenGL driver

//  GL_VENDOR
//  Returns the company responsible for this GL implementation.
//  This name does not change from release to release. 
    m_pVender = ::glGetString( GL_VENDOR );

//  GL_RENDERER
//  Returns the name of the renderer. This name is typically specific
//  to a particular configuration of a hardware platform.
//  It does not change from release to release. 
    m_pRenderer = ::glGetString( GL_RENDERER );

//  GL_VENDOR and GL_RENDERER together uniquely specify a platform,
//  and will not change from release to release. They should be used
//  to identify the platform.

//  GL_VERSION
//  Returns a version or release number. 
    m_pVersion = ::glGetString( GL_VERSION );

//  GL_EXTENSIONS
//  Returns a space-separated list of supported extensions to GL.
//  Because GL does not include queries for the performance
//  characteristics of an implementation, it is expected that some
//  applications will be written to recognize known platforms and will
//  modify their GL usage based on known performance characteristics
//  of these platforms.

//  The format and contents of the string that glGetString() returns depend
//  on the implementation, except that extension names will not include
//  space characters and will be separated by space characters
//  in the GL_EXTENSIONS string, and that all strings are null-terminated.

    const GLubyte *e = ::glGetString( GL_EXTENSIONS );
	// This string is deleted in the destructor
	m_ExtensionPtr = new GLubyte[ _mbslen( e )+1 ];
	if ( m_ExtensionPtr == NULL )
	{
		TRACE("Couldn't allocate memory for the extension list.\n");
		return;
	}
	// make a local copy to mess with
	::_mbscpy( m_ExtensionPtr, e );

	GLubyte *c = m_ExtensionPtr;
	bool bDone = false;
	
	while ( !bDone )
	{
		GLubyte *extension = c;
		while ( (0 != *c ) && ( ' ' != *c ) )
			c++;
		if ( 0 == *c )
			bDone = TRUE;
		*c = 0; // terminate the string
		// Now create a new CString object and add it to the array
        m_ExtensionArray.Add( extension );

        // skip to next string
		extension = ++c;
	}

// End of the extended driver information
}