|
In DirectDraw applications, it will often be necessary to load bitmaps that do not match the color depth of the screen. With a growing number of available pixel formats, and no way to know what new formats the future may hold, this leaves us with a bit of a problem - How to convert the color depth of an image, in such a way that we can insure compatibility with a wide range of hardware? The easiest way to achieve this is by using the graphics functions provided by Window - GDI. While these routines are slower than DirectDraw operations, they provide an easy means to convert your images to match the pixel format of the screen. And if we do this conversion when loading the image there is no impact to the performance, as it never needs to be repeated unless we change the pixel format of the display. The function provided below loads an image from a bitmap file, creates an offscreen surface that matches the pixel format of the primary surface, and copies the image to the surface. Optionally, the function can set the key color for the offscreen surface as well. Some additional notes on this function:
LPDIRECTDRAWSURFACE4
bitmap_surface(LPCTSTR file_name, DWORD *keycolor = NULL) // load the interface bitmap bit =
(HBITMAP)LoadImage(NULL,file_name,IMAGE_BITMAP, // return if we failed to load the bitmap if (!bit) return NULL; // get bitmap dimensions BITMAP bitmap; // create surface HRESULT ddrval; // attempt to create surface ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL); // created ok? if (ddrval!=DD_OK) { // no, clear pointer surf=NULL; } else { // yes, get a DC for the surface surf->GetDC(&hdc); // generate a compatible DC HDC bit_dc=CreateCompatibleDC(hdc); // blit the interface to the surface
SelectObject(bit_dc,bit); // release the DCs
surf->ReleaseDC(hdc); // set up the color key if (keycolor) {
DDCOLORKEY key; } } // unload bitmap DeleteObject(bit); // return pointer to caller return surf; |
Visitors Since 1/1/2000:
|