Chapter 4 Sample

Published February 22, 2007
Advertisement
The last couple of days I have been working through the program at the end of
Chapter 4 of the OpenGL book. It includes almost all of the concepts presented so far
so it makes a good review. Its over 800 lines of code so this will be a long entry.

The define/include section contains an entry I have not seen before: #inlcude
The comments identify it as Windows constants. I fished around for a while in various
help files and web sites but I could not find a more detailed definition of the file,
so I opened it up to take a look. It seems to be a large number of #defines dealing
with a large variety of subjects. So group of Windows constants it is.

The variable declaration section contains an enumerator primtypes_t that will be used
to keep track of the current primitive type when drawing.

The WinMain section is fairly straightforward. It calls his SetupWindow() function
and then enters the message loop. Here he deals with any messages, renders the scene,
swaps the buffers, and deals with window closing through the KillWindow() function.

The first functioned defined is WindProc. The variables declared at the beginning
are static so they do not have to be created every time. The usual system stuff is dealt
with here like minimizing, powersave mode etc. Closing the window is handled by posting
WM_QUIT to the message queue which will cause the message loop to call the KillWIndow()
function.

The interesting thing in this function is handling the WM_CHAR message. From the
Win32 SDK Help:

Quote:The WM_CHAR message is posted to the window with the keyboard focus when a
WM_KEYDOWN message is translated by the TranslateMessage function. WM_CHAR contains
the character code of the key that was pressed.


So this is where he handles the keyboard input that lets you change the display. There
is a switch statement in the DisplayScene() function that uses this information.

The next function is SetupWindow(). Much of what appears here has been covered earlier in
the book. It is here that he fills out the window class structure, registers the class etc.
He has added error handling for this program. This is done by having a message box
displayed when a function returns an error value.

The SetupWindow() function also sets up the coordinates for the window. This is
something I had not seen before and involves the structure RECT:
typedef struct _RECT {    // rc     LONG left;     LONG top;     LONG right;     LONG bottom; } RECT; 


Obviously this is used to define a rectangle.

He dimensions the rectangle adjusted for borders and style. This is done with
BOOL AdjustWindowRectEx():
Quote:The AdjustWindowRectEx function calculates the
required size of the rectangle of a window with extended style based on the desired
client-rectangle size. The window rectangle can then be passed to the CreateWindowEx
function to create a window whose client area is the desired size.

BOOL AdjustWindowRectEx(

LPRECT lpRect, // address of client-rectangle structure
DWORD dwstyle, // window styles
BOOL bMenu, // menu-present flag
DWORD dwExstyle // extended style
);


He can then use the window rectangle (which was passed by reference) in the
CreateWindowEx() function.

After creating the window the call is made to get the device context. The pixel format
is set and chosen. The rendering context is created and set to current.

Then ShowWindow() is called. It is set as the foreground window and set as the keyboard
focus. SetFocus():
Quote:The SetFocus function sets the keyboard focus to the specified window.
All subsequent keyboard input is directed to this window. The window, if any,
that previously had the keyboard focus loses it.

HWND SetFocus(

HWND hwnd // handle of window to receive focus
);


The perspective is set for the screen size with ResizeScene(). Finally he calls
InitializeScene().

InitializeScene() is the next function defined. It is used to set the values that
only need to be dealt with once. This includes things like point size, antialiasing,
line width etc. It also includes some stuff like shading that have not been covered
in the book yet.

The next function definition is ResizeScene(). For the most part this includes stuff
that looks like it will be covered in the next few chapters. The viewport size is set.
The projection matrix is set to the identity matrix. There is a function call for
gluOrtho2D() it is passed the left, right, top, and bottom coorditantes of the scene.
I am going to assume that it uses this information to construct a matrix that is multiplied
by the current matrix (projection) to give a 2D orthographic viewing region with the
clipping planes defined by the parameters passed to it.

Finally he selects the modelview matrix and sets it equal to the identity matrix.

The next function is DisplayScene(). This is pretty straightforward. Depending on what
g_currentPrimitive's current value is (an enumerator value set by keyboard input) he draws
a sample of one of the primitives presented in the chapter. Some of these are done with
random vertices, some not (random could be problematic if used for for the quadrilaterals).

The final function is KillWindow(). I was really looking forward to this one because
I have been having some problems getting my windows programs to exit correctly. I
have to open the task manager and end the process manually. However I found that I am
doing exactly what is in this function:

1) Restore original display settings
2) Release the rendering context
3) Delete the rendering context
4) Release the device context
5) Destroy the window
6) Unregister the window class

I guess that something may have changed since this book was published, or this could
be a problem with Dev-C++ wanting it done another way. I used all the error handling
but do not get an error message when I click on the X and the window fails to close.

So far I have only been using the Win32 SDK Help file as a way to look up functions and
other information. However I found (by accident) that it can be read like a book by flipping
through pages. It looks like its practically an instruction manual for Windows programming.
My next entry will detail my adventures in solving my window closing problems. Hopefully
with help from the SDK help file.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement