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 highlighted code, inside the existing code, and then we will go through 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 see in the new code is this:
while (window.isOpen())
{
The very last thing we see in the new code is a closing }
. We have created a while
loop. Everything between the opening {
and closing }
of the while
loop will continue to execute, over and over, potentially forever.
Look closely between the parentheses (...)
of the while
loop as shown highlighted in the next code:
while (window.isOpen())
The full explanation of this code will have to wait until we discuss loops and conditions in Chapter 4: Loops, Arrays, Switch, 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 onto the next statement. Exactly how a window is closed will be 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 see what, at first glance, might look a bit like ASCII art:
/* **************************************** Handle the player's input **************************************** */
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 .
This 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 this 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.
Jump to the next C-style comment and it is plain 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:
- Get the player's input (if any).
- Update the scene based on things such as artificial intelligence, physics, or the player's input.
- Draw the current scene.
- Repeat the above at a fast enough rate to create a smooth and 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 labeled Handle the player's input
we have the following code:
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
This code checks whether the Escape key is currently 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
At the moment 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 do is to rub out the previous frame of animation using the code:
window.clear();
What we do now is draw each and every object from the game. At the moment, however, we don't have any game objects.
The next line of code is this:
window.display();
When we draw all the game objects, we are drawing them to a hidden surface ready to be displayed. The code window.display()
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. The process is called double buffering.
Also, notice 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 until you press the Esc keyboard key.