Loading function pointers with GLAD and win32

Hey i wonder why GLAD init fails, even though i have a successfully created a context and made it current.

Here im setting up then window:


WIN32_PLATFORM Win32_WindowSetup(HINSTANCE hInstance, char *program_name, i32 gl_ver_max, i32 gl_ver_min)
{
    WIN32_PLATFORM result = {};
    
    
    result.wnd_class_ex.cbSize = sizeof(result.wnd_class_ex);
    result.wnd_class_ex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    result.wnd_class_ex.lpfnWndProc = &WndProc;
    result.wnd_class_ex.cbClsExtra = 0;
    result.wnd_class_ex.cbWndExtra = 0;
    result.wnd_class_ex.hInstance = hInstance;
    result.wnd_class_ex.hIcon = 0;
    result.wnd_class_ex.hCursor = 0;
    result.wnd_class_ex.hbrBackground = 0;
    result.wnd_class_ex.lpszMenuName = 0;
    result.wnd_class_ex.lpszClassName = program_name;
    result.wnd_class_ex.hIconSm = 0;
    result.gl_ver_max = gl_ver_max;
    result.gl_ver_min = gl_ver_min;
    
    if(!RegisterClassEx(&result.wnd_class_ex)) 
    {
        MessageBox(0, "Couldn't Register Class", "win32_error", 0);
        
    }
    else
    {
        // Loading OpenGL functions
        Win32_LoadOpenGLFunctionPointers(&result);
        
        
        result.hwnd = CreateWindowEx(0, program_name,
                                     program_name, WS_OVERLAPPEDWINDOW,
                                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                                     0, 0, hInstance, 0);
        if(!result.hwnd)
        {
            MessageBox(0, "Couldn't Create Window", "win32_error", 0);
        }
        else
        {
            running = true;
            ShowWindow(result.hwnd, SW_SHOW);
        }
    }
    
    
    
    if(gl_ver_max <= 2)
        Win32_CreateOldOpenGLContext(&result);
    else if(WGL_CHOOSE_PIXELFORMAT_ARB && WGL_CREATE_CONTEXT_ARB)
        Win32_CreateModernOpenGLContext(&result);
    
    return result;
    
}

And in Win32_LoadOpenGLFunctionPointers() im making a fake context to load gl-functions using if(!gladLoadGLLoader((GLADloadproc)wglGetProcAddress))

void Win32_LoadOpenGLFunctionPointers(WIN32_PLATFORM *win32)
{
    DWORD err = {};
    // Making a fake window/glContext etc, needed for loading OpenGL function pointers
    HWND fake_hwnd = CreateWindowEx(0, win32->wnd_class_ex.lpszClassName, "FAKE", WS_OVERLAPPEDWINDOW,
                                    0, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0,
                                    0, win32->wnd_class_ex.hInstance, 0);
    
    
    PIXELFORMATDESCRIPTOR fake_pfd = {};
    fake_pfd.nSize = sizeof(fake_pfd);
    fake_pfd.nVersion = 1;
    fake_pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    fake_pfd.iPixelType = PFD_TYPE_RGBA;
    fake_pfd.cColorBits = 32;
    fake_pfd.cDepthBits = 24;
    fake_pfd.cStencilBits = 8;
    fake_pfd.iLayerType = PFD_MAIN_PLANE;
    
    HDC fake_dc = GetDC(fake_hwnd);
    
    i32 pix_format = ChoosePixelFormat(fake_dc, &fake_pfd);
    SetPixelFormat(fake_dc, pix_format, &fake_pfd);
    
    HGLRC fake_gl_context = wglCreateContext(fake_dc);
    
    if(!fake_gl_context)
    {
        
        err = GetLastError();
        MessageBox(0, "wglCreateContext: fail", "couldn't create fake context for OpenGL", 0);
    }
    else
    {
        
        if(!wglMakeCurrent(fake_dc, fake_gl_context))
        {
            err = GetLastError();
        }
        else
        {
            // Load the OpenGL functions
            if(!gladLoadGLLoader((GLADloadproc)wglGetProcAddress))
            {
                i32 a = 0;
            }
            else
            {
                wglChoosePixelFormatARB = (wglChoosePixelFormatARB_pfn)wglGetProcAddress("wglChoosePixelFormatARB");
                if(wglChoosePixelFormatARB) WGL_CHOOSE_PIXELFORMAT_ARB = 1; else WGL_CHOOSE_PIXELFORMAT_ARB = 0;
                wglCreateContextAttribsARB = (wglCreateContextAttribsARB_pfn)wglGetProcAddress("wglCreateContextAttribsARB"); 
                if(wglCreateContextAttribsARB) WGL_CREATE_CONTEXT_ARB = 1; else WGL_CREATE_CONTEXT_ARB = 0; 
            }
            
        }
        
    }
    
    
    wglMakeCurrent(0, 0);
    wglDeleteContext(fake_gl_context);
    DestroyWindow(fake_hwnd);
    
}

However if(!gladLoadGLLoader((GLADloadproc)wglGetProcAddress))
seems to fail.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.