Learn C++ for Game Development Bruce Sutherland Apress"
Contents About the Author About the Technical Reviewer Acknowledgments Introduction xv xvii xix xxi Chapter 1: Beginning C++ 1 Compilers 2 Programming Paradigms 2 Procedural Programming 2 Object-Oriented Programming 2 Generic Programming 3 C++ Game Programming 3 Our First C++ Program 3 Summary 4 Part 1: Procedural Programming 5 Chapter 2: Writing a Guessing Game with C++ Types 7 Dynamic Versus Static Typed Languages 7 V
vi Contents Declaring Variables 7 Defining Variables 8 Integers 8 Unsigned Integers 9 Two's Complement 9 Floating Point Numbers 10 Boolean Values 11 Enums 11 Switching from One Type to Another 13 static_cast 13 A Simple Guessing Game 14 Summary 16 Chapter 3: Creating Calculators with Operators 17 The Assignment Operator 17 Arithmetic Operators 18 The Addition Operator 18 The Subtraction Operator 18 The Multiplication and Division Operators 19 The Modulo Operator 19 A Simple Arithmetic Calculator 20 Relational Operators 22 Equality Operators 22 Greater-Than Operators 23 Less-Than Operators 23 Simple Comparison Calculators 23 Bitwise Operators 27 Hexadecimal Number Representation 27 The Binary & (AND) Operator 28 The Binary I (OR) Operator 30 The Binary A (Exclusive OR) Operator 30
The Left Shift («) Operator 31 The Right Shift (») Operator 32 Logical Operators 32 The && Operator 32 The II Operator 32 Unary Operators 33 Arithmetic Unary Operators 33 The Logical Not Unary Operator 34 The One's Complement Operator 34 Summary 35 Chapter 4: Beginning C++ Game Development with Arrays 37 The C++ Array 37 Pointers 39 Pointer Arithmetic 41 Dereferencing Pointers 41 Pointers and Arrays 41 C Style Strings in Arrays 42 Working with C Style Strings 44 Text Adventure Game 46 Summary 47 Chapter 5: Functions, the Building Blocks of C++ 49 Writing Our First Function 49 Passing Parameters to Functions 50 Return Values 51 Passing by Pointer 51 Passing by Reference 52 Structures 53 Adding Functions to Text Adventure 54 Summary 55
viii Contents Chapter 6: Making Decisions with Flow Control 57 The if Statement 57 The else and else if Statements 58 The for Loop 60 The while Loop 61 The switch Statement 62 The break and continue Keywords 64 The goto Statement 65 Adding a Game Loop to Text Adventure 66 Summary 69 Chapter 7: Organizing Projects Using Files and Namespaces 71 Source and Header Files 71 Creating Namespaces 73 Updating Text Adventure with Source Files, Header Files, and Namespaces 74 Summary 77 Part 2: Object-Oriented Programming 79 Chapter 8: Object-Oriented Programming with Classes 81 Object-Oriented Programming 81 Encapsulation 82 Constructors and Destructors 85 Method Overloading 88 Operator Overloading 90 Updating Text Adventure to Use Classes 92 Summary 95 Chapter 9: Controlling Data with Access Modifiers 97 The static Keyword 97 Creating static Local Variables 98 Using static class Member Variables 98
Using static Member Methods 101 Using static to Alter Global Scope 102 The const Keyword 103 Constant Variables 104 Constant Pointers 105 Constant Parameters 105 Constant Member Methods 105 Two More Keywords 107 The inline Keyword 107 The friend Keyword 108 Summary 109 Chapter 10: Building Games with Inheritance 111 Inheriting from a Base Class 111 Constructors and Destructors in Derived Classes 114 Method Overriding 117 Updating Text Adventure 117 Creating an Entity Class 117 Adding Inheritance to the Player Class 118 Adding Rooms 118 Moving Through Rooms 123 Summary 126 Chapter 11: Designing Game Code with Polymorphism 127 Virtual Methods 127 Downcasting and Upcasting with dynamic_cast 129 Creating Interfaces with Pure Virtual Methods... 130 Using Polymorphism in Text Adventure 131 Summary 137
x Contents Chapter 12: Copying and Assigning Data to Objects 139 Copy Constructors 139 Assignment Operators 141 Move Semantics 142 Summary 143 Part 3: The STL 145 Chapter 13: The STL String Class 147 Standard string and basic_string 147 Constructing Strings 148 Working with Strings 149 Accessing String Data Through Iterators 150 Searching Within Strings 152 Formatting Data with stringstream 153 Summary 155 Chapter 14: STL Array and Vector 157 The STL Array Template 157 The STL Vector Class 159 Sorting Arrays and Vectors 160 Summary 161 Chapter 15: STL List 163 Understanding Array and List Memory Layouts 163 Building a List Class 164 The STL List Template 166 Summary 167
Chapter 16: STL's Associative Containers 169 The STL set Container 169 The STL map Container 170 Binary Search Trees 171 Fast Data Access Using a Hash Map 173 STL unordered set and unordered map 174 Summary 176 Chapter 17: STL's Stack and Queue 177 The STL stack Container 177 The STL queue Container 178 Summary 179 Chapter 18: STL's bitset 181 Creating bitset Objects 181 Working with bitsets 182 Summary 184 Chapter 19: Using the STL in Text Adventure 185 Using STL array to Store Room Pointers 185 Using a vector and a map to Store Options 186 Adding Gameplay to Text Adventure 189 Summary 198 Part 4: Generic Programming 199 Chapter 20: Template Programming 201 Compile Versus Runtime Time Compilation 201 const Versus constexpr 203 assert Versus static assert 204 Summary 204
xii Contents Chapter 21: Practical Template Programming 205 Creating Singleton Classes with Templates 205 Implementing the EventManager Class 208 The EventHandler Interface 208 The Event Class 208 The EventManager Implementation 210 Calculating SDBM Hash Values Using a Template Metaprogram 216 Using an Event to Quit the Game 218 Summary 221 Part 5: C++ Game Programming 223 Chapter 22: Managing Memory for Game Developers 225 Static Memory 225 The C++ Stack Memory Model 226 Working with Heap Memory 228 Writing a Basic Single Threaded Memory Allocator 231 Summary 238 Chapter 23: Useful Design Patterns for Game Development 239 Using the Factory Pattern in Games 239 Decoupling with the Observer Pattern 244 Easily Adding New Functionality with the Visitor Pattern 249 Summary 251 Chapter 24: Using File 10 to Save and Load Games 253 What Is Serialization? 253 The Serialization Manager 254 Saving and Loading Text Adventure 258 Summary 265
Chapter 25: Speeding Up Games with Concurrent Programming 267 Running Text Adventure in Its Own Thread 267 Sharing Data Between Threads Using Mutexes 269 Using Futures and Promises 272 Summary 274 Chapter 26: Supporting Multiple Platforms in C++ 277 Ensuring Types Are the Same Size on Multiple Platforms 277 Using the Preprocessor to Determine Target Platform 279 Summary 283 Chapter 27: Wrapping Up 285 An Overview of Text Adventure 285 Summary 289 Index 291