Game Development Reference
In-Depth Information
Chapter 7
Organizing Projects Using Files
and Namespaces
In Chapter 6 we ended with a file that contains functions and structures that were related to completely
different high-level concepts. Some were there to control the game loop, others to obtain user input and
write output, and a structure to represent the player model. Eventually this code would become difficult
to manage and work with if we were to continue down this path of adding everything to a single file.
We have already seen that we can split new functions into declarations and definitions as well as
being able to use the # include directive to be able to use functionality that is contained within
separate files. All of the examples we have used so far have used the using namespace std;
directive. In this chapter we look at how we can organize our own programs into separate files and
also create namespaces to group together functionality. To wrap up, we'll use what we learn to
reorganize our Text Adventure game to allow us to expand more easily in the coming chapters.
Source and Header Files
C++ supports the ability to separate code out into separate source and header files to better
organize programs. The header files of a project contain declarations of data types and functions
that can then be used in source files. Source files contain the definitions of functions that are built by
the compiler. Listing 7-1 shows the source code contained in a file named HelloWorld.cpp .
Listing 7-1. HelloWorld.cpp
#include <iostream>
using namespace std;
void HelloWorld()
{
cout << "Hello World!";
}
71
 
Search WWH ::




Custom Search