examples/Qt/ISFEditor/ISFEditor_app/Spout/SpoutGLDXinterop.cpp
//
// Create an OpenGL window and context for situations where there is none
// Not used if applications already have an OpenGL context
// Always call CloseOpenGL afterwards.
//

Create an OpenGL window and context for situations where there is none
Not used if applications already have an OpenGL context
Always call CloseOpenGL afterwards.
Create an OpenGL window and context for situations where there is none Not used if applications already have an OpenGL context Always call CloseOpenGL afterwards .
bool spoutGLDXinterop::InitOpenGL()
{
	m_hdc = NULL;
	m_hwndButton = NULL;
	m_hRc = NULL;
	HGLRC glContext = wglGetCurrentContext();
	if(glContext == NULL) {
		if(!m_hwndButton || !IsWindow(m_hwndButton)) {
			m_hwndButton = CreateWindowA("BUTTON",
				            "SpoutOpenGL",
							WS_OVERLAPPEDWINDOW | CS_OWNDC,
							0, 0, 32, 32,
							NULL, NULL, NULL, NULL);
		}
		if(!m_hwndButton) { 
			MessageBoxA(NULL, "Error 1", "InitOpenGL", MB_OK);
			return false; 
		}
		m_hdc = GetDC(m_hwndButton);
		if(!m_hdc) { 
			MessageBoxA(NULL, "Error 2", "InitOpenGL", MB_OK); 
			return false; 
		}
		PIXELFORMATDESCRIPTOR pfd;
		ZeroMemory( &pfd, sizeof( pfd ) );
		pfd.nSize = sizeof( pfd );
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.cColorBits = 32;
		pfd.cDepthBits = 16;
		pfd.iLayerType = PFD_MAIN_PLANE;
		int iFormat = ChoosePixelFormat(m_hdc, &pfd);
		if(!iFormat) { 
			MessageBoxA(NULL, "Error 3", "InitOpenGL", MB_OK);
			return false; 
		}
		if(!SetPixelFormat(m_hdc, iFormat, &pfd)) {
			DWORD dwError = GetLastError();
			char temp[128];
			sprintf_s(temp, "InitOpenGL Error 4\nSetPixelFormat\nError %d (%x)", dwError, dwError);
			MessageBoxA(NULL, temp, "InitOpenGL", MB_OK); 
			return false; 
		}
		m_hRc = wglCreateContext(m_hdc);
		if(!m_hRc) { 
			MessageBoxA(NULL, "Error 5", "InitOpenGL", MB_OK); 
			return false; 
		}
		wglMakeCurrent(m_hdc, m_hRc);
		if(wglGetCurrentContext() == NULL) {
			MessageBoxA(NULL, "Error 6", "InitOpenGL", MB_OK);
			return false; 
		}
	}
	return true;
}
bool spoutGLDXinterop :: InitOpenGL ( ) { m_hdc = NULL ; m_hwndButton = NULL ; m_hRc = NULL ; HGLRC glContext = wglGetCurrentContext ( ) ; if ( glContext == NULL ) { if ( ! m_hwndButton || ! IsWindow ( m_hwndButton ) ) { m_hwndButton = CreateWindowA ( " " , " " , WS_OVERLAPPEDWINDOW | CS_OWNDC , 0 , 0 , 32 , 32 , NULL , NULL , NULL , NULL ) ; } if ( ! m_hwndButton ) { MessageBoxA ( NULL , " " , " " , MB_OK ) ; return false ; } m_hdc = GetDC ( m_hwndButton ) ; if ( ! m_hdc ) { MessageBoxA ( NULL , " " , " " , MB_OK ) ; return false ; } PIXELFORMATDESCRIPTOR pfd ; ZeroMemory ( & pfd , sizeof ( pfd ) ) ; pfd . nSize = sizeof ( pfd ) ; pfd . nVersion = 1 ; pfd . dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER ; pfd . iPixelType = PFD_TYPE_RGBA ; pfd . cColorBits = 32 ; pfd . cDepthBits = 16 ; pfd . iLayerType = PFD_MAIN_PLANE ; int iFormat = ChoosePixelFormat ( m_hdc , & pfd ) ; if ( ! iFormat ) { MessageBoxA ( NULL , " " , " " , MB_OK ) ; return false ; } if ( ! SetPixelFormat ( m_hdc , iFormat , & pfd ) ) { DWORD dwError = GetLastError ( ) ; char temp [ 128 ] ; sprintf_s ( temp , " \n \n " , dwError , dwError ) ; MessageBoxA ( NULL , temp , " " , MB_OK ) ; return false ; } m_hRc = wglCreateContext ( m_hdc ) ; if ( ! m_hRc ) { MessageBoxA ( NULL , " " , " " , MB_OK ) ; return false ; } wglMakeCurrent ( m_hdc , m_hRc ) ; if ( wglGetCurrentContext ( ) == NULL ) { MessageBoxA ( NULL , " " , " " , MB_OK ) ; return false ; } } return true ; }
Create an OpenGL window and context for situations where there is none
Not used if applications already have an OpenGL context
Always call CloseOpenGL afterwards.
Create an OpenGL window and context for situations where there is none Not used if applications already have an OpenGL context Always call CloseOpenGL afterwards .
// For InitOpenGL and CloseOpenGL
 // We only need an OpenGL context with no render window because we don't draw to it
 // so create an invisible dummy button window. This is then independent from the host
 // program window (GetForegroundWindow). If SetPixelFormat has been called on the
 // host window it cannot be called again. This caused a problem in Mapio.
 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx
 //
 // CS_OWNDC allocates a unique device context for each window in the class. 
 //
 // 2000 (0x7D0) The pixel format is invalid.
 // Caused by repeated call of  the SetPixelFormat function
