Beginning C++ Game Programming
Book information
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." Credits About the Author About the Reviewer www.PacktPub.com Table of Contents Preface 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 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 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 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 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 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 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 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 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 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 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 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 Play a sound when the player is hit Play a sound when getting a pickup Make a splat sound when a zombie is shot FAQ Summary Abstraction and Code Management – Making Better Use of OOP The Thomas Was Late game Features of Thomas Was Late Creating a project from the template The project assets Game level designs GLSL Shaders The graphical assets close-up The sound assets close-up Adding the assets to the project Structuring the Thomas Was Late code Building the game engine Reusing the TextureHolder class Coding Engine.h Coding Engine.cpp Coding the Engine class constructor definition Coding the run function definition Coding the input function definition Coding the update function definition Coding the draw function definition The Engine class so far Coding the main function FAQ Summary Advanced OOP – Inheritance and Polymorphism Inheritance Extending a class Polymorphism Abstract classes – virtual and pure virtual functions Building the PlayableCharacter class Coding PlayableCharacter.h Coding PlayableCharacter.cpp Building the Thomas and Bob classes Coding Thomas.h Coding Thomas.cpp Coding Bob.h Coding Bob.cpp Updating the game engine to use Thomas and Bob Updating Engine.h to add an instance of Bob and Thomas Updating the input function to control Thomas and Bob Updating the update function to spawn and update the PlayableCharacter instances Spawning Thomas and Bob Updating Thomas and Bob each frame Drawing Bob and Thomas FAQ Summary Building Playable Levels and Collision Detection Designing some levels Building the LevelManager class Coding LevelManager.h Coding the LevelManager.cpp file Coding the loadLevel function Updating the engine Collision detection Coding the detectCollisions function More collision detection Summary Sound Spatialization and HUD What is Spatialization? Emitters, attenuation, and listeners How SFML handles spatialization Building the SoundManager class Coding SoundManager.h Coding the SoundManager.cpp file Coding the constructor Coding the playFire function Coding the rest of the SoundManager functions Adding SoundManager to the game engine Populating the sound emitters Coding the populateEmitters function Playing sounds The HUD class Coding HUD.h Coding the HUD.cpp file Using the HUD class Summary Extending SFML Classes, Particle Systems, and Shaders The SFML Drawable class An alternative to inheriting from Drawable Why it is best to inherit from Drawable? Building a particle system Coding the Particle class Coding Particle.h Coding the Particle.cpp file Coding the ParticleSystem class Coding ParticleSystem.h Coding the ParticleSystem.cpp file Using ParticleSystem Adding a ParticleSystem object to the Engine class Initializing ParticleSystem Updating the particle system in each frame Starting the particle system Drawing the particle system OpenGL, shaders, and GLSL The programmable pipeline and shaders Coding a fragment shader Coding a vertex shader Adding shaders to the Engine class Loading the shaders Updating and drawing the shader in each frame Summary Before you go... Thanks! Index
Similar books
Political Obligation
2010 · PDF
Beginning C++ Game Programming: Learn C++ from scratch by building fun games
2024 · PDF
Growing Up and Getting By: International Perspectives on Childhood and Youth in Hard Times
2021 · EPUB
Android Programming for Beginners: Build in-depth, full-featured Android apps starting from zero programming experience, 3rd Edition. Code
2021 · ZIP
Learning Java by Building Android Games: Learn Java and Android from scratch by building five exciting games, 3rd Edition
2021 · PDF
Learning Java by Building Android Games: Learn Java and Android from scratch by building five exciting games, 3rd Edition
2021 · EPUB
Android Programming for Beginners: Build in-depth, full-featured Android apps starting from zero programming experience
2021 · EPUB
Android Programming for Beginners: Build in-depth, full-featured Android apps starting from zero programming experience, 3rd Edition
2021 · PDF