Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning C++ Game Programming

You're reading from   Beginning C++ Game Programming Learn to program with C++ by building fun games

Arrow left icon
Product type Paperback
Published in Oct 2019
Publisher Packt
ISBN-13 9781838648572
Length 746 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (25) Chapters Close

Preface 1. Chapter 1: C++, SFML, Visual Studio, and Starting the First Game 2. Chapter 2: Variables, Operators, and Decisions – Animating Sprites FREE CHAPTER 3. Chapter 3: C++ Strings and SFML Time – Player Input and HUD 4. Chapter 4: Loops, Arrays, Switches, Enumerations, and Functions – Implementing Game Mechanics 5. Chapter 5: Collisions, Sound, and End Conditions – Making the Game Playable 6. Chapter 6: Object-Oriented Programming – Starting the Pong Game 7. Chapter 7: Dynamic Collision Detection and Physics – Finishing the Pong Game 8. Chapter 8: SFML Views – Starting the Zombie Shooter Game 9. Chapter 9: C++ References, Sprite Sheets, and Vertex Arrays 10. Chapter 10: Pointers, the Standard Template Library, and Texture Management 11. Chapter 11: Collision Detection, Pickups, and Bullets 12. Chapter 12: Layering Views and Implementing the HUD 13. Chapter 13: Sound Effects, File I/O, and Finishing the Game 14. Chapter 14: Abstraction and Code Management – Making Better Use of OOP 15. Chapter 15: Advanced OOP – Inheritance and Polymorphism 16. Chapter 16: Building Playable Levels and Collision Detection 17. Chapter 17: Sound Spatialization and the HUD 18. Chapter 18: Particle Systems and Shaders 19. Chapter 19: Game Programming Design Patterns – Starting the Space Invaders ++ Game 20. Chapter 20: Game Objects and Components 21. Chapter 21: File I/O and the Game Object Factory 22. Chapter 22: Using Game Objects and Building a Game 23. Chapter 23: Before You Go... 24. Other Books You May Enjoy

The main game loop

We need a way to stay in the program until the player wants to quit. At the same time, we should clearly mark out where the different parts of our code will go as we progress with Timber!!!. Furthermore, if we are going to stop our game from exiting, we had better provide a way for the player to exit when they are ready; otherwise, the game will go on forever!

Add the following highlighted code to the existing code and then we will go through it and discuss it all:

int main()
{
    // Create a video mode object
    VideoMode vm(1920, 1080);
    // Create and open a window for the game
    RenderWindow window(vm,	“Timber!!!”, Style::Fullscreen);
    while (window.isOpen())
    {
        /*
        ****************************************
        Handle the players input
        ****************************************
        */
        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
            window.close();
        }
        /*
        ****************************************
        Update the scene
        ****************************************
        */
        /*
        ****************************************
        Draw the scene
        ****************************************
        */
        // Clear everything from the last frame
        window.clear();
        // Draw our game scene here
        // Show everything we just drew
        window.display();
    }
    return 0;
}

While loops

The very first thing we saw in the new code is as follows:

while (window.isOpen())
{

The very last thing we saw in the new code is a closing }. We have created a while loop. Everything between the opening ({) and closing (}) brackets of the while loop will continue to execute, over and over, potentially forever.

Look closely between the parentheses (...) of the while loop, as shown here:

while (window.isOpen())

The full explanation of this code will have to wait until we discus loops and conditions in Chapter 4, Loops, Arrays, Switches, Enumerations, and Functions – Implementing Game Mechanics. What is important for now is that when the window object is set to closed, the execution of the code will break out of the while loop and move on to the next statement. Exactly how a window is closed is covered soon.

The next statement is, of course, return 0;, which ends our game.

We now know that our while loop will whiz round and round, repeatedly executing the code within it, until our window object is set to closed.

C-style code comments

Just inside the while loop, we can see what, at first glance, might look a bit like ASCII art:

        /*
        ****************************************
        Handle the player’s input
        ****************************************
        */

Important note

ASCII art is a niche but fun way of creating images with computer text. You can read more about it here: https://en.wikipedia.org/wiki/ASCII_art.

The previous code is simply another type of comment. This type of comment is known as a C-style comment. The comment begins with (/*) and ends with (*/). Anything in between is just for information and is not compiled. I have used this slightly elaborate text to make it absolutely clear what we will be doing in each part of the code file. And of course, you can now work out that any code that follows will be related to handling the player’s input.

Skip over a few lines of code and you will see that we have another C-style comment, announcing that in that part of the code, we will be updating the scene.

If you jump to the next C-style comment, it will be clear where we will be drawing all the graphics.

Input, update, draw, repeat

Although this first project uses the simplest possible version of a game loop, every game will need these phases in the code. Let’s go over the steps:

  1. Get the player’s input (if any).
  2. Update the scene based on things such as artificial intelligence, physics, or the player’s input.
  3. Draw the current scene.
  4. Repeat these steps at a fast-enough rate to create a smooth, animated game world.

Now, let’s look at the code that actually does something within the game loop.

Detecting a key press

Firstly, within the section that’s identifiable by the comment with the Handle the player’s input text, we have the following code:

if (Keyboard::isKeyPressed(Keyboard::Escape))
{
        window.close();
}

This code checks whether the Esc key is currently being pressed. If it is, the highlighted code uses the window object to close itself. Now, the next time the while loop begins, it will see that the window object is closed and jump to the code immediately after the closing curly brace of the while loop and the game will exit. We will discuss if statements more fully in Chapter 2, Variables, Operators, and Decisions – Animating Sprites.

Clearing and drawing the scene

Currently, there is no code in the Update the scene section, so let’s move on to the Draw the scene section.

The first thing we will do is rub out the previous frame of animation using the following code:

window.clear();

What we would do now is draw every object from the game. However, we don’t have any game objects.

The next line of code is as follows:

window.display();

When we draw all the game objects, we are drawing them to a hidden surface ready to be displayed. The window.display() code flips from the previously displayed surface to the newly updated (previously hidden) one. This way, the player will never see the drawing process as the surface has all the sprites added to it. It also guarantees that the scene will be complete before it is flipped. This prevents a graphical glitch known as tearing. This process is called double buffering.

Also note that all this drawing and clearing functionality is performed using our window object, which was created from the SFML RenderWindow class.

Running the game

Run the game and you will get a blank, full screen window that remains open until you press the Esc key.

That is good progress. At this stage, we have an executing program that opens a window and loops around, waiting for the player to press the Esc key to exit. Now, we are able to move on to drawing the background image of the game.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image