Game Development Reference
In-Depth Information
2.
This function is wrapped into the
CreateContextFull()
function, which selects an
appropriate pixel format and makes the context current:
HGLRC CreateContextFull( sLGLAPI* LGL3, HDC DeviceContext,
int BitsPerPixel, int ZBufferBits, int StencilBits,
int Multisample, int VersionMajor, int VersionMinor )
{
bool FormatSet = ChooseAndSetPixelFormat( LGL3,
DeviceContext,
BitsPerPixel, ZBufferBits, StencilBits, Multisample );
if ( !FormatSet ) return 0;
HGLRC RenderContext = CreateContext( LGL3,
DeviceContext, VersionMajor, VersionMinor );
if ( !RenderContext ) return 0;
if ( !MakeCurrent( LGL3, DeviceContext, RenderContext ) )
{ return 0; }
Reload( LGL3 );
return RenderContext;
}
It returns the created OpenGL rendering context,
HGLRC
on Windows, and updates
pointers in
LGL3
structure to correspond to the created context.
The previously described function has many side effects, and some
functional programmers claim it is inconsistent. Another approach is to
return a new
HGLRC
together with the new
LGL3
(or as a part of new
LGL3
),
so you can make it current later at your own will, and still has an access to
the old context. We will leave this idea as an exercise for the reader.
The function
Reload()
, previously mentioned, reloads pointers to OpenGL functions
in the
sLGLAPI
structure. This indirection is important since we need to emulate the
behavior of some OpenGL 3 functions on OpenGL ES 2.
Pixel format selection also uses another OpenGL extension:
WGL_ARB_pixel_
format
available at
http://www.opengl.org/registry/specs/ARB/wgl_
pixel_format.txt
.
3.
That means we have to choose and set the pixel format twice. The code is as follows:
bool ChooseAndSetPixelFormat( sLGLAPI* LGL3, HDC
DeviceContext,
int BitsPerPixel, int ZBufferBits, int StencilBits,
int Multisample )
{
PIXELFORMATDESCRIPTOR PFD;
memset( &PFD, 0, sizeof( PFD ) );