As you are about to learn in this chapter, there are a million little details about writing games that no one talks about. It's a simple matter to surf the web or read one of the run-of-the-mill game programming books to find a few different ways to draw textured polygons in Direct3D, but when it comes to figuring out your initialization sequence or controlling your main loop you'll find little discussion. Most programmers who haven't performed these tasks are left with making some initial assumptions and they usually come up with less than perfect solutions only after time-consuming experimentation. In other words, they waste a lot of precious development time.
I've written this chapter to show you the ins and outs of writing code to initialize your games and controlling a main loop. As you check out the code in this chapter, keep in mind that the solutions provided shouldn't always be used verbatim. It's really important that you develop an understanding of how and why they work, and, if you do, you'll have some neat bits and pieces to add to your toolbox. What's even more likely is you'll make some significant improvements after you try some of these ideas. More power to you. Truly elegant solutions and algorithms never just fall out of the sky; they usually come to you after seeing some code that falls slightly short of what is needed.
With any luck you'll view some of the code in this chapter as slightly less than elegant and upon tweaking or rewriting it you'll send me an email gloating about your improvements. Don't bother because I'll just send you an email in return to inform you that you were always a pawn in my master plan.
Every piece of software, including games, has three key functional parts: initialization, the core or main loop, and clean up. Initialization prepares your canvas for painting pixels. The main loop accepts and translates user input, changes the game state, and renders the game state until the loop is broken. This loop is broken by a user input or some kind of failure. The clean up code releases key system resources, closes files, and exits back to the operating system.