### Step 1: Loading the Palette Entries

The following code in DDEX5 loads the palette entries with the values in the lower half of the All.bmp file (the part of the bitmap that contains the red donuts):

// First, set all colors as unused.

for(i=0; i<256; i++)

{

torusColors[i] = 0;

}

// Lock the surface and scan the lower part (the torus area),

// and keep track of all the indexes found.

ddsd.dwSize = sizeof(ddsd);

while (lpDDSOne->Lock(NULL, &ddsd, 0, NULL) == DDERR_WASSTILLDRAWING)

;

// Search through the torus frames and mark used colors.

for(y=480; y<480+384; y++)

{

for(x=0; x<640; x++)

{

torusColors[((BYTE *)ddsd.lpSurface)[y*ddsd.lPitch+x]] = 1;

}

}

lpDDSOne->Unlock(NULL);

The torusColors array is used as an indicator of the color index of the palette used in the lower half of the All.bmp file. Before it is used, all of the values in the torusColors array are reset to 0. The off-screen buffer is then locked in preparation for determining if a color index value is used.

The torusColors array is set to start at row 480 and column 0 of the bitmap. The color index value in the array is determined by the byte of data at the location in memory where the bitmap surface is located. This location is determined by the lpSurface member of the DDSURFACEDESC structure, which is pointing to the memory location corresponding to row 480 and column 0 of the bitmap (y ´ lPitch + x). The location of the specific color index value is then set to 1. The y value (row) is multiplied by the lPitch value (found in the DDSURFACEDESC structure) to get the actual location of the pixel in linear memory.

The color index values that are set in torusColors will be used later to determine which colors in the palette are rotated. Because there are no common colors between the background and the red donuts, only those colors associated with the red donuts are rotated. If you want to check whether this is true or not, just remove the "*ddsd.lPitch" from the array and see what happens when you recompile and run the program. (Without multiplying y´lPitch, the red donuts are never reached and only the colors found in the background are indexed and later rotated.) For more information about width and pitch, see Width and Pitch.