DirectxDraw加载位图

1.通过HDC加载

//载入位图
HBITMAP hBitmap;
hBitmap = (HBITMAP)LoadImage(NULL, "test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

HDC hdcImage;
HDC hdc;
BITMAP bm;

lpBackBuffer->Restore();

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

GetObject(hBitmap, sizeof(bm), &bm);

lpBackBuffer->GetDC(&hdc);
BitBlt(hdc, 0, 0, 800, 600, hdcImage, 0, 0, SRCCOPY);
lpBackBuffer->ReleaseDC(hdc);
DeleteDC(hdcImage);

 

2.自己实现文件格式读取图片数据(摘之Windows游戏编程大师技巧 demo7_10.cpp)

#define SCREEN_BPP      8    // bits per pixel

#define BITMAP_ID            0x4D42 // universal id for a bitmap
#define MAX_COLORS_PALETTE   256

// TYPES //////////////////////////////////////////////////////

// basic unsigned types
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;

// container structure for bitmaps .BMP file
typedef struct BITMAP_FILE_TAG
{
    BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header
    BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette
    PALETTEENTRY     palette[256];      // we will store the palette here
    UCHAR            *buffer;           // this is a pointer to the data

} BITMAP_FILE, *BITMAP_FILE_PTR;


//根据对应的格式读取BMP图像数据到内存中
int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
    // this function opens a bitmap file and loads the data into bitmap
    int file_handle,  // the file handle
        index;        // looping index
    UCHAR   *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
    OFSTRUCT file_data;          // the file data information

    // open the file if it exists
    if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
        return(0);

    // now load the bitmap file header
    _lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));

    // test if this is a bitmap file
    if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
    {
        // close the file
        _lclose(file_handle);

        // return error
        return(0);
    } // end if

    // now we know this is a bitmap, so read in all the sections

    // first the bitmap infoheader

    // now load the bitmap file header
    _lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));

    // now load the color palette if there is one
    if (bitmap->bitmapinfoheader.biBitCount == 8)
    {
        _lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));

        // now set all the flags in the palette correctly and fix the reversed 
        // BGR RGBQUAD data format
        for (index=0; index < MAX_COLORS_PALETTE; index++)
        {
            // reverse the red and green fields
            int temp_color                = bitmap->palette[index].peRed;
            bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;
            bitmap->palette[index].peBlue = temp_color;

            // always set the flags word to this
            bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
        } // end for index

    } // end if

    // finally the image data itself
    _lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);

    // now read in the image, if the image is 8 or 16 bit then simply read it
    // but if its 24 bit then read it into a temporary area and then convert
    // it to a 16 bit image

    if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 || 
        bitmap->bitmapinfoheader.biBitCount==24)
    {
        // delete the last image if there was one
        if (bitmap->buffer)
            free(bitmap->buffer);

        // allocate the memory for the image
        if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
        {
            // close the file
            _lclose(file_handle);

            // return error
            return(0);
        } // end if

        // now read it in
        _lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);

    } // end if
    else
    {
        // serious problem
        return(0);

    } // end else

    // close the file
    _lclose(file_handle);

    // flip the bitmap
    Flip_Bitmap(bitmap->buffer, 
        bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8), 
        bitmap->bitmapinfoheader.biHeight);

    // return success
    return(1);

} // end Load_Bitmap_File

///////////////////////////////////////////////////////////

int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
{
    // this function is used to flip bottom-up .BMP images

    UCHAR *buffer; // used to perform the image processing
    int index;     // looping index

    // allocate the temporary buffer
    if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
        return(0);

    // copy image to work area
    memcpy(buffer,image,bytes_per_line*height);

    // flip vertically
    for (index=0; index < height; index++)
        memcpy(&image[((height-1) - index)*bytes_per_line],
        &buffer[index*bytes_per_line], bytes_per_line);

    // release the memory
    free(buffer);

    // return success
    return(1);

} // end Flip_Bitmap

///////////////////////////////////////////////////////////////

int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds,int color)
{
    DDBLTFX ddbltfx; // this contains the DDBLTFX structure

    // clear out the structure and set the size field 
    DDRAW_INIT_STRUCT(ddbltfx);

    // set the dwfillcolor field to the desired color
    ddbltfx.dwFillColor = color; 

    // ready to blt to surface
    lpdds->Blt(NULL,       // ptr to dest rectangle
        NULL,       // ptr to source surface, NA            
        NULL,       // ptr to source rectangle, NA
        DDBLT_COLORFILL | DDBLT_WAIT,   // fill and wait                   
        &ddbltfx);  // ptr to DDBLTFX structure

    // return success
    return(1);
} // end DDraw_Fill_Surface

 

posted @ 2012-08-07 14:59  冷夜 - 网游编程技术  阅读(1698)  评论(0编辑  收藏  举报