Table of Contents
Beginning C++ Game Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Dedication
Preface
Timber!!!
Zombie Arena Table of Contents Thomas was Late
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. C++, SFML, Visual Studio, and Starting the First Game
The games
Timber!!!
Zombie Arena
Thomas was Late
Meet C++
Microsoft Visual Studio
SFML
Setting up the development environment
What about Mac and Linux?
Installing Visual Studio Express 2015 on your desktop Setting up SFML
Creating a reusable project template
Planning Timber!!!
Creating a project from the template
Project assets
Outsourcing assets
Making your own sound FX
Adding assets to the project
Exploring assets
Understanding screen and internal coordinates Starting to code the game
Making code clearer with comments
#including Windows essentials
The main function
Presentation and syntax
Returning values from a function
Running the game
Opening a window using SFML
#including SFML features
OOP, classes, objects
Using namespace sf
SFML VideoMode and RenderWindow
Running the game
The main game loop
While loops
C-style code comments
Input, update, draw, repeat
Detecting a key press
Clearing and drawing the scene
Running the game
Drawing the game background
Preparing the sprite using a texture
Double-buffering the background sprite
Running the game
Handling errors
Configuration errors
Compile errors
Link errors
Bugs
FAQ
Summary
2. Variables, Operators, and Decisions Animating Sprites
C++ variables
Types of variable
Constants
User-defined types
Declaring and initializing variables
Declaring variables
Initializing variables
Declaring and initializing in one step
Declaring and initializing user-defined types
Manipulating variables
C++ arithmetic and assignment operators
Getting things done with expressions
Adding clouds, a tree, and a buzzing bee
Preparing the tree
Preparing the bee
Preparing the clouds
Drawing the tree, the bee, and the clouds
Random numbers
Generating random numbers in C++
Making decisions with if and else
Logical operators
C++ if and else
If they come over the bridge, shoot them! Or do this instead
Reader challenge
Timing
The frame-rate problem
The SFML frame-rate solution
Moving the clouds and the bee
Giving life to the bee
Blowing the clouds
FAQ
Summary
3. C++ Strings, SFML Time, Player Input, and HUD
Pausing and restarting the game
C++ strings
Declaring strings
Assigning a value to strings
Manipulating strings
SFML Text and Font
Adding a score and a message
Adding a time bar
FAQ
Summary
4. Loops, Arrays, Switch, Enumerations, and Functions Implementing Game Mechanics
Loops
while loops
Breaking out of a while loop
for loops
Arrays
Declaring an array
Initializing the elements of an array
Quickly initializing the elements of an array
So what do these arrays really do for our games?
Making decisions with switch
Class enumerations
Getting started with functions
Function return types
Function names
Function parameters
The function body
Function prototypes
Organizing functions
Function gotcha!
Final word on functions - for now
Absolute final word on functions - for now
Growing the branches
Preparing the branches
Updating the branch sprites each frame
Drawing the branches
Moving the branches
FAQ
Summary
5. Collisions, Sound, and End Conditions Making the Game Playable
Preparing the player (and other sprites)
Drawing the player and other sprites
Handling the player's input
Handling setting up a new game
Detecting the player chopping
Detecting a key being released
Animating the chopped logs and the ax
Handling death
Simple sound FX
How SFML sound works?
When to play the sounds
Adding the sound code
Improving the game and the code
FAQ
Summary
6. Object-Oriented Programming, Classes, and SFML Views
Planning and starting the Zombie Arena game
Creating a project from the template
The project assets
Exploring the assets
Adding the assets to the project
OOP
What is OOP?
Encapsulation
Polymorphism
Inheritance
Why do it like this?
What is a class?
The class variable and function declarations
The class function definitions
Using an instance of a class
Constructors and getter functions
Jumping around in the code
Building the Player-the first class
Coding the Player class header file
Coding the Player class function definitions
Controlling the game camera with SFML View
Starting the Zombie Arena game engine
Managing the code files
Starting coding the main game loop
FAQ
Summary
7. C++ References, Sprite Sheets, and Vertex Arrays
C++ References
References summary
SFML vertex arrays and sprite sheets
What is a sprite sheet?
What is a vertex array?
Building a background from tiles
Building a vertex array
Using the vertex array to draw
Creating a randomly generated scrolling background
Using the background
FAQ
Summary
8. Pointers, the Standard Template Library, and Texture Management
Pointers
Pointer syntax
Declaring a pointer
Initializing a pointer
Reinitializing pointers
Dereferencing a pointer
Pointers are versatile and powerful
Dynamically allocated memory
Passing a pointer to a function
Declaring and using a pointer to an object
Pointers and arrays
Summary of pointers
The Standard Template Library
What is a Map
Declaring a Map
Adding data to a Map
Finding data in a Map
Removing data from a Map
Checking the size of a Map
Checking for keys in a Map
Looping/iterating through the key-value pairs of a Map
The auto keyword
STL summary
The TextureHolder Class
Coding the TextureHolder header file
Coding the TextureHolder function definitions
What exactly have we achieved with TextureHolder? Building a horde of zombies
Coding the Zombie.h file
Coding the Zombie.cpp file
Using the Zombie class to create a horde
Bringing the horde to life (back to life)
Using the TextureHolder class for all textures
Change the way the background gets its textures
Change the way Player gets its texture
FAQ
Summary
9. Collision Detection, Pickups, and Bullets
Coding the Bullet class
Coding the Bullet header file
Coding the Bullet source file
Making the bullets fly
Including the Bullet class
Control variables and the bullet array
Reloading the gun
Shooting a bullet
Updating the bullets each frame
Drawing the bullets each frame
Giving the player a crosshair
Coding a class for pickups
Coding the Pickup header file
Coding the Pickup class function definitions
Using the Pickup class
Detecting collisions
Has a zombie been shot?
Has the player been touched by a zombie? Has the player touched a pickup?
FAQ
Summary
10. Layering Views and Implementing the HUD
Adding all the Text and HUD objects
Updating the HUD each frame
Drawing the HUD, and the home and level up screens
FAQ
Summary
11. Sound Effects, File I/O, and Finishing the Game
Saving and loading the high-score
Preparing sound effects
Leveling up
Restarting the game
Playing the rest of the sounds
Adding sound effects while the player is reloading Make a shooting sound