You are previewing Game Programming All in One, Third Edition.
O'Reilly logo
Game Programming All in One, Third Edition

Book Description

Game Programming All in One, Third Edition gives aspiring game programmers the skills that are needed to create professional-quality games. If you have a working knowledge of C or C++ and are ready to expand your skills into the field of game programming, then get ready to begin your journey with this latest edition! You won't cover the topic of programming in general, but rather the specifics of programming for games. Using the cross-platform Allegro game library, you'll learn how to write complete games that will run on almost any operating system. Both Windows® and Linux® screenshots are displayed throughout. Using the techniques taught within this book and the tools included on the CD-ROM, you'll be able to write standard Windows and DirectX® programs without the cost of an expensive compiler.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. What Is Allegro?
    2. Target Audience
    3. Massive Compiler Support!
    4. System Requirements
    5. Book Summary
  5. I. Cross-Platform Game Programming with the Allegro Game Library
    1. 1. Demystifying Game Development
      1. Introduction
        1. Practical Game Programming
        2. Goals Revisited
      2. The High-Level View of Game Development
      3. Recognizing Your Personal Motivations
        1. Decision Point: College versus Job
        2. Every Situation Is Unique
        3. A Note about Specialization
        4. Game Industry Speculation
        5. Emphasizing 2D
        6. Finding Your Niche
          1. Pocket Trivia Takes a Bow
          2. Perfect Match for the Fun of It
      4. Getting into the Spirit of Gaming
        1. Starship Battles: An Inspired Fan Game
          1. Overview of the Game
          2. Creating Game Graphics: The Hard Way
        2. Axis & Allies: Hobby Wargaming
          1. Overview of the Game
          2. Concluding the Game
        3. Setting Realistic Expectations for Yourself
      5. An Introduction to Allegro
        1. DirectX Is Just Another Game Library
        2. Introducing the Allegro Game Library
          1. Allegro's 2D and 3D Graphics Features
          2. Allegro's Sound Support Features
          3. Additional Allegro Features
        3. Supporting Multiple C++ Compilers
      6. Summary
      7. Chapter Quiz
    2. 2. Getting Started with the Allegro Game Library
      1. Allegro's Versatility Explained
      2. Configuring Your Favorite C++ Compiler
        1. Overview of the Compiler Configurations
        2. Windows Platform
          1. Microsoft Visual C++ 6.0
            1. Installing Allegro into Visual C++ 6.0
            2. Creating a Test Project for Dynamic Linking
            3. Creating a Test Project for Static Linking
          2. Microsoft Visual C++ 7.0–7.1 (2002–2003)
            1. Creating a Test Project
            2. Configuring the Project
          3. Microsoft Visual C++ 8.0 (2005)
            1. Creating a Test Project
            2. Configuring the Project
          4. Bloodshed Dev-C++ 5.0
            1. Creating a Test Project
            2. Configuring the Project
            3. Configuring the Project for Static Build
          5. Borland C++ 5.5 / C++ Builder
        3. Linux Platform
        4. Mac Platform
      3. Taking Allegro for a Spin
        1. Introducing Some of Allegro's Features
        2. Running the GetInfo Program
        3. Adding to the GetInfo Program
      4. Summary
      5. Chapter Quiz
    3. 3. 2D Vector Graphics Programming
      1. Your Ideal Game?
      2. Introduction
      3. Graphics Fundamentals
        1. The InitGraphics Program
          1. The InitGraphics Source Code
        2. The DrawBitmap Program
      4. Drawing Graphics Primitives
        1. Drawing Pixels
        2. Drawing Lines and Rectangles
          1. Horizontal Lines
          2. Vertical Lines
          3. Regular Lines
          4. Rectangles
          5. Filled Rectangles
          6. The Line Drawing Callback Function
        3. Drawing Circles and Ellipses
          1. Circles
          2. Filled Circles
          3. Ellipses
          4. Filled Ellipses
          5. Circle Drawing Callback Function
        4. Drawing Splines, Triangles, and Polygons
          1. Splines
          2. Triangles
          3. Polygons
        5. Filling In Regions
      5. Printing Text on the Screen
        1. Constant Text Output
        2. Variable Text Output
        3. Testing Text Output
      6. Fun with Math and Vectors
      7. Summary
      8. Chapter Quiz
    4. 4. Writing Your First Allegro Game
      1. Tank War
        1. Creating the Tanks
        2. Firing Weapons
        3. Tank Movement
        4. Collision Detection
        5. The Complete Tank War Source Code
          1. The Tank War Header File
          2. The Tank War Source File
        6. Summary
        7. Chapter Quiz
    5. 5. Getting Input from the Player
      1. Handling Keyboard Input
        1. The Keyboard Handler
        2. Detecting Key Presses
        3. The Stargate Program
        4. Buffered Keyboard Input
        5. Simulating Key Presses
        6. The KeyTest Program
      2. Handling Mouse Input
        1. The Mouse Handler
        2. Reading the Mouse Position
        3. Detecting Mouse Buttons
        4. Showing and Hiding the Mouse Pointer
        5. The Strategic Defense Game
        6. Setting the Mouse Position
        7. Limiting Mouse Movement and Speed
        8. Relative Mouse Motion
        9. Using a Mouse Wheel
      3. Handling Joystick Input
        1. The Joystick Handler
        2. Detecting Controller Stick Movement
          1. Reading the Axes
          2. Reading the Joystick Flags
        3. Detecting Controller Buttons
        4. Testing the Joystick Routines
          1. The ScanJoystick Program
          2. The Test Joystick Program
      4. Summary
      5. Chapter Quiz
    6. 6. Mastering the Audible Realm
      1. The PlayWave Program
      2. Sound Initialization Routines
        1. Detecting the Digital Sound Driver
        2. Reserving Voices
        3. Setting an Individual Voice Volume
        4. Initializing the Sound Driver
        5. Removing the Sound Driver
        6. Changing the Volume
      3. Standard Sample Playback Routines
        1. Loading a Sample File
        2. Playing and Stopping a Sample
        3. Altering a Sample's Properties
        4. Creating and Destroying Samples
      4. Low-Level Sample Playback Routines
        1. Allocating and Releasing Voices
        2. Starting and Stopping Playback
        3. Status and Priority
        4. Controlling the Playback Position
        5. Altering the Playback Mode
        6. Volume Control
        7. Pitch Control
        8. Panning Control
      5. The SampleMixer Program
      6. Playing Music
        1. Midi Basics
        2. Loading a Midi File
        3. Getting the Midi Length
        4. Playing a Midi File
        5. Example Program
      7. Summary
      8. Chapter Quiz
  6. II. Sprite Programming
    1. 7. Basic Bitmap Handling and Blitting
      1. Introduction
      2. Dealing with Bitmaps
        1. Creating Bitmaps
        2. Cleaning House
        3. Bitmap Information
        4. Acquiring and Releasing Bitmaps
        5. Bitmap Clipping
        6. Loading Bitmaps from Disk
          1. Reading a Bitmap File
          2. Saving Images to Disk
          3. Saving a Screenshot to Disk
      3. Blitting Functions
        1. Standard Blitting
        2. Scaled Blitting
        3. Masked Blitting
        4. Masked Scaled Blitting
      4. Enhancing Tank War—From Vectors to Bitmaps
      5. Summary
      6. Chapter Quiz
    2. 8. Introduction to Sprite Programming
      1. Basic Sprite Handling
        1. Drawing Regular Sprites
        2. Drawing Scaled Sprites
        3. Drawing Flipped Sprites
        4. Drawing Rotated Sprites
          1. Eight-Way Rotations
          2. Sixteen-Way Rotations
          3. Thirty-Two-Way Rotations
          4. The RotateSprite Program
          5. Additional Rotation Functions
        5. Drawing Pivoted Sprites
          1. The PivotSprite Program
          2. Additional Pivot Functions
        6. Drawing Translucent Sprites
      2. Enhancing Tank War
        1. What's New?
          1. New Tanks
          2. New Sprites
        2. Modifying the Source Code
          1. The Tank War Header File
          2. The Tank War Source Code File
      3. Summary
      4. Chapter Quiz
    3. 9. Sprite Animation
      1. Animated Sprites
        1. Drawing an Animated Sprite
        2. Creating a Sprite Handler
          1. The SpriteHandler Program
        3. Grabbing Sprite Frames from an Image
          1. The SpriteGrabber Program
        4. The Next Step: Multiple Animated Sprites
          1. The MultipleSprites Program
      2. Drawing Sprite Frames Directly
        1. The drawframe Function
        2. Testing the drawframe Function
      3. Enhancing Tank War
      4. Summary
      5. Chapter Quiz
    4. 10. Advanced Sprite Programming
      1. Compressed Sprites
        1. Creating and Destroying Compressed Sprites
        2. Drawing Compressed Sprites
        3. The RLESprites Program
      2. Compiled Sprites
        1. Using Compiled Sprites
        2. Testing Compiled Sprites
      3. Collision Detection
        1. The CollisionTest Program
      4. Wrapping Up the Sprite Code
        1. Sprite Definition
        2. Sprite Implementation
        3. Sprite Handler Definition
        4. Sprite Handler Implementation
        5. Testing the Sprite Classes
      5. Angular Velocity
      6. Summary
      7. Chapter Quiz
    5. 11. Programming the Perfect Game Loop
      1. Timers
        1. Installing and Removing the Timer
        2. Slowing Down the Program
        3. The TimerTest Program
      2. Interrupt Handlers
        1. Creating an Interrupt Handler
        2. Removing an Interrupt Handler
        3. The InterruptTest Program
      3. Using Timed Game Loops
        1. Slowing Down the Gameplay . . . Not the Game
        2. The TimedLoop Program
      4. Summary
      5. Chapter Quiz
  7. III. Scrolling Backgrounds
    1. 12. Programming Tile-Based Scrolling Backgrounds
      1. Introduction to Scrolling
        1. A Limited View of the World
      2. Introduction to Tile-Based Backgrounds
        1. Backgrounds and Scenery
        2. Creating Backgrounds from Tiles
        3. Tile-Based Scrolling
        4. Creating a Tile Map
      3. Enhancing Tank War
        1. Exploring the All-New Tank War
        2. Why Are the New Features Not Present?
        3. The New Tank War Source Code
          1. Header Definitions
          2. Bullet Functions
          3. Tank Functions
          4. Keyboard Input Functions
          5. Game Setup Functions
          6. Main Function
      4. Summary
      5. Chapter Quiz
    2. 13. Creating a Game World: Editing Tiles and Levels
      1. Creating the Game World
        1. Installing Mappy
        2. Creating a New Map
        3. Importing the Source Tiles
        4. Saving the Map File as FMP
        5. Saving the Map File as Text
      2. Loading and Drawing Mappy Level Files
        1. Using a Text Array Map
      3. Enhancing Tank War
        1. Description of New Improvements
        2. Modifying the Tank War Project
          1. Updating tankwar.h
          2. Updating tank.c
          3. Updating setup.c
          4. Updating bullet.c
          5. Updating main.c
        3. Future Changes to Tank War
      4. Summary
      5. Chapter Quiz
    3. 14. Loading Native Mappy Files
      1. Studying the Mappy Allegro Library (MappyAL)
        1. The MappyAL Library
          1. Loading a Mappy File
          2. Retrieving the Tile Number
          3. Drawing a Background Layer
          4. Drawing a Foreground Layer
      2. Loading a Native Mappy File
      3. Enhancing Tank War
        1. Proposed Changes to Tank War
        2. Modifying Tank War
          1. Modifying tankwar.h
          2. Modifying setup.c
          3. Modifying tank.c
          4. Modifying main.c
      4. Summary
      5. Chapter Quiz
    4. 15. Vertical Scrolling Arcade Games
      1. Building a Vertical Scroller Engine
        1. Creating Levels Using Mappy
        2. Filling in the Tiles
        3. Let's Scroll It
      2. Writing a Vertical Scrolling Shooter
        1. Describing the Game
        2. The Game's Artwork
        3. Writing the Source Code
          1. warbirds.h
          2. main.c
      3. Summary
      4. Chapter Quiz
    5. 16. Horizontal Scrolling Platform Games
      1. Understanding Horizontal Scrolling Games
      2. Developing a Platform Scroller
      3. Creating Horizontal Platform Levels with Mappy
        1. Separating the Foreground Tiles
        2. Performing a Range Block Edit
      4. Developing a Scrolling Platform Game
        1. Describing the Game
        2. The Game Artwork
        3. Using the Platform Scroller
        4. Writing the Source Code
      5. Summary
      6. Chapter Quiz
  8. IV. Taking it to the Next Level
    1. 17. The Importance of Game Design
      1. Game Design Basics
        1. Inspiration
        2. Game Feasibility
        3. Feature Glut
        4. Back Up Your Work
        5. Game Genres
          1. Fighting Games
          2. Action/Arcade Games
          3. Adventure Games
          4. First-Person Shooters
          5. Flight Simulators
          6. Galactic Conquest Games
          7. Real-Time Strategy Games
          8. Role-Playing Games
          9. Sports Simulation Games
          10. Third-Person Shooters
          11. Turn-Based Strategy Games
          12. Space Simulation Games
          13. Real-Life Games
          14. Massively Multiplayer Online Games
      2. Game Development Phases
        1. Initial Design
        2. Game Engine
        3. Prototype
        4. Game Development
        5. Quality Control
        6. Beta Testing
      3. Post-Production
        1. Official Release
        2. Out the Door or Out the Window?
        3. Managing the Game
        4. A Note about Quality
        5. Empowering the Engine
        6. Quality versus Trends
          1. Consider Future Trends
          2. Take Out the Guesswork
        7. Innovation versus Inspiration
        8. The Infamous Game Patch
        9. Expanding the Game
      4. Future-Proof Design
        1. Game Libraries
        2. Game Engines and SDKs
      5. What Is Game Design?
      6. The Design Document
      7. The Importance of Good Game Design
      8. The Two Types of Designs
        1. Mini Design
        2. Complete Design
      9. A Sample Design Doc
        1. General Overview
        2. Target System and Requirements
        3. Story
        4. Theme: Graphics and Sound
        5. Menus
        6. Playing a Game
        7. Characters and NPCs Description
        8. Artificial Intelligence Overview
        9. Conclusion
      10. A Sample Game Design: TREK
        1. User Interface
      11. Main Display
        1. The Galaxy
        2. Ship Movement
        3. Alien Races
        4. Combat and Damage
        5. Ship-to-Ship Combat
        6. Ship Classes
          1. Federation
          2. Klingon
          3. Romulan
        7. Ship Systems
      12. Game Design Mini-FAQ
      13. Summary
      14. Chapter Quiz
    2. 18. Using Datafiles to Store Game Resources
      1. Understanding Allegro Datafiles
      2. Creating Allegro Datafiles
      3. Using Allegro Datafiles
        1. Loading a Datafile
        2. Unloading a Datafile
        3. Loading a Datafile Object
        4. Unloading a Datafile Object
        5. Finding a Datafile Object
      4. Testing Allegro Datafiles
      5. Enhancing Tank War
        1. Modifying the Game
          1. Generating the Datafile Header
          2. Modifying tankwar.h
          3. Modifying setup.c
          4. Modifying bullet.c
          5. Modifying input.c
          6. Modifying main.c
        2. Final Comments about Tank War
      6. Summary
      7. Chapter Quiz
    3. 19. Playing Movies and Cut Scenes
      1. Playing FLI Animation Files
        1. The FLI Callback Function
        2. The PlayFlick Program
        3. Playing an FLI from a Memory Block
      2. Loading FLIs into Memory
        1. Opening and Closing FLI Files
        2. Processing Each Frame of the Animation
        3. The LoadFlick Program
        4. The ResizeFlick Program
      3. Summary
      4. Chapter Quiz
    4. 20. Introduction to Artificial Intelligence
      1. The Fields of Artificial Intelligence
        1. Expert Systems
        2. Fuzzy Logic
        3. Genetic Algorithms
        4. Neural Networks
      2. Deterministic Algorithms
        1. Random Motion
        2. Tracking
        3. Patterns
      3. Finite State Machines
      4. Revisiting Fuzzy Logic
        1. Fuzzy Logic Basics
        2. Fuzzy Matrices
      5. A Simple Method for Memory
      6. Artificial Intelligence and Games
      7. Summary
      8. Chapter Quiz
    5. 21. Multi-Threading
      1. Multi-Threading
        1. Abstracting the Parallel Processing Problem
        2. The pthreads-Win32 Library
        3. Programming POSIX Threads
          1. Creating a New Thread
          2. Killing a Thread
          3. Mutexes: Protecting Data from Threads
        4. The MultiThread Program
      2. Summary
      3. Chapter Quiz
    6. 22. Publishing Your Game
      1. Is Your Game Worth Publishing?
      2. Whose Door to Knock On
        1. Learn to Knock Correctly
        2. No Publisher, So Now What?
      3. Contracts
        1. Non-Disclosure Agreement
        2. The Actual Publishing Contract
      4. Milestones
        1. Bug Report
        2. Release Day
      5. Interviews
        1. Paul Urbanus: Urbonix, Inc.
        2. Niels Bauer: Niels Bauer Software Design
      6. Summary
      7. References
      8. Chapter Quiz
      9. Epilogue
        1. Reviewing the Final Version of Tank War
        2. What's Next?
  9. V. Appendices
    1. A. 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
      15. Chapter 15
      16. Chapter 16
      17. Chapter 17
      18. Chapter 18
      19. Chapter 19
      20. Chapter 20
      21. Chapter 21
      22. Chapter 22
    2. B. ASCII Table
    3. C. Numbering Systems: Binary, Decimal, and Hexadecimal
      1. Binary
      2. Decimal
      3. Hexadecimal
    4. D. Recommended Books and Websites
      1. All in One Support on the Web
      2. Game Development Websites
      3. Publishing, Game Reviews, and Download Sites
      4. Engines
      5. Independent Game Developers
      6. Industry
      7. Computer Humor
      8. Recommended Books