Preface xx
Chapter 1: Welcome to Beginning C++ Game Programming Third Edition! 1
The games we will build ..................................................................................................... 2
Timber!!! • 2
Pong • 3
Zombie Arena • 4
Platform game • 4
Why you should learn game programming using C++ in 2024 ............................................ 5
SFML • 7
Microsoft Visual Studio • 8
What about Mac and Linux? • 10
Installing Visual Studio 2022 • 10
Setting up SFML ................................................................................................................ 12
Creating a new project in Visual Studio 2022 .................................................................... 14
Configuring the project properties • 18
Planning Timber!!! ............................................................................................................ 20
The project assets .............................................................................................................. 23
Making your own sound FX • 24
Adding the assets to the project • 24
Exploring the assets • 24
Understanding screen and internal coordinates ............................................................... 25
Getting started with coding the game ............................................................................... 28
Making code clearer with comments • 28
The main function • 28
Presentation and syntax • 29
Returning values from a function • 30
Running the game • 31
Opening a window using SFML ......................................................................................... 31
Including SFML features • 32
OOP, classes, and objects • 32
Using namespace sf • 34
SFML VideoMode and RenderWindow • 34
Running the game • 35
The game loop .................................................................................................................. 36
while loops • 37
C-style code comments • 38
Input, update, draw, repeat • 38
Detecting a key press • 39
Clearing and drawing the scene • 39
Running the game • 40
Drawing the game’s background ...................................................................................... 40
Preparing the sprite using a texture • 40
Double buffering the background sprite • 42
Running the game • 43
Handling errors ................................................................................................................. 44
Configuration errors • 44
Compile errors • 44
Link errors • 45
Bugs • 45
Summary .......................................................................................................................... 45
Frequently asked questions .............................................................................................. 45
Chapter 2: Variables, Operators, and Decisions: Animating Sprites 47
Learning all about C++ variables ....................................................................................... 48
Types of variables • 48
User-defined types • 50
Declaring and initializing variables • 50
Declaring variables • 50
Initializing variables • 51
Declaring and initializing in one step • 51
Constants • 52
Uniform initialization • 52
Declaring and initializing user-defined types • 53
Seeing how to manipulate the variables ........................................................................... 54
C++ arithmetic and assignment operators • 54
Getting things done with expressions • 55
Assignment • 55
Increment and decrement • 57
Adding clouds, a buzzing bee, and a tree ........................................................................... 59
Preparing the tree • 59
Preparing the bee • 61
Preparing the clouds • 62
Drawing the tree, the bee, and the clouds • 64
Random numbers ............................................................................................................. 65
Generating random numbers in C++ • 66
Making decisions with if and else ..................................................................................... 67
Logical operators • 67
C++ if and else • 69
If they come over the bridge, shoot them! • 69
Else do this instead • 69
Reader challenge • 71
Timing .............................................................................................................................. 72
The frame rate problem • 72
The SFML frame rate solution • 73
Moving the clouds and the bee .......................................................................................... 76
Giving life to the bee • 76
Blowing the clouds • 80
Summary .......................................................................................................................... 86
Frequently Asked Questions ............................................................................................. 86
Chapter 3: C++ Strings, SFML Time: Player Input and HUD 89
Pausing and restarting the game ....................................................................................... 89
C++ strings ........................................................................................................................ 93
Declaring strings • 93
Assigning a value to strings • 93
String Concatenation • 94
Getting the string length • 94
Manipulating strings another way with StringStream • 95
SFML Text and SFML Font • 96
Adding a score and a message ........................................................................................... 97
Adding a time-bar ........................................................................................................... 104
Summary ......................................................................................................................... 110
Frequently asked questions .............................................................................................. 111
Chapter 4: Loops, Arrays, Switch, Enumerations, and Functions: Implementing
Game Mechanics 113
Loops ............................................................................................................................... 114
while loops • 114
Breaking out of a loop • 117
for loops • 119
Arrays ............................................................................................................................... 121
Declaring an array • 121
Initializing the elements of an array • 122
Quickly initializing the elements of an array • 122
What do these arrays really do for our games? • 123
Making decisions with switch ......................................................................................... 124
Class enumerations .......................................................................................................... 126
Getting started with functions ......................................................................................... 129
Who designed all this weird and frustrating syntax and why is it the way it is? • 131
Function return types • 133
Function names • 137
Function parameters • 138
The function body • 139
Function prototypes • 139
Organizing functions • 140
Function scope • 140
A final word on functions – for now • 141
Growing the branches ...................................................................................................... 142
Preparing the branches • 143
Updating the branch sprites in each frame • 144
Drawing the branches • 146
Moving the branches • 147
Summary ........................................................................................................................ 150
Frequently asked questions ............................................................................................ 150
Chapter 5: Collisions, Sound, and End Conditions: Making the Game
Playable 153
Preparing the player (and other sprites) .......................................................................... 153
Drawing the player and other sprites ............................................................................... 155
Handling the player’s input ............................................................................................. 156
Handling setting up a new game • 158
Detecting the player chopping • 159
Detecting a key being released • 163
Animating the chopped logs and the axe • 165
Handling death ................................................................................................................ 167
Simple sound effects ....................................................................................................... 170
How SFML sound works • 170
When to play the sounds • 171
Adding the sound code • 171
Improving the game and code .......................................................................................... 175
Summary ......................................................................................................................... 177
Frequently asked questions ............................................................................................. 177
Chapter 6: Object-Oriented Programming – Starting the Pong Game 179
Object-oriented programming ........................................................................................ 180
Encapsulation • 181
Polymorphism • 181
Inheritance • 181
Why use OOP? • 182
What exactly is a class? • 183
The theory of a Pong bat ................................................................................................. 183
Declaring the class, variables, and functions • 183
The class function definitions • 187
Using an instance of a class • 189
Creating the Pong project ................................................................................................ 190
Coding the Bat class ......................................................................................................... 192
Coding Bat.h • 192
Constructor functions • 193
Continuing with the Bat.h explanation • 194
Coding Bat.cpp • 195
Using the Bat class and coding the main function ........................................................... 198
Summary ........................................................................................................................ 203
Frequently asked questions ............................................................................................ 203
Chapter 7: AABB Collision Detection and Physics – Finishing
the Pong Game 205
Coding the Ball class ....................................................................................................... 206
Using the Ball class ......................................................................................................... 209
Collision detection and scoring ........................................................................................ 211
Running the game ............................................................................................................ 214
Learning about the C++ spaceship operator ..................................................................... 215
Summary ......................................................................................................................... 216
Frequently asked questions ............................................................................................. 217
Chapter 8: SFML Views – Starting the Zombie Shooter Game 219
Planning and starting the Zombie Arena game ............................................................... 220
Creating a new project • 221
The project assets • 223
Exploring the assets • 224
Adding the assets to the project • 225
OOP and the Zombie Arena project ................................................................................. 225
Building the player – the first class ................................................................................. 226
Coding the Player class header file • 227
Coding the Player class function definitions • 233
Controlling the game camera with SFML View ............................................................... 243
Starting the Zombie Arena game engine ......................................................................... 245
Managing the code files .................................................................................................. 249
Starting to code the main game loop ................................................................................ 251
Summary ......................................................................................................................... 261
Frequently asked questions ............................................................................................. 261
Chapter 9: C++ References, Sprite Sheets, and Vertex Arrays 263
Understanding C++ references ........................................................................................ 264
Summarizing references • 267
SFML vertex arrays and sprite sheets .............................................................................. 267
What is a sprite sheet? • 268
What is a vertex array? .................................................................................................... 270
Building a background from tiles • 270
Building a vertex array • 271
Using the vertex array to draw • 272
Creating a randomly generated scrolling background ..................................................... 273
Using the background ..................................................................................................... 280
Summary ........................................................................................................................ 283
Frequently asked questions ............................................................................................ 283
Chapter 10: Pointers, the Standard Template Library, and Texture Management
285
Learning about pointers ................................................................................................. 285
Pointer syntax • 286
Declaring a pointer • 287
Initializing a pointer • 288
Reinitializing pointers • 289
Dereferencing a pointer • 290
Pointers are versatile and powerful • 292
Dynamically allocated memory • 292
Passing a pointer to a function • 294
Declaring and using a pointer to an object • 295
Pointers and arrays • 295
Summary of pointers • 296
Learning about the Standard Template Library .............................................................. 297
What is a vector? • 298
Declaring a vector • 298
Adding data to a vector • 298
Accessing data in a vector • 299
Removing data from a vector • 299
Checking the size of a vector • 299
Looping/iterating through the elements of a vector • 299
What is a map? • 300
Declaring a map • 300
Adding data to a map • 301
Finding data in a map • 301
Removing data from a map • 301
Checking the size of a map • 301
Checking for keys in a map • 302
Looping/iterating through the key-value pairs of a map • 302
The auto keyword • 303
STL summary • 303
Summary ........................................................................................................................ 303
Frequently asked questions ............................................................................................ 304
Chapter 11: Coding the TextureHolder Class and Building a Horde of Zombies
305
Implementing the TextureHolder class ........................................................................... 305
Coding the TextureHolder header file • 306
Coding the TextureHolder function definitions • 307
What have we achieved with TextureHolder? • 310
Building a horde of zombies ............................................................................................ 310
Coding the Zombie.h file • 310
Coding the Zombie.cpp file • 313
Using the Zombie class to create a horde • 318
Bringing the horde to life (or back to life) • 322
Using the TextureHolder class for all textures ................................................................. 327
Changing the way the background gets its textures • 327
Changing the way the Player class gets its texture • 328
Summary ........................................................................................................................ 329
Frequently asked questions ............................................................................................ 329
Chapter 12: Collision Detection, Pickups, and Bullets 331
Coding the Bullet class .................................................................................................... 332
Coding the Bullet header file • 332
Coding the Bullet source file • 335
Coding the shoot function • 335
Calculating the gradient in the shoot function • 338
Making the gradient positive in the shoot function • 338
Calculating the ratio between X and Y in the shoot function • 338
Finishing the shoot function explanation • 339
More bullet functions • 340
The Bullet class’s update function • 340
Making the bullets fly ...................................................................................................... 341
Including the Bullet class • 342
Control variables and the bullet array • 342
Reloading the gun • 343
Shooting a bullet • 345
Updating the bullets in each frame • 347
Drawing the bullets in each frame • 347
Giving the player a crosshair • 348
Coding a class for pickups ............................................................................................... 352
Coding the Pickup header file • 352
Coding the Pickup class function definitions • 355
Using the Pickup class ..................................................................................................... 360
Detecting collisions ........................................................................................................ 364
Has a zombie been shot? • 365
Has the player been touched by a zombie? • 368
Has the player touched a pickup? • 369
Summary ........................................................................................................................ 370
Frequently asked questions ............................................................................................ 370
Chapter 13: Layering Views and Implementing the HUD 371
Adding all the Text and HUD objects ................................................................................ 371
Updating the HUD .......................................................................................................... 375
Drawing the HUD, home, and level-up screens ............................................................... 378
Summary ........................................................................................................................ 381
Chapter 14: Sound Effects, File I/O, and Finishing the Game 383
Saving and loading the high score ................................................................................... 383
Preparing sound effects .................................................................................................. 386
Allowing the player to level up and spawning a new wave .............................................. 387
Restarting the game ........................................................................................................ 390
Playing the rest of the sounds .......................................................................................... 391
Adding sound effects while the player is reloading • 391
Making a shooting sound • 392
Playing a sound when the player is hit • 392
Playing a sound when getting a pickup • 393
Making a splat sound when a zombie is shot • 394
Summary ........................................................................................................................ 395
Frequently asked questions ............................................................................................ 395
Chapter 15: Run! 397
About the game ............................................................................................................... 398
Creating the project ........................................................................................................ 401
Coding the main function ............................................................................................... 403
Handling input .............................................................................................................. 408
Coding the Factory class .................................................................................................. 413
Advanced OOP: inheritance and polymorphism .............................................................. 415
Inheritance • 415
Extending a class • 416
Polymorphism • 418
Abstract classes: virtual and pure virtual functions • 419
Design patterns ................................................................................................................ 421
Entity Component System pattern .................................................................................. 422
Why lots of diverse object types are hard to manage • 422
Using a generic GameObject for better code structure • 422
Prefer composition over inheritance • 424
Factory pattern • 426
C++ smart pointers • 428
Shared pointers • 428
Unique pointers • 430
Casting smart pointers • 431
Coding the GameObject class • 432
Coding the Component class • 435
Coding the Graphics class • 436
Coding the Update class • 438
Running the code • 439
What next? • 439
Summary ........................................................................................................................ 440
Chapter 16: Sound, Game Logic, Inter-Object Communication, and the Player
441
Coding the SoundEngine class ........................................................................................ 442
Code the Game logic ....................................................................................................... 445
Coding the LevelUpdate class • 446
Coding the player: Part 1 ................................................................................................. 459
Coding the PlayerUpdate class • 460
Coding the PlayerGraphics class • 464
Coding the factory to use all our new classes .................................................................. 470
Remembering the texture coordinates • 470
Running the game ........................................................................................................... 474
Summary ........................................................................................................................ 475
Chapter 17: Graphics, Cameras, Action 477
Cameras, draw calls, and SFML View .............................................................................. 477
Coding the camera classes .............................................................................................. 479
Coding the CameraUpdate class • 479
Coding the CameraGraphics class part 1 • 484
The SFML View class • 488
Coding the CameraGraphics class part 2 • 490
Adding camera instances to the game ............................................................................. 495
Running the game ........................................................................................................... 498
Summary ........................................................................................................................ 499
Chapter 18: Coding the Platforms, Player Animations, and Controls 501
Coding the platforms ...................................................................................................... 501
Coding the PlatformUpdate class • 502
Coding the update function for the PlatformUpdate class • 504
Coding the PlatformGraphics class • 508
Building some platforms in the factory • 511
Running the game ............................................................................................................ 512
Adding functionality to the player ................................................................................... 513
Coding the player controls • 514
Running the game ........................................................................................................... 520
Coding the Animator class ............................................................................................... 521
Coding the player animations ......................................................................................... 524
Running the game ........................................................................................................... 533
Summary ........................................................................................................................ 534
Chapter 19: Building the Menu and Making It Rain 535
Building an interactive menu .......................................................................................... 536
Coding the MenuUpdate class • 536
Coding the MenuGraphics class • 543
Building a menu in the factory • 548
Running the game ........................................................................................................... 550
Making it rain .................................................................................................................. 551
Coding the RainGraphics class • 551
Making it rain in the factory • 556
Running the game ........................................................................................................... 558
Summary ........................................................................................................................ 558
Chapter 20: Fireballs and Spatialization 559
What is spatialization? .................................................................................................... 559
Emitters, attenuation, and listeners • 560
Handling spatialization using SFML ............................................................................... 560
Upgrading the SoundEngine class ................................................................................... 563
Fireballs .......................................................................................................................... 565
Coding the FireballUpdate class • 565
Coding the FireballGraphics class • 574
Coding FireballGraphics.h • 575
Coding FireballGraphics.cpp • 576
Building some fireballs in the factory • 580
Running the code ............................................................................................................ 582
Summary ........................................................................................................................ 582
Chapter 21: Parallax Backgrounds and Shaders 583
Learning about OpenGL, shaders, and GLSL ................................................................... 583
The programmable pipeline and shaders • 584
Coding a hypothetical fragment shader • 585
Coding a hypothetical vertex shader • 586
Finishing the CameraGraphics class ............................................................................... 587
Breaking up the new draw code • 591
Coding a shader for the game .......................................................................................... 595
Running the completed game ......................................................................................... 595
Summary ........................................................................................................................ 596
Further reading ............................................................................................................... 597
Why subscribe? ............................................................................................................... 599
Other Books You May Enjoy 601
Index 607
Always dreamed of creating your own games? With the third edition of Beginning C++ Game Programming, you can turn that dream into reality! This beginner-friendly guide is updated and improved to include the latest features of VS 2022, SFML, and modern C++20 programming techniques. You'll get a fun introduction to game programming by building four fully playable games of increasing complexity. You'll build clones of popular games such as Timberman, Pong, a Zombie survival shooter, and an endless runner.
The book starts by covering the basics of programming. You'll study key C++ topics, such as object-oriented programming (OOP) and C++ pointers and get acquainted with the Standard Template Library (STL). The book helps you learn about collision detection techniques and game physics by building a Pong game. As you build games, you'll also learn exciting game programming concepts such as vertex arrays, directional sound (spatialization), OpenGL programmable shaders, spawning objects, and much more. You'll dive deep into game mechanics and implement input handling, levelling up a character, and simple enemy AI. Finally, you'll explore game design patterns to enhance your C++ game programming skills.
By the end of the book, you'll have gained the knowledge you need to build your own games with exciting features from scratch.
Set up your game project in VS 2022 and explore C++ libraries such as SFML
Build games in C++ from the ground up, including graphics, physics, and input handling
Implement core game concepts such as game animation, game physics, collision detection, scorekeeping, and game sound
Implement automatically spawning objects and AI to create rich and engaging experiences
Learn advanced game development concepts, such as OpenGL shaders, texture atlases, and parallax backgrounds
Scale and reuse your game code with modern game programming design patterns
This book is perfect for you if you have no C++ programming knowledge, you need a beginner-level refresher course, or you want to learn how to build games or just use games as an engaging way to learn C++. Whether you aspire to publish a game (perhaps on Steam) or just want to impress friends with your creations, you'll find this book useful