載入點陣圖檔案到DirectDraw的方法(轉)

post0發表於2007-08-12
載入點陣圖檔案到DirectDraw的方法(轉)[@more@]

  Introduction

  介紹

  

  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.

  在這篇文章裡,我將簡要敘述一下使用Windows函式來載入任何型別的BMP圖象檔案資料,並將資料存

  放到一個新建立的DirectDraw的表面中。在這以後的篇幅裡,我們將使用DirectX 7.0 SDK。

  

  DirectDraw Surface Creation

  建立DirectDraw表面

  

  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.

  建立一個DirectDraw表面是非常容易的,這個函式將根據大小來建立一個表面。這同樣能用於多種多

  樣的表面建立函式,如果你要製作一個引擎類,你也許會把這個放在引擎的某處。

  

  Listing 1

  程式碼列表 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.

  所有的這些程式碼是為表面建立一個DDSURFACEDESC2的資料結構來記錄大小尺度,並且告訴DirectDraw

  那是一個離屏表面。然後,呼叫DirectDraw(在這部分,DirectDraw是lpdd的指標)真正建立這個表面

  。

  

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

  要載入.BMP圖形檔案,我們將使用標準的Windows圖形庫(GDI)。這個是最好的方法,因為如果我們是

  在一個調色盤的影片模式,那麼Windows將自動將點陣圖對映到最接近的調色盤顏色。以下的程式碼是繪

  制一個點陣圖資料到表面……

  

  Listing 2

  程式碼列表 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

  程式碼列表 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);

  }

  

  

  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:

  圍繞著以下部分,我們將結束此章節,這裡是一些例程程式碼,它載入"test.bmp"檔案(寬=128,高=12

  8)到lpddTest表面中,並且將其釋放:

  

  Listing 4

  程式碼列表 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

  * lpddsTest表面已經包含了"test.bmp"檔案資料

  */

  

  /*

  * Release the surface

  * 釋放表面

  */

  lpddsTest->Release();

  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-952205/,如需轉載,請註明出處,否則將追究法律責任。

相關文章