O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game Development Patterns and Best Practices

Book Description

Utilize proven solutions to solve common problems in game development

About This Book

  • Untangle your game development workflow, make cleaner code, and create structurally solid games
  • Implement key programming patterns that will enable you to make efficient AI and remove duplication
  • Optimize your game using memory management techniques

Who This Book Is For

If you are a game developer who wants to solve commonly-encountered issues or have some way to communicate to other developers in a standardized format, then this book is for you. Knowledge of basic game programming principles and C++ programming is assumed.

What You Will Learn

  • Learn what design patterns are and why you would want to use them
  • Reduce the maintenance burden with well-tested, cleaner code
  • Employ the singleton pattern effectively to reduce your compiler workload
  • Use the factory pattern to help you create different objects with the same creation logic and reduce coding time
  • Improve game performance with Object Pools
  • Allow game play to interact with physics or graphics in an abstract way
  • Refractor your code to remove common code smells

In Detail

You’ve learned how to program, and you’ve probably created some simple games at some point, but now you want to build larger projects and find out how to resolve your problems. So instead of a coder, you might now want to think like a game developer or software engineer. To organize your code well, you need certain tools to do so, and that’s what this book is all about.

You will learn techniques to code quickly and correctly, while ensuring your code is modular and easily understandable.

To begin, we will start with the core game programming patterns, but not the usual way. We will take the use case strategy with this book. We will take an AAA standard game and show you the hurdles at multiple stages of development. Similarly, various use cases are used to showcase other patterns such as the adapter pattern, prototype pattern, flyweight pattern, and observer pattern. Lastly, we’ll go over some tips and tricks on how to refactor your code to remove common code smells and make it easier for others to work with you. By the end of the book you will be proficient in using the most popular and frequently used patterns with the best practices.

Style and approach

This book takes a step-by-step real-life case studies approach. Every pattern is first explained using a bottleneck. We will show you a problem in your everyday workflow, and then introduce you to the pattern, and show you how the pattern will resolve the situation.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Introduction to Design Patterns
    1. Chapter overview
    2. Your objective
      1. What are design patterns
    3. Why you should plan for change
      1. Understanding UML class diagrams
      2. Relationships between classes
        1. Inheritance
        2. Aggregation
        3. Composition
      3. Implements
    4. Separating the why and the how
      1. Understanding the separation of concerns
    5. An Introduction to interfaces
      1. Polymorphism refresher
      2. Understanding interfaces
    6. The advantages of compartmentalizing code
      1. The structure of the Mach5 engine
        1. Mach5 core engines and systems
          1. The app
          2. The StageManager
          3. The ObjectManager
          4. The graphics engine
          5. Tools and utilities
    7. The problems with using design patterns in games
    8. Setting up the project
    9. Summary
  3. One Instance to Rule Them All - Singletons
    1. Chapter overview
    2. Your objective
    3. An overview on class access specifiers
    4. The static keyword
      1. Static keyword inside a function
      2. Static keyword in class definitions
      3. Static as a file global variable
    5. Pros and cons of global variables
    6. What is a Singleton?
      1. Keeping the single in Singleton
      2. Deleting our object correctly
    7. Learning about templates
    8. Templatizing Singletons
    9. Advantages/disadvantages of using only one instance
    10. The Singleton in action - the Application class
    11. Summary
  4. Creating Flexibility with the Component Object Model
    1. Chapter overview
      1. Your objectives
    2. Why a monolithic game object is a bad design
      1. The monolithic game object
      2. The problem with object behavior
      3. The benefits of the monolithic game object
    3. Why inheritance hierarchies are inflexible
      1. Organizing the code by what it does, not what it is
      2. Avoiding the Diamond of Death
    4. The Strategy pattern and the Decorator pattern
      1. The Strategy pattern explained
      2. The Decorator pattern explained
    5. The Component Object Model explained
      1. Implementing the Component Object Model
      2. Implementing components
      3. Creating and removing objects and components
    6. Performance concerns
    7. Summary
  5. Artificial Intelligence Using the State Pattern
    1. Chapter overview
      1. Your objectives
        1. The State pattern explained
        2. Introduction to State Machines
        3. An overview of enumerations
        4. Acting on states
          1. Issues with conditionals
      2. Expanding on the State Machine
      3. The State pattern in action - the M5StateMachine class
      4. The State pattern in action - StageManager
      5. Issues with FSMs
    2. Summary
  6. Decoupling Code via the Factory Method Pattern
    1. Chapter overview
    2. Your objective
      1. The trouble with switch statements
    3. The Dependency Inversion Principle
    4. The Factory method pattern
      1. The Static Factory
        1. The Dynamic Factory
      2. Creating our Stage Builders
      3. The template builder
      4. Creating the Dynamic Factory class
      5. Using the Dynamic Factory
    5. Creating a component and Object Factory
      1. The Templated Factory
    6. Architecting versus over-architecting
    7. Summary
  7. Creating Objects with the Prototype Pattern
    1. Your objectives
      1. The trouble with using a factory for game objects
      2. Using builders with objects
      3. Solution - reading from files
    2. The Prototype pattern explained
      1. The virtual constructor
      2. The problem with constructors
      3. The benefits of a virtual constructor
      4. We don't need to know the type
      5. No need to subclass
      6. It's easy to make exact copies
    3. Examples of the clone method in Mach5
      1. The Gfx and collider components
      2. Cloning an object
      3. Choosing a copy constructor
      4. Covariant return types
    4. Loading archetypes from a file
      1. Archetype files
      2. The object manager
    5. Summary
  8. Improving Performance with Object Pools
    1. Chapter overview
      1. Your objectives
    2. Why you should care about memory
    3. The Object Pool pattern explained
    4. Implementing a basic object pool
    5. Operator overloading in C++
    6. Building the object pool for Mach5
    7. Issues with object pools
    8. Summary
  9. Controlling the UI via the Command Pattern
    1. Chapter overview
      1. Your objectives
    2. How can we control actions through buttons?
      1. Callback functions
      2. Repeated code in the component
    3. The Command pattern explained
      1. Two parameters and beyond
      2. Pointers to member functions
      3. Pointer to member command
    4. The benefits of the command pattern
      1. Treating a function call like an object
      2. Physically decoupling the client and the function call
      3. Temporal decoupling
      4. Undo and redo
    5. Easy UI with commands in Mach5
      1. Using commands
    6. Summary
  10. Decoupling Gameplay via the Observer Pattern
    1. Chapter overview
      1. Your objectives
    2. How gameplay creeps into every system
      1. Hardcoding requirements
      2. Polling
    3. The Observer pattern explained
      1. The Subject and Observer
      2. The Player
      3. The Observers
      4. Push versus Pull
    4. Benefits of using the Observer pattern
      1. Problems using the Observer pattern
        1. Dangling references
        2. Overuse
        3. Implementing interfaces
        4. When to notify
    5. Summary
  11. Sharing Objects with the Flyweight Pattern
    1. Chapter overview
      1. Your objectives
    2. Introductions to particles
    3. Implementing particles in Mach5
    4. Why memory is still an issue
    5. Introduction to the Flyweight pattern
    6. Transitioning to ParticleSystems
      1. Creating different system types
        1. Developing the ParticleFactory
        2. Using the ParticleFactory
    7. Summary
  12. Understanding Graphics and Animation
    1. Chapter overview
      1. Your objectives
    2. Introduction to monitor refresh rates
      1. What is a pixel?
      2. The horizontal and vertical blank
      3. Refresh rate
    3. Double buffering
      1. The back buffer
      2. VSync
    4. Triple buffering
    5. LCD monitors
    6. Time-based movement and animation
      1. Frame-based movement
      2. Time-based movement
    7. Summary
  13. Best Practices
    1. Chapter overview
      1. Your objectives
    2. Learning fundamental code quality techniques
      1. Avoid magic numbers
      2. White space
        1. Indentation
        2. Blank lines and spaces
      3. Comments and self-documenting code
        1. Commenting
    3. Learning and understand the uses of the const keyword
      1. Const function parameters
      2. Const classes as parameters
        1. Const member functions
      3. Problems with const
    4. Learning how iteration can improve your game and code design
      1. The game development cycle
        1. Production phase
          1. Prototyping
          2. Playtesting
          3. Conducting a playtest
          4. Iteration
      2. Meeting milestones
    5. Learning when to use scripting in a game
      1. Introduction to assembly
      2. Moving to higher-level programming languages
      3. Introducing the compiler
      4. Introduction to scripting languages
        1. Using interpreters
        2. Just in time compilation
      5. Why use a scripting language?
      6. When to use C++
      7. Compiled versus scripting
    6. Summary