使用win api初始化opengl

问题描述 投票:0回答:1
error(1) Severity Code Description Project File String Suppression Status
Warning C28252 Inconsistent annotation for "WinMain": _Param_(3) contains "SAL_null(__no)" in the previous instance. See c:\program files (x86)\windows kits\10\include\10.0.19041.0\um\winbase.h(1006). Project1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8 

error(2) Severity Code Description Project File String Suppression Status
Warning C28253 Inconsistent annotation for "WinMain": _Param_(3) contains "SAL_null(__maybe)" in this instance. See c:\program files (x86)\windows kits\10\include\10.0.19041.0\um\winbase.h(1006). Project1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8

error(3)    Severity Code Description Project File String Suppression Status
Error LNK2019 reference to an unresolved external main character in the function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ). Project1 C:\Users\setro\OneDrive\Desktop\Project1\MSVCRTD.lib(exe_main.obj) 1 

error(4)    Severity Code Description Project File String Suppression Status
LNK1120 error of unresolved external elements: 1 Project1 C:\Users\setro\OneDrive\Desktop\Project1\x64\Debug\Project1.exe 1 
#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd)
{
    MSG msg = { 0 };
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if (!RegisterClass(&wc))
        return 1;
    CreateWindowW(wc.lpszClassName, L"openglversioncheck", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 640, 480, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0) > 0)
        DispatchMessage(&msg);

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
            PFD_TYPE_RGBA,        // The kind of framebuffer. RGBA or palette.
            32,                   // Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                   // Number of bits for the depthbuffer
            8,                    // Number of bits for the stencilbuffer
            0,                    // Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd);
        SetPixelFormat(ourWindowHandleToDeviceContext, letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent(ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0, (char*)glGetString(GL_VERSION), "OPENGL VERSION", 0);

        //wglMakeCurrent(ourWindowHandleToDeviceContext, NULL); Unnecessary; wglDeleteContext will make the context not current
        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
    }
    break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

摘自网站https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)

winapi opengl
1个回答
0
投票

WinMain()
的第三个参数不是可选的,因此使用
__in
代替
__in_opt
(或者根本不注释你的
WinMain()
,因为你不是一开始调用它的人)。

此外,您缺少

WINAPI
上的
WinMain()
调用约定,这就是链接器找不到它的原因。

© www.soinside.com 2019 - 2024. All rights reserved.