HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

OpenGL forward-compatible context creation on Windows

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
forwardcompatiblewindowsopenglcontextcreation

Problem

Is this the proper method for creating an OpenGL 3.3+ forward-compatible context?

```
// Create window
WNDCLASSEX wc;
ZeroMemory( &wc, sizeof( wc ) );
wc.cbSize = sizeof( wc );
wc.lpfnWndProc = _eventHandler;
wc.hInstance = GetModuleHandle( NULL );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.lpszClassName = "OpenGL 3.3";
if ( !RegisterClassEx( &wc ) ) Error( "Failed to register window class!" );
if ( !( _window = CreateWindowEx( WS_EX_CLIENTEDGE, "OpenGL 3.3", "OpenGL 3.3", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL ) ) )
Error( "Failed to create the window!" );
if ( !( _hdc = GetDC( _window ) ) ) Error( "Failed to retrieve device context!" );

// Choose pixel format
int pixelFormat;
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
if ( !( pixelFormat = ChoosePixelFormat( _hdc, &pfd ) ) ) Error( "Failed to find suitable pixel format!" );
if ( !SetPixelFormat( _hdc, pixelFormat, &pfd ) ) Error( "Failed to set pixel format!" );

// Create temporary context and make sure we have support
HGLRC tempContext = wglCreateContext( _hdc );
if ( !tempContext ) Error( "Failed to create temporary context!" );
if ( !wglMakeCurrent( _hdc, tempContext ) ) Error( "Failed to activate temporary context!" );

int major, minor; glGetIntegerv( GL_MAJOR_VERSION, &major ); glGetIntegerv( GL_MINOR_VERSION, &minor );
if ( major < 3 || minor < 2 ) Error( "OpenGL 3.2 is not supported!" );

// Create forward compatible context
int attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, major,
WGL_CONTEXT_MINOR_VERSION_ARB, minor,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,

Solution

This looks right to me. I am a bit tired right now and usually use GLFW.

You might be interested in this page on the OpenGL wiki describing setting up the context after 3.1 which is essentially the same work.

Context

StackExchange Code Review Q#1263, answer score: 4

Revisions (0)

No revisions yet.