Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

3D Game Programming All In One (2004)

.pdf
Скачиваний:
139
Добавлен:
17.08.2013
Размер:
17.91 Mб
Скачать

28Chapter 1 Introduction to 3D Game Development

and server, it still behaves as a client connected to a server, but the netcode has a short-cir- cuit link to other netcode in the same game instance, and no data goes out to the network.

Bandwidth is a problem because of the large, open terrain environments Torque supports, as well as the large number of clients Torque can handle—up to 128 or more per server, which means that there is a high probability that many different objects can be moving and updating at the same time. Torque uses several strategies to maximize available bandwidth.

It sends updates to what is most important to a client at a greater frequency than it updates data that is less important.

It sends only the absolute minimum number of bits needed for a given piece of data.

It only sends the part of the object state that has changed.

It caches common strings and data so that they need only be transmitted once.

Packet loss is a problem because the information in lost data packets must somehow be retransmitted, yet in many cases the data in the dropped packet, if sent again directly, will be stale by the time it gets to the client.

Latency is a problem in the simulation because the network delay in data transmission makes the client's view of the world perpetually out of sync with the server. Twitch-style FPS games, for which Torque was initially designed, require instant control response in order to feel anything but sluggish. Also, fast-moving objects can be difficult for highly latent players to hit. In order to solve these problems, Torque employs the following strategies:

Interpolation is used to smoothly move an object from where the client thinks it is to where the server says it is.

Extrapolation is used to guess where the object is going based on its state and rules of movement.

Prediction is used to form an educated guess about where an object is going based on rules of movement and client input.

The network architecture is layered: At the bottom is the OS/platform layer, above that the notify protocol layer, followed by the NetConnection object and event management layer.

Using Torque in This Book

As you've seen, the Torque Game Engine is powerful, feature rich, flexible, and controllable. What we will do in this book is create all of the different elements of the game that we'll need and then write game control script code to tie it all together.

All of the program code, artwork, and audio resources you will need are included on the companion CD, along with the tools needed to manipulate them and create your own.

Team LRN

Moving Right Along

29

At first glance that may not seem to be too daunting a task. But remember, we will be wearing all of the game developer hats. So we will be creating our own models (players, buildings, decorations, and terrains), recording our own sound effects, placing all of these things in a virtual world of our own fabrication, and then devising game rules and their scripted implementations to make it all happen.

Daunted yet?

Hey, it's not going to be that hard. We've got Torque!

The CD

There are several different setup options available from the CD. The simplest and most complete is the Full Install. The most minimal installation will install the Torque Engine Executable and the appropriate file paths for a sample game, with supporting scripts.

Installing Torque

If you want to install only the Torque Game Engine, without the various chapter files, extra utilities, or demo games, then do the following:

1.Browse to your CD in the \Torque folder.

2.Locate the Setup.exe file and double-click it to run it.

3.Click the Next button for the Welcome screen.

4.Click the Next button for the Destination screen, taking the default program group location.

5.At the Select Components screen there is a Full Installation drop-down menu. Select this menu by clicking in it, and change it by selecting Custom Installation. Then click the Next button.

6.From the Components list, select Torque and click the Next button.

7.Select the defaults for the remaining screen, clicking Next for each one.

Moving Right Along

There you go. You now have the basic Torque Game Engine plus a sample game installed. Enjoy!

Of course, if you are following along with the game development in this book, you will need to return to the CD and install all the other components when they are needed.

During the chapter, we've looked at computer games from many different angles—the industry, the genres, and the different roles of developers, as well as an exploration into what things make a game engine work and how they relate to each other.

Team LRN

30 Chapter 1 Introduction to 3D Game Development

In the next chapter, we'll get into the basics of programming. We'll use the Torque Engine itself to run our example programs as we work through the chapter. This will develop skills we'll need in later chapters when we start delving into real game programming scripts.

Team LRN

chapter 2

Introduction to

Programming

My intent with this chapter is to help you understand programming concepts and techniques and leave you with a foundation upon which you can build more advanced skills. By the end of this chapter, you will be proficient with a

powerful programming editor; understand how to create, compile, and run programs you've written yourself; have a reasonable sense of programming problem-solving methods; and become familiar with valuable debugging tips and techniques.

UltraEdit-32

To write our programs, we will need to use a text editor, or programming editor. This kind of editor differs from a word processor, which is what most people use for writing documents, books, memos, and church bulletins.

A good programming editor has several useful features:

A project feature that allows you to organize your source files

A fully featured grep (find, search, and replace) capability

Syntax highlighting

A function finder or reference

Macro capability

Bookmarks

Text balancing or matching

I use a shareware editor called UltraEdit-32 (UltraEdit), written by Ian D. Meade, included on the companion CD for this book. It also has several other useful features that I'll

demonstrate later in this chapter.

31

Team LRN

32 Chapter 2 Introduction to Programming

grep? What Kind of Name Is That?

The name grep comes from the UNIX world, where strange and wonderful names and incantations for programs abound. grep is derived from the command string "g/re/p" which first appeared in old line editor programs on early UNIX systems. The "g" meant global, the "re" meant regular expression, and the "p" meant print, as in print to the screen. If you entered that command into the editor's command line, you were telling the editor to globally search, using regular expression syntax, and then print the results—and the expression would then follow those characters. Eventually that command string was migrated outside of the editor program and incorporated into a command that was usable from the UNIX command shell as a way of specifying how to look and what to look for when you are searching files that contain a particular piece of text. Over time, the name grep became synonymous with searching files for embedded text and has become a common term in the programming world, even in non-UNIX environments. Now it is often used as a verb meaning "search for text in files."

Program Setup and Configuration

After you insert the companion CD into your computer's CD drive, use Windows Explorer to browse your way on the CD into the folder called 3DGPAi1. Find setup.exe, double-click it, and follow the installation instructions.

Next, browse your way on the CD into the folder called UltraEdit-32. Find setup.exe, dou- ble-click it, and follow the installation instructions.

Finally, browse your way on the CD into the folder called UE Tools. Find setup.exe and double-click it to run it and follow the installation instructions. This will install UE Project Maker in the 3DGPAi1 folder on your C drive. This tool will automatically generate project files that you can use with UltraEdit-32.

Setting Up Projects and Files

n o t e

Use the UE sample folder in the 3DGPAi1 folder.

Like any decent editor environment, UltraEdit-32 allows us to organize the files we want to work with using a projects concept. You can create, in UltraEdit-32, virtual folders and save links to your files in these folders. By doing this, you can create a quick and convenient access channel to files that are stored anywhere, even somewhere on the network! Setting up your projects can be a bit tedious, however, depending on your needs. To help you with setup, I have written a utility called UltraEdit Project Maker (UEPM), which is included on the companion CD. I'll tell you more about using UEPM later, but right now, let's dive in and manually set up a project.

Team LRN

UltraEdit-32 33

Configuring UltraEdit

To configure UltraEdit, follow these steps:

1.Launch UltraEdit by selecting Start, Program Files, UltraEdit, UltraEdit-32 Text Editor.

2.Close any open files you may have in UltraEdit by selecting Window, Close All Files.

3.In UltraEdit, select View, Views/Lists, File Tree View. A new docked window will appear on the left side (see Figure 2.1). This is the File Tree View.

4.In the File Tree View there is a drop-down combo box (it has a down-pointing arrow at its right end; see Figure 2.2). If the text in the combo box does not say "Project Files," then click the arrow on the right and select Project Files from the list that drops down. When the name has been set to Project Files, we refer to this as the Project View.

5.Right-click in the white area of the Project View to get a pop-up menu. Select Show Names Only.

6.If the Project View is freefloating (not docked), then repeat the right-click and this time select Allow Docking if it isn't already selected. Then click and hold (grab) the colored bar at the top of the File List View/Project View window where it says "File List

View" and drag it to the left Figure 2.1 Locating the File Tree/Project View. side of your UltraEdit win-

dow such that the colored bar remains in the dark gray space, but the left side of the view window disappears off the left side of the UltraEdit window. You should see the outline of the view window change from a wide gray line to a thin black line. Let go of the mouse button and the

view will be docked. Figure 2.2 Changing the File List View to the Project View.

Team LRN

34 Chapter 2 Introduction to Programming

7.Select the menu item Project, New Project/Workspace. Browse your way to C:\3DGPAi1\UESampleProject. A Save dialog box will appear. Type in the project name (uesample), and make sure you have the Project Files type selected in the combo box of the dialog box. Next, the Project dialog box will appear. If you are given an alert that tells you the file already exists, and asks if you want to save, click "Yes".

8.Click the Relative Paths and Relative to Project File check boxes and make sure they are checked.

9.Click New Group and type in SubFolder and then click on the OK button. The SubFolder entry will appear in the list.

10.Select the SubFolder entry so that it is highlighted, and then click New Group and type in SubSubFolder, then click on the OK button. The SubSubFolder entry will appear in the list nested under SubFolder. You may need to click on the SubFolder icon to make the plus sign appear next to SubFolder, and then click on the plus sign to ensure that SubSubFolder appears nested inside.

11.Select the root entry (it's marked by the [ - ] symbol). Next click on the New Group button and type in SubFolderTwo. The SubFolderTwo entry will appear in the list.

12.Double-check your entries and compare the results with Figure 2.3. Click Close to make the dialog box go away.

13.Using the menu item File, Open, browse your way to C:\3DGPAi1\UESampleProject and open the file called sample file 1.txt. Do the same for C:\3DGPAi1\UESampleProject\sample file 2.txt. You should now have only these two files open.

14.Open the Project dialog box again, by selecting Project, File/Settings, and click the root entry to select it.

15.Click +All Open Files.

Figure 2.3 Project dialog box with folder hierarchy.

The two open files will be

added to the project's file

 

 

list at the root level. Close

 

the Project dialog box.

Team LRN

Figure 2.4 Final form of the Project/Workspace Setup dialog box.

UltraEdit-32 35

16.Close both of your open files.

17.Next, open C:\3DGPAi1\UESampleProject\SubFolder\sample file 3.txt and C:\3DGPAi1\UESampleProject\SubFolder\sample file 4.txt.

18.Now reopen the Project dialog box, select the SubFolder entry, and click +All Open Files.

19.Close all of your open files.

20.Repeat steps 18 and 19 for the files located in C:\3DGPAi1\UESampleProject\SubFolderTwo and C:\3DGPAi1\UESampleProject\SubFolder\SubSubFolder, ensuring that you add the file links in the appropriate project folder.

After following these steps, you should have a Project Setup dialog box that looks like Figure 2.4, and your Project View should look like Figure 2.5. You may need to click on the plus sign in front of the folder entries in order to expand the folders to match the view in the figure.

As the saying goes, there is more than one way to skin a cat, and in this case there are other ways to set up your project. You can do it all from within the Project/Workspace dialog box using the Add File button. You can also use the Add Active File button to add whatever file is currently the one being edited in UltraEdit. You can experiment and find the method that works best for you. I tend to use a combination of All Files

and Add Active File, depending on my needs at the time.

Go ahead and open a few files and close them again, to get a feel for how the Project View works.

Search and Replace

The search capabilities of UltraEdit are quite extensive and thorough. I'm going to focus on the few most important: finding specific text, finding specific text and replacing it, jumping to a line number, and advanced searching using

Figure 2.5 Final form of the Example Project View.

Team LRN

36Chapter 2 Introduction to Programming

wildcards and patterns. To practice the various features, open the UESample project, and open the file called sample file 1.txt. It has some text extracted from an early revision of Chapter 1 that we can hack away at.

Find

Select the Search, Find menu item, and you should get the Find dialog box (see Figure 2.6). Make sure the option check boxes match the ones in Figure 2.6. Now, type in the word you want to find, then click the OK button. The Find dialog box will go away, your text insertion point will jump to the first found instance of the word you want, and the word will be highlighted. Try this using the word "indie". See that?

Okay, now get your Find dialog box back and try doing this with the various options. Notice that the Find operates on the currently active file in the editor. Check out the various options, like searching "down" the file and then searching back "up" the file. Change your search word to "INDIE" (all capital letters) and then try your search again. Note that the Find still locates the word. Now try it with the Match Case option checked. Notice that you get an error message: Search String Not Found.

When searching, you will often have more than one match to your search criteria. If you are not using the List Lines option, then you can move through each match in the text by using Search, Find Next to continue to find matching terms as you move toward the end of the file (down). Using Search, Find Prev will do the same thing moving toward the start of the file (up). However, you will probably want to quickly get acquainted with using the keyboard shortcut F3 for Find Next and Ctrl+F3 for Find Prev.

T i p

A quick and convenient way to search for other occurrences of a word that is already written and visible in the active window is to highlight the word (double-click it), press Ctrl+F (the shortcut for Find), and then press Enter. The insertion point will jump to the next occurrence of the word. Then keep pressing F3 to move to the next, and the next, and the next, ad infinitum. UltraEdit will keep starting over from the beginning of the file until it finds no more matches.

Figure 2.6 The Find dialog box set for a basic search.

A feature of the Find dialog box that I think is particularly useful is the List Lines Containing String option. With this checked, all instances of the word you are looking for will be listed as complete lines in a separate window. Try it by searching for the word "action" with case sensitivity turned off. This should give you a window with a list of lines in it. Each line contains at least one instance

Team LRN

UltraEdit-32 37

of the search term you used. If you double-click a line, you will see the text and insertion point in your edit window jump to where that line is located and highlight the line.

Special Find Characters

When using Find, there are some things you may want to search for that are not normal alphanumeric characters or punctuation marks—the end of a line, for example.

These are handled by using special characters that are a combination of an escape character and a symbol. The escape character is the caret ("^"; you get this when you hold down the Shift key and type the number "6" on North American keyboards) and is paired with a symbol that is a normal character. Whenever Find sees the combination of the caret in front of a character, it knows it is doing a special character search.

Of course, the first special character is the caret itself; otherwise we would never be able to do a search for a caret in text. Look at the following table for a list of the most common special Find characters.

These do not require you to turn on the Regular Expressions switch in the Find dialog box, although they are the same as some of the Regular Expressions entries.

Special Characters Used in a Basic Find Function

Special Symbol

What the Program Looks For

^^

caret character ("^"; sometimes called Up Arrow)

^s

highlighted text (only while a macro is running)

^c

contents of the Clipboard (only while a macro is running)

^b

page break

^p

new line (carriage return and line feed) (Windows/DOS files)

^r

new line (carriage return only) (Macintosh files)

^n

new line (line feed only) (UNIX files)

^t

tab character

Replace

Select the Search, Replace menu item, and you should get the Replace dialog box (see Figure 2.7). This dialog box is similar to the Find dialog box, but with more options and a field in which to enter the replacement text.

Team LRN