c++ - C++ 中的 OpenGL - 运行时崩溃

标签 c++ visual-studio opengl

我正在 Visual Studio 2005 中使用 C++ 开发一些 OpenGL。

// SetUpOpenGL sets the pixel format and a rendering
// context then returns the RC
HGLRC COpenGLBaseWnd::SetUpOpenGL(HWND hwnd)
{
    static PIXELFORMATDESCRIPTOR pfd = 
    {
        sizeof (PIXELFORMATDESCRIPTOR), // strcut size 
        1,                              // Version number
        PFD_DRAW_TO_WINDOW |    // Flags, draw to a window,
            PFD_DOUBLEBUFFER |    // enable double buffering
            PFD_SUPPORT_OPENGL, // use OpenGL
        PFD_TYPE_RGBA,          // RGBA pixel values
        24,                     // 24-bit color
        0, 0, 0,                // RGB bits & shift sizes.
        0, 0, 0,                // Don't care about them
        0, 0,                   // No alpha buffer info
        0, 0, 0, 0, 0,          // No accumulation buffer
        32,                     // 32-bit depth buffer
        0,                      // No stencil buffer
        0,                      // No auxiliary buffers
        PFD_MAIN_PLANE,         // Layer type
        0,                      // Reserved (must be 0)
        0,                      // No layer mask
        0,                      // No visible mask
        0                       // No damage mask
    };

    pCDC = pWnd->GetDC();
    hDC = pCDC->GetSafeHdc();

    PixelFormatID = ChoosePixelFormat(hDC, &pfd);

    if (!PixelFormatID)
    {
        // catch errors here.
        // If nMyPixelFormat is zero, then there's
        // something wrong... most likely the window's
        // style bits are incorrect (in CreateWindow() )
        // or OpenGl isn't installed on this machine
        //printf("ChoosePixelFormat Failed %d\r\n",GetLastError());
        abort();
        exit(-1);
    }

    if (pfd.dwFlags & PFD_NEED_PALETTE)
    {
        //printf("Choosen Pixel Format requires a palette.\r\n");
        abort();
        exit(-1);
    }

   SetPixelFormat(hDC, PixelFormatID, &pfd);

而 SetPixelFormat 调用是它在运行时发生的地方。烦人的是,它只会在我的机器上崩溃,而不会在我同事的机器上崩溃。

我找到了 this answer ,在 stackoverflow 上看起来是相关的,但是我不知道如何使用 C++ 中的这些信息来解决问题,或者这不是同一个问题。

我需要有关如何在 C++ 或其他潜在解决方案中实现该解决方案的建议。

最佳答案

确保在创建窗口类时,未指定 CS_PARENTDC 而指定了 CS_OWNDC

SetPixelFormat() 是否检测到 DC 是共享的(弄乱上述任一标志的效果)我不知道,但 OpenGL 将无法正常工作,除非窗口有一个专用的hDC值。

关于c++ - C++ 中的 OpenGL - 运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/561224/

相关文章:

c++ - 使用 GLuint 而不是 unsigned int 有什么好处?

c++ - 混合调试和发布库/二进制文件 - 不好的做法?

c# - MEF 是否需要 .NET 4?

opengl - 为 OpenGL 中的几何着色器提供 4 个顶点

.net - 持续集成是否消除了对庞大的 Visual Studio 解决方案的需求?

visual-studio - DOCKER_REGISTRY 变量未设置。默认为空字符串

opengl - 如何使正字法文本始终显示/最顶部?

c++ - 位补函数在 C++ 的 For 循环中做什么?

c++ - 为什么将对象分配给 map 会产生空对象?

c++ - 如何通过像素完美缩放/移动来修复 SFML 顶点数组中的垂直伪影线?