Procedural Content Generation for C++ Game Development

Book description

Get to know techniques and approaches to procedurally generate game content in C++ using Simple and Fast Multimedia Library

About This Book

  • This book contains a bespoke Simple and Fast Multimedia Library (SFML) game engine with complete online documentation
  • Through this book, you'll create games that are non-predictable and dynamic and have a high replayability factor
  • Get a breakdown of the key techniques and approaches applied to a real game.

Who This Book Is For

If you are a game developer who is familiar with C++ and is looking to create bigger and more dynamic games, then this book is for you. The book assumes some prior experience with C++, but any intermediate concepts are clarified in detail. No prior experience with SFML is required.

What You Will Learn

  • Discover the systems and ideology that lie at the heart of procedural systems
  • Use Random number generation (RNG) with C++ data types to create random but controlled results
  • Build levels procedurally with randomly located items and events
  • Create dynamic game objects at runtime
  • Construct games using a component-based approach
  • Assemble non-predictable game events and scenarios
  • Operate procedural generation to create dynamic content fast and easily
  • Generate game environments for endless replayability

In Detail

Procedural generation is a growing trend in game development. It allows developers to create games that are bigger and more dynamic, giving the games a higher level of replayability. Procedural generation isn't just one technique, it's a collection of techniques and approaches that are used together to create dynamic systems and objects. C++ is the industry-standard programming language to write computer games. It's at the heart of most engines, and is incredibly powerful. SFML is an easy-to-use, cross-platform, and open-source multimedia library. Access to computer hardware is broken into succinct modules, making it a great choice if you want to develop cross-platform games with ease.

Using C++ and SFML technologies, this book will guide you through the techniques and approaches used to generate content procedurally within game development.

Throughout the course of this book, we'll look at examples of these technologies, starting with setting up a roguelike project using the C++ template. We'll then move on to using RNG with C++ data types and randomly scattering objects within a game map. We will create simple console examples to implement in a real game by creating unique and randomised game items, dynamic sprites, and effects, and procedurally generating game events. Then we will walk you through generating random game maps. At the end, we will have a retrospective look at the project.

By the end of the book, not only will you have a solid understanding of procedural generation, but you'll also have a working roguelike game that you will have extended using the examples provided.

Style and approach

This is an easy-to-follow guide where each topic is explained clearly and thoroughly through the use of a bespoke example, then implemented in a real game project.

Table of contents

  1. Procedural Content Generation for C++ Game Development
    1. Table of Contents
    2. Procedural Content Generation for C++ Game Development
    3. Credits
    4. About the Author
    5. Acknowledgment
    6. About the Reviewer
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
        1. Extra Exercises
      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
    9. 1. An Introduction to Procedural Generation
      1. Procedural generation versus random generation
        1. Procedural generation
        2. Random generation
      2. Introducing randomness
        1. Pseudorandom number generation
        2. Why computers can't generate truly random numbers
        3. Generating random numbers in C++
        4. Generating random numbers within a range
      3. Seeds
        1. Defining seeds
        2. Using seeds
        3. Generating random seeds during the runtime
        4. Controlled randomness is the key to generating random numbers
      4. The use of procedural generation in games
        1. Saving space
        2. Map generation
      5. Texture creation
        1. Animation
        2. Sound
      6. Benefits of procedural generation
        1. Larger games can be created
        2. Procedural generation can be used to lower budgets
        3. An increase in gameplay variety
        4. An increase in replayability
      7. The drawbacks of procedural generation
        1. More taxing on the hardware
        2. Worlds can feel repetitive
        3. You sacrifice quality control
        4. You may generate an unplayable world
        5. It is hard to script set game events
      8. A brief history of rogue-like games
      9. How we'll implement procedural generation
        1. Populating environments
        2. Creating unique game objects
      10. Creating unique art
      11. Audio manipulation
        1. Behavior and mechanics
        2. Dungeon generation
      12. Component-based design
      13. The complete game
      14. Exercises
      15. Summary
    10. 2. Project Setup and Breakdown
      1. Choosing an IDE
        1. Microsoft Visual Studio
        2. Code::Blocks
        3. Other IDEs
        4. Build systems
      2. Breaking down the game template
        1. Download templates
        2. The class diagram
        3. The object hierarchy
        4. Level data
        5. Collision
        6. Input
      3. Simple and Fast Multimedia Library (SFML)
        1. Defining SFML
        2. Why we'll be using SFML
        3. Learning SFML
        4. Alternatives
      4. Polymorphism
        1. Inheritance
        2. Virtual functions
        3. Pure virtual functions
        4. Pointers and object slicing
      5. The roguelike template setup
        1. Downloading SFML
        2. Linking SFML
        3. Running the project
        4. Adding an item
        5. Updating and drawing
      6. Exercises
      7. Summary
    11. 3. Using RNG with C++ Data Types
      1. Setting the game seed
      2. Setting Boolean values randomly
        1. Generating a number between 0 and 1
        2. Choosing if an item spawns
      3. Random number distribution
      4. Giving the player random stats
      5. Accessing random elements of a collection
      6. Spawning a random item
      7. Generating random characters
      8. Repeating loops
      9. Spawning a random number of items
      10. Exercises
      11. Summary
    12. 4. Procedurally Populating Game Environments
      1. Potential obstacles
        1. Keeping within the bounds of a level
        2. Avoiding overlapping objects
        3. Creating meaningful levels
      2. Level tiles
      3. Defining the spawn area
        1. Calculating the level bounds
        2. Checking the underlying game grid
      4. Selecting a suitable game tile
        1. Randomly selecting a tile
        2. Checking whether a tile is suitable
        3. Converting to absolute position
      5. Spawning items at a random location
        1. Expanding the spawning system
        2. Using enumerators to denote an object type
        3. Optional parameters
        4. The complete spawn functions
        5. Updating the spawn code
        6. Randomly spawning enemies
      6. Spawning random tiles
        1. Adding a new game tile
        2. Choosing a random tile
        3. Implementing the SpawnRandomTiles function
      7. Exercises
      8. Summary
    13. 5. Creating Unique and Randomized Game Objects
      1. Creating a random player character
        1. Choosing a player class
        2. An overview of sprites and textures
        3. Setting an appropriate sprite
        4. Buffing the player stats
        5. Random character traits
        6. Returning the player traits array
        7. Setting trait sprites
      2. Procedurally generating an enemy class
      3. Procedural items
        1. Random Gem and Heart classes
        2. Random gold class
      4. The random potion class
        1. Creating a random potion
        2. Determining potion pickups
      5. Exercises
      6. Summary
    14. 6. Procedurally Generating Art
      1. How procedural generation is used with art
        1. Using sprite effects and modifiers
        2. Combining multiple textures
        3. Creating textures from scratch
        4. Creating complex animations
      2. The benefits of procedurally generated art
        1. Versatility
        2. Cheap to produce
        3. It requires little storage
      3. The drawbacks of procedurally generated art
        1. Lack of control
        2. Repeatability
        3. Performance heavy
      4. Using SFML sprite modifiers
        1. How colors work in SFML
        2. Creating sprites of a random color
          1. Selecting a preset color at random
          2. Generating a color at random
        3. Creating sprites of a random size
      5. Saving modified sprites
        1. Passing a texture into an image
        2. Drawing to a RenderTexture class
        3. Saving an image to a file
      6. Creating enemy sprites procedurally
        1. Breaking sprites into components
        2. The draw setup
        3. Randomly selecting sprite components
        4. Loading the default armor textures
        5. Choosing the armor tier
        6. Rendering the armor textures
        7. Rendering the final textures
        8. Overriding the default draw behavior
        9. Debugging and testing
      7. Editing the game tiles
      8. Exercises
      9. Summary
    15. 7. Procedurally Modifying Audio
      1. An introduction to SFML audio
        1. sf::Sound versus sf::Music
        2. sf::SoundBuffer
      2. Selecting a random main track
      3. Adding sound effects
      4. Editing sound effects
      5. Playing a sound function
        1. The audio listener
        2. Creating a fluctuation in a pitch
      6. 3D sound – spatialization
        1. The audio listener
        2. The minimum distance
        3. Attenuation
        4. The position of the sound
          1. Fixed positions
          2. Moving positions
      7. Exercises
      8. Summary
    16. 8. Procedural Behavior and Mechanics
      1. An introduction to pathfinding
        1. What is a pathfinding algorithm?
        2. Dijkstra's algorithm
          1. The A* algorithm
      2. A breakdown of A*
        1. Representing a level as nodes
        2. The open and closed list
        3. The H, G, and F costs
          1. The H value
          2. The G value
          3. The F value
        4. The Manhattan distance
        5. Parenting nodes
        6. The pseudo-algorithm
      3. Coding the A* pathfinding algorithm
        1. The Tile datatype
        2. Creating supporting functions
          1. The Level class
          2. The Enemy class
        3. Variable declarations
        4. Precalculating the H values
        5. Defining the main loop
        6. Finding the adjacent nodes
        7. Calculating the G and F costs
          1. Calculating the G and F cost
        8. Checking for superior paths
        9. Creating the final path
      4. Implementing A* in the game
        1. Enabling the enemy to follow a path
        2. Calling the pathfinding behavior
        3. Viewing our path
      5. Procedurally generated level goals
        1. The variable and function declarations
        2. Generating a random goal
        3. Checking whether a goal is complete
        4. Drawing the goal on the screen
      6. Exercises
      7. Summary
    17. 9. Procedural Dungeon Generation
      1. The benefits of procedural level design
        1. Replayability
        2. A reduction in development time
        3. Larger game worlds
      2. Considerations
        1. A lack of control
        2. Required computing power
        3. Suitability
      3. An overview of dungeon generation overview
        1. Generating rooms
        2. Generating a maze
        3. Connecting rooms and mazes
      4. The recursive backtracker
      5. Procedurally generating a dungeon
        1. Changing how we view the maze
        2. Updating the Game and Level classes
        3. Generating a maze
          1. Preparing before the generation of a maze
          2. Carving passages
        4. Adding rooms
      6. Choosing the tile textures
        1. The if/else approach
        2. Bitwise tile maps
        3. Calculating the tile values
        4. Mapping the tile value to textures
        5. Calculating tile textures
        6. Creating unique floor themes
        7. Adding entry and exit points
        8. Setting a player's spawn location
        9. Undoing the debug changes
      7. Exercises
      8. Summary
    18. 10. Component-Based Architecture
      1. Understanding component-based architecture
        1. Problems with a traditional inheritance-based approach
          1. Convoluted inheritance structures
          2. Circular dependencies
        2. Benefits of component-based architecture
          1. Avoiding complex inheritance structures
          2. Code is broken into highly reusable chunks
          3. Highly maintainable and scalable
        3. The disadvantages of component-based architecture
          1. Code can become too fragmented
          2. Unnecessary overhead
          3. Complex to use
        4. An overview
      2. Designing the component system
      3. C++ templates
        1. Using templates
        2. Template declarations
        3. Using templates
        4. Template specialization
      4. Function overloading
      5. Creating a base component
      6. Component functions
        1. Attaching a component
        2. Retuning a component
      7. Creating a transform component
        1. Encapsulating transform behavior
        2. Adding a transform component to the player
        3. Using the transform component
        4. Updating the game code
      8. Creating a SpriteComponent
        1. Encapsulating sprite behavior
        2. Adding a sprite component to the player class
        3. The updated drawing pipeline
        4. Updating the game code
      9. Creating an audio component
        1. Defining the behavior of an audio component
        2. Adding an audio component to the player class
        3. Using the audio component
      10. Exercises
      11. Summary
    19. 11. Epilogue
      1. Project breakdown
        1. Procedurally populating environments
        2. Creating unique and random game objects
        3. Procedurally generating art
        4. Procedurally modifying audio
        5. Procedural behavior and mechanics
        6. Procedural dungeon generation
        7. Component-based architecture
      2. The pros and cons of procedural generation
        1. Pros
        2. Cons
      3. Summary
    20. Index

Product information

  • Title: Procedural Content Generation for C++ Game Development
  • Author(s): Dale Green
  • Release date: January 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781785886713