فهرست مطالب :
Table of Contents
Preface xv
Chapter 1: C++, SFML, Visual Studio, and Starting the
First Game 1
The games we will build 1
Timber!!! 2
Pong 2
Zombie Arena 3
Thomas was late 4
Space Invaders ++ 4
Meet C++ 5
Microsoft Visual Studio 6
SFML 6
Setting up the development environment 7
What about Mac and Linux? 7
Installing Visual Studio 2019 Community edition 8
Setting up SFML 10
Creating a new project 12
Configuring the project properties 16
Planning Timber!!! 18
The project assets 22
Outsourcing the assets 22
Making your own sound FX 22
Adding the assets to the project 23
Exploring the assets 23
Understanding screen and internal coordinates 24
Getting started with coding the game 27
Making code clearer with comments 27
The main function 28
Table of Contents
[ ii ]
Presentation and syntax 28
Returning values from a function 29
Running the game 30
Opening a window using SFML 30
#including SFML features 31
OOP, classes, and objects 32
Using namespace sf 33
SFML VideoMode and RenderWindow 34
Running the game 35
The main game loop 35
While loops 37
C-style code comments 37
Input, update, draw, repeat 38
Detecting a key press 38
Clearing and drawing the scene 39
Running the game 39
Drawing the game's background 39
Preparing the Sprite using a Texture 40
Double buffering the background sprite 42
Running the game 43
Handling errors 43
Configuration errors 44
Compile errors 44
Link errors 44
Bugs 45
Summary 45
FAQ 45
Chapter 2: Variables, Operators, and Decisions –
Animating Sprites 47
C++ variables 47
Types of variables 48
User-defined types 49
Declaring and initializing variables 49
Declaring variables 49
Initializing variables 50
Declaring and initializing in one step 50
Constants 50
Declaring and initializing user-defined types 51
Manipulating variables 52
C++ arithmetic and assignment operators 52
Getting things done with expressions 53
Table of Contents
[ iii ]
Adding clouds, a tree, and a buzzing bee 56
Preparing the tree 56
Preparing the bee 57
Preparing the clouds 58
Drawing the tree, the bee, and the clouds 60
Random numbers 62
Generating random numbers in C++ 62
Making decisions with if and else 63
Logical operators 63
C++ if and else 65
If they come over the bridge, shoot them! 65
Shoot them … or else do this instead 65
Reader challenge 67
Timing 68
The frame rate problem 68
The SFML frame rate solution 69
Moving the clouds and the bee 71
Giving life to the bee 71
Blowing the clouds 75
Summary 79
FAQ 80
Chapter 3: C++ Strings and SFML Time – Player Input and HUD 81
Pausing and restarting the game 81
C++ Strings 84
Declaring Strings 84
Assigning a value to a String 85
Manipulating Strings 85
SFML's Text and Font classes 86
Implementing the HUD 87
Adding a time-bar 93
Summary 100
FAQ 100
Chapter 4: Loops, Arrays, Switches, Enumerations, and
Functions – Implementing Game Mechanics 101
Loops 102
while loops 102
Breaking out of a while loop 104
for loops 105
Arrays 106
Declaring an array 107
Table of Contents
[ iv ]
Initializing the elements of an array 107
Quickly initializing the elements of an array 108
What do these arrays really do for our games? 108
Making decisions with switch 109
Class enumerations 111
Getting started with functions 113
Function return types 114
Function names 117
Function parameters 118
The function body 118
Function prototypes 119
Organizing functions 120
Function gotcha! 120
More on functions 121
An absolute final word on functions – for now 121
Growing the branches 122
Preparing the branches 123
Updating the branch sprites each frame 124
Drawing the branches 126
Moving the branches 127
Summary 129
FAQ 130
Chapter 5: Collisions, Sound, and End Conditions –
Making the Game Playable 131
Preparing the player (and other sprites) 131
Drawing the player and other sprites 133
Handling the player's input 135
Handling setting up a new game 136
Detecting the player chopping 137
Detecting a key being released 141
Animating the chopped logs and the axe 142
Handling death 145
Simple sound FX 147
How SFML sound works 147
When to play the sounds 148
Adding the sound code 148
Improving the game and the code 151
Summary 153
FAQ 153
Table of Contents
[ v ]
Chapter 6: Object-Oriented Programming – Starting the
Pong Game 155
OOP 155
Encapsulation 156
Polymorphism 157
Inheritance 157
Why use OOP? 157
What exactly is a class? 158
The theory of a Pong Bat 159
The class variable and function declarations 159
The class function definitions 161
Using an instance of a class 163
Creating the Pong project 164
Coding the Bat class 166
Coding Bat.h 166
Constructor functions 168
Continuing with the Bat.h explanation 168
Coding Bat.cpp 169
Using the Bat class and coding the main function 172
Summary 177
FAQ 177
Chapter 7: Dynamic Collision Detection and Physics –
Finishing the Pong Game 179
Coding the Ball class 179
Using the Ball class 183
Collision detection and scoring 185
Running the game 188
Summary 188
FAQ 188
Chapter 8: SFML Views – Starting the Zombie Shooter Game 191
Planning and starting the Zombie Arena game 192
Creating a new project 193
The project assets 195
Exploring the assets 196
Adding the assets to the project 197
OOP and the Zombie Arena project 197
Building the player – the first class 198
Coding the Player class header file 199
Coding the Player class function definitions 206
Table of Contents
[ vi ]
Controlling the game camera with SFML View 215
Starting the Zombie Arena game engine 218
Managing the code files 222
Starting to code the main game loop 224
Summary 233
FAQ 234
Chapter 9: C++ References, Sprite Sheets, and Vertex Arrays 235
C++ references 236
References summary 239
SFML vertex arrays and sprite sheets 240
What is a sprite sheet? 240
What is a vertex array? 241
Building a background from tiles 242
Building a vertex array 242
Using the vertex array to draw 244
Creating a randomly generated scrolling background 245
Using the background 251
Summary 254
FAQ 255
Chapter 10: Pointers, the Standard Template Library, and
Texture Management 257
Learning about Pointers 258
Pointer syntax 259
Declaring a pointer 260
Initializing a pointer 261
Reinitializing pointers 262
Dereferencing a pointer 262
Pointers are versatile and powerful 264
Dynamically allocated memory 264
Passing a pointer to a function 266
Declaring and using a pointer to an object 267
Pointers and arrays 268
Summary of pointers 269
The Standard Template Library 269
What is a map? 270
Declaring a map 271
Adding data to a Map 271
Finding data in a map 271
Removing data from a map 272
Checking the size of a map 272
Checking for keys in a map 272
Table of Contents
[ vii ]
Looping/iterating through the key-value pairs of a map 273
The auto keyword 274
STL summary 274
The TextureHolder class 274
Coding the TextureHolder header file 275
Coding the TextureHolder function definitions 276
What have we achieved with TextureHolder? 278
Building a horde of zombies 278
Coding the Zombie.h file 279
Coding the Zombie.cpp file 282
Using the Zombie class to create a horde 287
Bringing the horde to life (back to life) 291
Using the TextureHolder class for all textures 297
Changing the way the background gets its textures 297
Changing the way the Player gets its texture 298
Summary 299
FAQ 299
Chapter 11: Collision Detection, Pickups, and Bullets 301
Coding the Bullet class 302
Coding the Bullet header file 302
Coding the Bullet source file 305
Making the bullets fly 310
Including the Bullet class 310
Control variables and the bullet array 311
Reloading the gun 311
Shooting a bullet 314
Updating the bullets each frame 315
Drawing the bullets each frame 316
Giving the player a crosshair 317
Coding a class for pickups 321
Coding the Pickup header file 321
Coding the Pickup class function definitions 325
Using the Pickup class 330
Detecting collisions 334
Has a zombie been shot? 335
Has the player been touched by a zombie? 338
Has the player touched a pickup? 339
Summary 340
FAQ 340
Table of Contents
[ viii ]
Chapter 12: Layering Views and Implementing the HUD 341
Adding all the Text and HUD objects 341
Updating the HUD 345
Drawing the HUD, home, and level-up screens 348
Summary 351
FAQ 352
Chapter 13: Sound Effects, File I/O, and Finishing the Game 353
Saving and loading the high score 353
Preparing sound effects 355
Leveling up 357
Restarting the game 360
Playing the rest of the sounds 360
Adding sound effects while the player is reloading 361
Making a shooting sound 361
Playing a sound when the player is hit 362
Playing a sound when getting a pickup 363
Making a splat sound when a zombie is shot 364
Summary 366
FAQ 366
Chapter 14: Abstraction and Code Management – Making
Better Use of OOP 367
The Thomas Was Late game 368
Features of Thomas Was Late 368
Creating the project 372
The project's assets 373
Game level designs 373
GLSL shaders 374
The graphical assets close up 374
The sound assets close up 374
Adding the assets to the project 375
Structuring the Thomas Was Late code 376
Building the game engine 378
Reusing the TextureHolder class 378
Coding Engine.h 381
Coding Engine.cpp 385
Coding the Engine class constructor definition 385
Coding the run function definition 387
Coding the input function definition 388
Coding the update function definition 390
Coding the draw function definition 391
The Engine class so far 393
Table of Contents
[ ix ]
Coding the main function 394
Summary 396
FAQ 396
Chapter 15: Advanced OOP – Inheritance and Polymorphism 397
Inheritance 397
Extending a class 398
Polymorphism 400
Abstract classes – virtual and pure virtual functions 401
Building the PlayableCharacter class 403
Coding PlayableCharacter.h 404
Coding PlayableCharacter.cpp 409
Building the Thomas and Bob classes 414
Coding Thomas.h 415
Coding Thomas.cpp 415
Coding Bob.h 417
Coding Bob.cpp 418
Updating the game engine to use Thomas and Bob 420
Updating Engine.h to add an instance of Bob and Thomas 420
Updating the input function to control Thomas and Bob 421
Updating the update function to spawn and update the
PlayableCharacter instances 422
Spawning Thomas and Bob 423
Updating Thomas and Bob each frame 424
Drawing Bob and Thomas 427
Summary 431
FAQ 431
Chapter 16: Building Playable Levels and Collision Detection 433
Designing some levels 434
Building the LevelManager class 438
Coding LevelManager.h 439
Coding the LevelManager.cpp file 441
Coding the loadLevel function 448
Updating the engine 451
Collision detection 455
Coding the detectCollisions function 455
More collision detection 462
Summary 463
Chapter 17: Sound Spatialization and the HUD 465
What is spatialization? 465
Emitters, attenuation, and listeners 466
Table of Contents
[ x
]
Handling spatialization using SFML 466
Building the SoundManager class 469
Coding SoundManager.h 469
Coding the SoundManager.cpp file 471
Coding the constructor 471
Coding the playFire function 473
Coding the rest of the SoundManager functions 474
Adding SoundManager to the game engine 475
Populating the sound emitters 476
Coding the populateEmitters function 477
Playing sounds 479
Implementing the HUD class 482
Coding HUD.h 482
Coding the HUD.cpp file 483
Using the HUD class 486
Summary 490
Chapter 18: Particle Systems and Shaders 491
Building a particle system 491
Coding the Particle class 493
Coding Particle.h 493
Coding the Particle.cpp file 494
Coding the ParticleSystem class 495
Exploring SFML's Drawable class and OOP 495
An alternative to inheriting from Drawable 497
Coding ParticleSystem.h 498
Coding the ParticleSystem.cpp file 500
Using the ParticleSystem object 503
Adding a ParticleSystem object to the Engine class 503
Initializing ParticleSystem 504
Updating the particle system each frame 506
Starting the particle system 506
Drawing the particle system 508
OpenGL, Shaders, and GLSL 511
The programmable pipeline and shaders 512
Coding a fragment shader 513
Coding a vertex shader 514
Adding shaders to the engine class 515
Loading the shaders 515
Updating and drawing the shader 516
Summary 519
Table of Contents
[ xi ]
Chapter 19: Game Programming Design Patterns –
Starting the Space Invaders ++ Game 521
Space Invaders ++ 522
Why Space Invaders ++? 524
Design patterns 525
Screen, InputHandler, UIPanel, and Button 526
Entity-Component pattern 528
Why lots of diverse object types are hard to manage 528
Using a generic GameObject for better code structure 529
Prefer composition over inheritance 530
Factory pattern 532
C++ smart pointers 534
Shared pointers 534
Unique pointers 536
Casting smart pointers 537
C++ assertions 538
Creating the Space Invaders ++ project 539
Organizing code files with filters 540
Adding a DevelopState file 541
Coding SpaceInvaders ++.cpp 541
Coding the GameEngine class 542
Coding the SoundEngine class 545
Coding the ScreenManager class 547
Coding the BitmapStore class 550
Coding the ScreenManagerRemoteControl class 552
Where are we now? 553
Coding the Screen class and its dependents 554
Coding the Button class 554
Coding the UIPanel class 556
Coding the InputHandler class 560
Coding the Screen class 565
Adding the WorldState.h file 568
Coding the derived classes for the select screen 569
Coding the SelectScreen class 569
Coding the SelectInputHandler class 571
Coding the SelectUIPanel class 573
Coding the derived classes for the game screen 576
Coding the GameScreen class 576
Coding the GameInputHandler class 580
Coding the GameUIPanel class 581
Table of Contents
[ xii
]
Coding the GameOverInputHandler class 583
Coding the GameOverUIPanel class 584
Running the game 586
Summary 588
Chapter 20: Game Objects and Components 589
Preparing to code the components 590
Coding the Component base class 590
Coding the collider components 591
Coding the ColliderComponent class 592
Coding the RectColliderComponent class 593
Coding the graphics components 596
Coding the GraphicsComponent class 596
Coding the StandardGraphicsComponent class 598
Coding the TransformComponent class 600
Coding update components 603
Coding the UpdateComponent class 603
Coding the BulletUpdateComponent class 604
Coding the InvaderUpdateComponent class 609
Explaining the update function 614
Explaining the dropDownAndReverse function 615
Explaining the isMovingRight function 616
Explaining the initializeBulletSpawner function 616
Coding the PlayerUpdateComponent class 616
Coding the GameObject class 622
Explaining the GameObject class 628
Explaining the update function 628
Explaining the draw function 629
Explaining the getGraphicsComponent function 630
Explaining the getTransformComponent function 630
Explaining the addComponent function 630
Explaining the getter and setter functions 632
Explaining the start function 633
Explaining the getComponentByTypeAndSpecificType function 633
Explaining the getEncompassingRectCollider function 634
Explaining the getEncompassingRectColliderTag function 635
Explaining the getFirstUpdateComponent function 635
Explaining the final getter functions 635
Summary 636
Chapter 21: File I/O and the Game Object Factory 637
The structure of the file I/O and factory classes 638
Describing an object in the world 640
Coding the GameObjectBlueprint class 642
Coding the ObjectTags class 646
Table of Contents
[ xiii ]
Coding the BlueprintObjectParser class 648
Coding the PlayModeObjectLoader class 652
Coding the GameObjectFactoryPlayMode class 654
Coding the GameObjectSharer class 658
Coding the LevelManager class 659
Updating the ScreenManager and
ScreenManagerRemoteControl classes 663
Where are we now? 664
Summary 666
Chapter 22: Using Game Objects and Building a Game 667
Spawning bullets 668
Coding the BulletSpawner class 668
Updating GameScreen.h 669
Handling the player's input 671
Using a gamepad 675
Coding the PhysicsEnginePlayMode class 678
Making the game 687
Understanding the flow of execution and debugging 692
Reusing the code to make a different game and building a
design mode 695
Summary 698
Chapter 23: Before You Go... 699
Thanks! 700
Other Books You May Enjoy 701
Index 705