Book Image

Procedural Content Generation for C++ Game Development

By : Dale Green
Book Image

Procedural Content Generation for C++ Game Development

By: Dale Green

Overview of this book

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.
Table of Contents (19 chapters)
Procedural Content Generation for C++ Game Development
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

Computer games are a vast medium with dozens of genres that have developed over the past three to four decades. Games are bigger and more immersive than ever, and gamers' expectations have never been higher. While linear games, ones that have a set story and fixed progression, are still commonplace, more and more dynamic and open-ended games are being developed.

Advances in computer hardware and video game technologies are giving a much more literal meaning to the phrase "game world". Game maps are constantly increasing in size and flexibility, and it's thanks to technologies such as procedural generation that it's possible. Two gamers who buy the same game may have very different experiences as content is generated on the fly.

In this book, we're going to introduce ourselves to procedural generation, learning the skills needed to generate content on the fly to create dynamic and unpredictable game systems and mechanics.

Provided with this book is a game template for a rogue-like C++ game. When we get the project compiled and set up in Chapter 2, Project Setup and Breakdown, you'll see that it's currently just an empty shell. However, as we work our way through the book, you'll be introduced to the concepts behind procedurally generated content through real-world examples. We will then implement these examples in the empty project.

What this book covers

Chapter 1, An Introduction to Procedural Generation, introduces us to the vast topic that it procedural generation. I've always felt a crucial part of really learning something is understanding why it's done the way it is. Its great knowing how something is done, but knowing its origin and why it's the way it is creates a much more complete picture and a deeper understanding. In this chapter, we'll go right back to the birth of procedural generation and its journey into modern computer games.

Chapter 2, Project Setup and Breakdown, explains how to set up the provided rogue-like game project in your chosen IDE with detailed instructions for both Visual Studio and Code::Blocks. It's written in C++/SFML, and we'll be extending it throughout this book. We'll also cover common issues that you may face and run the project for the first time.

Chapter 3, Using RNG with C++ Data Types, explores random number generation (RNG), including the problems surrounding it and how we can use it with C++ data types to achieve random results during runtime. RNG lies at the heart of procedural generation and is how we emulate computers acting randomly and achieve dynamic results with our algorithms.

Chapter 4, Procedurally Populating Environments, helps us develop our level further by spawning items and enemies in random locations around the map. Procedurally generated environments is a staple in procedurally generated games, and spawning game objects at random locations is a big step toward achieving this.

Chapter 5, Creating Unique, Randomized Objects, explores ways in which we can create unique and randomized game objects. Certain items will be procedurally generated during runtime, which means that there will be a vast number of possible combinations. We'll cover the skills and techniques that were used to achieve this in the earlier chapters. We'll pull it all together and build a procedural system!

Chapter 6, Procedurally Generating Art, steps up our procedural efforts by moving away from the simple setting up of member variables randomly to the creation of procedural art and graphics. We'll procedurally create textures for our enemies and alter the level sprites to give each floor of our dungeon a unique feel.

Chapter 7, Procedurally Modifying Audio, looks at the nearest cousin of art, audio, using similar techniques to create variance in our sounds. We'll also use SFML's audio functions to create specialized 3D sound, bringing more depth to our levels.

Chapter 8, Procedural Behavior and Mechanics, uses everything that we've learned so far to create complex procedural behavior and mechanics in the form of pathfinding and unique level goals. We'll give our enemies the intelligence to traverse levels and chase the player. We'll also create unique level goals with unique rewards for the player to carry out.

Chapter 9, Procedural Dungeon Generation, finishes our work on the game project. We're going to implement what is perhaps the most icon feature of roguelike games: procedurally generated levels. All the way through the book, we've been working with the same fixed level. So, it's about time we started generating them procedurally! We'll also create some variance between levels and implement the goal generator that we created in the previous chapter.

Chapter 10, Component-Based Architecture, takes a look at component-based design, since our work on our template project is now complete. Procedural generation is all about flexibility. So, it makes sense that we'd want to work with the most flexible architecture that we can. Component-based architecture can achieve this, and having a good understanding of this design approach will help you progress and build larger systems in the future.

Chapter 11, Epilogue, takes a retrospective look at the project and the topics that we covered as we finish our procedural journey. For each area of procedural generation that we've used, we'll also identify some jumping-off points should you wish to explore the topic in depth.

What you need for this book

Throughout the course of writing this book, I used Visual Studio Community 2015 for Windows Desktop. It's a great IDE with all the tools that we need to create a C++ game for Windows. It's available for free from Microsoft, so I highly recommend that you download it and use it throughout the course of the book.

Don't worry if you've never used it before; we'll cover the project setup in detail so that you'll become accustomed to the parts of the IDE that we'll be using. I'll also provide the setup instructions for Code::Blocks. If you opt not to use an IDE, you'll need access to a C++ compiler so that you can run the project that we'll be working on in the book.

Who this book is for

This book is aimed at those who have knowledge of game development in C++ and are looking to incorporating procedural generation into their games. It will assume a fairly solid understanding of the fundamentals of programming, such as data types, return types, method calls, and so on. An understanding of the concepts behind game development is also assumed as we won't be looking at the underlying engine.

A game template is provided, and we'll use SFML to extend it throughout the course of the book. No prior experience with SFML is assumed. After completing the book, you will have a solid understanding of what procedurally generated content is, how it is used in games, and the collection of practical skills that will be applied to a real game.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " We called std::srand() and set a new seed, but each time we run the program, we're setting the same seed again "

A block of code is set as follows:

Stirng myStringLiteral = "hello";
string myString = { 'h', 'e', 'l', 'l', 'o', '\0' };

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

// If the enemy is dead remove it.
if (enemy.IsDead())
{
    enemyIterator = m_enemies.erase(enemyIterator);

    // If we have an active goal decrement killGoal.
    if (m_activeGoal)
    {
        --m_killGoal;
    }
}

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: " In Code::Blocks, add the following to the project's Build Options and Search Directories tab."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Extra Exercises

At the end of each chapter, there are a number of review questions and further exercises that can be completed. While not crucial to the book, it's advised that you complete them so that you can gauge your understanding of the topics covered and gain more experience.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/6713OT_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.