Game C++ Programming
This text is a reference for learning and mastering C++ programming for game development (applied perspective). In these pages, we provide links to official code and documentation, discuss the syntax and algorithms, present code examples in self-contained sandboxes with explanations.
Copyright © 2023 Benjamin Kenwright.
The code for this book was developed by Benjamin Kenwright and is licensed under the MIT license.
The cover art was created by Benjamin Kenwright and is based on a particle explosion effect.
Special thanks to reviewers, colleagues and friends for input and discussion while developing the initial versions of the text.
Contents
Chapter 1 Introduction and Overview
- About this book
- The Journey Begins.....
- Why is Game Programming Different?
- Source Control (e.g., Repository Management - GIT and SVN)
- Limited Resources
- Road to Success
- Work Smarter Not Harder
- Why Fix Warnings and Errors?
- Don’t Be Afraid To Try New Things
- Trial and Error
- The Program Works The Same ‘Every’ Time (Not ‘Sometimes’)
- Don’t Assume - Know!
- Fast Isn’t Fast Enough
- Initialization and Randomness
- How Simple?
- Formatting and Indentation
- Why C++ and not C# or Java?
- Lower-Case and Upper-Case
- Comments? Too Much? Why Comment?
- Naming Conventions (m_, Underscore?)
- Most Importantly, Have Fun
Chapter 2 Basics C++ (Once Upon A Time...)
- Computers Only Do What You Tell Them
- What Is A Function (Or Method)?
- main() - The Journey Begins
- How To Compile Your Program? (Visual Studio)
- Hello World - Tradition...
- #include
- Don’t Put Code In The :h!
- externs
- Definition & Declaration
- What Is A Variable? (The Basics)
- Understanding Strings
- Displaying Strings With printf (e.g., dprintf - What Is The ‘%’ Sign For?)
- What About ‘cout’ and ‘cin’? More Object Orientated?
- Other Input Output Methods
- Summary
Chapter 3 Deeper Understanding
- Memory and Variables
- References ‘&’
- Fundamental Variables
- Casting - changing type
- Why Double Precision? (i.e., float Or double)
- sizeof
- Number Wrapping
- typedef (Tidier Code)
- Unions & Structs - making better use of memory variable organisation
Chapter 4 Making Decisions
- if / else Conditional Statement
- switch / case
- Don’t Confuse Operators = And ==
- Order Of Operations Matters (e.g., Multiplication, Assignment, and Addition)
- Loops (while, do − while, for)
- Infinite Loops
- ‘Break’ing The Loop
- Nesting
Chapter 5 Functions
- Function Syntax
- Declaring and Defining a function (.h/.cpp)
- Calling A Function
- Recursion
- Overloading Functions
- Default Arguments
- Variable Scope
- Static Functions And Static Variables
Chapter 6 Nuts & Bolts of Object Oriented Programming
- C++, Extensions to C
- Why Object Oriented Programming (OOP)?
- The Laws of Inheritances
- What is a class?
- Declaring A Class
- Don’t Forget The \;" At The End
- Access Specifiers (Public/Protected/Private)
- Constructors/Destructors
- Static Members
- friends
- Inheritance
- Overriding
- ‘pure’ virtual
- Copy-Constructor
- Managers - Creating/Destroying/Managing Classes
- operator overloading +/=*
Chapter 7 Understanding Memory
- Why do we need to understand memory?
- What is a pointer? (*)
- Indirect Operators
- Using pointers to objects (− >)
- Allocating and de-allocating memory
- What is in the memory? Random!
- Leaking Memory (a sinking ship)
- Constants (Const), Safety, and Reliability
- Arrays
- String Manipulation
- Converting Numbers To Strings And Back
- Passing And Returning ‘Reference (&)’, ‘Pointer (*)’ or ‘Value’
- Endianness
Chapter 8 Templates
- What Is A Template?
- Why Do We Need Templates?
- Over Using Templates
- Template Example
- Template Functions, Classes, Parameters
- T or TYPE
- Final Thoughts
Chapter 9 Errors and Exceptions
Chapter 10 Practical Aspects
Chapter 11 Simple Games
Chapter 12: Beyond (i.e., What Now?)
- Truly Mastering C++
- Graphics, DirectX, OpenGL
- Windows WinMain(..)
- Video Consoles (e.g., Sony’s PS4 and Microsoft’s XBOX)
- Error Handling
- Signed vs. Unsigned
- Pass by Value, by Reference, or by Pointer?
- Const or Not
- Exception specification