Game Development Reference
In-Depth Information
The second example is where we call Vehicle::PrintName . This form of qualification is more explicit.
This is the form of qualification I would recommend in this situation where we would be calling the
same named function from two different namespaces. The explicit qualification makes it clear which
of the namespace functions we would like to use.
This is also as complicated as we need namespaces to be for now. We will now rewrite the Text
Adventure example from the previous chapter to use multiple source files and namespaces.
Updating Text Adventure with Source Files, Header Files,
and Namespaces
By the end of Chapter 6 our Text Adventure game consisted of five functions, an enum class , and
a struct . The number of these elements will only increase as we flesh out the functionality of our
game. It would not take very long for our only source file to consist of thousands of lines of code and
be unmanageable.
In this section we are going to split the code we created in Listing 6-12 into separate files. We will
also use a namespace to group our game loop functions together. We begin by taking a look at the
Chapter7-TextAdventure.cpp file in Listing 7-5, which is included with the sample accompanying this
chapter.
Listing 7-5. Chapter7-TextAdventure.cpp
#include "stdafx.h"
#include "Player.h"
#include "GameLoop.h"
int _tmain(int argc, _TCHAR* argv[])
{
Player player;
GameLoop::WelcomePlayer(player);
bool isPlaying = true;
while (isPlaying)
{
isPlaying = GameLoop::RunGame();
}
return 0;
}
Our source file is now much shorter and much easier to read. The major differences to the function
when comparing it to the version in Listing 6-12 are the uses of the GameLoop namespace qualifier on
the WelcomePlayer and the RunGame function calls.
You can see at the top of the file that we now include two new header files, Player.h and
GameLoop.h . We'll take a look at Player.h in Listing 7-6.
 
Search WWH ::




Custom Search