Earthweb 104

Delphi Graphics and Game Programming Exposed with DirectX 7.0 Delphi Graphics and Game Programming Exposed with DirectX 7.0 When declaring an object method, it can be defined as one of three types static, virtual, or dynamic. Each one has its strengths and weaknesses. Static methods do not gain the benefits of inheritance or polymorphism, one of the cornerstones of object-oriented programming. Virtual and dynamic methods both allow for inheritance and polymorphism, but the way in which...

Overflow Checking

Similar to range checking, overflow checking controls the generation of overflow checking code for certain integer arithmetic operations. The code for each of these integer arithmetic operations is followed by additional code that verifies that the result is within the supported range. Thus, when this option is turned on, additional code is generated for every arithmetic operation performed, degrading performance. Again, turn this option on for debugging purposes, but turn it off for the final...

The Device Context

A device context represents a surface upon which graphical output can be drawn. Specifically, a device context represents the surface of an output device such as a monitor, printer, or plotter, and gives the developer access to modify its appearance. This access is accomplished through the specific device's device driver. device context (DC) Represents the surface of an output device such as a monitor, printer, or plotter, allowing the application to perform graphical output. Under regular...

The Illusion of Motion

Creating the illusion of animation was simple enough. However, even the most complex and beautifully rendered animation would quickly get boring if it stayed in the same spot. So, how do we go about moving the animated sprite across the screen to produce a more interesting and believable animation Again, the process is quite simple. On the most primitive level, when the application prepares to draw the next frame of animation, simply change the horizontal or vertical coordinate (or both) at...

Earthweb

Delphi Graphics and Game Programming Exposed with DirectX 7.0 Delphi Graphics and Game Programming Exposed with DirectX 7.0 We can do this by setting our threshold amount to a specific time interval, measured in milliseconds. Our current interval measurement would then be set to the system time of the last increment of the frame number. In the game loop, we then check to see if the amount of time indicated in the threshold has elapsed since the last increment of the frame number. If so, we...

The Game Loop

As mentioned above, the actual game loop is usually the focal point of a game application. The game loop is where all of the action takes place. It processes a series of instructions over and over as quickly as possible to produce the on- screen images that reflect the current state of the game. This processing continues with or without user interaction, and is precisely the method by which the real-time event-driven model discussed previously is implemented. Technically speaking, a game loop...

Optimization Theory

While optimization can be considered more an art than a science, there are a few general optimization rules that everyone should follow. The most important step in optimization is knowing where to optimize. After you've determined where the bottlenecks are in application execution, the next step is to evaluate the problem and determine how to optimize the code. You could take a guess at which part of your code is the slowest, but this method will most likely result in wasted time and increased...

Force Feedback Effects

Force feedback effects are divided into six different categories conditions, constant forces, periodic forces, ramp forces, device-specific effects, and custom forces. Each one of these is defined by an overall basic effect structure, which describes certain global attributes for the effect such as duration and direction, and a more type-specific structure, which describes very specific effect attributes depending on its category. Some of these categories even have subtypes. This makes for a...

Painting on the Canvas

Objects derived from TGraphicControl and TCustomControl provide a TCanvas property for performing graphical output. This allows the developer to determine the appearance of the control. The TCanvas object, as an encapsulation of the device context, contains numerous methods for drawing graphics, making the use of most GDI function calls unnecessary. However, when using GDI API functions that are not encapsulated by the TCanvas object, the Handle property can be used in parameters that require a...

Earthweb 109

Delphi Graphics and Game Programming Exposed with DirectX 7.0 Delphi Graphics and Game Programming Exposed with DirectX 7.0 Listing 11-2 Scaling a bitmap image this is used as an argument to the Sin function to create an oscillating scale factor Angle Integer procedure TfrmDXAppMain.FormCreate(Sender TObject) begin initialize the angle variable Angle 0 end procedure TfrmDXAppMain.FormActivate(Sender TObject) begin create a palette based on this bitmap FPalette DDLoadPalette(FDirectDraw,...

Earthweb 119

Delphi Graphics and Game Programming Exposed with DirectX 7.0 Delphi Graphics and Game Programming Exposed with DirectX 7.0 Modeling aggression is simple enough. The application simply examines the horizontal and vertical position of the enemy unit and the player unit, and adjusts the enemy unit's horizontal and vertical positions to bring it close to the player. For example, if the player unit's horizontal position is less than the enemy's, subtracting 1 from the enemy's horizontal position...

Setting the Data Format

Once the device has been created, you must instruct DirectInput as to the format in which input data should be reported. This step is required, as input data from a mouse, for example, would be completely different than that retrieved from the keyboard or a joystick. You may be asking, why should we do this if DirectInput knows what kind of device we just created Again, this is part of DirectInput's open architecture. Setting the data format in this manner allows the developer to declare a...

Modifying an Effect at Run Time

In order to further customize an effect to reflect changes in the game world, an effect can be modified while it is playing, provided that this is supported by the driver. As we examined earlier, we can use the GetEffectInfo method to determine which parameters can be dynamically changed during effect playback. In practice, we can change any of the parameters at any time the parameters listed in dwDynamicParams indicate those parameters that can be changed without restarting the effect....

General Optimization Techniques

We gain a lot of benefit from the automatic optimization that Delphi provides. We can also improve performance by being aware of the inner workings of certain Delphi code constructs. However, we should also be aware of general coding practices that can be used to improve performance in any language. It is very common for the point of execution within an application to jump from one point to another during the course of execution. For example, an If Then statement causes the point of execution...

Basic Theory

The first thing we need to accomplish is creating an appropriate palette. For an effective fire, we'll create a palette that ranges from white in the upper slots (255-192), through yellow and orange, to red, and finally to black in the lowest slots (63-0). We could adjust the palette range somewhat and put some blues in, to simulate a cooler flame, or perhaps add some grays at the end, which would effectively simulate smoke. This effect involves moving pixels that already exist in a surface....

Buffer Controls

The IDirectSoundBuffer interface contains several methods by which the application can control certain aspects of the sound. Specifically, an application can control pan, frequency, and volume. This allows a developer to implement several special sound effects that can greatly enhance a game. For example, the pan control can be used to move the sound of an engine whine from side to side as a spaceship crosses the screen. Volume could be used to reduce and finally eliminate an engine whine as a...

Summary

In this chapter, we discussed several techniques for playing both sound effects and music. We examined various DirectSound methods for performing digitized audio playback, including changing the format of the primary buffer as well as duplicating sound buffers. We also covered the steps required to output MIDI music as well as more superior techniques for playing audio tracks from a CD. When coding for sound or music output using DirectSound or Win32 multimedia API functions, it is important to...

Game Controller Objects

Game controllers come with a variety of objects with which the user can provide input data. These objects include axes, POVs, and buttons. Although most game controllers exhibit input objects that are not available on the keyboard and mouse, data retrieved from these objects is not unlike that retrieved from more conventional input devices. Lateral and Rotational Axes Game controller axes come in two flavors lateral and rotational. A lateral axis moves in a straight line, such as left and right...

The Delphi Games Creator DGC

Another freeware component suite, the DGC gives developers access to DirectDraw, DirectSound, DirectPlay, and Directlnput. Written by John Pullen, Paul Bearne, and Jeff Kurtz, the DGC comes complete with source code. The DGC also comes with editors for bundling sound and graphics into libraries, and will map all images in a graphics library to a single palette. Palette cycling and cool transition effects are just some of the added functionality available with this component suite. It also...

Check for the Existence of the CD

Once the CD audio device is opened, we can poll it to determine if a CD is actually inserted. To accomplish this, we need a variable of the type TMCI_Status_Parms. The dwItem member of this data structure must be set to MCI_STATUS_MEDIA_PRESENT to indicate that we wish to test for the presence of a CD in the drive. We then call the MCISendCommand function, passing MCI_STATUS in its second parameter. For example determine if a CD is present in the drive StatusParms.dwItem...

The DirectX Architecture

The DirectX API itself, in the form of various COM objects and their methods, acts as an interface into the DirectX drivers. On the back side, the DirectX drivers can be thought of as two separate systems the hardware abstraction layer HAL and the hardware emulation layer HEL . HAL hardware abstraction layer the DirectX drivers responsible for direct interface to hardware capabilities. HEL hardware emulation layer the DirectX drivers responsible for emulating certain functionality if not...

Enumerating Input Devices

Technically speaking, this step is not required if your application will only support keyboard and mouse input devices. It may be useful if the target machine has more than one keyboard or mouse attached, but you can still tell DirectInput to create objects that represent the primary keyboard and mouse. However, game controllers are another story. Since there are no primary or default game controllers, applications that wish to support joysticks, steering wheels, etc., must obtain individual...

The DirectX Baseline Application

In an effort to expedite the creation of DirectX applications, this book contains a baseline DirectX application that instantiates DirectDraw, sets up a page flipping architecture, and implements a high-speed rendering loop that can attain the highest animation speed possible. This is implemented by providing an event handler for the TApplication's Onldle event. The Onldle event is called whenever the application becomes idle if the Done parameter of this method is set to False, this event will...

Windows Functions

Windows provides several functions to developers for determining game controller capabilities and retrieving input data. For the most part, these functions are incredibly powerful and can report almost as much data as their Directlnput counterparts. For most applications, these functions may supply all the required power. Note MMSystem must be added to the Uses clause of any unit employing these functions. Game controller capabilities can be retrieved by using the joyGetDevCaps function....

Project JEDI

If you have the DirectX end user redistributable files installed which you probably do if you've played any recent games , all you will need are the Delphi import files converted from the original DirectX C C header files. All code in this book is based around the Project JEDI DirectX header files written by Erik Unger. Project JEDI Joint Endeavor of Delphi Innovators is a nonprofit organization composed of Delphi programmers all over the world. Their goal is to convert C C header files for...

Peripheral Input Devices

Peripheral input devices consist of game controllers that are not standard equipment with new computers. Such devices include joysticks, game pads, rudder pedals, and a plethora of other types of input devices of every shape and size. These devices are generally suited for use with twitch style games that require good hand-eye coordination, such as flight simulators or first-person shooters. However, several of the more esoteric devices may provide a much more intuitive feel for the user, as...

Game Programming In Delphi

When most people hear about Delphi, they think of rapid database application development. They think of its uses in the corporate environment as a wonderful tool for making myriad database accessing applications, a task for which it is indeed well suited. They compare it to other RAD tools like Visual Basic and PowerBuilder, contrasting their relative strengths and weaknesses in data access, speed, scalability, etc. This attitude has been prevalent since Delphi 1, and is indeed one of the...

Creating the Input Device

The Directlnput object has been created, and we've presented the user with a list of input devices from which one device has been selected. Given this device's instance GUID, we can now create an IDirectlnputDevice object representing the device. We create this object by calling IDirectlnput's CreateDevice method. The CreateDevice method is defined as rguid PGUID the device instance GUID var the IDirectInputDevice object pUnkOuter IUnknown unused, set to nil HResult returns a DirectX error code...

Sound Effects

Sound effects are very important in a game, as they help to immerse the player in the environment depicted by the game's playing area. Viewing an animation of a ship exploding is much more exciting when accompanied by a loud roar. Walking down a dark passageway is much more realistic when the hollow sounds of boot heels on damp stone is heard. Using sound effects in this manner can draw the user into the environment in ways that even the most advanced 3-D engine could never hope to accomplish....

Direct Draw Tutorial

DirectX has hopefully been installed to your machine by now, and we have the units required to give us access to DirectX components and functionality. At this point, we can begin exploring the world of DirectX programming and all of its nuances, pitfalls, and rewards. DirectDraw is the core, most fundamental DirectX component, and as such, is a good starting point for a discussion of DirectX programming. We will go over a good number of DirectDraw concepts, many of which are mirrored in other...

DelphiX

A freeware component suite written by Hiroyuki Hori, DelphiX comes complete with source code and gives the developer access to DirectDraw, Direct3D, DirectSound, Directlnput including force feedback support , and DirectPlay. There are several cool components in this suite which allow the developer to bundle graphics and sound into compressed libraries, and includes some awesome alpha blending functionality. The examples clearly demonstrate some of the advanced techniques capable with DelphiX,...

Drawing the Sprite

We must lock both the backbuffer surface and the surface containing the sprite images. Once this is done, we iterate through each row of the sprite, and each pixel in the row. For each pixel in the current row being drawn, we check to see if it is equal to the specified transparent color. If it is not, we go ahead and set the appropriate pixel in the destination memory to the value of the pixel being checked. If it is indeed equal to the transparent color, we simply move on to the next pixel in...

Functional Overview

Similar to DirectDraw, DirectSound programming consists of creating a DirectSound object and several sound buffers. The DirectSound object itself represents the developer's direct interface to the audio hardware. Through the IDirectSound interface, the application can query the audio hardware's capabilities, create sound buffers, and control other aspects of the hardware itself. Of primary interest to DirectSound programmers is the secondary sound buffer object or specifically the...

Transparency Issues

So far, the examples we've seen all demonstrate sprite animation against a solid black background. This may work for some games, but what if we wanted to have some sort of cool background graphic behind our sprites, like a starfield or perhaps a forest, mountains, or other type of setting This not only makes the overall animation more visually stimulating, but it improves its believability and makes the overall gaming experience more realistic. To do this, we need to draw the frames of the...

Collision Detection

All games, at some point, must deal with the situation in which one sprite touches another. For some games, this may be when a bullet strikes an alien or when an animated mouth collides with a glowing white pellet to eat it. A game application must determine when these situations happen, and this is where collision detection comes in. There exist as many different methods to perform collision detection as there do methods to draw a sprite. Many articles have been written on this subject,...

Initialization

As with DirectDraw, there are several steps the developer must follow to set up DirectSound before loading and playing sound buffers. Some of these steps are somewhat optional, but in general, DirectSound is instantiated by performing the following Creating the DirectSound object Setting the cooperative level Retrieving audio device capabilities Setting the format of the primary buffer Enumerating Audio Devices DirectSound provides a function that returns information for every audio output...

Retrieving the Force Feedback System State

Another useful method of the IDirectInputDevice2 interface is the GetForceFeedbackState method. This method returns a series of flags which indicate the overall state of the force feedback device. The GetForceFeedbackState method is defined as function GetForceFeedbackState var pdwOut DWORD HResult flags indicating overall device state returns a DirectX error code The pdwOut parameter is simply a variable that, upon return, will be initialized with the overall force feedback system status. This...

Palettes

As previously discussed, working under palettized video modes provides a number of advantages and disadvantages. Palettized modes can be a real pain at times, requiring careful planning on the part of art production and special considerations on the part of the programmer. However, the advantages of working under a 256-color video mode usually outweigh the disadvantages, primarily because it is a very speedy mode to work under, and palette animation can be used for special effects. The Windows...

Locking a Surface

This method requires direct access to the memory that contains both the source bitmap and the destination, which is usually the backbuffer surface. Using normal GDI functions, this would be impossible. This is one of the things that makes DirectDraw so attractive to game developers. To retrieve a pointer to surface memory, we must use the Lock method of the IDirectDrawSurface4 object. Using this method, we can access the entire surface memory or provide the coordinates for accessing only a...