GHOST_TSuccess GHOST_WindowWin32::swapBuffers() { HDC hDC = m_hDC; if (is_crappy_intel_card()) hDC = ::wglGetCurrentDC(); return ::SwapBuffers(hDC) == TRUE ? GHOST_kSuccess : GHOST_kFailure; }
void GHOST_ContextWGL::initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD) { HWND dummyHWND = NULL; HDC dummyHDC = NULL; HGLRC dummyHGLRC = NULL; HDC prevHDC; HGLRC prevHGLRC; int iPixelFormat; #ifdef WITH_GLEW_MX wglewContext = new WGLEWContext; memset(wglewContext, 0, sizeof(WGLEWContext)); delete m_wglewContext; m_wglewContext = wglewContext; #endif SetLastError(NO_ERROR); prevHDC = ::wglGetCurrentDC(); WIN32_CHK(GetLastError() == NO_ERROR); prevHGLRC = ::wglGetCurrentContext(); WIN32_CHK(GetLastError() == NO_ERROR); dummyHWND = clone_window(m_hWnd, NULL); if (dummyHWND == NULL) goto finalize; dummyHDC = GetDC(dummyHWND); if (!WIN32_CHK(dummyHDC != NULL)) goto finalize; iPixelFormat = choose_pixel_format_legacy(dummyHDC, preferredPFD); if (iPixelFormat == 0) goto finalize; PIXELFORMATDESCRIPTOR chosenPFD; if (!WIN32_CHK(::DescribePixelFormat(dummyHDC, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD))) goto finalize; if (!WIN32_CHK(::SetPixelFormat(dummyHDC, iPixelFormat, &chosenPFD))) goto finalize; dummyHGLRC = ::wglCreateContext(dummyHDC); if (!WIN32_CHK(dummyHGLRC != NULL)) goto finalize; if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC))) goto finalize; #ifdef WITH_GLEW_MX if (GLEW_CHK(wglewInit()) != GLEW_OK) fprintf(stderr, "Warning! WGLEW failed to initialize properly.\n"); #else if (GLEW_CHK(glewInit()) != GLEW_OK) fprintf(stderr, "Warning! Dummy GLEW/WGLEW failed to initialize properly.\n"); #endif // the following are not technially WGLEW, but they also require a context to work #ifndef NDEBUG delete m_dummyRenderer; delete m_dummyVendor; delete m_dummyVersion; m_dummyRenderer = _strdup(reinterpret_cast<const char *>(glGetString(GL_RENDERER))); m_dummyVendor = _strdup(reinterpret_cast<const char *>(glGetString(GL_VENDOR))); m_dummyVersion = _strdup(reinterpret_cast<const char *>(glGetString(GL_VERSION))); #endif s_singleContextMode = is_crappy_intel_card(); finalize: WIN32_CHK(::wglMakeCurrent(prevHDC, prevHGLRC)); if (dummyHGLRC != NULL) WIN32_CHK(::wglDeleteContext(dummyHGLRC)); if (dummyHWND != NULL) { if (dummyHDC != NULL) WIN32_CHK(::ReleaseDC(dummyHWND, dummyHDC)); WIN32_CHK(::DestroyWindow(dummyHWND)); } }
GHOST_TSuccess GHOST_WindowWin32::installDrawingContext(GHOST_TDrawingContextType type) { GHOST_TSuccess success; switch (type) { case GHOST_kDrawingContextTypeOpenGL: { // If this window has multisample enabled, use the supplied format if (m_multisampleEnabled) { if (SetPixelFormat(m_hDC, m_msPixelFormat, &sPreferredFormat) == FALSE) { success = GHOST_kFailure; break; } // Create the context m_hGlRc = ::wglCreateContext(m_hDC); if (m_hGlRc) { if (::wglMakeCurrent(m_hDC, m_hGlRc) == TRUE) { if (s_firsthGLRc) { if (is_crappy_intel_card()) { if (::wglMakeCurrent(NULL, NULL) == TRUE) { ::wglDeleteContext(m_hGlRc); m_hGlRc = s_firsthGLRc; } else { ::wglDeleteContext(m_hGlRc); m_hGlRc = NULL; } } else { ::wglCopyContext(s_firsthGLRc, m_hGlRc, GL_ALL_ATTRIB_BITS); ::wglShareLists(s_firsthGLRc, m_hGlRc); } } else { s_firsthGLRc = m_hGlRc; } if (m_hGlRc) { success = ::wglMakeCurrent(m_hDC, m_hGlRc) == TRUE ? GHOST_kSuccess : GHOST_kFailure; } else { success = GHOST_kFailure; } } else { success = GHOST_kFailure; } } else { success = GHOST_kFailure; } if (success == GHOST_kFailure) { printf("Failed to get a context....\n"); } } else { if (m_stereoVisual) sPreferredFormat.dwFlags |= PFD_STEREO; // Attempt to match device context pixel format to the preferred format int iPixelFormat = EnumPixelFormats(m_hDC); if (iPixelFormat == 0) { success = GHOST_kFailure; break; } if (::SetPixelFormat(m_hDC, iPixelFormat, &sPreferredFormat) == FALSE) { success = GHOST_kFailure; break; } // For debugging only: retrieve the pixel format chosen PIXELFORMATDESCRIPTOR preferredFormat; ::DescribePixelFormat(m_hDC, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &preferredFormat); // Create the context m_hGlRc = ::wglCreateContext(m_hDC); if (m_hGlRc) { if (::wglMakeCurrent(m_hDC, m_hGlRc) == TRUE) { if (s_firsthGLRc) { if (is_crappy_intel_card()) { if (::wglMakeCurrent(NULL, NULL) == TRUE) { ::wglDeleteContext(m_hGlRc); m_hGlRc = s_firsthGLRc; } else { ::wglDeleteContext(m_hGlRc); m_hGlRc = NULL; } } else { ::wglShareLists(s_firsthGLRc, m_hGlRc); } } else { s_firsthGLRc = m_hGlRc; } if (m_hGlRc) { success = ::wglMakeCurrent(m_hDC, m_hGlRc) == TRUE ? GHOST_kSuccess : GHOST_kFailure; } else { success = GHOST_kFailure; } } else { success = GHOST_kFailure; } } else { success = GHOST_kFailure; } if (success == GHOST_kFailure) { printf("Failed to get a context....\n"); } // Attempt to enable multisample if (m_multisample && WGL_ARB_multisample && !m_multisampleEnabled && !is_crappy_intel_card()) { success = initMultisample(preferredFormat); if (success) { // Make sure we don't screw up the context if (m_hGlRc == s_firsthGLRc) s_firsthGLRc = NULL; m_drawingContextType = GHOST_kDrawingContextTypeOpenGL; removeDrawingContext(); // Create a new window GHOST_TWindowState new_state = getState(); m_nextWindow = new GHOST_WindowWin32((GHOST_SystemWin32 *)GHOST_ISystem::getSystem(), m_title, m_left, m_top, m_width, m_height, new_state, type, m_stereo, m_multisample, m_parentWindowHwnd, m_multisampleEnabled, m_msPixelFormat); // Return failure so we can trash this window. success = GHOST_kFailure; break; } else { m_multisampleEnabled = GHOST_kSuccess; printf("Multisample failed to initialized\n"); success = GHOST_kSuccess; } } } } break; case GHOST_kDrawingContextTypeNone: success = GHOST_kSuccess; break; default: success = GHOST_kFailure; } return success; }