![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The BMP file is then opened, and the file and image data structures are loaded: ifstream bmp( palettefile, ios::binary | ios::nocreate ); bmp.read( (char*)&bmpfilehdr, sizeof(bmpfilehdr) ); bmp.read( (char*)&bmpinfohdr, sizeof(bmpinfohdr) ); The freshly loaded data is used to check the BMP files signature and the files bit depth: char* ptr=(char*)&bmpfilehdr.bfType; if (*ptr!='B' || *++ptr!='M') { TRACE("invalid bitmap\n"); return FALSE; } if (bmpinfohdr.biBitCount!=8) { TRACE("not 8 bit file!\n"); return FALSE; } If the BM signature is not present, then the file is either corrupted or not a BMP file at all. In that case, a diagnostic message is displayed and the function returns FALSE. Files that have bit depths less than eight will do us little good. Four-bit files, for instance, use only 16 colors, which would be a waste in a 256-color mode. Files with bit depths of 16 or more dont have palettes at all. If the files bit depth is not equal to eight, the function returns. Next, the number of colors in the palette is calculated: if (bmpinfohdr.biClrUsed==0) ncolors=256; else ncolors=bmpinfohdr.biClrUsed; It is not uncommon for the biClrUsed field to be zero, indicating that the file contains the maximum number of colors for the given bit depth. We assign our local ncolors integer to 256 if the biClrUsed field is zero. Otherwise, the biClrUsed field is used to assign the ncolors integer. The next step that the InstallPalette() function performs is loading the palette from disk and initializing the array of PALETTEENTRY structures: bmp.read( (char*)quad, sizeof(RGBQUAD)*ncolors ); for( int i=0; i<ncolors; i++) { pe[i].peRed = quad[i].rgbRed; pe[i].peGreen = quad[i].rgbGreen; pe[i].peBlue = quad[i].rgbBlue; pe[i].peFlags = D3DPAL_READONLY; } The quad array is used to load the palette. A loop then copies values from the quad array to the pe array. The D3DPAL_READONLY constant is used to indicate that the colors in the array should not be modified. Now, we can create the DirectDraw palette: HRESULT r=ddraw->CreatePalette( DDPCAPS_8BIT, pe, &palette, 0 ); if (r!=DD_OK) { TRACE("failed to load palette data from file\n"); return FALSE; } The palette is created with the DirectDraw CreatePalette() function. The DDPCAPS_8BIT constant indicates to DirectDraw that the palette data we are supplying is 8-bit data. The pe array is supplied as the second argument. The third CreatePalette() argument is the address of a pointer to the new palette. Finally, the new palette is attached to the primary and back surfaces: primsurf->SetPalette( palette ); backsurf->SetPalette( palette ); This step completes the task of the InstallPalette() function. The BMP file is closed automatically when the function returns because the ifstream object used to open the file will go out of scope. The CreateDevice() FunctionThe CreateDevice() function is called by the OnCreate() function. Its task is to create the Direct3D device: BOOL RMWin::CreateDevice() { d3drm->CreateDeviceFromSurface( 0, ddraw, backsurf, &device ); device->SetQuality( D3DRMRENDER_GOURAUD ); return TRUE; } The CreateDeviceFromSurface() function is used to create the device. The function takes four arguments. The first is a GUID (Globally Unique Identifier) that identifies the device. Using zero causes Direct3D to choose a device automatically. Youll need to use a specific GUID only if you want to override Direct3Ds default selection. The GetGUID() function can be used to retrieve specific GUIDs. See Chapter 4 for a discussion of the GetGUID() function. The second CreateDeviceFromSurface() argument is a pointer to the DirectDraw interface. The third argument is the surface that is to be used in creating the device. We are using the backsurf surface that we created with the InitMainSurfaces() function. This means that Direct3D will use the backsurf surface as a rendering target. The final CreateDeviceFromSurface() argument is the address of the device pointer. After the device is created, the Direct3DRMDevice SetQuality() function is used to set the devices rendering quality to Gouraud (flat rendering quality is used by default).
|
![]() |
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 whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |