Skip to main content
GameDev.net gamedev.net
๐Ÿ”’ Locked

Help with using WGL

Started by Chris_F Sep 24, 2014 at 11:11 PM 2 replies 2.3k views
Original Post
Chris_F
Chris_F

Can someone help me out? I'm looking for a bare minimum example (preferably just one file) for how to set up a core profile context with sRGB framebuffer using WGL.

mark ds
mark ds

This *should* create a GL3.3 core window. Note I've removed all error checking etc. just to show what needs to be done. Always make sure your hWnds, hDCs and hGLRC are valid. It assumes you're using GLEW.

Edit to add: It also assumes you know Win32...

Look for wglChoosePixelFormatARB (and the structure above it) near the bottom of the code.


HWND CreateGLWindow( void )
{
	HWND hWnd;
	HDC hDC;
	HGLRC hGLRC;
	WNDCLASSEX wc;
	PIXELFORMATDESCRIPTOR pfd;

	// create dummy GL window and get function pointers from GLEW

	ZeroMemory( &wc, sizeof( WNDCLASSEX ) );

	wc.cbSize = sizeof( WNDCLASSEX );
	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC) DefWindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = GetModuleHandle( NULL );
	wc.hIcon = 0;
	wc.hCursor = NULL;
	wc.hbrBackground = 0;
	wc.lpszMenuName = 0;
	wc.lpszClassName = L"dummy window class";
	wc.hIconSm = 0;

	RegisterClassEx( &wc );
	
	hWnd = CreateWindowEx( WS_EX_APPWINDOW, L"dummy window class", L"", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL );

	hDC = GetDC( hWnd );

	ZeroMemory( &pfd, sizeof( PIXELFORMATDESCRIPTOR ) );

	pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 24;
	pfd.cStencilBits = 8;

	SetPixelFormat( hDC, ChoosePixelFormat( hDC, &pfd ), &pfd );
	hGLRC = wglCreateContext( hDC );

	wglMakeCurrent( hDC, hGLRC );

	glewInit();

	wglMakeCurrent( NULL, NULL );
						
	wglDeleteContext( hGLRC );

	DestroyWindow( hWnd );

	UnregisterClass( L"dummy window class", GetModuleHandle( NULL ) );



	// Create GL window

	ZeroMemory( &wc, sizeof( WNDCLASSEX ) );

	wc.cbSize = sizeof( WNDCLASSEX );
	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wc.lpfnWndProc = (WNDPROC) WindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = sizeof( LONG_PTR );
	wc.hInstance = GetModuleHandle( NULL );
	wc.hIcon = 0;
	wc.hCursor = NULL;
	wc.hbrBackground = 0;
	wc.lpszMenuName = 0;
	wc.lpszClassName = L"Window Class";
	wc.hIconSm = 0;

	RegisterClassEx( &wc );

	hWnd = CreateWindowEx( NULL, L"Window Class", L"Window Caption", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL );

	hDC = GetDC( hWnd );

	const unsigned int maxFormats = 20; // max number of returned pixel formats
	int pixelFormats[maxFormats]; // array of returned pixel formats, best at index 0...
	unsigned int numFormatsAvailable; // number of matching formats

	const int pfdAttributes[] =
	{
		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
		WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
		WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, GL_FALSE,
		WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
		WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
		WGL_COLOR_BITS_ARB, 32,
		WGL_DEPTH_BITS_ARB, 24,
		WGL_STENCIL_BITS_ARB, 8,
		0
	};
				
	wglChoosePixelFormatARB( hDC, pfdAttributes, NULL, maxFormats, pixelFormats, &numFormatsAvailable );

	SetPixelFormat( hDC, pixelFormats[0], &pfd );
				
	const int glContextAttributes[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
		WGL_CONTEXT_MINOR_VERSION_ARB, 3,
		//WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
		0
	};

	hGLRC = wglCreateContextAttribsARB( hDC, NULL, glContextAttributes );

	wglMakeCurrent( hDC, hGLRC );

	ShowWindow( hWnd, SW_SHOW );
	
	return hWnd;
}


Chris_F
Chris_F

Thanks Mark, I believe that was exactly what I was looking for.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.