Platform SDK: DirectX

Step 2: Rotate the Palettes

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See DirectDraw Visual Basic Tutorials.

[C++]

The updateFrame sample function in DDEx5 works in much the same way as it did in Tutorial 4 (DDEx4). It first blits the background into the back buffer, and then it blits the three donuts in the foreground. However, before it flips the surfaces, updateFrame changes the palette of the primary surface from the palette index that was created in the doInit function, as shown in the following code.

// Change the palette. 
if(lpDDPal->GetEntries(0, 0, 256, pe) != DD_OK) 
{ 
    return; 
} 
 
for(i=1; i<256; i++) 
{ 
    if(!torusColors[i]) 
    { 
       continue; 
    } 
    pe[i].peRed = (pe[i].peRed+2) % 256; 
    pe[i].peGreen = (pe[i].peGreen+1) % 256; 
    pe[i].peBlue = (pe[i].peBlue+3) % 256; 
} 
 
if(lpDDPal->SetEntries(0, 0, 256, pe) != DD_OK) 
{ 
    return; 
} 
 

The IDirectDrawPalette::GetEntries method in the first line queries palette values from a DirectDrawPalette object. Because the palette entry values pointed to by pe should be valid, the method will return DD_OK and continue. The loop that follows checks torusColors to determine if the color index was set to 1 during its initialization. If so, the red, green, and blue values in the palette entry pointed to by pe are rotated.

After all of the marked palette entries are rotated, the IDirectDrawPalette::SetEntries method is called to change the entries in the DirectDrawPalette object. This change takes place immediately if you are working with a palette set to the primary surface.

With this done, the surfaces are subsequently flipped.