Beginning C++ Game Programming - Second Edition

Book description

Get to grips with programming techniques and game development using C++ libraries and Visual Studio 2019

Key Features

  • Learn game development and C++ with a fun, example-driven approach
  • Build clones of popular games such as Timberman, Zombie Survival Shooter, a co-op puzzle platformer, and Space Invaders
  • Discover tips to expand your finished games by thinking critically, technically, and creatively

Book Description

The second edition of Beginning C++ Game Programming is updated and improved to include the latest features of Visual Studio 2019, SFML, and modern C++ programming techniques. With this book, you'll get a fun introduction to game programming by building five fully playable games of increasing complexity. You'll learn to build clones of popular games such as Timberman, Pong, a Zombie survival shooter, a coop puzzle platformer and Space Invaders.

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 particle effects, directional sound (spatialization), OpenGL programmable shaders, spawning objects, and much more. 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

What you will learn

  • Set up your game development project in Visual Studio 2019 and explore C++ libraries such as SFML
  • Explore C++ OOP by building a Pong game
  • Understand core game concepts such as game animation, game physics, collision detection, scorekeeping, and game sound
  • Use classes, inheritance, and references to spawn and control thousands of enemies and shoot rapid-fire machine guns
  • Add advanced features to your game using pointers, references, and the STL
  • Scale and reuse your game code by learning modern game programming design patterns

Who this book is for

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.

Table of contents

  1. Beginning C++ Game Programming Second Edition
    1. Why subscribe?
  2. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  3. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  4. Chapter 1: C++, SFML, Visual Studio, and Starting the First Game
    1. The games we will build
      1. Timber!!!
      2. Pong
      3. Zombie Arena
      4. Thomas was late
      5. Space Invaders ++
    2. Meet C++
    3. Microsoft Visual Studio
    4. SFML
    5. Setting up the development environment
      1. What about Mac and Linux?
      2. Installing Visual Studio 2019 Community edition
      3. Setting up SFML
      4. Creating a new project
      5. Configuring the project properties
    6. Planning Timber!!!
    7. The project assets
      1. Outsourcing the assets
      2. Making your own sound FX
      3. Adding the assets to the project
      4. Exploring the assets
    8. Understanding screen and internal coordinates
    9. Getting started with coding the game
      1. Making code clearer with comments
      2. The main function
      3. Presentation and syntax
      4. Returning values from a function
      5. Running the game
    10. Opening a window using SFML
      1. #including SFML features
      2. OOP, classes, and objects
      3. Using namespace sf
      4. SFML VideoMode and RenderWindow
      5. Running the game
    11. The main game loop
      1. While loops
      2. C-style code comments
      3. Input, update, draw, repeat
      4. Detecting a key press
      5. Clearing and drawing the scene
      6. Running the game
    12. Drawing the game’s background
      1. Preparing the Sprite using a Texture
      2. Double buffering the background sprite
      3. Running the game
    13. Handling errors
      1. Configuration errors
      2. Compile errors
      3. Link errors
      4. Bugs
    14. Summary
    15. FAQ
  5. Chapter 2: Variables, Operators, and Decisions – Animating Sprites
    1. C++ variables
      1. Types of variables
      2. Declaring and initializing variables
    2. Manipulating variables
      1. C++ arithmetic and assignment operators
      2. Getting things done with expressions
    3. Adding clouds, a tree, and a buzzing bee
      1. Preparing the tree
      2. Preparing the bee
      3. Preparing the clouds
      4. Drawing the tree, the bee, and the clouds
    4. Random numbers
      1. Generating random numbers in C++
    5. Making decisions with if and else
      1. Logical operators
      2. C++ if and else
      3. If they come over the bridge, shoot them!
      4. Shoot them … or else do this instead
      5. Reader challenge
    6. Timing
      1. The frame rate problem
      2. The SFML frame rate solution
    7. Moving the clouds and the bee
      1. Giving life to the bee
      2. Blowing the clouds
    8. Summary
    9. FAQ
  6. Chapter 3: C++ Strings and SFML Time – Player Input and HUD
    1. Pausing and restarting the game
    2. C++ Strings
      1. Declaring Strings
      2. Assigning a value to a String
      3. Manipulating Strings
    3. SFML's Text and Font classes
    4. Implementing the HUD
    5. Adding a time-bar
    6. Summary
    7. FAQ
  7. Chapter 4: Loops, Arrays, Switches, Enumerations, and Functions – Implementing Game Mechanics
    1. Loops
      1. while loops
      2. for loops
    2. Arrays
      1. Declaring an array
      2. Initializing the elements of an array
      3. What do these arrays really do for our games?
    3. Making decisions with switch
    4. Class enumerations
    5. Getting started with functions
      1. Function return types
      2. Function names
      3. Function parameters
      4. The function body
      5. Function prototypes
      6. Organizing functions
      7. Function gotcha!
      8. More on functions
      9. An absolute final word on functions – for now
    6. Growing the branches
      1. Preparing the branches
      2. Updating the branch sprites each frame
      3. Drawing the branches
      4. Moving the branches
    7. Summary
    8. FAQ
  8. Chapter 5: Collisions, Sound, and End Conditions – Making the Game Playable
    1. Preparing the player (and other sprites)
    2. Drawing the player and other sprites
    3. Handling the player's input
      1. Handling setting up a new game
      2. Detecting the player chopping
      3. Detecting a key being released
      4. Animating the chopped logs and the axe
    4. Handling death
    5. Simple sound FX
      1. How SFML sound works
      2. When to play the sounds
      3. Adding the sound code
    6. Improving the game and the code
    7. Summary
    8. FAQ
  9. Chapter 6: Object-Oriented Programming – Starting the Pong Game
    1. OOP
      1. Encapsulation
      2. Polymorphism
      3. Inheritance
      4. Why use OOP?
      5. What exactly is a class?
    2. The theory of a Pong Bat
      1. The class variable and function declarations
      2. The class function definitions
      3. Using an instance of a class
    3. Creating the Pong project
    4. Coding the Bat class
      1. Coding Bat.h
      2. Constructor functions
      3. Continuing with the Bat.h explanation
      4. Coding Bat.cpp
    5. Using the Bat class and coding the main function
    6. Summary
    7. FAQ
  10. Chapter 7: Dynamic Collision Detection and Physics – Finishing the Pong Game
    1. Coding the Ball class
    2. Using the Ball class
    3. Collision detection and scoring
    4. Running the game
    5. Summary
    6. FAQ
  11. Chapter 8: SFML Views – Starting the Zombie Shooter Game
    1. Planning and starting the Zombie Arena game
      1. Creating a new project
      2. The project assets
      3. Exploring the assets
      4. Adding the assets to the project
    2. OOP and the Zombie Arena project
    3. Building the player – the first class
      1. Coding the Player class header file
      2. Coding the Player class function definitions
    4. Controlling the game camera with SFML View
    5. Starting the Zombie Arena game engine
    6. Managing the code files
    7. Starting to code the main game loop
    8. Summary
    9. FAQ
  12. Chapter 9: C++ References, Sprite Sheets, and Vertex Arrays
    1. C++ references
      1. References summary
    2. SFML vertex arrays and sprite sheets
      1. What is a sprite sheet?
      2. What is a vertex array?
      3. Building a background from tiles
      4. Building a vertex array
      5. Using the vertex array to draw
    3. Creating a randomly generated scrolling background
    4. Using the background
    5. Summary
    6. FAQ
  13. Chapter 10: Pointers, the Standard Template Library, and Texture Management
    1. Learning about Pointers
      1. Pointer syntax
      2. Declaring a pointer
      3. Initializing a pointer
      4. Reinitializing pointers
      5. Dereferencing a pointer
      6. Pointers are versatile and powerful
      7. Pointers and arrays
      8. Summary of pointers
    2. The Standard Template Library
      1. What is a map?
      2. Declaring a map
      3. Adding data to a Map
      4. Finding data in a map
      5. Removing data from a map
      6. Checking the size of a map
      7. Checking for keys in a map
      8. Looping/iterating through the key-value pairs of a map
      9. The auto keyword
      10. STL summary
    3. The TextureHolder class
      1. Coding the TextureHolder header file
      2. Coding the TextureHolder function definitions
      3. What have we achieved with TextureHolder?
    4. Building a horde of zombies
      1. Coding the Zombie.h file
      2. Coding the Zombie.cpp file
      3. Using the Zombie class to create a horde
      4. Bringing the horde to life (back to life)
    5. Using the TextureHolder class for all textures
      1. Changing the way the background gets its textures
      2. Changing the way the Player gets its texture
    6. Summary
    7. FAQ
  14. Chapter 11: Collision Detection, Pickups, and Bullets
    1. Coding the Bullet class
      1. Coding the Bullet header file
      2. Coding the Bullet source file
    2. Making the bullets fly
      1. Including the Bullet class
      2. Control variables and the bullet array
      3. Reloading the gun
      4. Shooting a bullet
      5. Updating the bullets each frame
      6. Drawing the bullets each frame
    3. Giving the player a crosshair
    4. Coding a class for pickups
      1. Coding the Pickup header file
      2. Coding the Pickup class function definitions
    5. Using the Pickup class
    6. Detecting collisions
      1. Has a zombie been shot?
      2. Has the player been touched by a zombie?
      3. Has the player touched a pickup?
    7. Summary
    8. FAQ
  15. Chapter 12: Layering Views and Implementing the HUD
    1. Adding all the Text and HUD objects
    2. Updating the HUD
    3. Drawing the HUD, home, and level-up screens
    4. Summary
    5. FAQ
  16. Chapter 13: Sound Effects, File I/O, and Finishing the Game
    1. Saving and loading the high score
    2. Preparing sound effects
    3. Leveling up
    4. Restarting the game
    5. Playing the rest of the sounds
      1. Adding sound effects while the player is reloading
      2. Making a shooting sound
      3. Playing a sound when the player is hit
      4. Playing a sound when getting a pickup
      5. Making a splat sound when a zombie is shot
    6. Summary
    7. FAQ
  17. Chapter 14: Abstraction and Code Management – Making Better Use of OOP
    1. The Thomas Was Late game
      1. Features of Thomas Was Late
      2. Creating the project
      3. The project's assets
    2. Structuring the Thomas Was Late code
    3. Building the game engine
      1. Reusing the TextureHolder class
      2. Coding Engine.h
      3. Coding Engine.cpp
      4. The Engine class so far
    4. Coding the main function
    5. Summary
    6. FAQ
  18. Chapter 15: Advanced OOP – Inheritance and Polymorphism
    1. Inheritance
      1. Extending a class
    2. Polymorphism
    3. Abstract classes – virtual and pure virtual functions
    4. Building the PlayableCharacter class
      1. Coding PlayableCharacter.h
      2. Coding PlayableCharacter.cpp
    5. Building the Thomas and Bob classes
      1. Coding Thomas.h
      2. Coding Thomas.cpp
      3. Coding Bob.h
      4. Coding Bob.cpp
    6. Updating the game engine to use Thomas and Bob
      1. Updating Engine.h to add an instance of Bob and Thomas
      2. Updating the input function to control Thomas and Bob
      3. Updating the update function to spawn and update the PlayableCharacter instances
      4. Drawing Bob and Thomas
    7. Summary
    8. FAQ
  19. Chapter 16: Building Playable Levels and Collision Detection
    1. Designing some levels
    2. Building the LevelManager class
      1. Coding LevelManager.h
      2. Coding the LevelManager.cpp file
    3. Coding the loadLevel function
    4. Updating the engine
    5. Collision detection
      1. Coding the detectCollisions function
      2. More collision detection
    6. Summary
  20. Chapter 17: Sound Spatialization and the HUD
    1. What is spatialization?
      1. Emitters, attenuation, and listeners
    2. Handling spatialization using SFML
    3. Building the SoundManager class
      1. Coding SoundManager.h
      2. Coding the SoundManager.cpp file
    4. Adding SoundManager to the game engine
    5. Populating the sound emitters
      1. Coding the populateEmitters function
    6. Playing sounds
    7. Implementing the HUD class
      1. Coding HUD.h
      2. Coding the HUD.cpp file
    8. Using the HUD class
    9. Summary
  21. Chapter 18: Particle Systems and Shaders
    1. Building a particle system
      1. Coding the Particle class
      2. Coding the ParticleSystem class
    2. Exploring SFML's Drawable class and OOP
      1. An alternative to inheriting from Drawable
      2. Using the ParticleSystem object
    3. OpenGL, Shaders, and GLSL
      1. The programmable pipeline and shaders
      2. Coding a fragment shader
      3. Coding a vertex shader
      4. Adding shaders to the engine class
      5. Loading the shaders
      6. Updating and drawing the shader
    4. Summary
  22. Chapter 19: Game Programming Design Patterns – Starting the Space Invaders ++ Game
    1. Space Invaders ++
      1. Why Space Invaders ++?
    2. Design patterns
      1. Screen, InputHandler, UIPanel, and Button
      2. Entity-Component pattern
      3. Prefer composition over inheritance
      4. Factory pattern
    3. C++ smart pointers
      1. Shared pointers
      2. Unique pointers
    4. Casting smart pointers
    5. C++ assertions
    6. Creating the Space Invaders ++ project
      1. Organizing code files with filters
      2. Adding a DevelopState file
      3. Coding SpaceInvaders ++.cpp
      4. Coding the GameEngine class
      5. Coding the SoundEngine class
      6. Coding the ScreenManager class
      7. Coding the BitmapStore class
      8. Coding the ScreenManagerRemoteControl class
    7. Where are we now?
    8. Coding the Screen class and its dependents
      1. Coding the Button class
      2. Coding the UIPanel class
      3. Coding the InputHandler class
      4. Coding the Screen class
    9. Adding the WorldState.h file
    10. Coding the derived classes for the select screen
      1. Coding the SelectScreen class
      2. Coding the SelectInputHandler class
      3. Coding the SelectUIPanel class
    11. Coding the derived classes for the game screen
      1. Coding the GameScreen class
      2. Coding the GameInputHandler class
      3. Coding the GameUIPanel class
      4. Coding the GameOverInputHandler class
      5. Coding the GameOverUIPanel class
    12. Running the game
    13. Summary
  23. Chapter 20: Game Objects and Components
    1. Preparing to code the components
    2. Coding the Component base class
    3. Coding the collider components
      1. Coding the ColliderComponent class
      2. Coding the RectColliderComponent class
    4. Coding the graphics components
      1. Coding the GraphicsComponent class
      2. Coding the StandardGraphicsComponent class
    5. Coding the TransformComponent class
    6. Coding update components
      1. Coding the UpdateComponent class
      2. Coding the BulletUpdateComponent class
      3. Coding the InvaderUpdateComponent class
      4. Coding the PlayerUpdateComponent class
    7. Coding the GameObject class
      1. Explaining the GameObject class
    8. Summary
  24. Chapter 21: File I/O and the Game Object Factory
    1. The structure of the file I/O and factory classes
    2. Describing an object in the world
    3. Coding the GameObjectBlueprint class
    4. Coding the ObjectTags class
    5. Coding the BlueprintObjectParser class
    6. Coding the PlayModeObjectLoader class
    7. Coding the GameObjectFactoryPlayMode class
    8. Coding the GameObjectSharer class
    9. Coding the LevelManager class
    10. Updating the ScreenManager and ScreenManagerRemoteControl classes
    11. Where are we now?
    12. Summary
  25. Chapter 22: Using Game Objects and Building a Game
    1. Spawning bullets
      1. Coding the BulletSpawner class
      2. Updating GameScreen.h
    2. Handling the player's input
      1. Using a gamepad
    3. Coding the PhysicsEnginePlayMode class
    4. Making the game
    5. Understanding the flow of execution and debugging
    6. Reusing the code to make a different game and building a design mode
    7. Summary
  26. Chapter 23: Before You Go...
    1. Thanks!
  27. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Beginning C++ Game Programming - Second Edition
  • Author(s): John Horton
  • Release date: October 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838648572