Advertisement
Guest User

Example of OpenGL Context Creation

a guest
Oct 2nd, 2013
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. LRESULT GLWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2. {
  3.     switch(uMsg)
  4.     {
  5.     case WM_CREATE:
  6.         {
  7.             if(!m_hdc)
  8.             {
  9.                 m_hdc = GetDC(hWnd);
  10.             }
  11.             SetupPixelFormat();
  12.  
  13.             if(!m_hglrc)
  14.             {
  15.                 int attribs[] = {
  16.                     WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  17.                     WGL_CONTEXT_MINOR_VERSION_ARB, 0,
  18.                     0};
  19.  
  20.                 HGLRC tmpContext;
  21.                 if(!(tmpContext = wglCreateContext(m_hdc)))
  22.                 {
  23.                     MessageBox(NULL, L"Can't create temp context", NULL, MB_OK);
  24.                 }
  25.                 if(!wglMakeCurrent(m_hdc, tmpContext))
  26.                 {
  27.                     MessageBox(NULL, L"Can't make temp context current", NULL, MB_OK);
  28.                 }
  29.  
  30.                 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  31.                 if(!wglCreateContextAttribsARB)
  32.                 { // If wgl_create_context doesn't exist.
  33.                     m_hglrc = tmpContext;
  34.                     tmpContext = NULL;
  35.                 }
  36.                 else
  37.                 { // If wgl_create_context exists.
  38.                     m_hglrc = wglCreateContextAttribsARB(m_hdc, 0, attribs);
  39.                     if(!m_hglrc)
  40.                     { // If trying a higher OpenGL version fails. (Maybe try to query for a lower OpenGL version this way? Is there a point?)
  41.                         m_hglrc = tmpContext;
  42.                         tmpContext = NULL;
  43.                     }
  44.                     // Everything is looking good, use it and delete the old context.
  45.                     wglDeleteContext(tmpContext);
  46.                     tmpContext = NULL;
  47.                     wglMakeCurrent(m_hdc, m_hglrc);
  48.                 }
  49.             }
  50.             m_isRunning = true;
  51.         }
  52.         break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement