1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
   | void CMainWindow::CreateGLWindow()
{
	TCHAR buf[255];
	DWORD windowStyle = WS_OVERLAPPEDWINDOW;	// Define Our Window Style
	DWORD windowExtendedStyle = WS_EX_APPWINDOW;	// Define The Window's Extended Style
 
	PIXELFORMATDESCRIPTOR pfd =			// pfd Tells Windows How We Want Things To Be
	{
		sizeof (PIXELFORMATDESCRIPTOR),		// Size Of This Pixel Format Descriptor
		1,					// Version Number
		PFD_DRAW_TO_WINDOW |			//Format Must Support Window
		PFD_SUPPORT_OPENGL |			// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,			// Must Support Double Buffering
		PFD_TYPE_RGBA,				// Request An RGBA Format
		(BYTE)gcf.nBitsPerPixel,		// Select Our Color Depth
		0, 0, 0, 0, 0, 0,			// Color Bits Ignored
		1,					// Alpha Buffer
		0,					// Shift Bit Ignored
		0,					// No Accumulation Buffer
		0, 0, 0, 0,				// Accumulation Bits Ignored
		16,					// 16Bit Z-Buffer (Depth Buffer)  
		0,					// No Stencil Buffer
		0,					// No Auxiliary Buffer
		PFD_MAIN_PLANE,				// Main Drawing Layer
		0,					// Reserved
		0, 0, 0					// Layer Masks Ignored
	};
 
	RECT windowRect = {0, 0, gcf.nWidth, gcf.nHeight};	// Define Our Window Coordinates (fullscreen)
 
	GLuint PixelFormat;					// Will Hold The Selected Pixel Format
 
	if (fullscreen){
		gcf.log(_T("Starting in Full screen mode"));
		screenMode.setDisplayMode(gcf.nWidth,gcf.nHeight,gcf.nBitsPerPixel);
		ShowCursor (FALSE);				// Turn Off The Cursor
		windowStyle = WS_POPUP;				// Set The WindowStyle To WS_POPUP (Popup Window)
		}
	else{
		// Adjust Window, Account For Window Borders
		gcf.log(_T("Starting in windowed mode"));
		windowRect.left = gcf.nXwnd;
		windowRect.top = gcf.nYwnd;
		windowRect.right = gcf.nXwnd+gcf.nWidth;
		windowRect.bottom = gcf.nYwnd+gcf.nHeight;
		AdjustWindowRectEx (&windowRect, windowStyle, 0, windowExtendedStyle);
		}
	swprintf_s(buf,255,_T("%d bits per pixel were requested"),gcf.nBitsPerPixel);
	gcf.log(buf);
 
	//Window creation
	CString className = AfxRegisterWndClass(
		CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
		LoadCursor(NULL, IDC_ARROW),
		(HBRUSH)GetStockObject(BLACK_BRUSH),
		AfxGetApp()->LoadIcon(IDR_MAINFRAME));
 
	BOOL bRet = CreateEx(
		windowExtendedStyle,
		className,
		_T("MyOpenGLwithMFC"),
		windowStyle,
		windowRect,
		NULL,
		0);
	if (bRet == 0) gcf.log(_T("Critical : Window creation failed !"));
 
	gcf.pDC = this->GetDC();
	gcf.hDC = gcf.pDC->GetSafeHdc();
 
	/*
	Our first pass, Multisampling hasn't been created yet, so we create a window normally
	If it is supported, then we're on our second pass
	that means we want to use our pixel format for sampling
	so set PixelFormat to arbMultiSampleformat instead
	*/
	if(!arbMultisampleSupported)
	{
		PixelFormat = ChoosePixelFormat (gcf.hDC, &pfd);	// Find A Compatible Pixel Format
		if (PixelFormat == 0)					// Did We Find A Compatible Format?
		{
			// Failed
			gcf.log(_T("Critical : No match for pixel format"));
		}
		else{
			swprintf_s(buf,255,_T("Pixel format selected : %d"),PixelFormat);
			gcf.log(buf);
			}
 
	}
	else
	{
		PixelFormat = arbMultisampleFormat;
	}
 
	if (SetPixelFormat (gcf.hDC, PixelFormat, &pfd) == FALSE){		// Try To Set The Pixel Format
		// Failed
		DWORD err = GetLastError();
		CString csE = ErrorString(err);
		gcf.log(_T("Critical : can't set pixel format"));
		}
 
	gcf.hRC = wglCreateContext (gcf.hDC);					// Try To Get A Rendering Context
	if (gcf.hRC == 0){							// Did We Get A Rendering Context?
		// Failed
		gcf.log(_T("Critical : can't get rendering context"));
		}
	// Make The Rendering Context Our Current Rendering Context
	if (wglMakeCurrent (gcf.hDC, gcf.hRC) == FALSE){
		// Failed
		gcf.log(_T("Critical : can't switch current rendering context"));
		}
 
	/*
	Now that our window is created, we want to queary what samples are available
	we call our InitMultiSample window
	if we return a valid context, we want to destroy our current window
	and create a new one using the multisample interface.
	*/
	if (gcf.bDoMultiSampling){//Multisampling can be set or disabled from config file
		if(!arbMultisampleSupported && CHECK_FOR_MULTISAMPLE){
			HINSTANCE hInst = AfxGetInstanceHandle( );
			if(InitMultisample(hInst,this->m_hWnd,pfd)){
				gcf.log(_T("Antialiasing is supported, starting multisampling"));
				DestroyGLWindow();
				CreateGLWindow();
				}
			}
		}
	// Init the openGL window parameters
	DoInitGL();
 
	//Show Window and make active
	ShowWindow(SW_NORMAL);
	SetForegroundWindow();
	SetFocus();
	if (fullscreen){	
		//No cursor visible
		ShowCursor(false);
		}
}
 
void CMainWindow::DestroyGLWindow()
{
	gcf.log(_T("Closing OpenGL window"));
	if (gcf.hDC != 0){
		wglMakeCurrent (gcf.hDC, 0);
		if (gcf.hRC != 0){
			// Release The Rendering Context
			wglDeleteContext(gcf.hRC);
			// Zero The Rendering Context
			gcf.hRC = 0;
			}
		ReleaseDC(gcf.pDC);
		gcf.pDC = NULL;
		gcf.hDC = 0;
		}// end drawing context exists
	DestroyWindow();
	if (fullscreen){
		ChangeDisplaySettings (NULL,0);	// Switch Back To Desktop Resolution
		ShowCursor (TRUE);			// Show The Cursor
		}	
 
} | 
Partager