26.4 OpenGL code in Windows

OpenGL is a platform-neutral library. It can run on operating systems such as X-Windows, the Macintosh, or Windows. In each of these systems you add a little extra code in order to interface your system to OpenGL.

The basic Win32 library includes a few built-in data types and functions to be used with OpenGL. Here's a block of code that shows how we initialize a window and update it. The data types PIXELFORMATDESCRIPTOR and HGLR are special built-in Windows types used for working with OpenGL. And ::ChoosePixelFormat, ::SelectPixelFormat, ::wglCreateContext, and ::SwapBuffers are built-in Windows methods for use with OpenGL.

int pixelformat_index; 
HGLR openglrenderingcontext; 
HDC hdc_view; 

//Initialize the Window. 
pixelformatindex = ::ChoosePixelFormat(hdc, &pixelFormat); 
::SelectPixelFormat(hdc, pixelformatindex, &pixelFormat); 
openglrenderingcontext = ::wglCreateContext(hdc_view); 
::glMakeCurrent(hdc_view, openglrenderingcontext); 
/* Use ::gl... and ::glu... calls to set GL states, add GL vertices, 
    load GL textures, 
//the example of a square was given in the previous subsection. */ 

//Update the Window 

Note that you don't need to re-initialize the window for every update. Each time you want to load the OpenGL state machine and send something new to the screen, it suffices to call ::glMakeCurrent again, using the same openglrenderingcontext. In the Pop Framework, the call to ::glMakeCurrent is encapsulated into our cGraphicsOpenGL::activate() method.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference