Programming Skills


People are often fearful of learning to program computers. Programming can get pretty hairy at times, but learning to program proficiently is not as difficult as it may seem. In fact, if you like to tinker with things and find out how they work, programming often seems like play.

The first step in learning to program is to understand what a program is.

What is a Computer Program?

Note

Computer programs are just complex sets of instructions. The instructions tell the computer how to perform a task, such as playing a game.


Imagine you want to bake a cake. If you're like me, you don't know how to do that. In that case, you'd probably go to a cookbook and find a recipe. The recipe is a set of instructions. If you follow the instructions exactly, you get a cake. If not, you may wind up with a foul-tasting mess.

A computer program is like a recipe. It's a set of instructions. The instructions in a program tell the computer how to be a game machine. If you write the instructions properly, you get a game. If not, you get a mess.

Each instruction in a computer program is made up of one or more statements in a programming language.

A what?

Computers don't understand human languages like English or Japanese. In fact, they don't really "understand" anything at all. But the computer's microprocessor, which is also called its central processing unit (CPU) can execute commands.

Note

Every computer has a microprocessor. You can think of the microprocessor as the computer's "brain."


To be executable, the commands in a program must be in binary. If they're not, the microprocessor won't be able to execute them.

So what's binary?

Binary is a number system. It's also called the base 2 number system. People normally use the decimal, or base 10, number system. In other words, we count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and so on. Binary is base 2, so you count 0, 1, 10, 11, 100, 101, 110, 111, 1000, and so forth. You really don't have to know the details of using binary in order to program computers. All you have to know is that a binary number is a group of 0s and 1s. All instructions you give to a microprocessor must be 0s and 1s, or the microprocessor won't be able to execute them.

When a program is running, all of its binary instructions are stored as bits in your computer's memory. A bit is simply a binary 0 or 1. Therefore, the binary number 10 is 2 bits long. In computers, the bits in memory or on a disk are grouped together. A group of 8 bits is a byte. The binary number 10011100 is 8 bits long so it fits in 1 byte. A group of 1024 bytes is a kilobyte; 1024 kilobytes is a megabyte; 1024 megabytes is a gigabyte; 1024 gigabytes is a terabyte. This is illustrated in Table 1.1.

Table 1.1. Groupings of Bits and Bytes in a Computer

A Group of...

Equals

8 bits

1 byte

1024 bytes

1 kilobyte

1024 kilobytes

1 megabyte

1024 megabytes

1 gigabyte

1024 gigabytes

1 terabyte


The C++ Programming Language

When I started in computer programming, you had to understand binary to be able to write programs. There wasn't really any way to avoid it. These days, that's not necessary. Consider yourself lucky. Programming in binary isn't much fun. Instead, you can now write programs in languages that are similar to the languages people speak. Most games are written in a language called C++ (pronounced see-plus-plus).

The C++ programming language enables you to write statements that let you control all parts of a computer, such as the display, sound card, or joystick. With C++ statements, you can display aliens on the screen, let the player control a space fighter, and shoot the aliens into space dust. C++ also lets you play music and add sound effects. It is a fast, efficient language that you must know to be a professional game programmer.

C++ programs start as C++ instructions that we store in text files. A text file is just what it sounds like: It's a file on the disk that contains textletters and numbers. C++ text files are also called source files. The entire collection of C++ instructions is called the source code of the program.

Tools of the Trade

Recall that microprocessors only execute commands that are binary numbers. C++ is not binary; it's more like English. How do you translate C++ statements into binary commands? Figure 1.1 gives the answer.

Figure 1.1. Translating a C++ source file into binary.


Figure 1.1 shows that there's a fair amount of work involved in getting source code translated into an executable binary program. In the next few sections, I'll explain each step.

Compilers

Figure 1.1 shows that you need a special program called a compiler to translate your C++ source code into binary. Therefore, you'll need a compiler to write your games. I've provided you one for free. It's called Dev-C++ and you'll find it on the CD that comes with this book. The instructions for installing it are in the Introduction.

Linkers

Compilers translate source code into an intermediate form called object code. Object code is binary, but it is not executable. Object code must be converted to executable code, which is an actual program that you can run on your computer. The tool that converts object code to executable code is a linker.

You'll need a linker to create your games. A linker is included with the Dev-C++ compiler on the CD and it's installed when you install Dev-C++. Whenever you compile your program with Dev-C++, the linker runs automatically, so your program is both compiled and linked in one step.

Note

To access the Dev-C++ compiler, just open the DVD and go to the \Tools\DevCPP folder. If you have not installed Dev-C++ on your computer, please turn to the Introduction and do that now.


Warning

All of the C++ source code you will encounter in this book is designed to work with Dev-C++. It may or may not compile with other compilers, such as Microsoft's Visual C++. Ideally, C++ source code should work with all compilers, but in reality, subtle differences exist between compilers produced by different companies. Therefore, I strongly recommend that you use Dev-C++ for all the programs in this book. After you become an experienced C++ programmer, you should have no problem compiling this book's programs with Visual C++ with only minimal changes.


What Is Dev-C++?

Although you'll often see me refer to Dev-C++ as a compiler, it is really an integrated development environment (IDE). An IDE contains everything you need to compile a program. This includes a program to edit your C++ source files, a compiler, a linker, and a debugger (linkers and debuggers will be explained shortly). The compiler, linker, and debugger that the Dev-C++ IDE uses are Windows versions of the GNU Compiler Collection (GCC). The GCC is an open source project that is written by a large and vibrant developer community. You can find the GCC at http://gcc.gnu.org. The Windows version of the GCC is also an open source project whose home page is www.mingw.org.


Debuggers

It's not possible for us to write perfect games in just one trywe'll make a lot of mistakes. In programming, mistakes are called bugs. It's normal for programmers to accidentally create thousands of bugs in each game they write. Yes, I did say thousands.

As a result of the mistakes we make, you and I need a tool to help us find and fix bugs. Appropriately enough, that tool is called a debugger. You get a debugger for free with Dev-C++. Like the linker, it's installed automatically.

Graphics Libraries

Many of the tasks involved in games are universal in game programming. For example, several times a second, every type of game gets user input, reacts to it, and updates the contents of the screen. No matter what type of game you're writing, your game follows this design.

The basic tasks in all games are by far some of the hardest. It's not easy to draw circles, lines, and so forth on the screen. A lot of very smart people had to think for a long time to learn how to do these tasks efficiently. The methods they use are very advanced and very involved. This stuff is not easy, and it used to be the case that every game programmer had to know how to do it all.

Fortunately, you no longer have to bother with learning how to do these basic tasks. When you write your games, you'll use a tool called a graphics library. A graphics library contains code that does all the basic graphics tasks such as drawing lines and circles, or displaying pictures on a screen. The library uses all the best methods and techniques that were developed by all the smart people I mentioned in the preceding paragraph. As a result, you already have code for the basic tasks you'll do when you write games. You don't have to write it yourself.

By the way, the graphics library you'll use is called OpenGL. OpenGL is used in high-powered games such as Quake and Doom from id Software. There is a version of the OpenGL graphics library for Dev-C++. The instructions for installing it are in the Introduction.

Microsoft also provides a graphics library called DirectX Graphics (more commonly called Direct3D). Like OpenGL, Direct3D is used for professional games. Direct3D is part of DirectX, which contains libraries for adding sound, music, and networking to games.

Whether you use OpenGL or Direct3D is largely a matter of preference. They both will get you where you need to go. Although this book primarily uses OpenGL, I've also provided the Microsoft DirectX Software Development Kit (SDK) on the DVD that comes with this book. It's in the folder\Tools\Microsoft DirectX SDK.

Tip

If you're planning on becoming a professional game programmer, it's best to be familiar with both OpenGL and DirectX.


If you have not installed Dev-C++ and OpenGL on your computer, please turn to the Introduction and do that now.

Game Engines

Games that are of the same type pretty much all work the same way. For example, all first-person shooters like Doom need code that does essentially the same set of tasks. All side-scrolling games, such as the old Super Mario Brothers or Sonic the Hedgehog, also use code that does basically the same things.

To make game programmers' lives easier, other programmers often develop game engines. A game engine performs the most common tasks that a particular type of game does. For instance, a game engine for first-person shooters handles all of the tasks associated with displaying scenery, drawing bad guys, and so forth. You have to insert your own scenery, code for your weapons, and any other code that is specific to your game. The game engine handles displaying buildings, trees, and things like that. Figure 1.2 shows how game engines are used.

Figure 1.2. Games are built in layers of code.


As Figure 1.2 shows, games use game engines to get their work done. Game engines, in turn, use graphics libraries to do all of their drawing. If you have a game engine and a graphics library, it saves you from writing a tremendous amount of code. All you have to do is add your game code on top of the game engine. That's the only part of the game that you have to write. Take my word for it; that's all you want to worry about. You'll find writing your game code challenging enough.

Note

The LlamaWorks2D game engine is on the CD in the folder \Tools\LlamaWorks2D, and you'll find instructions for installing LlamaWorks2D in the Introduction.


As mentioned earlier, different types of games each need their own game engine. So you may run across game engines that are for first-person shooters, others that are for flight simulators, and so on. The best game engines are generic enough to be used for more than one type of game. Many excellent game engines are available commercially. The people who write them usually require you to buy the engine or pay a royalty when you sell your game. Some require both.

There are also outstanding game engines you can get for free. In fact, I've written one for you and provided it on the CD that accompanies this book. It's called LlamaWorks2D. Throughout the rest of this book, I'll show you how to use it and how to add your own code to it to produce many types of games.

If you have not installed the LlamaWorks2D game engine on your computer, please turn to the Introduction and do that now.

Games in 2D?

From the name LlamaWorks2D, you've probably figured out that this game engine does 2D rather than 3D games. You may also be wondering why I don't teach you how to do 3D games. The answer is simple: It's hard.

This book teaches the essential skills every professional game programmer must have. It does so by teaching you to write games in 2D, which is considerably simpler than doing 3D. However, don't think that your game programming skills will be anything less than professional when you get done. Many games today are written in 2D. Command and Conquer by Westwood Studios and SimCity by Maxis are two examples. They are both top-selling games that are written with 2D graphics.

Also, many 3D game programming techniques require that you first understand 2D game programming. You would be surprised how much 2D programming there is in 3D games. Therefore, the best way to start learning to write games is with 2D graphics. It gives you the fundamentals, which makes learning 3D game programming much easier.




Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net