線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2209
推到 Plurk!
推到 Facebook!

[推薦]載入BitMap文件到 DirectDraw

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-08-14 18:11:52 IP:61.218.xxx.xxx 未訂閱
載入Bitmap文件到 DirectDraw    Loading Bitmap Files into DirectDraw by Kieren Johnstone    載入位圖文件到 DirectDraw    譯:sea_bug    以下文章中英對照:    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();
}
 
時間就是金錢---[ 發問前請先找找舊文章] 發表人 - axsoft 於 2002/08/15 10:57:27
系統時間:2024-03-29 12:53:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!