Jump to content
  • Advertisement
  • Remove ads and support GameDev.net for only $3. Learn more: The New GDNet+: No Ads!

  • 06/22/00 10:57 AM
    Sign in to follow this  

    Loading Bitmap Files into DirectDraw

    Graphics and GPU Programming

    Myopic Rhino
    [size="5"]Introduction

    Hi there.

    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.


    [size="5"]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

    void CreateSurface(LPDIRECTDRAWSURFACE7 *lpSource, int xs, int ys)
    {
    DDSURFACEDESC2 ddsd;

    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    ddsd.dwWidth = xs;
    ddsd.dwHeight = ys;
    lpdd->CreateSurface(&ddsd, lpSource, NULL);
    }
    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.


    [size="5"]Bitmap Loading

    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....

    Listing 2

    void DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hBitmap, int x, int y, int width, int height)
    {
    HDC hdcImage;
    HDC hdc;
    BITMAP bm;

    if (lpSurface == NULL || hBitmap == NULL)
    return;
    lpSurface->Restore();

    hdcImage = CreateCompatibleDC(NULL);
    SelectObject(hdcImage, hBitmap);

    GetObject(hBitmap, sizeof(bm), &bm);
    width = width == 0 ? bm.bmWidth : width;
    height = height == 0 ? bm.bmHeight : height;

    lpSurface->GetDC(&hdc);
    BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY);
    lpSurface->ReleaseDC(hdc);
    DeleteDC(hdcImage);
    }

    and here is the code that loads, blits, then unloads the bitmap:

    Listing 3

    void CreateBitmapSurface(LPDIRECTDRAWSURFACE7 lpSurface, char *fname, int xs, int ys)
    {
    HBITMAP hBitmap;

    CreateSurface(&lpSurface, xs, ys);
    hBitmap = LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    DrawHBitmap(lpSurface, hBitmap, 0, 0, xs, ys);
    DeleteObject(hBitmap);
    }

    [size="5"]Quick Example

    And to round it all up, here's some example code that loads the file "test.bmp" (width = 128, height = 128) into the lpddsTest surface, and then releases it again:

    Listing 4

    void Example(void)
    {
    /*
    * Declare the surface object
    */
    LPDIRECTDRAWSURFACE7 lpddsTest;

    /*
    * Load the bitmap file into it
    */
    CreateBitmapSurface(lpddsTest, "test.bmp", 128, 128);

    /*
    * The lpddsTest surface now contains the "test.bmp" file
    */

    /*
    * Release the surface
    */
    lpddsTest->Release();
    }


      Report Article
    Sign in to follow this  


    User Feedback


    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!