Beginning C++ Game Programming
上QQ阅读APP看书,第一时间看更新

Starting to code the game

Open up Visual Studio if it isn't already, open up the Timber project (if it isn't already open) by left-clicking it from the Recent list on the main Visual Studio window.

The first thing we will do is to rename our main code file. It is currently called HelloSFML.cpp and we will rename it to the more appropriate Timber.cpp. The .cpp stands for C plus plus.

  1. Find the Solution Explorer window on the right-hand side.
  2. Locate the HelloSFML.cpp file under the Source Files folder.
  3. Right-click HelloSFML.cpp and choose Rename.
  4. Edit the filename to Timber.cpp and press Enter.

Make some minor edits in the code window so that you have exactly the same code as shown next. You can do so in exactly the same way that you would with any text editor or word processor; you could even copy and paste it if you prefer. After you have made the slight edits, we can talk about them:

// Include important C++ libraries here 
#include "stdafx.h"  
int main() 
{ 
   return 0; 
} 

This simple C++ program is a good place to start. Let's go through it line by line

Making code clearer with comments

As you can see, the only code that needed to change was a little bit at the very top. The first line of code is this:

// Include important C++ libraries here 
 

Any line of code that starts with // is a comment and is ignored by the compiler. As such, this line of code does nothing. It is used to leave any information that we might find useful when we come back to the code at a later date. The comment ends at the end of the line, so anything on the next line is not part of the comment. There is another type of comment called a multi-line or c-style comment, which can be used to leave comments that take up more than a single line. We will see some of them later in this chapter. Throughout this book I will leave hundreds of comments to help add context and further explain the code.

#including Windows essentials

Now that you know what comments are for, you can probably take a decent guess at what the next line of code does. Here it is again:

#include "stdafx.h" 

The #include  directive tells Visual Studio to include, or add the contents of another file before compiling. The effect of this is that some other code, that we have not written ourselves, will be a part of our program when we run it. The process of adding code from other files into our code is called pre-processing and, perhaps unsurprisingly, is performed by something called a pre-processor. The file extension .h stands for header file.

You might be wondering what this code will do? The stdafx.h file actually contains more #include directives itself. It adds into our program, all the necessary code that is required to run our program on Windows. We will never need to see this file and definitely don't need to concern ourselves with what is in it. We just need to add the line of code at the top of every game that we make.

What is more significant and relevant to us, and the reason it is worth discussing the #include directive, is that we will add many more #include directives at the top of our code files. This is to include code that we will use and take the trouble to understand.

The main files that we will be including are the SFML header files, which give us access to all the cool game coding features. We will also use #include to access the C++ Standard Library header files. These header files give us access to core features of the C++ language itself.

That's two lines squared away, so let's move on.

The main function

The next line we see in our code is this:

int main() 

The code int is what is known as a type. C++ has many types and they represent different types of data. An int is an integer or whole number. Hold that thought and we will come back to it in a minute.

The main() code part is the name of the section of code that follows. This section of code is marked out between the opening curly brace { and the next closing curly brace }.

So, everything in between these curly braces {...} is a part of main. We call a section of code like this, a function.

Every C++ program has a main function and it is the place where the execution (running) of the entire program will start. As we progress through the book, eventually our games will have many code files. However, there will only ever be one main function, and no matter what code we write, our game will always begin execution from the first line of code inside the opening curly brace of the main function.

For now, don't worry about the strange brackets that follow the function name (). We will discuss them further in Chapter 4: Loops, Arrays, Switch, Enumerations, and Functions-Implementing Game Mechanics, where we will get to see functions in a whole new and more interesting light.

Let's look closely at the one single line of code within our Main function.

Presentation and syntax

Take a look at the entirety of our Main function again:

int main() 
{ 
   return 0; 
} 

We can see that inside Main there is just one single line of code, return 0;. Before we move on to find out what this line of code does, let's look at how it is presented. This is useful because it can help us prepare to write code that is easy to read, and distinguish, from other parts of our code.

First notice that return 0; is indented to the right by one tab. This clearly marks it out as being internal to the main function. As our code grows in length we will see that indenting our code and leaving white space will be essential to maintaining readability.

Next, notice the punctuation on the end of the line. A semicolon ; tells the compiler that it is the end of the instruction and whatever follows it is a new instruction. We call an instruction terminated by a semicolon, a statement.

Note that the compiler doesn't care whether you leave a new line or even a space between the semicolon and the next statement. However, not starting a new line for each statement will lead to desperately hard-to-read code, and missing the semicolon altogether will result in a syntax error so that the game will not compile or run.

A section of code together, often denoted by its indentation with the rest of the section, is called a block.

Now that you are comfortable with the idea of the main function, indenting your code to keep it tidy and putting a semicolon on the end of each statement, we can move on to find out exactly what the return 0; statement actually does.

Returning values from a function

Actually, return 0; does almost nothing in the context of our game. The concept, however, is an important one. When we use the return keyword, either on its own or followed by a value, it is an instruction for the program execution to jump/move back to the code that got the function started in the first place.

Often this code that got the function started will be yet another function somewhere else in our code. In this case, however, it is the operating system that started the main function. So, when the line return 0; is executed, the main function exits and the entire program ends.

As we have a 0 after the return keyword, that value is also sent to the operating system. We could change the value of zero to something else and that value would be sent back instead.

We say that the code that starts a function, calls the function, and that the function returns the value.

You don't need to fully grasp all this function information just yet. It is just useful to introduce it here. There's one last thing on functions before we move on. Remember the int from int Main()? That tells the compiler that the type of value returned from Main must be an int (integer/whole number). We can return any value that qualifies as an int. Perhaps 0, 1, 999, 6358, and so on. If we try and return something that isn't an int, perhaps 12.76, then the code won't compile and the game won't run.

Functions can return a big selection of different types, including types that we invent for ourselves! That kind of type, however, must be made known to the compiler in the way we have just seen.

This little bit of background information on functions will make things smoother as we progress.

Running the game

You can actually run the game at this point. Do so by clicking the Local Windows Debugger button in the quick-launch bar of Visual Studio, or you can use the F5 shortcut key.

You will just get a flash of a black screen. This flash is the C++ console, which we can use for debugging our game. We don't need to do this for now. What is happening is that our program is starting, executing from the first line of Main, which of course is return 0; and then immediately exiting back to the operating system.