Beginning C++ Game Programming
Book information
Description
Cover......Page 1 Copyright......Page 3 About PACKT......Page 5 Contributors......Page 6 Table of Contents......Page 8 Preface......Page 22 The games we will build......Page 32 Pong......Page 33 Zombie Arena......Page 34 Space Invaders ++......Page 35 Meet C++......Page 36 SFML......Page 37 What about Mac and Linux?......Page 38 Installing Visual Studio 2019 Community edition......Page 39 Setting up SFML......Page 41 Creating a new project......Page 43 Configuring the project properties......Page 47 Planning Timber!!!......Page 49 Making your own sound FX......Page 53 Exploring the assets......Page 54 Understanding screen and internal coordinates......Page 55 Making code clearer with comments......Page 58 Presentation and syntax......Page 59 Returning values from a function......Page 60 Opening a window using SFML......Page 61 #including SFML features......Page 62 OOP, classes, and objects......Page 63 Using namespace sf......Page 64 SFML VideoMode and RenderWindow......Page 65 The main game loop......Page 66 C-style code comments......Page 68 Detecting a key press......Page 69 Drawing the game's background......Page 70 Preparing the Sprite using a Texture......Page 71 Double buffering the background sprite......Page 73 Handling errors......Page 74 Link errors......Page 75 FAQ......Page 76 C++ variables......Page 78 Types of variables......Page 79 Declaring variables......Page 80 Constants......Page 81 Declaring and initializing user-defined types......Page 82 C++ arithmetic and assignment operators......Page 83 Getting things done with expressions......Page 84 Preparing the tree......Page 87 Preparing the bee......Page 88 Preparing the clouds......Page 89 Drawing the tree, the bee, and the clouds......Page 91 Generating random numbers in C++......Page 93 Logical operators......Page 94 Shoot them … or else do this instead......Page 96 Reader challenge......Page 98 The frame rate problem......Page 99 The SFML frame rate solution......Page 100 Giving life to the bee......Page 102 Blowing the clouds......Page 106 Summary......Page 110 FAQ......Page 111 Pausing and restarting the game......Page 112 Declaring Strings......Page 115 Manipulating Strings......Page 116 SFML's Text and Font classes......Page 117 Implementing the HUD......Page 118 Adding a time-bar......Page 124 FAQ......Page 131 Chapter 4: Loops, Arrays, Switch, Enumerations, and Functions – Implementing Game Mechanics......Page 132 while loops......Page 133 Breaking out of a while loop......Page 135 for loops......Page 136 Arrays......Page 137 Initializing the elements of an array......Page 138 What do these arrays really do for our games?......Page 139 Making decisions with switch......Page 140 Class enumerations......Page 142 Getting started with functions......Page 144 Function return types......Page 145 Function names......Page 148 The function body......Page 149 Function prototypes......Page 150 Function gotcha!......Page 151 An absolute final word on functions – for now......Page 152 Growing the branches......Page 153 Preparing the branches......Page 154 Updating the branch sprites each frame......Page 155 Drawing the branches......Page 157 Moving the branches......Page 158 Summary......Page 160 FAQ......Page 161 Preparing the player (and other sprites)......Page 162 Drawing the player and other sprites......Page 164 Handling the player's input......Page 166 Handling setting up a new game......Page 167 Detecting the player chopping......Page 168 Detecting a key being released......Page 172 Animating the chopped logs and the axe......Page 173 Handling death......Page 176 How SFML sound works......Page 178 Adding the sound code......Page 179 Improving the game and the code......Page 182 FAQ......Page 184 OOP......Page 186 Encapsulation......Page 187 Why use OOP?......Page 188 What exactly is a class?......Page 189 The class variable and function declarations......Page 190 The class function definitions......Page 192 Using an instance of a class......Page 194 Creating the Pong project......Page 195 Coding Bat.h......Page 197 Continuing with the Bat.h explanation......Page 199 Coding Bat.cpp......Page 200 Using the Bat class and coding the main function......Page 203 FAQ......Page 208 Coding the Ball class......Page 210 Using the Ball class......Page 214 Collision detection and scoring......Page 216 FAQ......Page 219 Chapter 8: SFML Views – Starting the Zombie Shooter Game......Page 222 Planning and starting the Zombie Arena game......Page 223 Creating a new project......Page 224 The project assets......Page 226 Exploring the assets......Page 227 OOP and the Zombie Arena project......Page 228 Building the player – the first class......Page 229 Coding the Player class header file......Page 230 Coding the Player class function definitions......Page 237 Controlling the game camera with SFML View......Page 246 Starting the Zombie Arena game engine......Page 249 Managing the code files......Page 253 Starting to code the main game loop......Page 255 Summary......Page 264 FAQ......Page 265 Chapter 9: C++ References, Sprite Sheets, and Vertex Arrays......Page 266 C++ references......Page 267 References summary......Page 270 What is a sprite sheet?......Page 271 What is a vertex array?......Page 272 Building a vertex array......Page 273 Using the vertex array to draw......Page 275 Creating a randomly generated scrolling background......Page 276 Using the background......Page 282 Summary......Page 285 FAQ......Page 286 Chapter 10: Pointers, the Standard Template Library, and Texture Management......Page 288 Learning about Pointers......Page 289 Pointer syntax......Page 290 Declaring a pointer......Page 291 Initializing a pointer......Page 292 Dereferencing a pointer......Page 293 Dynamically allocated memory......Page 295 Passing a pointer to a function......Page 297 Declaring and using a pointer to an object......Page 298 Pointers and arrays......Page 299 The Standard Template Library......Page 300 What is a map?......Page 301 Finding data in a map......Page 302 Checking for keys in a map......Page 303 Looping/iterating through the key-value pairs of a map......Page 304 The TextureHolder class......Page 305 Coding the TextureHolder header file......Page 306 Coding the TextureHolder function definitions......Page 307 Building a horde of zombies......Page 309 Coding the Zombie.h file......Page 310 Coding the Zombie.cpp file......Page 313 Using the Zombie class to create a horde......Page 318 Bringing the horde to life (back to life)......Page 322 Changing the way the background gets its textures......Page 328 Changing the way the Player gets its texture......Page 329 FAQ......Page 330 Chapter 11: Collision Detection, Pickups, and Bullets......Page 332 Coding the Bullet header file......Page 333 Coding the Bullet source file......Page 336 Including the Bullet class......Page 341 Reloading the gun......Page 342 Shooting a bullet......Page 345 Updating the bullets each frame......Page 346 Drawing the bullets each frame......Page 347 Giving the player a crosshair......Page 348 Coding the Pickup header file......Page 352 Coding the Pickup class function definitions......Page 356 Using the Pickup class......Page 361 Detecting collisions......Page 365 Has a zombie been shot?......Page 366 Has the player been touched by a zombie?......Page 369 Has the player touched a pickup?......Page 370 FAQ......Page 371 Adding all the Text and HUD objects......Page 372 Updating the HUD......Page 376 Drawing the HUD, home, and level-up screens......Page 379 Summary......Page 382 FAQ......Page 383 Saving and loading the high score......Page 384 Preparing sound effects......Page 386 Leveling up......Page 388 Playing the rest of the sounds......Page 391 Making a shooting sound......Page 392 Playing a sound when the player is hit......Page 393 Playing a sound when getting a pickup......Page 394 Making a splat sound when a zombie is shot......Page 395 FAQ......Page 397 Chapter 14: Abstraction and Code Management – Making Better Use of OOP......Page 398 Features of Thomas Was Late......Page 399 Creating the project......Page 403 Game level designs......Page 404 The sound assets close up......Page 405 Adding the assets to the project......Page 406 Structuring the Thomas Was Late code......Page 407 Reusing the TextureHolder class......Page 409 Coding Engine.h......Page 412 Coding the Engine class constructor definition......Page 416 Coding the run function definition......Page 418 Coding the input function definition......Page 419 Coding the update function definition......Page 421 Coding the draw function definition......Page 422 The Engine class so far......Page 424 Coding the main function......Page 425 FAQ......Page 427 Inheritance......Page 428 Extending a class......Page 429 Polymorphism......Page 431 Abstract classes – virtual and pure virtual functions......Page 432 Building the PlayableCharacter class......Page 434 Coding PlayableCharacter.h......Page 435 Coding PlayableCharacter.cpp......Page 440 Building the Thomas and Bob classes......Page 445 Coding Thomas.cpp......Page 446 Coding Bob.h......Page 448 Coding Bob.cpp......Page 449 Updating Engine.h to add an instance of Bob and Thomas......Page 451 Updating the input function to control Thomas and Bob......Page 452 Updating the update function to spawn and update the PlayableCharacter instances......Page 453 Spawning Thomas and Bob......Page 454 Updating Thomas and Bob each frame......Page 455 Drawing Bob and Thomas......Page 458 FAQ......Page 462 Chapter 16: Building Playable Levels and Collision Detection......Page 464 Designing some levels......Page 465 Building the LevelManager class......Page 469 Coding LevelManager.h......Page 470 Coding the LevelManager.cpp file......Page 472 Coding the loadLevel function......Page 479 Updating the engine......Page 482 Coding the detectCollisions function......Page 486 More collision detection......Page 493 Summary......Page 494 What is spatialization?......Page 496 Handling spatialization using SFML......Page 497 Coding SoundManager.h......Page 500 Coding the constructor......Page 502 Coding the playFire function......Page 504 Coding the rest of the SoundManager functions......Page 505 Adding SoundManager to the game engine......Page 506 Populating the sound emitters......Page 507 Coding the populateEmitters function......Page 508 Playing sounds......Page 510 Coding HUD.h......Page 513 Coding the HUD.cpp file......Page 514 Using the HUD class......Page 517 Summary......Page 521 Building a particle system......Page 522 Coding Particle.h......Page 524 Coding the Particle.cpp file......Page 525 Exploring SFML's Drawable class and OOP......Page 526 An alternative to inheriting from Drawable......Page 528 Coding ParticleSystem.h......Page 529 Coding the ParticleSystem.cpp file......Page 531 Adding a ParticleSystem object to the Engine class......Page 534 Initializing ParticleSystem......Page 535 Starting the particle system......Page 537 Drawing the particle system......Page 539 OpenGL, Shaders, and GLSL......Page 542 The programmable pipeline and shaders......Page 543 Coding a fragment shader......Page 544 Coding a vertex shader......Page 545 Loading the shaders......Page 546 Updating and drawing the shader......Page 547 Summary......Page 550 Chapter 19: Game Programming Design Patterns – Starting the Space Invaders ++ Game......Page 552 Space Invaders ++......Page 553 Why Space Invaders ++?......Page 555 Design patterns......Page 556 Screen, InputHandler, UIPanel, and Button......Page 557 Why lots of diverse object types are hard to manage......Page 559 Using a generic GameObject for better code structure......Page 560 Prefer composition over inheritance......Page 561 Factory pattern......Page 563 Shared pointers......Page 565 Unique pointers......Page 567 Casting smart pointers......Page 568 C++ assertions......Page 569 Creating the Space Invaders ++ project......Page 570 Organizing code files with filters......Page 571 Coding SpaceInvaders ++.cpp......Page 572 Coding the GameEngine class......Page 573 Coding the SoundEngine class......Page 576 Coding the ScreenManager class......Page 578 Coding the BitmapStore class......Page 581 Coding the ScreenManagerRemoteControl class......Page 583 Where are we now?......Page 584 Coding the Button class......Page 585 Coding the UIPanel class......Page 587 Coding the InputHandler class......Page 591 Coding the Screen class......Page 596 Adding the WorldState.h file......Page 599 Coding the SelectScreen class......Page 600 Coding the SelectInputHandler class......Page 602 Coding the SelectUIPanel class......Page 604 Coding the GameScreen class......Page 607 Coding the GameInputHandler class......Page 611 Coding the GameUIPanel class......Page 612 Coding the GameOverInputHandler class......Page 614 Coding the GameOverUIPanel class......Page 615 Running the game......Page 617 Summary......Page 619 Chapter 20: Game Objects and Components......Page 620 Coding the Component base class......Page 621 Coding the collider components......Page 622 Coding the ColliderComponent class......Page 623 Coding the RectColliderComponent class......Page 624 Coding the GraphicsComponent class......Page 627 Coding the StandardGraphicsComponent class......Page 629 Coding the TransformComponent class......Page 631 Coding the UpdateComponent class......Page 634 Coding the BulletUpdateComponent class......Page 635 Coding the InvaderUpdateComponent class......Page 640 Explaining the update function......Page 645 Explaining the dropDownAndReverse function......Page 646 Coding the PlayerUpdateComponent class......Page 647 Coding the GameObject class......Page 653 Explaining the update function......Page 659 Explaining the draw function......Page 660 Explaining the addComponent function......Page 661 Explaining the getter and setter functions......Page 663 Explaining the getComponentByTypeAndSpecificType function......Page 664 Explaining the getEncompassingRectCollider function......Page 665 Explaining the final getter functions......Page 666 Summary......Page 667 Chapter 21: File I/O and the Game Object Factory......Page 668 The structure of the file I/O and factory classes......Page 669 Describing an object in the world......Page 671 Coding the GameObjectBlueprint class......Page 673 Coding the ObjectTags class......Page 677 Coding the BlueprintObjectParser class......Page 679 Coding the PlayModeObjectLoader class......Page 683 Coding the GameObjectFactoryPlayMode class......Page 685 Coding the GameObjectSharer class......Page 689 Coding the LevelManager class......Page 690 Updating the ScreenManager and ScreenManagerRemoteControl classes......Page 694 Where are we now?......Page 695 Summary......Page 697 Chapter 22: Using Game Objects and Building a Game......Page 698 Coding the BulletSpawner class......Page 699 Updating GameScreen.h......Page 700 Handling the player's input......Page 702 Using a gamepad......Page 706 Coding the PhysicsEnginePlayMode class......Page 709 Making the game......Page 718 Understanding the flow of execution and debugging......Page 723 Reusing the code to make a different game and building a design mode......Page 726 Summary......Page 729 Chapter 23: Before You Go.........Page 730 Thanks!......Page 731 Other Books You May Enjoy......Page 732 Index......Page 736
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
Beginning C++ Game Programming
2016 · PDF
Android Programming for Beginners: Build in-depth, full-featured Android apps starting from zero programming experience
2021 · EPUB