Home > Articles

  • Print
  • + Share This
This chapter is from the book

Sample T3LIB Applications

We are almost done with this chapter—whoooweee! I'm getting sick of this stuff—I'm dying to get to the 3D part! Anyway, just to be complete, what I want to do now is show you a number of sample applications that use the Game Console T3DCONSOLE2.CPP as the starting point for real demos that show off graphics, sound, music, and input. The demos are going to actually do something, because I am hijacking them from the first Tricks. However, they are all updated now. This way, you can see some real examples of using the console template. However, note that the sound and music demos are less glamorous, and don't really need much.

Lastly, as we proceed through the book and build the T3DLIB game engine, we will add modules T3DLIB4, 5, 6, and so forth each chapter or so.

Windowed Applications

As it stands, the T3DCONSOLE2.CPP is really a demonstration of windowed 16-bit applications, but I always like to see something with a little more girth to it—don't you? Therefore, I converted one of the artificial intelligence demos from the first Tricks (pattern systems demo) to 16-bit and put it in a window. The name of the program on the CD is DEMOII3_1.CPP|EXE and a screenshot is shown in Figure 3.14. To run the program, make sure the desktop is in 16-bit color mode. Also, if you want to compile it, make sure you include all the library modules!

Figure 3.14Figure 3.14 A screenshot of a windowed application AI demo.

In any case, the demo not only shows off 16-bit windowed mode, but it shows how to load bitmaps, use the BOB library, make sounds, and a lot more all in 16-bit graphics mode. Lots to learn by studying the code.

Full-Screen Applications

Writing a full-screen application is no different with our library; in fact, it's better because there is less chance of resource sharing problems. Moreover, in 256-color modes, you have full control over the entire palette, rather than losing access to the first and last ten entries, as with windowed 256-color modes. Additionally, you can switch into any bit depth and resolution with full-screen modes and not worry about the desktop.

As a demo of a full-screen application, I have taken one of the 256-color physics demos from the first Tricks and worked it into our new Game Console template. The name of the program is DEMOII3_2.CPP|EXE. Figure 3.15 shows a screenshot of the program in action. This program again illustrates the use of bitmaps, BOBs, and palettized 256-color modes.

Figure 3.15Figure 3.15 A screenshot of a 256-color, full-screen physics demo.

Sound and Music

Working with sound and music under DirectSound and DirectMusic isn't hard, but it's not easy, either. The systems are very complex, and have a lot of functionality. However, all we want to be able to do is load sound and music off disk and play the darn things! The T3DLIB2.CPP|H module handles all this in a very clean and simple way.

With that in mind, the sound and music demo is going to be rather plain graphically, so you can focus on just the calls to make sound and music work. Therefore, I have taken a demo from the first Tricks that basically works with both DirectSound and DirectMusic at the same time. The demo is a simple Windows application that has a menu and enables you to select a MIDI song to play, while at the same time you can "fire" off sound effects. Also, the demo structure is based on a stripped down version of the Game Console to minimize anything extraneous.

The name of the demo is DEMOII3_3.CPP|EXE, and Figure 3.16 features a screenshot of the application in action (what there is of it). Additionally, this demo uses a couple of resources such as a menu and cursor; thus, there are a few extra files that you need to add to your project:

  • DEMOII3_3.RC—The Windows resource file with the menu

  • DEMOII3_3RES.H—The header with identifiers for the resources

  • T3DX.ICO—The cursor icon used by the program

Figure 3.16Figure 3.16 A screenshot of the sound and music demo.

As usual, this is still a DirectX application, and you need to include the DirectX .LIB files. Technically, you don't need DDRAW.LIB, but you should always keep all of them in your link list to be safe. Also, because there aren't any graphics or input calls to T3DLIB1.CPP or T3DLIB2.CPP, do not include the .CPP files in the build—you will get link errors if you do!

Working with Input

The last examples of using the library are input demos. As you know, there are three primary input devices used in a game:

  • Keyboard

  • Mouse

  • Joystick

Of course, these days a joystick means a lot of things, but usually we can classify all devices that aren't a mouse or keyboard as a joystick.

Using the input library is very simple. It's contained in T3DLIB2.CPP|H and supports the keyboard, mouse, and joystick devices. With just a few function calls, you can initialize DirectInput, acquire an input device, and read from it in your main loop. What we are going to do is review demos of each device: keyboard, mouse, and joystick. Of course, you are more than free to use all input devices together, but the demos will keep them separate, so you can better see what's going on.

Keyboard Demo

As usual, the demo are based on code from the first Tricks. Let's begin with the keyboard demo DEMOII3_4.CPP|EXE. A screenshot is shown in Figure 3.17. Basically, this demo enables you to move a BOB skeleton around the screen. If you look at the code that reads the keyboard, you will notice that the data structure returned is nothing more than a 256 BYTE array of BOOLEANs, each representing one key. The table is shown here:

UCHAR keyboard_state[256]; // contains keyboard state table

Figure 3.17Figure 3.17 A screenshot of the keyboard demo.

To access the table, you use the DirectInput keyboard constants, which are all of the form DK_*. Table 3.2 from a previous section lists some of the more common constants. All you need to do to test whether a key is pressed. After you have retrieved the keyboard data with a call to DInput_Read_Keyboard(), you can use the following simple test:

if (keyboard_state[DIK_UP])
 {
 // if the up arrow is pressed this will execute
 } // end if

To compile the demo program, you will need all the DirectX .LIB files, along with T3DLIB1.CPP|H, T3DLIB2.CPP|H, and T3DLIB3.CPP|H.

Mouse Demo

The mouse demo is again based on code from the first Tricks. It's a little paint program, as shown in Figure 3.18. The code is in the file DEMOII3_5.CPP|EXE. The program shows off a number of interesting things about GUI programming, such as object picking, mouse tracking, and so forth. So, make sure to check out the painting code in addition to the mouse tracking code.

The mouse input device can operate in two modes: absolute or relative. I prefer relative because I can always figure out where I am, so the API wrapper puts the system in that mode. Hence, each time you read the mouse, you get deltas from the last call. The mouse is read each time you call DInput_Read_Mouse() and is stored in the following global mouse record:

DIMOUSESTATE mouse_state; // contains state of mouse

Figure 3.18Figure 3.18 A screenshot of the mouse-based painting program.

Once again, the structure of a DIMOUSESTATE object is as follows:

typedef struct DIMOUSESTATE {
 LONG lX; // x-axis
 LONG lY; // y-axis
 LONG lZ; // z-axis
 BYTE rgbButtons[4]; // state of the buttons
} DIMOUSESTATE, *LPDIMOUSESTATE;

To compile this program, you will need all the DirectX .LIB files, along with T3DLIB1.CPP|H, T3DLIB2.CPP|H, and T3DLIB3.CPP|H.

Joystick Demo

The "joystick" device on computers these days can be almost anything from gamepads to paddles. However, any joystick-like device is nothing more than a collection of two types of input devices:

  • Analog axes

  • Buttons

For example, if you take a look at Figure 3.19, we see four different joystick-like devices: a typical joystick, an advanced joystick, a game pad, and a steering wheel/paddle. You might hesitate to call these all joysticks, but they really are. The joystick has two axes: the x and y, along with two buttons. The gamepad is all buttons. And finally, the paddle consists of a rotational axis along with a couple of buttons.

Figure 3.19Figure 3.19 Four basic joystick devices.

Of course, in DirectInput speak, there are constants to define some of the most prevalent subtypes of joysticks, like plain joysticks, flight sticks, paddles, and gamepads, but in general they are all used in the same way. That is, you retrieve either smoothly varying data back from an analog slider or yoke, and a number of digital on/off signals representing the buttons. The data record returned the call to DInput_Read_Joystick() from the DirectInput module of our library is stored in the global

DIJOYSTATE joy_state;  // contains state of joystick

which is of the form

typedef struct DIJOYSTATE {
 LONG lX; // x-axis
 LONG lY; // y-axis
 LONG lZ; // z-axis
 LONG lRx; // rotation about x-axis
 LONG lRy; // rotation about y-axis
 LONG lRz; // rotation about z-axis
 LONG rglSlider[2]; // u,v slider positions
 DWORD rgdwPOV[4]; // point of view hat state
 BYTE rgbButtons[32]; // state of buttons 0..31
} DIJOYSTATE, *LPDIJOYSTATE;

As a demo of using the joystick device, I have hijacked yet another program from the first Tricks that is a little Centipede-like game (not much of a game, though). The program is named DEMOII3_6.CPP|EXE on the CD. A screenshot is shown in Figure 3.20. As usual, you will need all the DirectX .LIB files, along with T3DLIB1.CPP|H, T3DLIB2.CPP|H, and T3DLIB3.CPP|H to compile.

NOTE

Make sure you check out some of the aspects of the demo relevant to game programming, such as the primitive game logic, collision detection, and weapon systems.

Figure 3.20Figure 3.20 A screenshot of the joystick demo.

  • + Share This
  • 🔖 Save To Your Account