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(); }