Skip to main content
Top

2014 | Book

Learn C++ for Game Development

insite
SEARCH

About this book

If you’re new to C++ but understand some basic programming, then Learn C++ for Game Development lays the foundation for the C++ language and API that you’ll need to build game apps and applications.

Learn C++ for Game Development will show you how to:

Master C++ features such as variables, pointers, flow controls, functions, I/O, classes, exceptions, templates, and the Standard Template Library (STL) Use design patterns to simplify your coding and make more powerful gamesManage memory efficiently to get the most out of your creativityLoad and save games using file I/O, so that your users are never disappointed

Most of today's popular console and PC game platforms use C++ in their SDKs. Even the Android NDK and now the iOS SDK allow for C++; so C++ is growing in use for today's mobile game apps. Game apps using C++ become much more robust, better looking, more dynamic, and better performing. After reading this book, you’ll have the skills to become a successful and profitable game app or applications developer in today’s increasingly competitive indie game marketplace.

The next stage is to take the foundation from this book and explore SDKs such as Android/Ouya, PlayStation, Wii, Nintendo DS, DirectX, Unity3D, and GameMaker Studio to make your career really take off.

Table of Contents

Frontmatter
Chapter 1. Beginning C++
Abstract
The C++ programming language was designed by Bjarne Stroustrup at Bell Labs, beginning in 1979. The goal of the language was to provide useful features from other languages of the time, such as Simula, with faster runtime performance. C was chosen as the basis for this new language due to its execution speed, portability, and wide adoption.
Bruce Sutherland

Procedural Programming

Frontmatter
Chapter 2. Writing a Guessing Game with C++ Types
Abstract
You will begin the process of learning C++ by learning about the different built-in types that the language supports. C++ has built-in support for integers, floating point numbers, booleans, and enumerators. This chapter covers all of these types. We also look at the different types of operators that can be used to manipulate and compare these types of values. It is useful to understand how C++ handles variables before we begin to create programs in the language. To start, we take a look at the difference between dynamically and statically typed languages, which includes how we declare and define variables in C++.
Bruce Sutherland
Chapter 3. Creating Calculators with Operators
Abstract
C++ provides operators that allow you to express all sorts of mathematical formulae in computer programs. You have access to operators that allow us to carry out algebra, compare values, and even manipulate the individual bit pattern of a variable’s value directly. This chapter is going to cover these C++ operators and end by having you create a simple command line calculator.
Bruce Sutherland
Chapter 4. Beginning C++ Game Development with Arrays
Abstract
We will begin to create our first C++ game in this chapter. So far this book has looked at the various types supplied by the C++ programming language as well as the operators that can be used to alter and compare these types. The next major area of the C++ programming language that you should understand to begin constructing complex programs is data storage. The individual types in C++, such as int and char, can hold single values, but often we will want to be able to remember multiple values all relating to similar things. We could achieve this by creating a unique variable for every object with a value that we wish to remember; however, C++ provides us with arrays to store multiple values together. This chapter takes a look at how arrays are used in C++ and also explores how we can use arrays to begin our text adventure game.
Bruce Sutherland
Chapter 5. Functions, the Building Blocks of C++
Abstract
Modern games consist of hundreds of thousands of lines of code and can be worked on by teams consisting of hundreds of people. C++ provides the ability to separate code into blocks called functions to allow us to build more modular programs.
Bruce Sutherland
Chapter 6. Making Decisions with Flow Control
Abstract
The programs that we have been writing until now have been very linear. That is, they begin with the first line in main and carry on executing each line at a time until the program finishes with the return statement on the last line of main. Sometimes we might decide to carry out different code paths based on values we have stored in variables or repeat a section of code until some condition is satisfied.
Bruce Sutherland
Chapter 7. Organizing Projects Using Files and Namespaces
Abstract
In Chapter 6 we ended with a file that contains functions and structures that were related to completely different high-level concepts. Some were there to control the game loop, others to obtain user input and write output, and a structure to represent the player model. Eventually this code would become difficult to manage and work with if we were to continue down this path of adding everything to a single file.
Bruce Sutherland

Object-Oriented Programming

Frontmatter
Chapter 8. Object-Oriented Programming with Classes
Abstract
The preceding part of this book covered a programming paradigm known as procedural programming. This part introduces the concept of object-oriented programming (OOP). Procedural programming was the major paradigm supported by the C programming language. The C++ programming language was conceived to support OOP in the same syntax as a C programming language.
Bruce Sutherland
Chapter 9. Controlling Data with Access Modifiers
Abstract
C++ allows you to control how data in variables can be accessed by using different access modifiers. There are modifiers that tell the compiler that a variable should be shared among classes or that the program cannot change the value of the variable after it has been initialized. Unfortunately some of the keywords supplied by C++ are reused and mean different things in different circumstances. I’ll cover the use of the static, const, mutable, and friend keywords in C++, and their multiple uses in this chapter.
Bruce Sutherland
Chapter 10. Building Games with Inheritance
Abstract
Being a productive video game developer involves writing well-organized code that can be reused as often as possible. C++ classes provide a feature named inheritance that helps us achieve both of these goals. Inheritance allows us to create generalized base classes that can be derived from to create classes with a more specific purpose. In this chapter you learn how this allows us to share behavior among classes.
Bruce Sutherland
Chapter 11. Designing Game Code with Polymorphism
Abstract
You saw how inheritance can be used to design reusable code in the last chapter. In this chapter you will learn how the power of inheritance can be unlocked in much more useful ways through the power of polymorphism. C++ supports the ability to access classes of different types through pointers to a base class, known as polymorphism. The program can determine the type of the object at runtime and call the correct method. The mechanism supplied by C++ to tell the compiler that a method can be different in the base class to any derived classes is the virtual method.
Bruce Sutherland
Chapter 12. Copying and Assigning Data to Objects
Abstract
You have seen how we can pass data into functions by value, by reference, or by pointer. If you choose to pass a class object into a function by value, C++ will make a copy of the class. This copy operation happens automatically or implicitly. This chapter is going to show you some of these implicit operations that can occur when using classes in your programs and the ways in which you can alter and manage these implicit operations.
Bruce Sutherland

The STL

Frontmatter
Chapter 13. The STL String Class
Abstract
The next few chapters of this book are going to introduce you to the Standard Template Library (STL). The STL contains classes that you can use to manipulate strings, store objects, and access them using several different patterns. The STL also provides iterators to traverse collections and algorithms for carrying out different types of operations on your data sets. You have already been using the STL string class throughout this book. This chapter looks at the different methods that the STL provides for working with classes and points out some of the reasons why using this class is more desirable than dealing with raw C strings.
Bruce Sutherland
Chapter 14. STL Array and Vector
Abstract
You saw in Chapter 13 that the STL provides different types of string classes by specializing a template to create different implementations. This is where the template part of the STL comes from. The STL string classes are what we call containers, because they store string data. In this chapter you will see two more of the STL’s containers in the array and vector templates.
Bruce Sutherland
Chapter 15. STL List
Abstract
The STL list template is another linear data collection. The previous chapter briefly mentioned the difference between array, vector, and list, and this chapter goes into more detail about why and when you would use these different collections. The main point to remember is that you should always use the correct tool for the job at hand. Video games rely on extracting the most performance possible from the target hardware, and access times to data in collections is a major bottleneck with modern processors. This chapter shows you how to build your own list class in an effort to help you understand the difference between arrays and linked lists.
Bruce Sutherland
Chapter 16. STL’s Associative Containers
Abstract
All of the STL containers you have seen so far have stored individual data elements. In this chapter you will be introduced to associative containers. These containers associate a key with the data values that you are inserting. In the case of the map and unordered_map containers, you are required to supply a value to use as the key, whereas the set and unordered_set containers automatically generate keys from the data you supply. I'll explain the differences between each of these containers as I work through some examples in this chapter, beginningwith the set container.
Bruce Sutherland
Chapter 17. STL’s Stack and Queue
Abstract
The STL containers you have seen so far have all provided options for storing and retrieving values and are suited to different circumstances. The stack and queue containers are designed for different reasons. Although they do store values, their behavior is much more important. I'll show you exactly how these containers behave in this chapter.
Bruce Sutherland
Chapter 18. STL’s bitset
Abstract
The last STL container type object that I cover is the bitset. I say STL type because the bitset is not strictly a container. It does not support iterators, the range-based for loops, or STL's algorithms. A bitset is used for tracking individual boolean values in a combined set. A traditional method for achieving the same result are flags implemented using bitwise operators and shifts.
Bruce Sutherland
Chapter 19. Using the STL in Text Adventure
Abstract
So far you have seen how a game can be built using classes, member variables, and C-style arrays. In this chapter you will see how you can use some of the STL classes covered in this part of the book to simplify the code you have been creating. This chapter uses polymorphism to store pointers to base classes in our STL containers. If you need a recap on polymorphism and how it is used, you should read over Chapter 11.
Bruce Sutherland

Generic Programming

Frontmatter
Chapter 20. Template Programming
Abstract
Parts 1 and 2 of this book introduced you to concepts and features of the C++ programming language that allow the procedural programming paradigm and the object-oriented programming paradigm. C++ provides functions and control flow statements that allow you to break your programs down into reusable blocks and it provides classes that allow you to encapsulate your data and bundle it with methods that act on that data. C++ also supports a third programming paradigm, generic programming.
Bruce Sutherland
Chapter 21. Practical Template Programming
Abstract
This chapter introduces you to some practical template examples so you can see how they can be deployed in your game development projects. You’ll see how to implement a Singleton class that will allow you to create objects that can be accessed from anywhere in your code base. The Singleton pattern also ensures that you can only create a single instance of those classes.
Bruce Sutherland

C++ Game Programming

Frontmatter
Chapter 22. Managing Memory for Game Developers
Abstract
This chapter is the first in the last part of this book, which will cover general game development topics that are important for new programmers to attempt to grasp. The first of these chapters is going to introduce you to the C++ memory model and the implications of using different types of memory.
Bruce Sutherland
Chapter 23. Useful Design Patterns for Game Development
Abstract
Design patters are like blueprints for your code. They are systems you can use to complete tasks that are very similar in nature that arise while you are developing games. Just as STL data structures are reusable collections that can be used when needed to solve specific problems, design patterns can be utilized to solve logical problems in your code.
Bruce Sutherland
Chapter 24. Using File IO to Save and Load Games
Abstract
Saving and loading game progress is a standard feature of all but the most basic games today. This means that you will need to know how to handle the loading and saving of game objects. This chapter covers one possible strategy for writing out the data you will need to be able to reinstate a player’s game.
Bruce Sutherland
Chapter 25. Speeding Up Games with Concurrent Programming
Abstract
Processor manufacturers have hit a ceiling on the number of cycles their CPUs can execute per second. This can be seen with modern CPUs in desktop computers, tablets, and mobile phones where CPU speeds are rarely seen over the 2.5 Ghz mark.
Bruce Sutherland
Chapter 26. Supporting Multiple Platforms in C++
Abstract
There will come a time in your game development career when you have to write code that will only work on a single platform. That code will have to be compiled out of other platforms. You will more than likely also have to find alternative implementations for each of the platforms you will be working on. Classic examples of such code can usually be found in interactions between your game and online login and microtransaction solutions such as Game Center, Google+, Xbox Live, PlayStation Network, and Steam.
Bruce Sutherland
Chapter 27. Wrapping Up
Abstract
The C++ programming language is a tool that will serve you well while trying to build video games. It provides low-level access to processors, which allows you to write efficient code for a wide variety of computer processors.
Bruce Sutherland
Backmatter
Metadata
Title
Learn C++ for Game Development
Author
Bruce Sutherland
Copyright Year
2014
Publisher
Apress
Electronic ISBN
978-1-4302-6458-3
Print ISBN
978-1-4302-6457-6
DOI
https://doi.org/10.1007/978-1-4302-6458-3

Premium Partner