- A+
所属分类:Windows游戏开发 原创文章
Directx初始化后,自动创建了两个Buffer
也就是两个Surface,双缓存技术
记得在windowsGUI绘制绘图
使用了双缓存技术
使用Directx也一样,我们先来创建两个surface
先初始化Direct3d
加入两个全局变量
1 2 |
LPDIRECT3DSURFACE9 backbuffer = NULL; LPDIRECT3DSURFACE9 surface = NULL; |
然后在相应的位置插入代码
在Game_Init函数中创建了Device后
1 2 3 |
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); HRESULT result = d3ddev->CreateOffscreenPlainSurface(500, 300,D3DFMT_X1R5G5B5, D3DPOOL_DEFAULT, &surface, NULL); if (result != D3D_OK) MessageBox(hwnd,"surface创建失败","错误",0); |
这里先使用GetBackBuffer
获取显示卡的缓存表面
然后用CreateOffscreenPlainSurface创建一个离屏表面
123456 HRESULT GetBackBuffer([in] UINT iSwapChain,[in] UINT BackBuffer,[in] D3DBACKBUFFER_TYPE Type,[out, retval] IDirect3DSurface9 **ppBackBuffer);参数
- iSwapChain [in]
- Type: UINT 指定交换链
- BackBuffer [in]
- Type: UINT 返回BackBuffer的索引。记得d3dpp.BackBufferCount = 1;创建Device时的参数
- Type [in]
- Type: D3DBACKBUFFER_TYPE Direct3D9不支持立体观察,所以值只能为D3DBACKBUFFER_TYPE_MONO
- ppBackBuffer [out, retval]
- Type: IDirect3DSurface9** 保存表面的指针的地址
12345678 HRESULT CreateOffscreenPlainSurface([in] UINT Width,[in] UINT Height,[in] D3DFORMAT Format,[in] D3DPOOL Pool,[out, retval] IDirect3DSurface9 **ppSurface,[in] HANDLE *pSharedHandle);参数
surface的简单使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
if (d3ddev->BeginScene()) { 随机绘制矩形 int r = rand() % 255; int g = rand() % 255; int b = rand() % 250; d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(r, g, b)); RECT rect; rect.left = rand() % SCREENW / 2; rect.right = rect.left + rand() % SCREENW / 2; rect.top = rand() % SCREENH / 2; rect.bottom = rect.top + rand() % SCREENH / 2; d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } |
随机颜色的RGB值
使用函数ColorFill填充离屏表面
随机窗口内的矩形块
将离屏表面传输到后台缓冲区中
然后显示出来
12345 HRESULT ColorFill([in] IDirect3DSurface9 *pSurface,[in] const RECT *pRect,[in] D3DCOLOR color);
1234567 HRESULT StretchRect([in] IDirect3DSurface9 *pSourceSurface,[in] const RECT *pSourceRect,[in] IDirect3DSurface9 *pDestSurface,[in] const RECT *pDestRect,[in] D3DTEXTUREFILTERTYPE Filter);
- Filter [in]
- Type: D3DTEXTUREFILTERTYPEFilter type. Allowable values are D3DTEXF_NONE, D3DTEXF_POINT, or D3DTEXF_LINEAR. For more information, seeD3DTEXTUREFILTERTYPE.
效果如下
下面开始加载位图
引入Directx扩展库头文件
链接扩展库
1 2 |
#include <d3dx9.h> #pragma comment(lib,"d3dx9.lib") |
在项目目录下放一张3.PNG
修改代码,在创建离屏表面后加
1 2 3 4 5 |
result = D3DXLoadSurfaceFromFile(surface, NULL, NULL, "3.PNG", NULL, D3DX_DEFAULT, 0, NULL); if (result != D3D_OK) { MessageBox(hwnd, "加载图片失败", "错误", 0); return false; } |
然后修改Game_Run,注释掉Scene中间的代码
直接加入
1 |
d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE); |
运行就可以看到图片渲染出来了
12345678910 HRESULT D3DXLoadSurfaceFromFile(_In_ LPDIRECT3DSURFACE9 pDestSurface,_In_ const PALETTEENTRY *pDestPalette,_In_ const RECT *pDestRect,_In_ LPCTSTR pSrcFile,_In_ const RECT *pSrcRect,_In_ DWORD Filter,_In_ D3DCOLOR ColorKey,_Inout_ D3DXIMAGE_INFO *pSrcInfo);参数
- pDestSurface [in]
- Type: LPDIRECT3DSURFACE9 要加载到表面的指针
- pDestPalette [in]
- Type: const PALETTEENTRY* Pointer to a PALETTEENTRY structure, the destination palette of 256 colors or NULL.(什么目的调色板,不知道什么鬼,为NULL)
- pDestRect [in]
- Type: const RECT* 指定表面的矩形位置. 设置为 NULL 则为整个表面
- pSrcFile [in]
- Type: LPCTSTR 文件名
- pSrcRect [in]
- Type: const RECT* 指定图片的矩形位置,设为NULL则为整个图片
- Filter [in]
- Type: DWORD 结合一个或多个 D3DX_FILTER 控制图片怎么样过滤。 指定D3DX_DEFAULT 等同于指定 D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER.
- ColorKey [in]
- Type: D3DCOLOR D3DCOLOR value to replace with transparent black, or 0 to disable the colorkey. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys Thus, for opaque black, the value would be equal to 0xFF000000.
- pSrcInfo [in, out]
- Type: D3DXIMAGE_INFO* Pointer to a D3DXIMAGE_INFO structure to be filled with a description of the data in the source image file, or NULL.
(哦米拖佛,设为NULL吧)