directdraw:载入位图文件到 DirectDraw



Loading Bitmap Files o 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 _disibledevent=>在这篇文章里我将简要叙述下使用Windows来载入任何类型BMP图象文件数据并将数据存
放到个新建立DirectDraw表面中在这以后篇幅里我们将使用DirectX 7.0 SDK

DirectDraw Surface Creation
创建DirectDraw表面

Creating a 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 you were writing an engine you\'d probably put it in there somewhere.
创建个DirectDraw表面是非常容易这个将根据大小来创建个表面这同样能用于多种多
表面创建如果你要制作个引擎类你也许会把这个放在引擎某处

Listing 1
代码列表 1

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

ZeroMemory(&ddsd, (ddsd));
ddsd.dwSize = (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 , DirectDraw is the lpdd poer) 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 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, x, y, width, height)
{
HDC hdcImage;
HDC hdc;
BITMAP bm;

(lpSurface NULL || hBitmap NULL)
;
lpSurface->Restore;

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

GetObject(hBitmap, (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, xs, 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) o 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 o it
* 载入位图文件
*/
CreateBitmapSurface(lpddsTest, “test.bmp”, 128, 128);

/*
* The lpddsTest surface now contains the “test.bmp” file
* lpddsTest表面已经包含了\"test.bmp\"文件数据
*/

/*
* Release the surface
* 释放表面
*/
lpddsTest->Release;
}


Tags:  directdrawerror directdraw不可用 directdraw加速 directdraw

延伸阅读

最新评论

发表评论