Loading Bitmap Files into DirectDraw

博客给出了几个函数代码,包括创建表面、绘制位图、创建位图表面等函数,还给出了一个示例,展示如何将位图文件加载到DirectDraw中,最后释放表面。
this post only for begginner.....

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. 
ExpandedBlockStart.gifContractedBlock.gifvoid CreateSurface(LPDIRECTDRAWSURFACE7 *lpSource, int xs, int ys) dot.gif{ 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. 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? 

ExpandedBlockStart.gifContractedBlock.gifvoid DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hBitmap, int x, int y, int width, int height) dot.gif{ 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, 00, SRCCOPY); lpSurface->ReleaseDC(hdc); DeleteDC(hdcImage); } 
and here is the code that loads, blits, then unloads the bitmap:
ExpandedBlockStart.gifContractedBlock.gifvoid CreateBitmapSurface(LPDIRECTDRAWSURFACE7 lpSurface, char *fname, int xs, int ys) dot.gif{ HBITMAP hBitmap; CreateSurface(&lpSurface, xs, ys); hBitmap = LoadImage(NULL, fname, IMAGE_BITMAP, 00, LR_LOADFROMFILE); DrawHBitmap(lpSurface, hBitmap, 00, 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: 
ExpandedBlockStart.gifContractedBlock.gifvoid Example(voiddot.gif/**//* * Declare the surface object */ LPDIRECTDRAWSURFACE7 lpddsTest; /**//* * Load the bitmap file into it */ CreateBitmapSurface(lpddsTest, 搕est.bmp? 128128); /**//* * The lpddsTest surface now contains the 搕est.bmp?file */ /**//* * Release the surface */ lpddsTest->Release(); }

转载于:https://www.cnblogs.com/LeighSword/archive/2004/07/23/26852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值