DirectDraw - Buffers and Bitmaps
This article extends the DirectDraw setup and foundation from the previous section, covering topics such as:
- High-colour modes
- Double buffering and page flipping
- Blitting
- Clipping
- Bitmaps
- Colour animation
- Working with windowed DirectX applications
High-colour modes
8-bit colour mode applications are good starting points when learning the fundamentals, and with much older hardware and/or software-based rasterisers (e.g. the DirectX9 2D engine) are all that is available.
Working with higher colour modes places more demands because of:
- increased computational requirements: working with an 8-bit palette can be acccomplished by a per-pixel basis, while 16-bit and 24-bit colour modes will require more sophisticated and consequently slower conversions or very large lookup tables
- increased GPU memory bandwidth requirements: only new AGP (accelerated graphics port) or PCI-E (peripheral component interconnect) express interfaces can sufficiently handle high-colour modes appreciably
16-bit colour modes
The actual colour mode available is decided by the hardware manufacturer. DirectX provides functions that enable an application to query the colour modes on offer.
Some examples of 16-bit colour modes include:
- Alpha.5.5.5 - this can visualised as three 5-bit blocks for red, green and blue colours, with the 16th bit dedicated to the alpha component (for image transparency). Each block can store a value up to 25 i.e. 32 values or shades of a given colour. This results in a palette matrix of 32x32x32 i.e. 32,768 colours.
- X.5.5.5 - this is similar to Alpha.5.5.5 except the transparency bit (the MSB, or most significant bit, see below excursion) is not used/is ignored.
- 5.6.5 - the most common 16-bit colour mode, with a 5-bit block for red and blue, and a 6-bit block for green (human eyes are more sensitive to green, so it was decided to add more range to the green spectrum). Overall that’s 32x64x32 or 65,536 colours.
Little endian and big endian ordering
A little excursion about how memory is ordered at the bit level.
A word is a fixed size/length block in memory that represents a unique operand, the dimensions of which are CPU dependent.
A colour word, in this article series, represents the block of memory that stores the red, green and blue shades for a given pixel. For 16-bit colour words (used for 16-bit colour modes) the 0th bit is known as the least significant bit (LSB) and the 15th bit is known as the most significant bit (MSB).

When a word is ordered (“filled”, if preferred) from the LSB first (as PCs do), the word adopts a little endian format. RGB colour words are organised such that red occupies the higher-order bits (e.g. the 10th to the 15th bits), with blue occupying the lower-order bits, and green somewhere in between. When assigning values red then green then blue in code, it is necessary to order (initialise) the word starting from the MSB. As such colour words adopt a big-endian format.
Building the colour word
With the above in mind, the following macros can be used to build colour words (as 2-byte USHORT) after passing 4-byte int for each red, green and blue parameter:
// Alpha.5.5.5, returns a 16-bit USHORT; the parameters are typically 32-bit int
#define _RGB16BIT555(r, g, b) (((r & 31) << 10) + ((g & 31) << 5) + (b & 31))
The blue (int) colour is masked so as to fit within the first 31 (25 - 1) values (the first five bits), the green colour is masked similarly but with an additional left-shift of 5 bits (equivalent to multipling the green shade by 25), and finally the red colour is masked and left-shifted by 10 bits.
Getting the pixel format
The RGB encoding available (the pixel format) is hardware dependent and therefore must be known by the application prior to rendering. One can deduce the pixel format with IDIRECTDRAWSURFACE7::GetPixelFormat():
HRESULT GetPixelFormat(LPDDPIXELFORMAT lpDDPixelFormat);
The sole parameter LPDDPIXELFORMAT is a pointer to DDPIXELFORMAT which is updated by GetPixelFormat() with information about the pixel format. One then queries the parameter (assuming the call hasn’t failed) to determine the pixel format.
Assuming that the DirectDraw primary surface (lpdds_primary) has already by initialised:
// assume this was initialised
LPDIRECTDRAWSURFACE7 lpdds_primary;
// this will store the pixel format
DDPIXELFORMAT ddPixel;
// memset to fill memory block with zeros, to prep ddPixel
memset(&ddPixel, 0, sizeof(ddPixel));
ddPixel.dwSize = sizeof(ddPixel);
lpdds_primary->GetPixelFormat(&ddPixel);
// now check the valid flags to determine the pixel format
if (ddPixel.dwFlags & DDPF_RGB){
// DDPF_RGB indicates that the surface is an RGB mode
// and can be queried via dwRGBBitCount
switch(ddPixel.dwRGBBitCount)
{
case 15: {
// 5.5.5 mode in use; can use the _RGB16BIT555 macro
} break;
case 16: {
// 5.6.5 mode in use
} break;
case 24: {
// 8.8.8 mode in use
} break;
case 32: {
// alpha(8).8.8.8 mode in use
} break;
default: break;
}
} else {
if (ddPixel.dwFlags & DDPF_PALETTEINDEXED8){
// DDPF_PALETTEINDEXED8 indicates 256 (8-bit) colour palettised mode
} else {
// something other pixel format...
}
}
So far, we need four variables/pointers to store:
- DirectDraw7 interface pointer (
LPDIRECTDRAW7) - DirectDraw surface description (
DDSURFACEDESC2) - DirectDraw primary surface pointer (
LPDIRECTDRAWSURFACE7) - Pixel format/RGB encoding provided (
DDPIXELFORMAT)
Plotting pixels in 16-bit
The previous section covered pixel plotting in some detail in 8-bit. In this section, we cover plotting pixels in 16-bit, i.e. without the need for a palette and instead referring to the pixel format of the hardware.
Like 8-bit colour modes, we still need to lock (and unlock) the video buffer. In general, one should minimise the number of times a buffer is locked and then unlocked: locking and unlocking buffers is expensive.
// builds a 16-bit colour value in 5.6.5 format (green dominant);
// note the green spectrum is 6 bits so covers (2^6 - 1) or 63 values
#define _RGB16BIT565(r, g, b) (((r & 31) << 11) + ((g & 63) << 5) + (b & 31))
// locking is handled outside this function so that we don't lock/unlock on a
// per pixel basis
inline void plotPixel16(
int x, int y,
int red, int green, int blue,
USHORT *videoBuffer, int lPitch, bool is16bitPitch)
{
USHORT pixel = _RGB16BIT565(red, green, blue);
// write the data
if (!is16bitPitch){
// recall lPitch is the memory width in bytes
// (8-bit blocks) but in 16-bit colour mode, pixels
// are stored in 16-bit words not 8-bit words,
// so we locate the next pixel every 16-bits and
// therfore adjust to a 16-bit
// block with a right-shift
videoBuffer[x + y*(lPitch >> 1)] = pixel;
} else {
videoBuffer[x + y*lPitch] = pixel;
}
}
In general, it is preferable to lock the buffer and draw all that is required before unlocking the buffer. The following shows this in action, and would be applied during a game loop:
int gameMain(void * params = NULL, int numParams = 0){
// check if user has pressed ESC this time around
if (KEYDOWN(VK_ESCAPE)){
SendMessage(mainWindowHandle, WM_CLOSE, 0, 0);
}
// surface desription init
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
if (FAILED(lpddsprimary->Lock(
NULL, $ddsd, DDLOCK_SURFACEPTR || DDLOCK_WAIT, NULL))){
// problem locking the primary surface
return(0);
}
USHORT * videoBuffer = (USHORT *) ddsd.lpSurface;
// now plot randomly located pixels with random colours
for (int index = 0; index < 1000; index++){
int red = rand() % 256;
int green = rand() % 256;
int blue = rand() % 256;
int x = rand() % 640;
int y = rand() % 480;
plotPixel16(
x, y, red, green, blue, videoBuffer, ddsd->lPitch, false);
}
// ..done, now unlock the primary surface
if (FAILED(lpddsprimary->Unlock(NULL))){
return(0);
}
return(1);
}
24-bit and 32-bit colour modes
24-bit colour modes use three 8-bit (on byte) per channel. An byte can hold up to 28 or 256 values, and hence across the RGB mode, there are 256x256x256 = 16.7 million colours. The red, green and blue channels are equally distributed, unlike 16-bit 5.6.5 mode, which is green heavy.
Since 24-bits is made up of three 8-bit blocks, the x- address for each colour word is located every three bytes. Within a colour word, the ordering from the low byte is still blue, then green, and then (the high byte) red.

The video buffer elements are of type DWORD (a 32-bit unsigned int). As shown above, the blue channel of pixel (0,0) resides in memory at 0, the green channel is one 32-bit int above this, and finally red is another above.
Plotting pixels in 24-bit mode involves setting the colour word with each colour channel in step, rather than apply bitwise operations as e.g. 16-bit macro that yield the entire colour word:
// again, assume buffer locking and unlocking is handled outside this function;
// recall UCHAR is an unsigned 8-bit int
inline void plotPixel24(
int x, int y,
int red, int green, int blue,
UCHAR *videoBuffer, int lPitch){
// DWORD 32-bit unsigned int as the address of the colour
// for the pixel (x, y)
DWORD pixelAddress = (x + x + x) + (y * lPitch);
// no bit fiddling here, simply set the colours in the
// buffer in the order B then G then R
videoBuffer[pixelAddress] = blue;
videoBuffer[pixelAddress + 1] = green;
videoBuffer[pixelAddress + 2] = red;
}
An equivalent macro that sets the colour word would be:
#define _RGB24BIT(r, g, b) (b + (g << 8) + (r << 16))
32-bit colour modes follow in that the blue channel resides in the low byte and each channel occupies 8-bits.
- Alpha(8).8.8.8 - the high byte represents the alpha channel (usually for transparency but also for other purposes). Being a 32-bit word, this fits entirely within 32-bit CPU registers.
- X(8).8.8 - this is similar to Alpha(8).8.8.8 but the alpha channel is not used at all.
It may seem as though 32-bit colours modes are redundant given that they don’t perform much more than 24-bit. Both support about 16.7 million colours. The situation is some GPUs do not support or address three-byte words and require a fourth byte to align words correctly. Hence, DirectX applications sometimes ignore 24-bit modes altogether and go with 32-bit colour modes without offering 24-bit colour modes.
We can extend the 16-bit pixel plotting function to suit 32-bit colours modes:
// builds a 32-bit colour value as a WORD
#define _RGB32BIT(alpha, r, g, b) (b + (g << 8) + (r << 16) + (alpha << 24))
// (32-bit) DWORD pitch
int lPitch32 = lPitch >> 2;
// locking is handled outside this function so that we don't lock/unlock on a
// per pixel basis
inline void plotPixel132(
int x, int y,
int alpha, int red, int green, int blue,
UINT *videoBuffer, int lPitch32)
{
// UINT is a 32-bit unsigned int
UINT pixel = _RGB32BIT(alpha, red, green, blue);
// write the data;
// lPitch is in 4 byte (32-bit) blocks
videoBuffer[x + y*(lPitch32)] = pixel;
}
Double buffering and page flipping
Drawing to an offscreen surface allows applications to hide surface operations (removing elements, updating pixel colours etc.) from viewers.
The process of drawing to an offscreen area and then copying it to a surface is known as double buffering.
A similar operation, known as page flipping. This is where the application draws to one of e.g. two visible areas (surfaces) before directing the hardware to display to newly drawn surface. Instead of copying an image to a surface, the hardware points to a different portion of memory.
Implementing double buffering
In short, one allocates a portion of memory for the offscreen area with the same resolution as the primary surface, fills the buffer with the next scene, before copying it to the primary surface.
Copying the data is somewhat involved, in that the memory stride (pitch) of the primary surface determines which C++ operations are required.
If the stride equals the horizontal resolution (i.e. there are no extraneous elements to the memory row) then one can call memcpy() e.g.:
// UCHAR is an unsigned 8-bit int
UCHAR * primaryBuffer;
// ...assume the primary buffer is already initialised
UCHAR * doubleBuffer = new UCHAR[640*480];
// ...assume the double buffer is filled later...
// e.g. doubleBuffer[x + (640*y)] = someColour
// the last parameter of memcpy() is the number of bytes to copy
// (from doubleBuffer to primaryBuffer)
memcpy((void *) primaryBuffer, (void *) doubleBuffer, 640*480);
If the stride is greater than the horizontal resolution, then one needs to skip the extraneous elements by advancing a pointer:
// UCHAR is an unsigned 8-bit int
UCHAR * primaryBuffer;
// ...assume the primary buffer is already initialised
UCHAR * doubleBuffer = new UCHAR[640*480];
// ...assume the double buffer is filled later...
// e.g. doubleBuffer[x + (640*y)] = someColour
for (int y = 0; y < 480; y++){
// copy the first 640 bytes (each pixel is one byte or 8-bits)
memcpy((void *) primaryBuffer, (void *) doubleBuffer, 640);
// memPitch is in number of bytes;
// advance the primary surface buffer by memPitch bytes
primaryBuffer += memPitch;
// get ready for the next line of pixels
doubleBuffer += 640;
}
Complex surfaces
Surfaces can be described as primary surfaces or secondary surfaces. Secondary surfaces are part of an animation chain that have the same resolution and colour depth as the primary surface. One can utilise page flipping to point the primary display to the next secondary surface.
Each offscreen surface (secondary surface) is a back buffer, and usually occupies the GPU VRAM. Page flipping in such cases is generally much faster than copying data between surfaces, though the number of back buffers is limited by the resolution requested and the amount of VRAM available.
For 8-bit colour depth, an m*n resolution will require m*n bytes per surface. A more common application which requires 16-bit colour depth with double buffering (i.e. one back buffer) will require 2*2*m*n bytes of VRAM (e.g. 640x480 at 16-bit with double buffering is 1,228,800 bytes, roughly 1.2 MB; likewise 2560x1440 at 32-bit colour depth with double buffering requires 7,372,800 bytes).
Creating a back buffer and relating it to the primary surface (in preparation for page flipping) is carried out in DirectX by creating a complex surface. Reviewing the procedure set out here
// assume the DirectDraw interface pointer lpdd7 has been initialised
// pointer to the PRIMARY surface interface
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
// pointer to A BACK BUFFER interface
LPDIRECTDRAWSURFACE7 lpddsBack = NULL;
// the surface description
DDSURFACEDESC2 ddsd;
// a general recommendation to clear (really fill with zeros) and prep ddsd
// with memset()
memset(&ddsd, 0, sizeof(ddsd));
// start initialising the structure's fields
ddsd.dwSize = sizeof(ddsd);
// the application can expect valid surface settings:
// ddsCaps (for DirectDraw) and a back buffer count (one in this case)
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
// now set the capabilities of the chosen field(s)
// set the back buffer count
ddsd.dwBackBufferCount = 1;
// set up for a complex, flippable surface
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
// now create the primary surface, and initialise lpddsprimary
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL))){
// failed to build the primary surface, clean up and exit
}
// get ready to request the back buffer
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
// now request the attached back buffer
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack))){
return (0);
}
// okay, now establised with the primary lpddsprimary
// and secondary surface lpddsBack...
To draw to the back buffer, simply lock the surface, draw to it then unlock it. The flip the page…this is decribed next.
Page flipping
Despite first appearances, a page-flipping mechanism does not change the definition of primary or secondary surfaces. A back buffer is always offscreen, with the primary surface always onscreen. That is, even though the pointer to a surface is updated during page flipping, the back buffer does not become the primary surface, and neither does the primary surface become a back buffer.
To flip the primary surface use Flip() (one does not flip a back buffer), the signature is given below:
HRESULT Flip(LPDIRECTDRAWSURFACE7 lpDDSurfaceTargetOverride, DWORD dwFlags);
The first parameter is advanced and passing NULL is normally fine. The second parameter is one of the following control flag:
- DDLIP_INTERVAL2 - flip after two retraces (vertical sync)
- DDLIP_INTERVAL3 - flip after three retraces
- DDLIP_INTERVAL4 - flip after four retraces
- DDFLIP_NOVSYNC - flip as close as possible to the next (monitor) scan line
- DDFLIP_WAIT - wait until a flip is possible (commonly used control flag)
The first three flags indicate how many vertical retraces to wait between each flip (defaults to one), and are only valid if DDSCAPS2_FLIPINTERVAL is declared by the ddsd surface description.
Both the back buffer and primary surface must be unlocked before the primary surface is flipped.
// this approach allows for repeated attempts
while(FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
The blitter (bit block transfers)
From a basic standpoint, drawing a bitmap to a surface involves can involve copying each pixel. For example, an 8x8 256-colour (8-bit) bitmap can be copied to a 640*480 surface at position (x,y) by the blitter as follows:
UCHAR *videoBuffer;
// assume this is initialised with an 8x8 image
UCHAR *bitmap;
int x;
int y;
// for each row of the bitmap
for (int bmY = 0; bmY < 8; bmY++){
// for each pixel along a row
for (int bmX = 0; bmX < 8; bmX++){
// pitch is 640 pixels; the origin (top-left)
// of the bitmap is at (x,y) on the surface
// and fills the surface to the right and below relative to this
videoBuffer[(x + bmX) + (y + bmY)*640] = bitmap[bmX + (bmY*8)];
}
}
The above code will copy everything over, including pixels that should be ignored i.e. transparent pixels (value of 0).
The above can be optimised to remove some of the addition and multiplication operators (in favour of bitwise operators) and ignore transparent pixels, with:
UCHAR *videoBuffer;
// assume this is initialised with an 8x8 image
UCHAR *bitmap;
int x;
int y;
// get the starting point on the surface
// i.e. videoBuffer += (x + (y)*640)
videoBuffer += (x + (y << 9) + (y << 7));
// used to read/write pixels
UCHAR currentPixel;
for (int bmY = 0; bmY < 8; bmY++){
// for each pixel along a row
for (int bmX = 0; bmX < 8; bmX++){
if (currentPixel = bitmap[bmX]){
// currentPixel > 0, not transparent
videoBuffer[bmX] = currentPixel;
}
}
bitmap += 8; // next row
videoBuffer += 640; // next line
}
The hardware blitter frees up the processor to work on other aspects.
DirectDraw provides two bitting methods, IDIRECTDRAWSURFACE7::Blt() and ``IDIRECTDRAWSURFACE7::BltFast()`. The former is more comprehensive, supporting additional features such as clipping, while the latter is simpler and consequently faster in simpler contexts.
HRESULT Blt(
LPRECT lpDestRect,
LPDIRECTDRAWSURFACE7 lpDDSrcSurface,
LPRECT lpSrcRect,
DWORD dwFlags,
LPDDBLTFX lpDDBltFx
)
LPRECT lpDestRect- the rectangle (portion of destination surface) to blit to (if NULL then blit to the entire surface)LPDIRECTDRAWSURFACE7 lpDDSrcSurface- the address to the source surfaceLPRECT lpSrcRect- the rectangle (portion of source surface) to blit from (if NULL then blit from the entire surface)DWORD dwFlags- defines blitting behaviour or operations, e.g. scaling, rotationLPDDBLTFX lpDDBltFx- an extensive structure detailing blitting properties e.g. special effects, what operations are supported
As mentioned, BltFast() is simpler:
HRESULT BltFast(
DWORD dwX,
DWORD dwY
LPDIRECTDRAWSURFACE7 lpDDSrcSurface,
LPRECT lpSrcRect,
DWORD dwTrans
)
The parameters dwX and dwY represent the x and y coordinates to blit to (on the destination surface). The last parameter dwTrans defines the type of blitter operation e.g. whether to adopt the source or destinations colour key (which colours should be treated as transparent, see later), or just copy with no transparency.
Never call Blt() on a locked surface. Locking (and unlocking) a surface is only required when one requires direct read-write access to the surface and would only be needed for manual (non-DirectDraw) based blitting. Simply call DirectDraw’s blitting methods without locking.
Filling a surface with a colour with the blitter
To fill a region of an 8-bit colour surface:
DDBLTFX ddBltFx;
RECT destRect;
// initialise the FX structure
memset(&ddBltFx, 0, sizeof(ddBltFx));
ddBltFx.dwSize = sizeof(ddBltFx);
// for 8-bit mode only e.g. colourIndex some value 0-255
// (actual colour is palette dependent);
// for higher modes e.g. for 16-bit mode green use
// ddBltFx.dwFillColor = __RGB16BIT565(0, 255, 0);
ddBltFx.dwFillColor = colourIndex;
// upper-left inclusive, these pixels are included
destRect.left = x1;
destRect.top = y1;
// lower-right exclusive, these pixels are a boundary,
// so really (x2 - 1) and (y2 - 1) are the lower right pixels
// included
destRect.right = x2;
destRect.bottom = y2;
lpddsPrimary->Blt(
destRect,
NULL,
NULL,
DDBLT_COLORFILL | DDBLT_WAIT,
&ddBltFx
);
The dwFlags DDBLT_COLORFILL and DDBLT_WAIT passed instruct the application to fill the destination surface with an RGB colour and wait until blitting is completed.
Blitting from a back buffer to the primary surface
The power of blitting is really the domain of moving bitmaps in blocks. This is often useful for sprites (bitmaps that move around the screen).
Blitting can only take place if the source and destination surfaces are defined. One can also blit from surfaces that are not related to the primary surface, though in the next example, we will blit from a back buffer (secondary surface) to the primary surface. (Later, when handling bitmaps is covered, this example can be extended to blit bitmaps.)
Following a slight modification to the colour fill blitter example:
// we don't require the Blt FX structure
RECT sourceRect, destRect;
sourceRect.left = x1;
sourceRect.top = y1;
sourceRect.right = x2;
sourceRect.bottom = y2;
destRect.left = x3;
destRect.top = y3;
destRect.right = x4;
destRect.bottom = y4;
lpddsPrimary->Blt(
destRect,
NULL,
&sourceRect,
DDBLT_WAIT,
NULL
);
Clipping
Clipping is the process of ignoring or omitting pixels or parts of an image that would otherwise be out of bounds of the viewport or window. DirectDraw does not support clipping of pixels or lines, so this must be implemented manually. DirectDraw does however support clipping of bitmaps.
Clipping on a pixel basis
To illustrate, one can define a rectangular region outside of which pixels will not be rendered.
// assume these define the upper-left and lower-right boundaries
// and are inclusive (pixels can occupy the boundary too)
int x1, y1, x2, y2;
// attempt to plot a pixel at (x, y) on a 640x480 8-bit surface
void plotPixelAndClip(
int x,
int y,
UCHAR colour,
UCHAR *videoBuffer){
if (x >= x1 && x <= x2 && y >= y1 && y <= y2){
videoBuffer[x + (y*640)];
}
}
To take into account the viewport dimensions, one can generalise the above as follows:
// assume these define the upper-left boundaries
// and are inclusive (pixels can occupy the boundary too)
int x1, y1;
// assume these viewport dimensions are already defined;
// we don't allow pixels to occupy the right and bottom boundaries
int width, height;
// attempt to plot a pixel at (x, y) on a 640x480 8-bit surface
void plotPixelAndClip(
int x,
int y,
UCHAR colour,
UCHAR *videoBuffer){
if (x >= x1 && x < width && y >= y1 && y < height){
videoBuffer[x + (y*640)];
}
}
Clipping without DirectDraw
As mentioned, DirectDraw support bitmap clipping. First, we review how to clip in software mode.
This can be achieved by overlaying the bitmap on the destination surface (e.g. the primary surface) and then clipping the bitmap where it lies outside of the viewport.
Let us assume the bitmap of dimensions width x height is first overlayed at a point (x, y) which may or may not reside in the viewport, with dimensions viewWidth x viewHeight. We overlay the bitmap, then identify the portion of the bitmap that does lie inside the viewport and only draw what lies inside the viewport.

int viewWidth = 640;
int viewHeight = 480;
void blitClipped(
int x,
int y,
int width,
int height,
UCHAR *bitmap,
UCHAR *videoBuffer,
int memPitch){
// make sure the bitmap top-left (origin) isn't out of the viewport
// or bitmap bottom-right isn't above and to the left of the viewport origin
if ((x >= viewWidth) || (y >= viewHeight) ||
(x + width <= 0) || (y + height) <= 0){
return;
}
// some of the bitmap is in the viewport, overlay it
// and find out, from the viewport's perspective, where the top-left
// and bottom-right of the bitmap are (exclusive)
int x1 = x;
int y1 = y;
// bottom-right at most is determined by width
// and height
int x2 = x1 + width - 1;
int y2 = y1 + height - 1;
// now check both corners don't reside outside
// the viewport and adjust if so
if (x1 < 0){
// can't draw top-left any further to the left
x1 = 0;
}
if (y1 < 0){
// can't draw top-left any further upwards
y1 = 0;
}
if (x2 >= viewWidth){
// can't draw bottom-right any further to the right
x2 = viewWidth - 1;
}
if (y2 >= viewHeight){
// can't draw bottom-right any further downwards
y2 = viewHeight - 1;
}
// now determine the (offset) origin of the bitmap
// from the viewport's perspective
int xOff = x1 - x;
int yOff = y1 - y;
// determine the no. of columns and rows
// of the bitmap to blit (this defines the
// area of the clipped bitmap)
int dx = x2 - x1 + 1;
int dy = y2 - y1 + 1;
// determine where blitting starts
// within the viewport (buffer)
videoBuffer += (x1 + (y1 * memPitch));
// determine where bitmap scanning
// starts (within the bitmap array)
bitmap += (xOff + (yOff * width));
// clipping prepared, start blitting
UCHAR currentPixel;
// for each row in the clipped bitmap
for (int bmY = 0; bmY < dy; bmY++){
// for each pixel along a row
for (int bmX = 0; bmX < dx; bmX++){
if (currentPixel = bitmap[bmX]){
// currentPixel > 0, not transparent
videoBuffer[bmX] = currentPixel;
}
}
bitmap += memPitch; // next row
videoBuffer += width; // next line
}
}
Clipping with DirectDraw
Instead of invoking a custom clipper, as above, one can utilise methods from the IDirectDrawClipper interface. To clip with DirectDraw:
- Create a clipper with
IDirectDrawClipper::CreateClipper() - Create a clipping list: a list of
RECTstructures for each clipped bitmap in the viewport [so the above example would involve two rectangles]. DirectDraw will only blit within the rectangles i.e. the clip list. - Set the clip list to the clipper
- Attach the clipper to the surface or window
The method IDIRECTDRAW7::CreateClipper() can in most cases be used without consideration to the parameters, as:
// assume DIRECTDRAW7 lpdd7 was initialised
// this will refer to the clipper
LPDIRECTDRAWCLIPPER lpddclipper = NULL;
if (FAILED(lpdd7->CreateClipper(0, &lpddclipper, NULL))){
// failed to create clipper, clean up and leave
return(0);
}
Creating a clipping list involves using a rather old data structure RGNDATA:
typedef struct _RGNDATA{
RGNDATAHEADER rdh;
char Buffer[1];
} RGNDATA;
The rectangles (the clipping list) is stored in a Buffer array (which can change in length dynamically). The array is stored in the stack and must be allocated appropriately before building the clipping list.
The RGNDATAHEADER acts as the header of a RGNDATA instance (region data) and is followed immediately (and continuously) by the Buffer array. The header contains information related to the array:
typedef struct _RGNDATAHEADER {
DWORD dwSize; // size of header in bytes
DWORD iType; // type of region data
DWORD nCount; // number of rectangles as RECTs
DWORD nRgnSize; // size of Buffer[]
RECT rcBound; // a bounding box around all RECTs
} RGNDATAHEADER;
(An example of initialising the clipping list will be given shortly.)
With the clipping list initialised, one then sets the clipping list with IDIRECTDRAWCLIPPER::SetClipList(). Finally, one then attaches the clipper to the surface with IDIRECTDRAWSURFACE7::SetClipper() (again, there is usually one one way to call these methods so the prototypes are not given here).
// assume DIRECTDRAW7 lpdd7 was initialised
// this will refer to the clipper
LPDIRECTDRAWCLIPPER lpddclipper = NULL;
if (FAILED(lpdd7->CreateClipper(0, &lpddclipper, NULL))){
// failed to create clipper, clean up and leave
return(0);
}
// initialise the clipping list
// (skipped here for now)
// assume this holds both the header and Buffer of RECTs
LPRGNDATA regionData;
if (FAILED(lpddclipper->SetClipList(®ionData, 0))){
// failed to set clipping list to clipper, clean up and leave
return (0);
}
// now attach clipper to surface (assume it is lpddsurface)
if (FAILED(lpddsurface->SetClipper(&lpddclipper))){
// failed to set clipper to surface, clean up and leave
return (0);
}
The clip list would take the form of an array of RECTs, for example:
RECT rectList[3] = {
{10, 10, 50, 50},
{100, 100, 200, 200},
{300, 300, 500, 450}
};
The code to set up the region data based on the above array is best managed by a function:
LPDIRECTDRAWCLIPPER attachClipper(
LPDIRECTDRAWSURFACE7 lpdds, int numRects, LPRECT clipList){
int index; // looping var
LPDIRECTDRAWCLIPPER lpddclipper; // pointer to the newly created dd clipper
LPRGNDATA regionData; // pointer to the region data
// first create the DirectDraw clipper
if (FAILED(lpdd7->CreateClipper(0, &lpddclipper, NULL)))
return(NULL);
// create the clip list....
// first allocate memory for region data
regionData =
(LPRGNDATA)malloc(sizeof(RGNDATAHEADER) + (numRects * sizeof(RECT)));
// now copy the rects into region data
memcpy(regionData->Buffer, clipList, (numRects * sizeof(RECT)));
// set up fields of header
regionData->rdh.dwSize = sizeof(RGNDATAHEADER);
regionData->rdh.iType = RDH_RECTANGLES;
regionData->rdh.nCount = numRects;
regionData->rdh.nRgnSize = numRects * sizeof(RECT);
// set the bounding rectangle
regionData->rdh.rcBound.left = 64000;
regionData->rdh.rcBound.top = 64000;
regionData->rdh.rcBound.right = -64000;
regionData->rdh.rcBound.bottom = -64000;
// find bounds of all clipping regions
for (index = 0; index < numRects; index++){
// test if the next rectangle unioned with the current bound is larger
if (clipList[index].left < regionData->rdh.rcBound.left)
regionData->rdh.rcBound.left = clipList[index].left;
if (clipList[index].right > regionData->rdh.rcBound.right)
regionData->rdh.rcBound.right = clipList[index].right;
if (clipList[index].top < regionData->rdh.rcBound.top)
regionData->rdh.rcBound.top = clipList[index].top;
if (clipList[index].bottom > regionData->rdh.rcBound.bottom)
regionData->rdh.rcBound.bottom = clipList[index].bottom;
}
// now we have computed the bounding rectangle region and set up the data
// now let's set the clipping list
if (FAILED(lpddclipper->SetClipList(regionData, 0))){
// release memory and return error
free(regionData);
return(NULL);
}
// now attach the clipper to the surface
if (FAILED(lpdds->SetClipper(lpddclipper))){
// release memory and return error
free(regionData);
return(NULL);
}
// all is well, so release memory and send back the pointer to the new clipper
free(regionData);
return(lpddclipper);
}
Calling the function is straighforward to the back buffer:
LPDIRECTDRAWCLIPPER lpddclipper = attachClipper(lpddsback, 3, rectList);
Bitmaps
There are numerous examples of bitmap files, including .pcx (PC Paint), .tga (Targa), .png (Portable Network Graphics) and (native to Windows) .bmp. This section applies solely on the latter.
A bitmap is composed of three parts, with an associated Win32 data structure:
- Bitmap file header,
BITMAPFILEHEADER - Bitmap info section,
BITMAPINFOHEADER - Bitmap data area, byte stream (array)
The bitmap file header is defined as:
typedef struct tagBITMAPFILEHEADER {
WORD bfType; // file type, 0x4D42 for .bmp
DWORD bfSize; // file size in bytes
WORD bfReserved1; // leave as 0
WORD bfReserved2; // leave as 0
DWORD bfOffBits; // offset or size of bitmap info in bytes
} BITMAPFILEHEADER;
The bitmap info section is composed of BITMAPINFOHEADER and (if required) RGBQUAD, the RGB palette information.
typedef struct tagBITMAPINFOHEADER {
DWORD biSize; // no. of bytes required by this struct
LONG biWidth; // bitmap width in pixels
LONG biHeight; // bitmap height in pixels (see below re. orientation)
WORD biPlanes; // no. of colour planes, must be 1
WORD biBitCount; // bits per pixel i.e. 1, 4, 8, 16, 24 or 32
DWORD biCompression; // type of compression, use BI_RGB for most cases
DWORD biSizeImage; // image size in bytes
LONG biXPelsPerMeter; // no. of pixels per meter along x-axis
LONG biYPelsPerMeter; // no. of pixels per meter along y-axis
DWORD biClrUsed; // no. of colours used by the this bitmap,
// usually 256 for 8-bit and 0 for higher
DWORD biClrImportant; // no. of important colours used by this bitmap,
// usually 256 for 8-bit and 0 for higher
} BITMAPINFOHEADER;
The palette applies to 8-bit colour modes and would then require an RGBQUAD instance:
typedef struct tagRGBQUAD{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}
If the parameter biHeight is negative then the bitmap is top-down DIB (device-independent bitmap) with an origin at the top-left corner. Contrastingly, a positive biHeight is a bottom-up DIB with the origin at the lower-left corner. Most bitmaps are bottom-up.
The bitmap data area is byte stream that defines the image pixels, in a line-by-line order. As implied by biHeight, the image will be upside-down if biHeight is positive. Some paint programs save updates to a bitmap in reverse order so the final output appears upside-down, with a postive biHeight. Later the application will know it is upside-down and reorientate it when loaded again.
In general (and in particular during development when changes are frequent), bitmaps are loaded from disk rather than embedding them into the exectuable as a resource. A Win32 API function that can load bitmaps is:
HANDLE LoadImage(
HINSTANCE hinst, // handle to the instance with the image (set to NULL)
LPCTSTR lpszName, // file name of image (filename or full path)
UINT uType, // image type (set to IMAGE_BITMAP)
int cxDesired, // desired width
int cyDesired, // desired height
UINT fuLoad // load flags
);
The desired width and height can be set to match the actual bitmap so that the application will not scale the bitmap. Otherwise, set this to zero and set the desired view dimensions later.
The load flags can normally be set to LR_LOADFROMFILE | LR_CREATEDIBSECTION to instruct the application to load the image according to the filename (lpszName) and to not translate the bitmap data to the current display device’s colour characteristics.
Loading bitmaps from disk and to memory
Once the bitmap file is loaded by the application, it is quite a lot of effort to get through each header and finally the byte stream. The following custom function handles all of this.
The following custom methods relate to a custom structure that stores a loaded bitmap:
typedef struct BITMAP_FILE_TAG{
BITMAPFILEHEADER bitmapfileheader; // bitmap file header
BITMAPINFOHEADER bitmapinfoheader; // bitmap file info (inc. palette if necessary)
PALETTEENTRY palette[256];
UCHAR *buffer; // pointer to the bitmap data
} BITMAP_FILE, *BITMAP_FILE_PTR;
To load a bitmap:
This custom function introduces several new Winbase.h functions, OpenFile(), _lread(), _lseek() and lclose().
OpenFile() is a somewhat general function used for creating, opening, reopening, or deleting files based on the uStyle parameter:
HFILE OpenFile(
LPCSTR lpFileName, // the name of the file
LPOFSTRUCT lpReOpenBuff, // pointer to an OFSTRUCT structure that
// receives file info when
// the file is first opened
UINT uStyle // the action to be taken
);
_lread() reads data from a specified file. This a 16-bit function that tends to be used for backwards compatibility. 32-bit applications should use ReadFile() instead.
UINT _lread(
HFILE hFile, // the file
LPVOID lpBuffer, // pointer to a buffer that contains the data read from the file
UINT uBytes // the number of bytes to be read from the file
);
_lclose() closes a file, making it unavailable for reading or writing. Like _lread(), this is a 16-bit function which can be replaced for 32-bit applications with CloseHandle() instead.
HFILE _lclose(
HFILE hFile // the file to close
);
_lseek() moves a file pointer to a specific location:
long _lseek(
int fd, // File descriptor referring to an open file
long offset, // Number of bytes from origin
int origin // Initial position
);
int loadBitmapFile(BITMAP_FILE_PTR bitmap, char *filename){
int fileHandle, index;
OFSTRUCT fileData; // the file data information
// open the file if it exists
if ((fileHandle = OpenFile(filename, &fileData, OF_READ)) == -1)
return(0);
// now load the bitmap file header
_lread(fileHandle, &bitmap->bitmapfileheader, sizeof(BITMAPFILEHEADER));
// test if this is a bitmap file
if (bitmap->bitmapfileheader.bfType != BITMAP_ID){
_lclose(fileHandle);
return(0);
}
// first the bitmap infoheader
// now load the bitmap file header
_lread(fileHandle, &bitmap->bitmapinfoheader, sizeof(BITMAPINFOHEADER));
// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8){
_lread(fileHandle, &bitmap->palette, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));
// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
for (index = 0; index < MAX_COLORS_PALETTE; index++){
// reverse the red and green fields
int tempColour = bitmap->palette[index].peRed;
bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
bitmap->palette[index].peBlue = tempColour;
// always set the flags word to this
bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
}
}
// finally the image data itself
_lseek(fileHandle, -(int)(bitmap->bitmapinfoheader.biSizeImage), SEEK_END);
// now read in the image, if the image is 8 or 16 bit then simply read it
// but if its 24 bit then read it into a temporary area and then convert
// it to a 16 bit image
if (bitmap->bitmapinfoheader.biBitCount == 8 ||
bitmap->bitmapinfoheader.biBitCount == 16 ||
bitmap->bitmapinfoheader.biBitCount == 24){
// delete the last image if there was one
if (bitmap->buffer)
free(bitmap->buffer);
// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *) malloc(bitmap->bitmapinfoheader.biSizeImage))){
_lclose(fileHandle);
return(0);
}
// now read it in
_lread(fileHandle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);
} else {
// serious problem
return(0);
}
#if 0
// write the file info out
printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d
\nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",
filename,
bitmap->bitmapinfoheader.biSizeImage,
bitmap->bitmapinfoheader.biWidth,
bitmap->bitmapinfoheader.biHeight,
bitmap->bitmapinfoheader.biBitCount,
bitmap->bitmapinfoheader.biClrUsed,
bitmap->bitmapinfoheader.biClrImportant);
#endif
// close the file
_lclose(fileHandle);
// flip the bitmap
flipBitmap(bitmap->buffer,
bitmap->bitmapinfoheader.biWidth *
(bitmap->bitmapinfoheader.biBitCount/8),
bitmap->bitmapinfoheader.biHeight);
// return success
return(1);
}
To unload a bitmap file:
int unloadBitmapFile(BITMAP_FILE_PTR bitmap){
if (bitmap->buffer){
free(bitmap->buffer);
bitmap->buffer = NULL;
}
// return success
return(1);
}
To flip a bitmap:
int flipBitmap(UCHAR *image, int bytesPerLine, int height){
UCHAR *buffer; // used to perform the image processing
int index;
// allocate the temporary buffer
if (!(buffer = (UCHAR *) malloc(bytesPerLine * height)))
return(0);
// copy image to work area
memcpy(buffer, image, bytesPerLine * height);
// flip vertically
for (index = 0; index < height; index++)
memcpy(&image[((height-1) - index) * bytesPerLine],
&buffer[index * bytesPerLine],
bytesPerLine);
free(buffer);
// return success
return(1);
}
Working with bitmaps
2D sprites can be animated based on single images or an image as a matrix of individual image cells, known as a template. Single images can be updated on the fly and accessed immediately in memory, whereas templates reduce the number of file loading for sprite animation. Templates artistically involve more work, and must be set out accurately and consistently; technically, they require additional extraction utilities.
Loading 8-bit images is straightforward (see below). It is possible (sometimes necessary) to modify the DirectDraw surface palette to match it with the artist’s palette and overall yield the desired look, with IDIRECTDRAWPALETTE::SetEntries().
The following snippet demonstrates 8-bit image handling and palette entry updates based on the custom functions described so far:
BITMAP_FILE bitmap;
if (!(loadBitmapFile(&bitmap, "path:\filename.bmp"))){
// error loading bitmap
return(0);
}
// set surface palette entries based on the bitmap palette
if (FAILED(lpddpal->SetEntries(0, 0, MAX_COLORS_PALETTE, bitmap.palette))){
// error setting palette entries, clean up and leave
return(0);
}
// do what's needed with bitmap.buffer etc.
// clean up
unloadBitmap(&bitmap);
Regarding the four parameters of IDIRECTDRAWPALETTE::SetEntries():
- Always set to zero, not used
- First colour entry to be set
- Number of entries to update/set
- An array of PALETTEENTRY structures that contains the palette entries that
SetEntries()uses to update the current object
Loading 16-bit images is relatively rare because very few paint applications support 16-bit images. Nonetheless, the approach is nearly identical to the above 8-bit methods (excluding palette entries).
When loading 24-bit images onto a 16-bit surface, one will need to apply a colour-scaling algorithm to convert the 24-bit word to a 16-bit word. This will be described in some detail in the next part.
Loading 24-bit images is straightforward with loadBitmapFile(). The bitmap buffer will hold the 3-byte word in the order blue, then green and then red.
As already mentioned, some GPUs will not support 24-bit surfaces and so it will be necessary to set up a 32-bit surface and pad the fourth byte (of a 32-bit colour word) with zeros.
#define _RGB32BIT(alpha, r, g, b) (b + (g << 8) + (r << 16) + (alpha << 24))
// when defining the pixel is needed...
// this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
DWORD pixel = _RGB32BIT(0, red, green, blue);
This will also be described in some detail in the next part.
Offscreen surfaces
Offscreen surfaces are useful, hardware accelerated holding points for bitmaps that only need to have the same dimensions as the bitmap they store.
The colour depth is matched to the primary surface, so there is no need to set this. It is necessary to set the colour key of offscreen surfaces (see snippet below). Colour keys are discussed in the next section, before moving on to blitting.
Creating offscreen surfaces
Creating offscreen surfaces that are neither the primary surface nor a (related) back buffer is somewhat similar to creating a primary surface, with a few alterations:
// assume the DirectDraw interface pointer lpdd7 has been initialised
LPDIRECTDRAWSURFACE7 createOffscreenSurface(
int width,
int height,
int memTypeFlag){
// pointer to the offscreen surface
LPDIRECTDRAWSURFACE7 lpddsOff = NULL;
// initialise the surface description
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
// start initialising the structure's fields
// decide which valid fields will be provided or required
// for the offscreen surface
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
// we need to define the width and height of all offscreen surfaces
// (this is usually set to handle whatever bitmap is stored, not
// necessarily the entire viewport)
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// now set the capabilities for an offscreen surface description,
// to occupy DDSCAPS_VIDEOMEMORY for video memory or
// DDSCAPS_SYSTEMMEMORY for system memory
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | memTypeFlag;
// set the offscreen colour key...(updated shortly, below)
DDCOLORKEY colourKey;
colourKey.dwColorSpaceLowValue = 0;
colourKey.dwColorSpaceHighValue = 0;
lpddsOff->SetColorKey(DDCKEY_SRCBLT, &colourKey);
// now create the offscreen surface
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsOff, NULL))){
// failed to build the offscreen surface, clean up and exit
return(0);
}
return lpddsOff;
}
Colour keys
Bitmaps are contained in rectangular cells though not all bitmap art is rectangular. When blitting bitmaps, it is only necessary to copy the part of the bitmap that illustrates the character or object, leaving all other pixels. To do this one can set a transparent colour, so that the destination surface pixels are not overlapped.
For this discussion, there are two ways of implementing transparency: source colour keying and destination colour keying. (Other colour keying, i.e. source overlay and destination overlay colour keying, can be used for video processing but are not discussed here.)
The colour key is handled by a structure, DDCOLORKEY, and will be needed when setting the colour key of the surface:
typedef struct _DDCOLORKEY{
DWORD dwColorSpaceLowValue;
DWORD dwColorSpaceHighValue;
} DDCOLORKEY, FAR* LPDDCOLORKEY;
The low and high values are colour depth dependent. For 8-bit these represent the colour indices. For high colour depths, these should be RGB encoded DWORDs e.g. __RGB16BIT565(0, 0, 32).
To set one colour, simply assign the same low and high value (they are both inclusive):
// black
colourKey.dwColorSpaceLowValue = 0;
colourKey.dwColorSpaceHighValue = 0;
To set a range of colours, apply inclusive values:
// 16-bit colour key range black to a red colour
colourKey.dwColorSpaceLowValue = _RGB16BIT565(0, 0, 0);
colourKey.dwColorSpaceHighValue = _RGB16BIT565(16, 0, 0);
Source colour keying defines the source surface colours that are not blitted to the destination surface. In effect, the colour key defines source colours that are considered transparent at the destination (i.e. which elements of the image should be replaced by pixels from the destination).
Updating createOffscreenSurface() with colour key settings:
LPDIRECTDRAWSURFACE7 createOffscreenSurfaceSrcBlitting(
LPDIRECTDRAW7 lpdd7,
int width,
int height,
int memTypeFlag,
int lowColourKeyValue,
int highColourKeyValue){
// pointer to the offscreen surface
LPDIRECTDRAWSURFACE7 lpddsOff = NULL;
// initialise the surface description
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
// start initialising the structure's fields
// decide which valid fields will be provided or required
// for the offscreen surface (with source colour key blitting)
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT;
// we need to define the width and height of all offscreen surfaces
// (this is usually set to handle whatever bitmap is stored, not
// necessarily the entire viewport)
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// now set the capabilities for an offscreen surface description,
// to occupy DDSCAPS_VIDEOMEMORY for video memory or
// DDSCAPS_SYSTEMMEMORY for system memory
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | memTypeFlag;
// set the offscreen colour key DDSD_CKSRCBLT
ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = lowColourKeyValue;
ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = highColourKeyValue;
// now create the offscreen surface
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsOff, NULL))){
// failed to build the offscreen surface, clean up and exit
return(0);
}
// now set the colour key for surface itself, with source blitting
DDCOLORKEY colourKey;
colourKey.dwColorSpaceLowValue = lowColourKeyValue;
colourKey.dwColorSpaceHighValue = highColourKeyValue;
if (lowColourKeyValue < highColourKeyValue){
// range of transparent colors required
lpddsOff->SetColorKey(DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &colourKey);
} else {
lpddsOff->SetColorKey(DDCKEY_SRCBLT, &colourKey);
}
return lpddsOff;
}
Destination colour keying is the opposite of source colour keying. It defines the source surface colours that are blitted on to the destination surface. If the destination pixel matches the color key, DirectDraw replaces it with the pixel from the source image (other destination surface colours take precendence and effectively mask the source image pixels).
Destination colour keying is only supported by hardware (HAL) and not supported by emulation (HEL).
Updating createOffscreenSurface() with colour key settings:
// assume the DirectDraw interface pointer lpdd7 has been initialised
LPDIRECTDRAWSURFACE7 createOffscreenSurfaceDestBlitting(
LPDIRECTDRAW7 lpdd7,
int width,
int height,
int memTypeFlag,
int lowColourKeyValue,
int highColourKeyValue){
// pointer to the offscreen surface
LPDIRECTDRAWSURFACE7 lpddsOff = NULL;
// initialise the surface description
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
// start initialising the structure's fields
// decide which valid fields will be provided or required
// for the offscreen surface (with source colour key blitting)
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKDESTBLT;
// we need to define the width and height of all offscreen surfaces
// (this is usually set to handle whatever bitmap is stored, not
// necessarily the entire viewport)
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// now set the capabilities for an offscreen surface description,
// to occupy DDSCAPS_VIDEOMEMORY for video memory or
// DDSCAPS_SYSTEMMEMORY for system memory
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | memTypeFlag;
// set the offscreen colour key DDSD_CKSRCBLT
ddsd.ddckCKDestBlt.dwColorSpaceLowValue = lowColourKeyValue;
ddsd.ddckCKDestBlt.dwColorSpaceHighValue = highColourKeyValue;
// now create the offscreen surface
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsOff, NULL))){
// failed to build the offscreen surface, clean up and exit
return(0);
}
// now set the colour key for surface itself, with destination blitting
DDCOLORKEY colourKey;
colourKey.dwColorSpaceLowValue = lowColourKeyValue;
colourKey.dwColorSpaceHighValue = highColourKeyValue;
if (lowColourKeyValue < highColourKeyValue){
// range of transparent colors required
lpddsOff->SetColorKey(DDCKEY_DESTBLT | DDCKEY_COLORSPACE, &colourKey);
} else {
lpddsOff->SetColorKey(DDCKEY_DESTBLT, &colourKey);
}
return lpddsOff;
}
Blitting from offscreen surfaces with Blt()
Combining many of the topics covered so far, applying source color keying based on memFlags:
// assume the DirectDraw interface pointer lpdd7, and surfaces for lpddsprimary
// and lpddsback have been initialised
DDSURFACEDESC2 ddsd;
LPDIRECTDRAWSURFACE7 offscreenSurface;
RECT destRect;
RECT sourceRect;
// initialise the FX structure
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT;
// prep the offscreen surface dimensions based on the
// bitmap
ddsd.dwWidth = 64;
ddsd.dwHeight = 64;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | memFlags;
// set the offscreen colour key DDSD_CKSRCBLT
ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
// now create the offscreen surface
if (FAILED(lpdd7->CreateSurface(&ddsd, &offscreenSurface, NULL))){
// failed to build the offscreen surface, clean up and exit
return(0);
}
// now assume we want to blit to back buffer, lpddsback at (x, y)...
// copy to (x, y)
destRect.left = x;
destRect.top = x;
destRect.right = x + 64 - 1;
destRect.bottom = y + 64 - 1;
// blit the entire source area
sourceRect.left = 0;
sourceRect.top = 0;
sourceRect.right = 64 - 1;
sourceRect.bottom = 64 - 1;
// no need to lock the surface if
// calling DirectDraw's Blt()
if (FAILED(lpddsback->Blt(
&destRect,
offscreenSurface,
&sourceRect,
(DDBLT | DDBLT_KEYSRC),
NULL))){
// error blitting from offscreen to back buffer
return (0);
}
The flag DDBLT_KEYSRC makes sure the blitter uses the colour key associated with the source surface, and is necessary even though the surface already has a colour key (of 0) defined.
Loading (scanning) bitmaps into a surface
One can load a bitmap (into a buffer with loadBitmapFile()) and then create a surface, scanning the bitmap into the surface’s buffer. The following outlines how this could be done with the offscreen buffer.
The following custom method createOffscreenSurface() creates an offscreen surface with dimensions that match a bitmap image as widthxheight:
LPDIRECTDRAWSURFACE7 createOffscreenSurface(
int width, int height, int memFlags, int color_key = 0){
DDSURFACEDESC2 ddsd; // working description for the new surface
LPDIRECTDRAWSURFACE7 lpdds; // temporary surface
// set to access caps, width, and height
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// set surface to offscreen plain
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | memFlags;
// create the surface
if (FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL)))
return(NULL);
if (color_key >= 0){
DDCOLORKEY color_key;
color_key.dwColorSpaceLowValue = 0;
color_key.dwColorSpaceHighValue = 0;
// now set the color key for source blitting
lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key);
}
return(lpdds);
}
The following method scans a cell of bitmap template into an existing surface. It is possible to start at a different point (i.e. cell of a template) within the bitmap by passing non-zero values for cx and cy.
int scanBitmapToSurface(
BITMAP_FILE_PTR bitmap, LPDIRECTDRAWSURFACE7 lpdds, int cx, int cy){
UCHAR *sourcePtr, *destPtr;
DDSURFACEDESC2 ddsd; // direct draw surface description
ddsd.dwSize = sizeof(ddsd);
// manual blit here, so lock the surface
lpdds->Lock(NULL,
&ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
// get the cell (or entire bitmap) coords
// from the surface lpdds description;
// compute position to start scanning bits from
cx = cx*(ddsd.dwWidth + 1) + 1;
cy = cy*(ddsd.dwHeight + 1) + 1;
// get the starting point along the linear bitmap buffer
sourcePtr = bitmap->buffer + (cx + cy*bitmap->bitmapinfoheader.biWidth);
// assign a pointer to the memory surface for manipulation
destPtr = (UCHAR *) ddsd.lpSurface;
// copy each line
for (int bitmapY = 0 ; bitmapY < ddsd.dwHeight; bitmapY++){
memcpy(destPtr, sourcePtr, ddsd.dwWidth);
// advance pointers
destPtr += (ddsd.lPitch); // (ddsd.dwWidth);
sourcePtr += bitmap->bitmapinfoheader.biWidth;
}
// unlock the surface
lpdds->Unlock(NULL);
return(1);
}
To load the bitmap into an offscreen surface, first load the bitmap (i.e. initialise bitmap), build an offscreen surface and finally scan the image into said surface.
LPDIRECTDRAWSURFACE7 offscreenSurface;
// load the 8-bit image
if (!loadBitmapFile(&bitmap, "filename.bmp"))
return(0);
// create a surface e.g. 64x64, with defaults
offscreenSurface = createOffscreenSurface(64, 64, 0);
// scan the bitmap from the origin
scanImageIntoSurface(&bitmap, offscreenSurface, 0, 0);
// unload the bitmap file, we no longer need it
unloadBitmapFile(&bitmap);
Bitmap rotation
Bitmap rotation is complicated and expensive, and as a result is only supported by DirectDraw via HAL. If the GPU does not support this (quite rare), then one will need to resort to writing their own algorithm in software.
To rotate bitmaps with DirectDraw, pass the flag DDBLT_ROTATIONANGLE and update the dwRotationAngle property of DDBLTFX:
DDBLTFX ddbltfx;
memset(&ddbltfx, 0, sizeof(ddbltfx));
ddbltfx.dwSize = sizeof(ddbltfx);
// units of 1/100 of a degree e.g. 45 degrees
ddbltfx.swRotationAngle = 4500;
// assume lpddsback and offscreenSurface are initialised;
// note ddbltfx is passed
if (FAILED(lpddsback->Blt(
&destRect,
offscreenSurface,
&sourceRect,
(DDBLT | DDBLT_KEYSRC | DDBLT_ROTATIONANGLE),
ddbltfx))){
// error blitting from offscreen to back buffer
return (0);
}
Bitmap scaling
Bitmap scaling can be accomplished by increasing the destination rectangle (struct) and blitting a 64x64 pixel image surface (back buffer or offscreen) to another surface.
For example, to scale to the dimensions mxn:
destRect.left = x;
destRect.top = x;
destRect.right = x + m - 1;
destRect.bottom = y + m - 1;
// blit the entire source area
sourceRect.left = 0;
sourceRect.top = 0;
sourceRect.right = 64 - 1;
sourceRect.bottom = 64 - 1;
// assume lpddsback and offscreenSurface are initialised
if (FAILED(lpddsback->Blt(
&destRect,
offscreenSurface,
&sourceRect,
(DDBLT | DDBLT_KEYSRC),
NULL))){
// error blitting from offscreen to back buffer
return (0);
}
Discrete sampling theory and bilinear filtering
Scaling bitmaps is not only applicable to image sizing but also part of more general operations e.g. texture mapping. Textures have to scaled to fit inside a polygon.
There are two types of mappings: forward mapping and inverse mapping.
Forward mapping takes each source pixel, passes them all through a mapping function and maps (plots) them on the destination surface. The mapping function determines which source pixels are reproduced on the destination, and this need not be all pixels e.g. when a source image is scaled down (some source pixels are effectively discarded by the function).
Inverse mapping works the other way, taking each destination pixel and then determining which source pixel(s) (plural being important here) should be used to in place of the destination pixel. In some cases, there may not be a suitable source pixel available, and so the affected pixel will have to replicated from a neighbouring destination pixel. For example, when an image is scaled up, some of the source pixels are duplicated at the destination.
In both cases, rather than copy the colour of the source pixel literally, it is more realistic to take an average (gradient) of adjacent source pixel colours before rendering the destination image. This gradient is handled by a bilinear filter.

Filtering is discussed in more detail later.
For now, bitmap scaling can be performed in software, taking a sample (hence the title) of the previous pixel to determine the colour the next pixel as the scaled image is rendered.
float sourceHeight;
float destHeight;
// doubled scale (factor of two increase), sampleRate = 0.5
// or scale down by a third (four pixels to three pixels),
// sampleRate is approx. 1.3
float sampleRate = sourceHeight / destHeight;
float sampleIndex = 0;
for (int index = 0; index < destHeight; index++){
destBitmapBuffer[index] = sourceBitmap[(int) sampleIndex];
// sampleIndex represents the next sourceBitmap pixel to copy
sampleIndex += sampleRate;
}
Colour effects
During the early days of DirectDraw development, developers were required to mainly target 8-bit colour applications. This obviously presented a number of limitations given that a 256 colour palette.
A number of strategies to diversify the colour schemes involved dynamically updating the colour palette at runtime. While modern day applications are not limited to 8-bit colour modes anymore, we go over these ideas in brief.
8-bit colour animation
Colour animation refers to the modification or shifting of colour palette entries at runtime. This provides effects for e.g. blinking lights, glowing objects.
The pixel where blinking can occur is handled by a custom structure:
typedef struct BLINKER_TYP {
int colourIndex; // palette index of colour to blink
PALETTEENTRY onColour; // "on" RGB value
PALETTEENTRY offColour; // "off" RGB value
int onTime; // no. of frames to keep "on"
int offTime; // no. of frames to keep "off"
// internal member
int counter; // counter for state transitions
int state; // state of light, -1 off, 1 on, 0 dead
} BLINKER, *BLINKER_PTR;
A function which blinks lights (pixels) takes in a command flag with a pointer to the above structure, which is stored statically in lights.
#define BLINKER_ADD 0 // add a light to database
#define BLINKER_DELETE 1 // delete a light from database
#define BLINKER_UPDATE 2 // update a light
#define BLINKER_RUN 3 // run normal
// ...
int blinkColours(int command, BLINKER_PTR newLight, int id){
static BLINKER lights[256]; // supports up to 256 blinking lights
static int initialised = 0; // tracks if function has initialised
if (!initialised){
initialised = 1;
// clear out all structures
memset((void *)lights, 0, sizeof(lights));
}
switch (command){
case BLINKER_ADD: {
for (int index = 0; index < 256; index++){
if (lights[index].state == 0){
// set light up
lights[index] = *newLight;
// set internal fields up
lights[index].counter = 0;
lights[index].state = -1; // off
// update palette entry
lpddpal->SetEntries(
0, lights[index].colourIndex, 1, &lights[index].offColour);
return(index);
}
}
} break;
case BLINKER_DELETE: {
if (lights[id].state != 0){
// kill the light
memset((void *)&lights[id], 0, sizeof(BLINKER));
return(id);
} else
return(-1);
} break;
case BLINKER_UPDATE: {
if (lights[id].state != 0){
// update on/off parms only
lights[id].onColour = newLight->onColour;
lights[id].offColour = newLight->offColour;
lights[id].onTime = newLight->onTime;
lights[id].offTime = newLight->offTime;
// update palette entry
if (lights[id].state == -1) {
lpddpal->SetEntries(
0, lights[id].colourIndex, 1, &lights[id].offColour);
} else
lpddpal->SetEntries(
0, lights[id].colourIndex, 1, &lights[id].onColour);
return(id);
} else
return(-1);
} break;
case BLINKER_RUN: {
// process each light
for (int index = 0; index < 256; index++) {
if (lights[index].state == -1) {
// off
// advance the counter and switch on if counter catches up
// with offTime during this cycle
if (++lights[index].counter >= lights[index].offTime){
lights[index].counter = 0;
lights[index].state = -lights[index].state;
// update colour
lpddpal->SetEntries(
0, lights[index].colourIndex, 1, &lights[index].onColour);
}
} else if (lights[index].state == 1) {
// on
// advance the counter and switch off if counter catches up
// with offTime during this cycle
if (++lights[index].counter >= lights[index].onTime){
lights[index].counter = 0;
lights[index].state = -lights[index].state;
// update colour
lpddpal->SetEntries(
0, lights[index].colourIndex, 1, &lights[index].offColour);
}
}
}
} break;
default: break;
}
return(1);
}
To start animation run of lights (but not necessarily blink all):
blinkLights(BLINKER_RUN, NULL, 0);
To create a new (red) light:
BLINKER temp;
PALETTEENTRY red = {255, 0, 0, PC_NOCOLLAPSE};
PALETTEENTRY green = {0, 255, 0, PC_NOCOLLAPSE};
PALETTEENTRY black = {0, 0, 0, PC_NOCOLLAPSE};
temp.colorIndex = 253;
temp.onColour = red;
temp.offColour = black;
temp.onTime = 30; // 30 cycles at 30fps = 1 sec
temp.offTime = 30;
int red_id = blinkColours(BLINKER_ADD, &temp, 0);
To update the (above red) light:
BLINKER temp;
temp.onColour = {255, 255, 0, PC_NOCOLLAPSE};;
temp.offColour = {0, 0, 0, PC_NOCOLLAPSE};
temp.onTime = 100; // 30 cycles at 30fps = 1 sec
temp.offTime = 200;
blinkColours(BLINKER_UPDATE, &temp, red_id);
To delete the (above red) light:
blinkColours(BLINKER_DELETE, NULL, red_id);
8-bit colour rotation or shifting
This technique involves updating the palette, taking a collection of colour entries (registers or range) and then (in effect) updating any pixel that has a colour from the collection with another colour from the collection. With an appropriate delay, it is possible to give the image the feeling of motion.
IDIRECTDRAWPALETTE::SetEntries() was described above. Another related method IDIRECTDRAWPALETTE::GetEntries() also has four parameters, as follows:
- Always set to zero, not used
- First colour entry to be retrieved
- Number of entries to retrieve
- An array of PALETTEENTRY structures that receives palette entries from the current object
To rotate or shift colours:
int rotateColours(int startIndex, int endIndex){
int colourCount = endIndex - startIndex + 1;
PALETTEENTRY workingPalette[256];
// init workingPalette with the current palette's entries
// in the range [startIndex to endIndex]
lpddpal->GetEntries(0, startIndex, colourCount, workingPalette);
// update the current palette, shifting the colours by one
// (move along to the adjacent colour entry),
// except for the last entry
lpddpal->SetEntries(0, startIndex + 1, colourCount - 1, workingPalette);
// handle that last colour (take it back to the beginning of the collection)
lpddpal->SetEntries(0, startIndex, 1, &workingPalette[colourCount - 1]);
// optional for now: update the shadow palette (discussed later)
// lpddpal->GetEntries(0, 0, 256, shadowPalette);
return(1);
}
For example, animating a waterfall:
// ...back in the game loop...
// e.g. the water colour entries (indices) are at 133-143
// in the palette
rotateColours(133, 143);
// wait 33 ms
Sleep(33);
// ...out of the game loop
Gamma correction
For 16-bit colour depth (or higher) DirectDraw applications, an RGB colour intensity I can be roughly equated to:
I * pixel(r, g, b) = pixel(I*r, I*g, I*b);
As shown, the relationship is linear. However, monitors are non-linear in respect of light intensity. The displayed intensities are shifted in accordance with an exponential, monitor response curve.
To correct for such nonlinearities, applications can apply gamma correction. Gamma correction is a nonlinear operation that controls the luminance (roughly, light intensity) of a video signal, without affecting the red, green or blue components.
Gamma correction across a targetted area is usually more optimal (rather than updating every pixel on the surface) can give the impression of colour animation (e.g. lightening or darkening) but is still a fairly expensive operation. One approach is via a 2D matrix, as a lookup table.
The lookup table can represent a limited RGB value range, where each row m represents one colour and each element in that row n represents each shade of said colour. One can then lookup the required shade for all colours in m by looking up a given intensity in n. This approach with lookup tables is used for several features, including transparency.
DirectDraw (since version 5.0) provides some support for gamma and colour control:
IDirectDrawGammaControl- this gives programmers control over the red, green and blue gamma ramps (gamma curves, represented in practice by the aforementioned lookup tables)IDirectDrawColorControl. this gives programmers control over the brightness, contrast, hue, saturation, sharpness and gamma of the entire primary surface or overlay (essentially, another surface overlapping a part of the primary surface)
Not all legacy GPUs support the above interfaces.
Running GDI with DirectDraw
In some cases, it may be necessary to access GDI grapical functions during development or prototyping. GDI graphical functions are much slower than DirectX functions, and long term would not normally be part of the application.
To invoke GDI methods etc. in DirectDraw applications, first get the GDI device context on an unlocked surface (only) with IDIRECTDRAWSURFACE7::GetDC():
LPDIRECTDRAWSURFACE7 lpdds;
HDC xdc;
// make sure the surface is not locked
if (FAILED(lpdds->GetDC(&xdc))){
// error getting GDI device context
return(0);
}
// surface now locked by GDI;
// do stuff with GDI e.g. invoke GDI graphical functions
COLORREF oldForeColour = SetTextColor(xdc, RGB(0,255,0));
COLORREF oldBackColour = SetBkColor(xdc, RGB(0,0,0));
int oldTransparencyMode = SetBkMode(xdc, TRANSPARENT);
TextOut(
xdc,
20,
30,
"GDI in DirectDraw app",
strlen("GDI in DirectDraw app")
);
// ...now release resources
if (FAILED(lpdds->ReleaseDC(xdc))){
// problem trying to release GDI
return(0);
}
// all done, surface no longer locked
// by GDI
From an example perspective, the above is not optimal. COLORREFs are 24-bit RGB encoded, and hence running this on e.g. an 8-bit colour depth will cause DirectX to spend time trying to find the closest colour match.
Getting hardware supported features
Sometimes it is necessary to determine the state or capability of the GPU hardware with respect to DirectX at runtime. Both the HEL and HAL featureset can be queried with IDIRECTDRAW7::GetCaps():
HRESULT GetCaps(
LPDDCAPS lpDDriverCaps, // HAL
LPDDCAPS lpDDHELCaps // HEL
);
The DDCAPS structure (pointed to by LPDDCAPS) is extensive and to a degree self-explanatory. Some examples of fields include:
dwCKeyCaps- color key caps (capabilities)dwFXCaps- stretching and effects capsdwPalCaps- palette capsdwAlphaBltConstBitDepths- one of several alpha bit-depth capsdwVidMemTotal- total video memorydwVidMemFree- total free video memorydwCurrVisibleOverlays- no. of overlays currently visibledwMaxVideoPorts- max. no. of live video portsddsCaps- general surface caps
Some useful surface related queries include:
HRESULT IDIRECTDRAW7::GetDisplayMode(LPDDSURFACEDESC2 lpDDSurfaceDesc2)- useful to find out if the system is currently in windowed modeHRESULT IDIRECTDRAWSURFACE7::GetSurfaceDesc(LPDDSURFACEDESC2 lpDDSurfaceDesc)- more details about a surfaceHRESULT IDIRECTDRAWPALETTE::GetCaps(LPDWORD lpdwCaps)- returnsDWORDflags that describe the palette’s capabilities e.g.DDCAPS_8BIT(supports 8-bit colour palettes),DDCAPS_ALPHA(supports an alpha component to each entry) andDDCAPS_PRIMARYSURFACE(palette is attached to a primary surface)
Windowed mode
Running DirectDraw applications in windowed mode is more involved than running applications in fullscreen mode. The resolution and colour depth of the desktop may not always match the DirectDraw application design/requirements.
There are several points/limitations to consider:
- With windowed applications, the primary surface is the entire screen display. Thus, drawing to the primary surface will require consideration to other windows running/open on screen. In such cases, it is necessary to first determine the exact location of the DirectDraw application (the client window) before drawing to the DirectDraw client area.
- Blitting to a windowed application is also more involved, since the blitter will need to be told where to clip to make sure regions outside of the client window are not affected.
- The above two problems are compounded by the fact that the windowed application can be resized and moved around!
- Running a windowed 8-bit colour depth application will also need to utilise a palette that other applications on screen remain unaffected. That is, one will need to reserve a number of palette entries for system colours, so that other applications can be understood by the user. This is outlined at the end of this section.
- Complex surfaces, secondary back buffers and page flipping cannot be used in windowed mode. (This also explains why windowed DirectX applications are generally slower than fullscreen applications.)
It is possible to create an offscreen buffer (surface) and blit from that to the primary surface (the whole screen).
Setting up a windowed DirectDraw application
Recall from the section re. Cooperative level, we now need to use the DDSCL_NORMAL level:
// assume these are global
HWND mainWindowHandle = NULL;
RECT clientAreaRequired = {0, 0, 640, 480};
HINSTANCE hinstance; // part of WinMain params
// in WinMain()...
HWND hwnd; // generic window handle
// create the window with
// WS_OVERLAPPED -> fixed size window with a title
// of dimensions 800 x 600 (this will be adjusted shortly)
if (!(hwnd = CreateWindowEx(
NULL, WINDOW_CLASS_NAME, "DirectDraw Windowed Demo", WS_OVERLAPPED | WS_VISIBLE,
0, 0, // initial position (x, y)
800, 600, NULL, NULL, hInstance, NULL)))
return(0);
// save main window handle
mainWindowHandle = hwnd;
// now make sure the client area is fixed at 640x480
// with AdjustWindowRectEx()
AdjustWindowRectEx(
clientAreaRequired,
GetWindowStyle(mainWindowHandle),
GetMenu(mainWindowHandle) != NULL,
GetWindowExStyle(mainWindowHandle));
// move the window a bit with MoveWindow()
// to get it to size automatically
MoveWindow(
mainWindowHandle,
CW_USEDEFAULT,
CW_USEDEFAULT,
clientAreaRequired.right - clientAreaRequired.left, // resized width
clientAreaRequired.bottom - clientAreaRequired.top, // resized height
false);
// in gameInit()...
LPDIRECTDRAW7 lpdd = NULL;
// create IDirectDraw interface 7.0 object and test for error
if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
return(0);
// set cooperation to normal for windowed applications
if (FAILED(lpdd->SetCooperativeLevel(mainWindowHandle, DDSCL_NORMAL))){
// error setting cooperative level
return(0);
};
// then continue with the DirectDraw init...
// pointer to the surface interface
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
// the surface description
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL))){
// failed to build the primary surface, clean up and exit
return(0);
}
// now attach a clipper to the primary surface
LPDIRECTDRAWCLIPPER lpddclipper = NULL;
if (FAILED(lpdd->CreateClipper(0, &lpddclipper, NULL))){
// error creating clipper
return(0);
}
// now set the clippers window handle via mainhandleWindow
if (FAILED(lpddclipper->SetHWnd(0, mainWindowHandle))){
// failed to set clipper window handle
return(0);
}
// finally, attach the clipper to the primary surface
if (FAILED(lpddsprimary->SetClipper(lpddclipper))){
// failed to attach clipper
return(0);
}
// okay, good to go...
The remaining discussion assumes the application is set up as above, in particular, the fixed window size with a client area guaranteed to be 640x480 (the window size can be whatever it needs to be).
Note that in order to release the clipper (with e.g. lpddclipper->Release()), this must be done twice: before and after releasing the surface. This is because, technically, there are two references to the clipper, one from CreateClipper() and the other from SetClipper().
Getting the window coordinates and colour depth
Given that the user can move the windowed application anywhere on the screen (after WinMain() has started), we need to retrieve the current coordinates before drawing to the client area.
To get the upper-left coordinates of the window, which includes any controls and borders:
BOOL GetWindowRect(
HWND hWnd, // window handle
LPRECT lpRect // address of coords structure
);
Next, we need to get the colour depth (pixel format) of screen.
// still in gameInit()...
int pixelFormat = 0;
DDPIXELFORMAT ddpixelFormat;
memset(&ddpixelFormat, 0, sizeof(ddpixelFormat));
ddpixelFormat.dwSize = sizeof(ddpixelFormat);
// continuing with the primary surface lpddsprimary
lpddsprimary->GetPixelFormat(&ddpixelFormat);
pixelFormat = ddpixelFormat.dwRGBBitCount;
Drawing to the client area
This involves finding the client area of the window (i.e. without the title bar, controls or borders) before drawing pixels/lines etc.
Continuing from the previous section:
// now in the game loop, about to draw something...
DDSURFACEDESC2 ddsd; // surface description
RECT client; // used to hold client rectangle
// make sure this isn't executed again
if (window_closed)
return(0);
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE)){
PostMessage(mainWindowHandle, WM_CLOSE, 0, 0);
window_closed = 1;
}
// get the window's client rectangle in screen coordinates
GetWindowRect(mainWindowHandle, &client);
// initialize structure
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
// lock the primary surface
lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
// get video pointer to primary surface
// cast to VOID * since we don't know what we are
// dealing with yet
UCHAR *primaryBuffer = (UCHAR *)ddsd.lpSurface;
// assume pixelFormat is global and initialised;
// with any pixelFormat, draw 10 random pixels at (x, y)
int x, y;
if (pixelFormat == 32){
for (int index = 0; index < 10; index++){
x = rand() % (client.right - client.left) + client.left;
y = rand() % (client.bottom - client.top) + client.top;
DWORD color = _RGB32BIT(0, rand() % 256, rand() % 256, rand() % 256);
*((DWORD *)(primaryBuffer + x*4 + y*ddsd.lPitch)) = color;
}
} else if (pixelFormat == 24){
for (int index = 0; index < 10; index++){
x = rand() % (client.right - client.left) + client.left;
y = rand() % (client.bottom - client.top) + client.top;
((primaryBuffer + x*3 + y*ddsd.lPitch))[0] = rand() % 256;
((primaryBuffer + x*3 + y*ddsd.lPitch))[1] = rand() % 256;
((primaryBuffer + x*3 + y*ddsd.lPitch))[2] = rand() % 256;
}
} else if (pixelFormat == 16){
for (int index = 0; index < 10; index++){
x = rand() % (client.right - client.left) + client.left;
y = rand() % (client.bottom - client.top) + client.top;
USHORT color = _RGB16BIT565(rand()%256, rand()%256, rand()%256);
*((USHORT *)(primaryBuffer + x*2 + y*ddsd.lPitch)) = color;
}
} else {
// assume 8-bit mode
for (int index = 0; index < 10; index++){
x = rand() %(client.right - client.left) + client.left;
y = rand() %(client.bottom - client.top) + client.top;
UCHAR color = rand() % 256;
primaryBuffer[x + y*ddsd.lPitch] = color;
}
}
// unlock primary buffer
if (FAILED(lpddsprimary->Unlock(NULL)))
return(0);
Windowed 8-bit DirectDraw applications
As alluded to, one will need to forgoe a handle of palette entries when running 256-colour DirectDraw applications in windowed mode. This is because of the Windows Palette Manager. When the DirectDraw application gets focus, Windows will start mapping this application’s palette to a hardware (physical) palette, which in turn applies to all other applications on screen.
Windows needs the first 10 and the last 10 entries for all other applications to look somewhat reasonable. (Therefore, if the DirectDraw application was always going to have the option to run in 8-bit windowed mode, then one would design a palette of 236, not 256, colours.)
Revisting palette entry initialisation:
PALETTENTRY palette[256];
int upperColour;
// deal with Windows Palette Manager first...
for (int colour = 0; colour < 10; colour++){
// doesn't matter what these are assigned, they're never used
// by the DirectDraw app
palette[colour].peRed = colour;
palette[colour].peGreen = 0;
palette[colour].peBlue = 0;
// do not map this entry to the physical palette
// i.e. other Windows applications should ignore
// this entry (use this if the DirectDraw app palette
// is likely to change following e.g. colour animation)
palette[colour].peFlags = PC_EXPLICIT;
// work on the final ten entries similarly...
upperColour = colour + 246;
palette[upperColour].peRed = upperColour;
palette[upperColour].peGreen = 0;
palette[upperColour].peBlue = 0;
palette[upperColour].peFlags = PC_EXPLICIT;
}
// start at the 11th entry and finish at element 245
for (int colour = 10; colour < 246; colour++){
palette[colour].peRed = rand() % 256;
palette[colour].peGreen = rand() % 256;
palette[colour].peBlue = rand() % 256;
// PC_NOCOLLAPSE prevents Windows or DirectX from
// automatically optimising the palette;
// PC_RESERVED instructs Windows not to map other
// Windows app colours to this entry
palette[colour].peFlags = PC_NOCOLLAPSE | PC_RESERVED;
}
// the palette interface received
LPDIRECTDRAWPALETTE lpddpal = NULL;
if (FAILED(lpdd7->CreatePalette(
DDCAPS_8BIT | DDCAPS_INITIALIZE,
palette,
&lpddpal,
NULL
))){
// problem setting up the display palette, clean up and exit
}
// okay, all ready...