Chapter 1: Introduction to DirectX Programming

This pre-publication chapter is from the book DirectX8 and VB.NET Development, by Keith Sink (0672322250).

In This Chapter

What Is DirectX?

DirectX is the building block used to create many of the Windows-based video games and multimedia applications we all enjoy today. It is a collection of ActiveX objects that encompass many of the elements used in video game and multimedia development. DirectX provides graphics support for animation, music and sound effects support for game excitement, and multiplayer support for network games. It also contains libraries that access the game devices and gives you the ability to work in 3D graphics. In a nutshell, DirectX is a collection of technologies designed to encapsulate much of multimedia development.

A History

To better understand the architecture of DirectX, you need to understand how it came about and why it was needed.

The Early Days

Since the early days of video game development, game developers had to rely on detailed knowledge of the hardware they were working with. This knowledge came from lots of experimentation and electronics know-how.

For example, to create some of the earliest games, such as Pong or Asteroids, game designers had to have a good understanding of what each chip on the game motherboard could give them. They needed to know how much memory they had to work with, what colors were available, and how to produce them. In some cases, they had to go to manufacturers and ask for help designing chips to achieve what they wanted to accomplish. Sound and graphics capabilities were primitive at best, but they managed. Because these early games were truly a pioneering endeavor, they also had to create new user controls to work with. These efforts resulted in a unique developers: part designer, part engineer. Because of the advanced knowledge needed to create arcade games in this environment, there were very few game designers.

The DOS Days

From the early technologies of arcade game developers and computer scientists arose the personal computer. With this invention, the need to know the chips was replaced with the need to know a programming language. As computers and programming languages evolved, more and more software people turned to game development. Some of the early computer games were very simple. They relied heavily on text-based and rudimentary graphics, much like the early arcade days. As computers developed in their capabilities, so did the games.

Early DOS games required knowledge of machine and assembly languages to access the operating system's lower levels. As time went on, more user-friendly languages were created, such as BASIC and LOGO; however, they didn't have the power of assembly language. Then came such languages as Pascal and C. With C, game developers could write advanced code routines without having to write tons of assembly code, thus making development efforts easier. C soon became the preferred language of game developers.

Through all these language changes, one problem remained. To write good games, developers still needed an in-depth knowledge of the hardware they were working with. They no longer had to know each chip's capabilities, but they had to understand things such as video cards, sound cards, and input devices. They also had to deal with memory constraints, which were becoming a hindrance to game development.

Along Came Windows

With the development of Microsoft Windows and other operating systems, creating standards in the computer industry became much easier. These standards included hardware interfaces, video memory standards, sound standards, and CPU memory specifications. However, there was still a lot of dissension in the industry, and not all hardware behaved as advertised on the Windows operating system. As a result, many games written in Windows had problems and suffered from performance hits. Microsoft realized this problem early on and focused its efforts on standardizing its environment and working to increase performance for the parts of the operating system that pertain to game and multimedia needs. These efforts were demonstrated in Windows 95, one of the first operating systems that made consistent hardware-software interaction a reality. Because all hardware manufacturers had to meet a similar standard and Microsoft controlled how its operating system communicated to the hardware, development became standard as well. In the DOS days, you had to create your own drivers for each type of video card, but with Windows, you had to develop to only one standard. Instead of spending time creating and understanding so many hardware specifications, developers relied on Microsoft to do all that for them and package it for their use in the OS.

There was one flaw with all of this. Windows was originally designed for the home and office, not for multimedia development. It lacked a few essential items that had become standard in the multimedia market—particularly graphics speed, which could no longer be achieved in the Windows environment as it had been in DOS. It was the same hardware, but there were many more layers between the developers' code and the hardware they wanted to control. The result was that many developers abandoned the idea of Windows game development and relied on the Windows ability to shell out to DOS.

Then There Was DirectX

After Microsoft realized that game developers agreed with its concepts but weren't willing to give up the performance they had become accustomed to in DOS, Microsoft began researching ways to enable developers to get to that hardware layer. Some of the early attempts included WinG, WinToon, and OpenGL.

WinG came into the software spotlight around the time of Windows 3.1 and Windows for WorkGroups. WinG was the first attempt at a full-blown graphics API (Application Programming Interface) library to overcome the limitations of the Windows API. WinG supported high-performance graphics techniques that opened up the Windows game development world to those people who worked exclusively in DOS.

WinToon was an early API for Windows 95 that provided the capability of easy animation playback. On top of full-screen playback capabilities, this API also offered quicker performance than existing Windows 95 APIs did. This technology helped improve the multimedia playback capabilities of animation programs such as kids' games and teaching programs.

OpenGL is a powerful set of APIs that allowed developers to cut through the Windows red tape to get to some of the hardware capabilities of 2D and 3D animation. Surprisingly, OpenGL was not created by Microsoft, but by Silicon Graphics, Inc. Silicon Graphics is one of the pioneers in 3D special effects, however, so it's not that surprising. Several games today still use OpenGL as well as DirectX because many video cards support both these technologies.

These technologies were a great boost, but ultimately came up short. As the game market exploded and the need for more games on the Windows 95 platform increased, developers at Microsoft went back to the drawing board. What they came up with was the Windows 95 Game Software Developer's Kit (SDK), which contained the first version of DirectX. DirectX was initially just for the computer game industry but has evolved to include other areas, such as innovations in the arcade industry and handheld Windows CE devices. As DirectX has developed, Microsoft has added network support, force feedback support, and 3D graphics capabilities. When innovations are needed, the DirectX team has continued to increase the capabilities of this remarkable software library.

Microsoft has also made it easier for DirectX to be used by more than one language. C and C++ had become the game developer's language of choice, so DirectX and the examples included with the SDK were designed with C developers in mind. As DirectX has become more accepted, and Visual Basic (VB) taken more seriously as a programming language, DirectX and Visual Basic have finally come together. It also hasn't hurt that Microsoft has been encouraging VB users to start using DirectX 7 in their applications.

Why Visual Basic and DirectX?

So we come down to the question of why DirectX is now available to Visual Basic developers but wasn't available before. To understand this issue, you need to see how VB has evolved as a viable programming language.

The Evolution of Visual Basic

Visual Basic is an offshoot of an early programming language: BASIC. Personal computer users have always used BASIC and other languages like Pascal to create their own simple, personal games. However, BASIC was not widely accepted as an efficient enough language to create serious, mass-produced video games.

After the creation of the Windows OS, most developers still found themselves confined to low-level languages such as C to create applications for this new platform. Microsoft, seeing that it needed as many developers on its side as possible, invested in a new language called Visual Basic. Visual Basic offered an easy-to-use interface, a language that was simple for first-time programmers to grasp, and the ability to create Windows programs quickly.

Although early versions of Visual Basic were not respected in the programming industry, VB soon found its own niche. After outside companies began making new controls and add-on modules for the VB environment, Visual Basic developers became more in demand. They could create a Windows application in half the time a C developer could. The Visual Basic environment, however, was too slow to handle graphics-intensive programming like games. Early VB game programmers had to rely on puzzle games and card games. Until recently. VB didn't have anywhere near C's speed or reliability, but that has changed. With Visual Basic 6, the gap is closing. Many consider Visual Basic 6 to be almost as fast as C++ code. It still lacks some of the true object-oriented aspects of C++, but it's giving C++ a run for its money.

DirectX for the Masses

It was a logical next step for the DirectX team. The bottom line is that the more people use DirectX, the more popular it will be and the quicker it will become the industry standard. DirectX came home to Visual Basic in the form of DirectX 7. First with DirectX 7 and now with version 8, Microsoft has continued and enhanced its commitment to the VB community. DirectX 8 now includes a DirectX type library for VB users and ActiveX-compliant programs to access the advanced graphics, sound, and multiplayer technologies reserved for the C developers. This addition has opened the floodgates for DirectX development. With DirectX, VB developers can now create the games they always wanted, the multimedia projects they had to rely on C++ for, and much more.

DirectX Uses

DirectX has the power to create and manipulate complex images and sounds. These features, combined with the ability to register user input and its network capabilities, make it a powerful library. With this library, you can create Kiosks that show off your latest products, games that have surround sound and stereo audio effects, and many other multimedia applications. The possibilities are limited only by your imagination.

Showcasing Products

One of the best uses of the DirectX object model is to create applications that can show off your products. For example, you could create a fully interactive demonstration to showcase a product such as a new car. You could change the car's exterior color or upholstery color. You could also see what the dash would look like with different options, such as the latest sound system, installed. Figure 1.1 shows how DirectX could be used to showcase a vintage car.

Figure 1.1 Showcasing a product.

Music and Sound

With sound, you could take the previous example a little further. How would you like to let the people looking at the car in your kiosk be able to listen to their new sound system? With DirectX, you could allow users to turn on the virtual stereo and listen to the difference in the types of sound systems your company offers. Figure 1.2 shows an example of a music selection program.

Figure 1.2 Music and sound.

User Interactions

What good is a program if users can't interact with it? One of the foundations of multimedia is the process of incorporating users' input into the experience. This feature draws users into your application and lets them decide the course of action. They can choose what doors to open on your virtual car, or they can move a joystick to defend the galaxy from its worst enemies. Through multimedia interaction, you can pull the user into your application. In Figure 1.3, the user interface allows the user to choose an answer to a trivia question.

Figure 1.3 Trivia question user interaction.

Screensavers and Multimedia

One of the fascinating things that can be done with DirectX is creating screensavers. Through Visual Basic, screensavers and similar multimedia applications have been around for some time. The advantage of DirectX coming onto the scene is that the graphics are much faster and smoother, the sound quality is much better, and the overall experience is much more enjoyable. Figure 1.4 shows a simple screensaver with animated bouncing balls.

Figure 1.4 Screensavers.

Game Programming

Game programming, of course, is a big part of the DirectX experience. With DirectX, games can be created for the Windows platform with less code. Because DirectX uses standard hardware drivers, there is better control over the hardware itself, which results in better hardware performance. Figure 1.5 showcases a simple game with a snowman as the main character.

Figure 1.5 Snowman game.

Synchronizing Data

One of the really remarkable features of DirectX is its ability to synchronize data across modems, a network, or even the Internet. With this feature, it's possible for developers to make complex programs that send and receive transmission packets across the great divide. A good example of synchronizing data can be seen in Figure 1.6, which shows an application that allows users to talk with each other instantly across the Net.

Figure 1.6 Synchronizing data.

The DirectX Components

The DirectX architecture is made up of many components. You can work with 3D graphics by way of Direct3D. DirectDraw gives you access to many of the animation technologies used in the game industry for decades. DirectInput enables you to understand user input from the keyboard, mouse, and joystick devices. DirectMusic and DirectSound give you access to the sound used in games today. Network capabilities, which are popular in many of today's games, come to you in DirectPlay. DirectX has not forgotten about ease of setup, either, so DirectSetup, which supplies an easy-to-install interface for DirectX, is included with the other DirectX components. Figure 1.7 shows a hierarchical layout of all the components that make up DirectX.

Figure 1.7 An architectural overview of the DirectX object model.

Direct3D

For those of you who love 3D animations, Direct3D is a fantastic addition to DirectX. With Direct3D, you can use DirectX to create complex 3D graphics on hardware that supports 3D rendering. The Direct3D component includes lighting effects, materials, and shading. Direct3D is also referred to as the DirectX Graphics component.

DirectDraw

DirectDraw is the heart and soul of DirectX 2D graphics, giving you many of the features needed for basic animation, such as blitting, clipping, and flipping. DirectDraw provides the methods for developers to talk directly to the graphics hardware without having to learn how each piece of hardware works. It allows for complete hardware independence.

DirectInput

DirectInput enables the developer to open up communication with the input devices needed in computer games and multimedia. DirectInput supports keyboard, mouse, and joystick devices. DirectInput can also control the features of ForceFeedback devices, such as the Microsoft SideWinder Force Feedback game controller.

DirectMusic

If your program needs music, then DirectMusic is for you. DirectMusic offers complex composition abilities as well as complete playback for multimedia applications. You can also add Direct Music soundtracks to multimedia applications to make them more appealing to users.

DirectPlay

Network games and applications are becoming an essential part of the game genre. To that end, DirectX has a component called DirectPlay that provides easy network access to your games. DirectPlay supports Lobbies, Messaging, and Management. It also supports TCP/IP, internetwork packet exchange (IPX), modems, and serial connections. With this technology, developers can take their games to the Net quickly and easily. This technology can also be used to create messaging applications similar to MSN Messenger Service or AOL's Instant Messenger.

DirectSetup

DirectSetup enables you to easily distribute DirectX with your application. With this component, your application can be packaged into a nice neat setup; when it's installed, all the libraries and support files are there where the user needs them.

DirectSound

Sound is what makes a game seem more realistic, so the more realistic the sound, the better the game. DirectSound provides mixing, hardware acceleration, and direct access to the sound device. You can use this component to make car doors slam, horns honk, and birds chirp.

Summary

This chapter has given you an overview of the history of DirectX. You have taken a look at the early days of programming for multimedia and seen how the need for faster graphics and better-quality games and multimedia helped drive the creation of DirectX.

To help you understand why Visual Basic is currently the language of choice and why DirectX is now available, this chapter has discussed the reasons multimedia and games were developed in languages besides Visual Basic for many years.

You have also briefly explored the common uses of DirectX, such as showcasing products with kiosks with information for consumers; incorporating user interaction; creating screensavers, games, and other multimedia applications; and synchronizing data.

You've also learned about the DirectX features available for multimedia development. The DirectX components enable you to incorporate 3D features with Direct3D; use the animation capabilities inherent in DirectDraw; register user input with DirectInput; add soundtracks to your application with DirectMusic; create a multiuser application with the network and modem capabilities of DirectPlay; package your DirectX application with DirectSetup; and use sound effects with DirectSound.

This chapter concludes your evaluation of DirectX from a bird's-eye view. In the next chapter, you will see how Visual Basic and DirectX work together.