O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning C++ Game Programming

Book Description

Learn C++ from scratch and get started building your very own games

About This Book

  • This book offers a fun way to learn modern C++ programming while building exciting 2D games
  • This beginner-friendly guide offers a fast-paced but engaging approach to game development
  • Dive headfirst into building a wide variety of desktop games that gradually increase in complexity
  • It is packed with many suggestions to expand your finished games that will make you think critically, technically, and creatively

Who This Book Is For

This book is perfect for you if any of the following describes you: You have no C++ programming knowledge whatsoever or need a beginner level refresher course, if you want to learn to build games or just use games as an engaging way to learn C++, if you have aspirations to publish a game one day, perhaps on Steam, or if you just want to have loads of fun and impress friends with your creations.

What You Will Learn

  • Get to know C++ from scratch while simultaneously learning game building
  • Learn the basics of C++, such as variables, loops, and functions to animate game objects, respond to collisions, keep score, play sound effects, and build your first playable game.
  • Use more advanced C++ topics such as classes, inheritance, and references to spawn and control thousands of enemies, shoot with a rapid fire machine gun, and realize random scrolling game-worlds
  • Stretch your C++ knowledge beyond the beginner level and use concepts such as pointers, references, and the Standard Template Library to add features like split-screen coop, immersive directional sound, and custom levels loaded from level-design files
  • Get ready to go and build your own unique games!

In Detail

This book is all about offering you a fun introduction to the world of game programming, C++, and the OpenGL-powered SFML using three fun, fully-playable games. These games are an addictive frantic two-button tapper, a multi-level zombie survival shooter, and a split-screen multiplayer puzzle-platformer.

We will start with the very basics of programming, such as variables, loops, and conditions and you will become more skillful with each game as you move through the key C++ topics, such as OOP (Object-Orientated Programming), C++ pointers, and an introduction to the Standard Template Library. While building these games, you will also learn exciting game programming concepts like particle effects, directional sound (spatialization), OpenGL programmable Shaders, spawning thousands of objects, and more.

Style and approach

This book offers a fun, example-driven approach to learning game development and C++. In addition to explaining game development techniques in an engaging style, the games are built in a way that introduces the key C++ topics in a practical and not theory-based way, with multiple runnable/playable stages in each chapter.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Beginning C++ Game Programming
    1. Beginning C++ Game Programming
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    6. Dedication
    7. Preface
      1. Timber!!!
      2. Zombie Arena
      3. Thomas was Late
      4. What this book covers
      5. What you need for this book
      6. Who this book is for
      7. Conventions
      8. Reader feedback
      9. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. C++, SFML, Visual Studio, and Starting the First Game
      1. The games
        1. Timber!!!
        2. Zombie Arena
        3. Thomas was Late
      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 Express 2015 on your desktop
        3. Setting up SFML
        4. Creating a reusable project template
      6. Planning Timber!!!
      7. Creating a project from the template
      8. Project assets
        1. Outsourcing assets
        2. Making your own sound FX
        3. Adding assets to the project
        4. Exploring assets
      9. Understanding screen and internal coordinates
      10. Starting to code the game
        1. Making code clearer with comments
        2. #including Windows essentials
        3. The main function
        4. Presentation and syntax
        5. Returning values from a function
        6. Running the game
      11. Opening a window using SFML
        1. #including SFML features
        2. OOP, classes, objects
        3. Using namespace sf
        4. SFML VideoMode and RenderWindow
        5. Running the game
      12. 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
      13. Drawing the game background
        1. Preparing the sprite using a texture
        2. Double-buffering the background sprite
        3. Running the game
      14. Handling errors
        1. Configuration errors
        2. Compile errors
        3. Link errors
        4. Bugs
      15. FAQ
      16. Summary
    9. 2. Variables, Operators, and Decisions – Animating Sprites
      1. C++ variables
        1. Types of variable
          1. Constants
          2. User-defined types
        2. Declaring and initializing variables
          1. Declaring variables
          2. Initializing variables
          3. Declaring and initializing in one step
          4. Declaring and initializing user-defined types
      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. Or 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. FAQ
      9. Summary
    10. 3. C++ Strings, SFML Time, Player Input, and HUD
      1. Pausing and restarting the game
      2. C++ strings
        1. Declaring strings
        2. Assigning a value to strings
        3. Manipulating strings
      3. SFML Text and Font
      4. Adding a score and a message
      5. Adding a time bar
      6. FAQ
      7. Summary
    11. 4. Loops, Arrays, Switch, Enumerations, and Functions – Implementing Game Mechanics
      1. Loops
        1. while loops
          1. Breaking out of a while loop
        2. for loops
      2. Arrays
        1. Declaring an array
        2. Initializing the elements of an array
          1. Quickly initializing the elements of an array
        3. So 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. Final word on functions - for now
        9. 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. FAQ
      8. Summary
    12. 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 ax
      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. FAQ
      8. Summary
    13. 6. Object-Oriented Programming, Classes, and SFML Views
      1. Planning and starting the Zombie Arena game
        1. Creating a project from the template
        2. The project assets
        3. Exploring the assets
        4. Adding the assets to the project
      2. OOP
        1. What is OOP?
        2. Encapsulation
        3. Polymorphism
        4. Inheritance
        5. Why do it like this?
        6. What is a class?
        7. The class variable and function declarations
        8. The class function definitions
        9. Using an instance of a class
        10. Constructors and getter functions
        11. Jumping around in the code
      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 coding the main game loop
      8. FAQ
      9. Summary
    14. 7. 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. FAQ
      6. Summary
    15. 8. Pointers, the Standard Template Library, and Texture Management
      1. 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
          1. Dynamically allocated memory
          2. Passing a pointer to a function
          3. Declaring and using a pointer to an object
        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 exactly 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. Change the way the background gets its textures
        2. Change the way Player gets its texture
      6. FAQ
      7. Summary
    16. 9. 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. FAQ
      8. Summary
    17. 10. Layering Views and Implementing the HUD
      1. Adding all the Text and HUD objects
      2. Updating the HUD each frame
      3. Drawing the HUD, and the home and level up screens
      4. FAQ
      5. Summary
    18. 11. 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. Make a shooting sound
        3. Play a sound when the player is hit
        4. Play a sound when getting a pickup
        5. Make a splat sound when a zombie is shot
      6. FAQ
      7. Summary
    19. 12. Abstraction and Code Management – Making Better Use of OOP
      1. The Thomas Was Late game
        1. Features of Thomas Was Late
        2. Creating a project from the template
        3. The project assets
          1. Game level designs
          2. GLSL Shaders
          3. The graphical assets close-up
          4. The sound assets close-up
          5. Adding the assets to the project
      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
          1. Coding the Engine class constructor definition
          2. Coding the run function definition
          3. Coding the input function definition
          4. Coding the update function definition
          5. Coding the draw function definition
        4. The Engine class so far
      4. Coding the main function
      5. FAQ
      6. Summary
    20. 13. 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
          1. Spawning Thomas and Bob
          2. Updating Thomas and Bob each frame
        4. Drawing Bob and Thomas
      7. FAQ
      8. Summary
    21. 14. 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
    22. 15. Sound Spatialization and HUD
      1. What is Spatialization?
        1. Emitters, attenuation, and listeners
      2. How SFML handles spatialization
      3. Building the SoundManager class
        1. Coding SoundManager.h
        2. Coding the SoundManager.cpp file
          1. Coding the constructor
          2. Coding the playFire function
          3. Coding the rest of the SoundManager functions
      4. Adding SoundManager to the game engine
      5. Populating the sound emitters
        1. Coding the populateEmitters function
      6. Playing sounds
      7. The HUD class
        1. Coding HUD.h
        2. Coding the HUD.cpp file
      8. Using the HUD class
      9. Summary
    23. 16. Extending SFML Classes, Particle Systems, and Shaders
      1. The SFML Drawable class
        1. An alternative to inheriting from Drawable
        2. Why it is best to inherit from Drawable?
      2. Building a particle system
        1. Coding the Particle class
          1. Coding Particle.h
          2. Coding the Particle.cpp file
        2. Coding the ParticleSystem class
          1. Coding ParticleSystem.h
          2. Coding the ParticleSystem.cpp file
        3. Using ParticleSystem
          1. Adding a ParticleSystem object to the Engine class
          2. Initializing ParticleSystem
          3. Updating the particle system in each frame
          4. Starting the particle system
          5. Drawing the particle system
      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 in each frame
      4. Summary
    24. 17. Before you go...
      1. Thanks!