Game programming is demanding. It pushes both programmer and hardware to
their limits. But it can also be extremely satisfying. In this chapter, you’ll be
introduced to the fundamentals of Cþþ, the standard language for AAA game
titles. Specifically, you’ll learn to:
n Display output in a console window
n Perform arithmetic computations
n Use variables to store, manipulate, and retrieve data
n Get user input
n Work with constants and enumerations
n Work with strings
Introducing Cþþ
Cþþ is leveraged by millions of programmers around the world. It’s one of the
most popular languages for writing computer applications—and the most
popular language for writing big-budget computer games.
Created by Bjarne Stroustrup, Cþþ is a direct descendant of the C language. In
fact, Cþþ retains almost all of C as a subset. However, Cþþ offers better ways
to do things and some brand-new capabilities, too.
Name:
Anonymous2013-09-09 14:48
Using Cþþ for Games
There are a variety of reasons why game programmers choose Cþþ. Here are a
few:
n It’s fast. Well-written Cþþ programs can be blazingly fast. One of
Cþþ’s design goals is performance. And if you need to squeeze out even
more performance from your programs, Cþþ allows you to use assembly
language—the lowest-level, human-readable programming language—to
communicate directly with the computer’s hardware.
n It’s flexible. Cþþ is a multi-paradigm language that supports different
styles of programming, including object-oriented programming. Unlike
some other modern languages, though, Cþþ doesn’t force one particular
style on a programmer.
n It’s well-supported. Because of its long history in the game industry,
there’s a large pool of assets available to the Cþþ game programmer,
including graphics APIs and 2D, 3D, physics, and sound engines. All of
this pre-exiting code can be leveraged by a Cþþ programmer to greatly
speed up the process of writing a new game.
Creating an Executable File
The file that you run to launch a program—whether you’re talking about a game
or a business application—is an executable file. There are several steps to
creating an executable file from Cþþ source code (a collection of instructions
in the Cþþ language). The process is illustrated in Figure 1.1.
1. First, the programmer uses an editor to write the Cþþ source code,
a file that usually has the extension .cpp. The editor is like a word
processor for programs; it allows a programmer to create, edit, and save
source code.
2. After the programmer saves a source file, he or she invokes a Cþþ
compiler—an application that reads source code and translates it into an
object file. Object files usually have the extension .obj.
3. Next, a linker links the object file to any external files as necessary, and
then creates the executable file, which generally ends with the extension
Name:
Anonymous2013-09-09 14:49
this pre-exiting code can be leveraged by a Cþþ programmer to greatly
speed up the process of writing a new game.
Creating an Executable File
The file that you run to launch a program—whether you’re talking about a game
or a business application—is an executable file. There are several steps to
creating an executable file from Cþþ source code (a collection of instructions
in the Cþþ language). The process is illustrated in Figure 1.1.
1. First, the programmer uses an editor to write the Cþþ source code,
a file that usually has the extension .cpp. The editor is like a word
processor for programs; it allows a programmer to create, edit, and save
source code.
2. After the programmer saves a source file, he or she invokes a Cþþ
compiler—an application that reads source code and translates it into an
object file. Object files usually have the extension .obj.
3. Next, a linker links the object file to any external files as necessary, and
then creates the executable file, which generally ends with the extension
Name:
Anonymous2013-09-09 14:50
then creates the executable file, which generally ends with the extension
2 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune
.exe. At this point, a user (or gamer) can run the program by launching
the executable file.
To help automate this process, it’s common for a programmer to use an all-inone
tool for development, called an IDE (Integrated Development Environment).
An IDE typically combines an editor, a compiler, and a linker, along with other
Figure 1.1
The creation of an executable file from Cþþ source code.
Introducing Cþþ 3
tools. A popular (and free) IDE for Windows is Microsoft’s Visual Cþþ Express
Edition. You can find out more about this IDE (and download a copy) at http:// www.microsoft.com/express/.
Name:
Anonymous2013-09-09 14:52
Dealing with Errors
When I described the process for creating an executable from Cþþ source, I left
out one minor detail—errors. If to err is human, then programmers are the most
human of us. Even the best programmers write code that generates errors the
first (or fifth) time through. Programmers must fix the errors and start the entire
process over. Here are the basic types of errors you’ll run into as you program in
Cþþ:
n Compile errors. These occur during code compilation. As a result, an
object file is not produced. These can be syntax errors, meaning that the
compiler doesn’t understand something. They’re often caused by something
as simple as a typo. Compilers can issue warnings, too. Although
you usually don’t have to heed the warnings, you should treat them as
errors, fix them, and recompile.
n Link errors. These occur during the linking process and may indicate
that something the program references externally can’t be found. These
errors are usually solved by adjusting the offending reference and starting
the compile/link process again.
n Run-time errors. These occur when the executable is run. If the program
does something illegal, it can crash abruptly. But a more subtle form of
run-time error, a logical error, can make the program simply behave in
unintended ways. If you’ve ever played a game where a character walked
on air (that is, a character who shouldn’t be able to walk on air), then
you’ve seen a logical error in action.