Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

## Chapter 7
Advanced DirectDraw and Bitmapped Graphics

> “There are a lot of decaffeinated brands on the market that are just as tasty as the real thing…”

> —Chris Knight, Real Genius

In this chapter I’m going to show you the guts of DirectDraw and start working on the first module of the graphics library (`T3DLIB1.CPP|H`), which will be the basis of all demos and games created in this book. A lot of material will be covered in this chapter, in addition to me throwing a graphics library at you that I’ll write through the course of this chapter. However, I promise that everything will be reasonably simple, while still complex enough to do something cool with. Here’s what this chapter will cover:

  • High-color modes
  • Page flipping and double buffering
  • The blitter
  • Clipping
  • Loading bitmaps
  • Color animation
  • Windowed DirectX
  • Getting information from DirectX

### Working with High-Color Modes

High-color modes (modes that require more than eight bits per pixel) are of course more visually pleasing to the eye than the 256-color modes. However, they aren’t used in software-based 3D engines for a number of reasons. The biggest reasons are as follows:

  • Computational speed—A standard 640×480 pixel frame buffer consists of 307,200 pixels. If each pixel is 8-bit, that means that most calculations can be done using a single byte per pixel and rasterization is simpler. On the other hand, in 16-bit or 24-bit modes, full RGB space calculations are usually employed (or very large lookup tables) and the speed is cut at least in half. Furthermore, two or three bytes per pixel must be written to the frame buffer instead of one as in 8-bit modes.

  Of course, with acceleration hardware, this isn’t as much of a problem for bitmapping or 3D (in fact, most 3D cards work in 24/32-bit color), but for software rasterization (which is what you’re learning in this book), it’s a big deal. You want to write the least amount of data per pixel as possible, and 8-bit mode meets this requirement (although it’s not as pretty as 16-bit). However, with 8-bit mode, you can rest assured that someone with a Pentium 75-100 might be able to play your game, and you won’t have to worry about your audience having a P233 with MMX and 3D acceleration at a minimum.

  • Memory bandwidth—This is something that people hardly ever take into consideration. Your PC has either an ISA (Industry Standard Architecture), VLB (VESA Local Bus), PCI (Peripheral Component Interface), or PCI/AGP (Accelerated Graphics Port) hybrid bus system. The bottom line is that everything but the AGP port is relatively slow compared to video clock rates. This means that although you may have a 500+ MHz Pentium III, it’s not going to do you any good if you have a PCI bus that’s bottlenecking your access to video RAM and/or acceleration hardware. Of course, a number of hardware optimizations can help in this area, such as caching, multi-port VRAM, and so forth, but there’s always a fill rate limit that you can never exceed no matter what you do. The moral of the story is that as you move to higher and higher resolutions and color depths, in many cases the memory bandwidth is more of a limiting factor than the processor’s speed. However, with AGP 2× and 4× this will become less of an issue.

Now that I’ve made my initial points about the appropriate uses for high-color modes, I’ll cover them in detail and show you how to work with them. I’ve decided to primarily target 8-bit modes to make the material and the 3D software easier to comprehend (3D is hard enough to understand without adding high-color RGB calculations to it). So let’s get started.

Working with high-color modes is conceptually similar to working with palletized modes, with the single caveat that you aren’t writing color indices into the frame buffer, but instead full RGB-encoded pixel values. This means that you must know how to create an RGB pixel encoding for the high-color modes that you want to work with. Figure 7.1 depicts a number of various 16-bit pixel encodings.

Figure 7.1 16-Bit RGB pixel encondings

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.