If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
In this article I will briefly describe the process of using Windows' functions to load a bitmap (.bmp) file of any type, and place it on a newly-created DirectDraw surface. In this particular article, we will be using the DirectX 7.0 SDK.
DirectDraw Surface Creation
Creating a new DirectDraw surface is very easy, and this function will create a surface of any size. This can also be used as a general-purpose surface creation function, and if you were writing an engine class you'd probably put it in there somewhere. Listing 1
All this code does is create a DDSURFACEDESC2 structure that describes the dimensions of the surface, and tells DirectDraw that it's an off-screen surface. Then, the call to DirectDraw (in this case, DirectDraw is the lpdd pointer) just creates the surface.
To load .bmp files we will use the standard Windows graphics library (GDI). This is best because if we're in a mode with a palette then Windows will automatically re-map the bitmap colours to the nearest in the palette. Here is the code that blits a loaded bitmap to a surface….
void DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hBitmap, int x, int y, int width, int height)
if (lpSurface == NULL || hBitmap == NULL)
hdcImage = CreateCompatibleDC(NULL);
GetObject(hBitmap, sizeof(bm), &bm);
width = width == 0 ? bm.bmWidth : width;
height = height == 0 ? bm.bmHeight : height;
BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY);
and here is the code that loads, blits, then unloads the bitmap: