You are previewing Beginning Game Programming, Third Edition.
O'Reilly logo
Beginning Game Programming, Third Edition

Book Description

Beginning Game Programming, Third Edition shows budding game developers how to take their game ideas from concept to reality. Requiring only a basic understanding of the C++ language, this unique guide covers all the skills needed to create 2D and 3D games using code written in DirectX. Each element of a game is taught step-by-step, from learning how to create a simple Windows program, to using the key DirectX components to render 2D and 3D, to adding sound to your game. Using the skills taught within each chapter, readers will develop their own game library, which they can build upon for future game projects. And this updated, new edition includes end-of-chapter quizzes and projects to help you practice your new skills! At the end of the book, newfound skills will be put to use as you create your own complete, fully functional game.

Table of Contents

  1. Copyright
    1. Dedication
  2. Foreword
  3. Acknowledgments
  4. About the Author
  5. Welcome to the Adventure!
    1. Where to Begin?
    2. What Will You Learn?
    3. Let’s Talk About Compilers
    4. What’s Your Skill Level?
    5. Conventions Used in This Book
    6. Book Summary
  6. I. Introduction to Game Programming with Windows and DirectX
    1. 1. Getting Started with Windows
      1. An Overview of Windows Programming
        1. “Getting” Windows
        2. Understanding Windows Messaging
        3. Multi-Tasking
        4. Multi-Threading
        5. Event Handling
      2. A Quick Overview of DirectX
        1. What Is Direct3D?
      3. The Basics of a Windows Program
        1. Creating Your First Win32 Project
        2. Understanding WinMain
          1. The WinMain Function Call
        3. A Complete WinMain
      4. What You Have Learned
      5. Review Questions
      6. On Your Own
    2. 2. Listening to Windows Messages
      1. Writing a Full-Blown Windows Program
        1. Understanding InitInstance
          1. The InitInstance Function Call
          2. The Structure of InitInstance
        2. Understanding MyRegisterClass
          1. The MyRegisterClass Function Call
          2. The Point of MyRegisterClass
        3. Exposing the Secrets of WinProc
          1. The WinProc Function Call
        4. Program Event Handler
          1. The Big WinProc Secret
      2. What Is a Game Loop?
        1. The Old WinMain
          1. The Need for Continuity
          2. The Real-Time Terminator
        2. WinMain and Looping
        3. Time to Take a Peek
          1. Plugging PeekMessage into WinMain
      3. The GameLoop Project
        1. Source Code for the GameLoop Program
          1. Drawing Bitmaps with Windows
        2. Running the GameLoop Program
      4. What You Have Learned
      5. Review Questions
      6. On Your Own
    3. 3. Initializing Direct3D
      1. Getting Started with Direct3D
        1. The Direct3D Interfaces
        2. Creating the Direct3D Object
        3. Taking Direct3D for a Spin
          1. Linking with the Direct3D Library
          2. Typing in the Source Code
          3. Running the Program
        4. Direct3D in Full-Screen Mode
          1. Modifying CreateWindow
          2. Changing the Direct3D Device Presentation Parameters
      2. What You Have Learned
      3. Review Questions
      4. On Your Own
  7. II. The Game Programming Toolbox
    1. 4. Drawing Bitmaps
      1. Surfaces and Bitmaps
        1. The Primary Surfaces
        2. Secondary Off-Screen Surfaces
          1. Creating a Surface
          2. Drawing the Surface (Blitting)
        3. The Create_Surface Example
        4. Loading Bitmaps from Disk
        5. The Load_Bitmap Program
        6. Recycling Code
      2. What You Have Learned
      3. Review Questions
      4. On Your Own
    2. 5. Getting Input with the Keyboard, Mouse, and Controller
      1. Keyboard Input
        1. DirectInput Object and Device
        2. Initializing the Keyboard
          1. Setting the Data Format
          2. Setting the Cooperative Level
          3. Acquiring the Device
        3. Reading Key Presses
      2. Mouse Input
        1. Initializing the Mouse
          1. Setting the Data Format
          2. Setting the Cooperative Level
          3. Acquiring the Device
        2. Reading the Mouse
      3. Xbox 360 Controller Input
        1. Initializing XInput
        2. Reading the Controller State
        3. Controller Vibration
        4. Testing XInput
      4. A Brief Introduction to Sprite Programming
        1. A Useful Sprite Struct
        2. Loading a Sprite Image
        3. Drawing a Sprite Image
      5. Bomb Catcher Game
        1. MyWindows.cpp
        2. MyDirectX.h
        3. MyDirectX.cpp
        4. MyGame.cpp
      6. What You Have Learned
      7. Review Questions
      8. On Your Own
    3. 6. Drawing and Animating Sprites
      1. What Is a Sprite?
      2. Loading the Sprite’s Image
      3. Transparent Sprites
        1. Initializing the Sprite Renderer
          1. Starting the Sprite Renderer
          2. Drawing a Sprite
          3. Stopping the Sprite Renderer
        2. Drawing Transparent Sprites
          1. Creating the Transparent Sprite Program
          2. Modifying MyDirectX.h
          3. Modifying MyDirectX.cpp
          4. Modifying MyGame.cpp
          5. Running the Trans_Sprite Program
      4. Drawing an Animated Sprite
        1. Working with Sprite Sheets
        2. The Animate Sprite Demo
          1. Updating MyDirectX.h
          2. Updating MyDirectX.cpp
          3. The Animate Sprite Source Code
      5. What You Have Learned
      6. Review Questions
      7. On Your Own
    4. 7. Transforming Sprites
      1. Sprite Rotation and Scaling
        1. 2D Transforms
          1. The Transformed 2D Matrix
          2. Sprite Scaling
          3. Sprite Rotation
          4. Sprite Translation
          5. Creating the Sprite Matrix
        2. Drawing a Transformed Sprite
        3. The Rotate_Scale_Demo Program
        4. Animation with Transforms
      2. What You Have Learned
      3. Review Questions
      4. On Your Own
    5. 8. Detecting Sprite Collisions
      1. Bounding Box Collision Detection
        1. Working with Rectangles
        2. Writing the Collision Function
        3. A New Sprite Structure
        4. Adjusting for Sprite Scaling
        5. The Bounding Box Demo Program
          1. Adding Code to MyDirectX.h
          2. Adding Code to MyDirectX.cpp
          3. MyGame.cpp
      2. Distance-Based Collision Detection
        1. Calculating Distance
        2. Codifying Distance
        3. Testing Distance-Based Collision
      3. What You Have Learned
      4. Review Questions
      5. On Your Own
    6. 9. Printing Text
      1. Creating a Font
        1. The Font Descriptor
        2. Creating the Font Object
        3. A Reusable MakeFont Function
      2. Printing Text with ID3DXFont
        1. Printing with DrawText
        2. Wrapping Text
      3. Testing Font Output
      4. What You Have Learned
      5. Review Questions
      6. On Your Own
    7. 10. Scrolling the Background
      1. Scrolling
        1. Backgrounds and Scenery
        2. Creating Backgrounds from Tiles
        3. Tile-Based Scrolling
          1. Tile-Based Static Scrolling Project
      2. Dynamically Rendered Tiles
        1. The Tile Map
        2. Creating a Tile Map Using Mappy
          1. Importing an Existing Bitmap File
          2. Exporting the Tile Map
        3. The Tile_Dynamic_Scroll Project
          1. Tile_Dynamic_Scroll Source Code
      3. Bitmap-Based Scrolling
        1. Theory of Bitmap-Based Scrolling
        2. Bitmap Scrolling Demo
      4. What You Have Learned
      5. Review Questions
      6. On Your Own
    8. 11. Playing Audio
      1. Using DirectSound
        1. Initializing DirectSound
        2. Creating a Sound Buffer
        3. Loading a Wave File
        4. Playing a Sound
      2. Testing DirectSound
        1. Creating the Project
        2. Modifying the “MyDirectX” Files
          1. MyDirectX.h Additions
          2. MyDirectX.cpp Additions
        3. Modifying MyGame.cpp
      3. What You Have Learned
      4. Review Questions
      5. On Your Own
    9. 12. Learning the Basics of 3D Rendering
      1. Introduction to 3D Programming
        1. The Key Components of 3D Programming
        2. The 3D Scene
          1. Introducing Vertices
          2. The Cartesian Coordinate System
          3. The Origin of Vertices
          4. Pay Careful Attention to the Camera!
        3. Moving to the Third Dimension
        4. Grabbing Hold of the 3D Pipeline
          1. Introducing Quads
        5. Vertex Buffers
          1. Creating a Vertex Buffer
          2. Filling the Vertex Buffer
        6. Rendering the Vertex Buffer
        7. Creating a Quad
          1. Drawing Triangles
          2. Creating a Quad
      2. The Textured Cube Demo
        1. MyGame.cpp
      3. What You Have Learned
      4. Review Questions
      5. On Your Own
    10. 13. Rendering 3D Model Files
      1. Creating and Rendering Stock Meshes
        1. Creating Stock Meshes
          1. Creating a Cube
          2. Creating a Sphere
          3. Creating a Cylinder
          4. Creating a Torus
          5. Creating a Teapot
        2. Rendering Stock Meshes
        3. The Stock_Mesh Program
      2. Loading and Rendering a Model File
        1. Loading an .X File
          1. Defining a New MODEL Structure
          2. Loading a Mesh
          3. Loading Materials and Textures
        2. Rendering a Complete Model
        3. Deleting a Model from Memory
        4. The Render_Mesh Program
          1. Modifying MyDirectX.h
          2. Modifying MyDirectX.cpp
          3. The Render_Mesh Source Code (MyGame.cpp)
      3. What You Have Learned
      4. Review Questions
      5. On Your Own
  8. III. Game Project
    1. 14. The Anti-Virus Game
      1. The Anti-Virus Game
        1. Playing the Game
          1. Firepower 1
          2. Firepower 2
          3. Firepower 3
          4. Firepower 4
          5. Firepower 5
          6. Charging the Nanobot
          7. Overloading the Weapons System!
        2. The Game’s Source Code
      2. What You Have Learned
      3. Review Questions
      4. On Your Own
  9. IV. Appendices
    1. A. Configuring Visual C++
      1. Setting Folders Manually
      2. Creating a New Project
      3. Changing the Character Set
    2. B. Resources for Further Study
      1. Game Development Websites
      2. Publishing, Game Reviews, and Download Sites
      3. Industry
      4. Computer Humor
      5. Recommended Books
        1. Advanced 2D Game Development
        2. Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach
        3. Programming an RTS Game with Direct3D
        4. Character Animation with Direct3D
        5. AI Techniques for Game Programming
        6. DarkBASIC Pro Game Programming, Second Edition
        7. Beginning C++ Game Programming
        8. C++ Programming for the Absolute Beginner, 2nd Edition
        9. Character Development and Storytelling for Games
        10. Game Design: The Art and Business of Creating Games
        11. High Score! The Illustrated History of Electronic Games
        12. Mathematics for Game Developers
        13. Programming Role Playing Games with DirectX, Second Edition
        14. Swords & Circuitry: A Designer’s Guide to Computer Role-Playing Games
    3. C. Chapter Quiz Answers
      1. Chapter 1
      2. Chapter 2
      3. Chapter 3
      4. Chapter 4
      5. Chapter 5
      6. Chapter 6
      7. Chapter 7
      8. Chapter 8
      9. Chapter 9
      10. Chapter 10
      11. Chapter 11
      12. Chapter 12
      13. Chapter 13
      14. Chapter 14
    4. D. Additional Examples
      1. Point Light Demo
      2. Black Wireframe Vertex Shader Demo
      3. Ambient-Diffuse Shader Demo