Author : Lar Mader
Page : 1 Next >>
DirectDraw Programming Tutorial by Lar Mader
Overview of DirectDraw
This tutorial will give a high level overview of DirectDraw, explaining the concepts needed to understand what DirectDraw achieves.
1. DirectX API groups
Microsoft's DirectX APIs are made up of the following groups:
DirectDraw - Direct access to video memory
DirectSound - Direct access to Sound hardware
DirectPlay - Support for networked multiplayer gaming
DirectInput - Support for gaming input devices such as joysticks
They are all designed to give the programmer APIs for directly accessing hardware. This paper will focus on techniques for using DirectDraw functions so you can create fast, state of the art programs.
2. What is DirectDraw
DirectDraw is essentially a video memory manager. Its most important capability is to allow the programmer to store and manipulate bitmaps directly in video memory. It lets you take advantage of the video hardware's blitter to blit these bitmaps within video memory. Being able to blit from video memory to video memory using the video hardware's blitter is much faster than blitting from system memory to video memory. This is especially true with today's 64 bit video cards that provide a 64 bit data path within video memory. Also, the hardware blit operates independently of the CPU and therefore frees up the CPU to continue working. Additionally, DirectDraw supports other hardware acceleration features of the video card, such as hardware support for sprites and z-buffering. DirectDraw 1.0 is currently available for Windows 95 and will be available for Windows NT this summer.
3. Relationship to WinG
Up until now programmers have used the WinG or CreateDIBSection technology to do fast animation in Windows. This approach gave the programmer direct access to the bitmap in system memory so that one can use optimized routines for drawing to the bitmap. WinG is better than using GDI for all operations that draw to the bitmap, because GDI could never be as fast as the optimized routines used by game programmers.
Once the WinG scene is composed on the bitmap, it is blitted from system memory to video memory, which causes it to be displayed. This technique is not as fast as Direct Draw because blitting from system memory to video memory is slower than blitting from video memory to video memory. Both the DirectDraw and WinG techniques can and will coexist in many complex games or applications since video memory is a limited resource.
4. DirectDraw surfaces - how you access video memory
In DirectDraw, the goal is to put as many bitmaps in video memory as possible. With DirectDraw, all of video memory is available to you. You can use it both to store various bitmaps, and for the primary and offscreen video buffers. All of these pieces of video memory are referred to as surfaces in Direct Draw. When you load a bitmap that will represent a sprite, and store it in video memory, you first create a surface, which is a piece of video memory, and then blit the bitmap into this surface, thereby effectively copying the bitmap into video memory. This bitmap can now live in video memory for you to use, for as long as you like.
The video memory that is currently displaying something onscreen is also a surface, and is referred to as the primary surface. This surface is as big as the amount of memory needed for the current display mode. If the display mode is 640x480 with 256 colors (8 bits per pixel), then the primary surface uses 307,200 bytes of video memory. You usually also create at least one offscreen surface that is the same size as the primary surface for page flipping. This means that you need 614,400 bytes of display memory just to get started, without even loading any bitmaps.
So, one of the most important hardware requirements for fast DirectDraw games is lots of video memory. When you run out of video memory, your DirectDraw surfaces will get created in system memory, and all the benefits of the hardware blits won't be available to these surfaces. Most video cards these days have at least 1 Meg of video memory, however 1 Meg is barely enough to get going. A 2 Meg video card is probably the practical minimum for getting even a fairly simple app working at its best.
DirectDraw provides functions for determining at run time the amount of video memory available, so the application can optimize its use of video memory.
5. The page flipping approach to animation
A scene is composed by blitting the bitmaps from their video memory buffer (surface) to another offscreen buffer (surface) also in video memory. The scene is then displayed by using the hardware's ability to flip the visible video surface to the offscreen surface so that the newly composed scene is now visible. This is known as page flipping and it is very fast. A page flip is fast because there is no memory copying involved. It is simply a matter of setting a register on the video card that tells it where to scan the video memory that will be used to send the signal to the monitor. DirectDraw shields the programmer from the hardware specifics of such operations, and provides a very simple Flip() API function to do the work. To do animation with page flipping:
Compose the scene in the offscreen surface
Flip to the offscreen surface to display it.
The previously displayed video buffer is now the offscreen buffer
Repeat step 1.
Frame rates for page flipping are quite high and are limited by the refresh rate of the monitor, since a page flip will only occur with the vertical retrace signal. This means that if your monitor is set for 72 Hz refresh, you could conceivably achieve frame rates up to 72 frames per second. Waiting for the vertical retrace is nice because it means that you won't see tearing with DirectDraw page flipping. Tearing is the phenomenon witnessed when you move a sprite on screen half way through the refresh of the screen. The sprite appears to tear as it is moved to its new location. For instance, the top half of an image may appear in one location, while the bottom half may be moved slightly to the right or left, which gives the appearance that the image is broken or "torn", right through the middle.
Graphics programmers have developed some sophisticated techniques for optimizing the performance of sprites, and these techniques can still be used in DirectDraw applications. However, learning these techniques won't help one learn or understand DirectDraw. I will, therefore, focus on the page flipping approach to animation. Page flipping is probably the easiest approach to animation, as well as the best one for demonstrating how DirectDraw works.
6. DirectDraw and COM
DirectX is implemented using COM objects. Using COM objects in DLLs has many benefits over simply exporting flat APIs from DLLs, including allowing for polymorphism and versioning. The main thing for the DirectDraw programmer to know is that using the COM objects is no harder than using any other API, especially from a C++ or Object Pascal app.
You don't need to mess with initializing COM, or calling QueryInterface to get the interfaces. The DirectDraw header file declares the C++ classes for the various DirectX objects. Instances of these classes are created for you by calling various create functions. From there its simply a matter of learning the various member functions.
DirectDraw Programming
This section will present code samples to demonstrate how to use DirectDraw. The code samples will be fully functional, however they will be as bare bones as possible to keep the point clear.
1. What you need to compile and link
First and foremost, you will want the DirectX SDK. This is currently available only on MSDN level II or above, and with MS Visual C++ 4.1. The SDK provides a descent help file, and excellent example programs.
To compile and link a DirectDraw application, you need the DDRAW.DLL, DDRAW.H, and you need to create an import library from DDRAW.DLL with the IMPLIB.EXE utility. Since DirectX is only a Win32 technology, you will need a compiler capable of generating Win32 apps. Borland C++ 4.52 and Borland C++ 5.0 provide a great platform for DirectX development.
To run DirectX technology, you must have the DirectX drivers installed on your system. Since this is a commonly used gaming technology, you may find that DirectX is already installed on your system. Look in your Windows/System directory to see if you have a copy of DDRAW.DLL already installed on your system. (Remember, this technology will not work on the 3.X versions of Windows NT.)
2. Initializing DirectDraw
The first thing to learn about DirectDraw is how to initialize it. Read the detailed comments in the following source code for explanations at each step.
// globals (ugh)
LPDIRECTDRAW lpDD; // DirectDraw object defined in DDRAW.H
/*
* Function to initialize DirectDraw
* Demonstrates:
* 1) Creating the Direct Draw Object
Page : 1 Next >>