Tricks of the Windows Game Programming Gurus:Journey into the Abbys

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Cool, huh? That’s the entire Win32/DirectX game. Well, almost. There are a few hundred lines of code in the `BLACKBOX.CPP` source file, but we’ll just pretend that it’s like DirectX and someone else wrote it (me!). Anyway, let’s take a quick look at the contents of Listing 1.3.

Basically, Windows needs to have what’s called an event loop. This is standard for all Windows programs since Windows is, for the most part, event-driven. However, games aren’t event-driven; they run at all times, whether the user does something or not. So we need to at least support a minimum event loop to make Windows happy. The code that implements this is in `WinMain()`—jeez, that’s a surprise, huh?

`WinMain()` is the main entry point for all Windows programs, just like `main()` is the entry point for all DOS/UNIX programs (please wash your mouth out if you said “UNIX” out loud). In any case, the `WinMain()` for FreakOut creates a window and then enters right into the event loop. If Windows needs to do something, it does so. When all the basic event handling is over, `Game_Main()` is called. This is where the real action occurs for our game.

If you wanted to, you could loop in `Game_Main()` forever, never releasing it back to the main event loop in `WinMain()`. But this would be bad because Windows would never receive any messages and you would starve the system. Alas, what we need to do is perform one frame of animation and logic and then return back to `WinMain()`. This way, Windows will continue to function and process messages. If this all sounds a little hocus-pocus, don’t worry—it gets worse in the next chapter <BG>.

Once in `Game_Main()`, the logic for FreakOut is executed. The game image is rendered into an offscreen workspace and then finally shown on the display at the end of the loop via the `DD_FLIP()` call. So what I want you to do is take a look at all the game states and try to follow each section of the game loop and what it does. To play the game, simply click on `FREAKOUT.EXE`. The program will launch immediately. The controls are

  Right arrow—Move paddle right.
  Left arrow—Move paddle left.
  Esc—Exit back to Windows.

Also, there’s a 100-point penalty if you miss the ball, so watch it!

When you feel comfortable with the game code and gameplay, try modifying the game and making changes to it. You could add different background colors (0–255 are valid colors), more balls, a paddle that changes size, and more sound effects (which I’m making right now with the Win32 API function `MessageBeep()` function).

### Summary

Well, I think that’s about the quickest crash course in game programming I have ever given! We covered a lot of ground, but think of it as the “back of the box” version of the book. I just wanted to give you a feel for all the things that we are going to talk about and learn in this book. In addition, it’s always good to take a look at a complete game because it generates a lot of questions for you to think about.

Now, before moving on to Chapter 2, which covers Windows programming, make sure that you feel comfortable with compiling the FreakOut game. If you aren’t, open up the compiler book and RTFM. I’ll be waiting.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.