- A+
所属分类:Windows游戏开发 原创文章
精灵就是一个小图片或者是一组小图片的合集
可以用于显示静态图片,制作2D游戏的动画
精灵Sprite是一个非常重要的概念
下面看看怎么在Direct3D中使用精灵
先声明两个全局
1 2 |
LPDIRECT3DTEXTURE9 img = NULL; LPD3DXSPRITE spriteobj = NULL; |
一个纹理,一个精灵
然后在Game_Init()中
创建精灵
加载纹理
1 2 3 4 |
D3DXCreateSprite(d3ddev, &spriteobj); D3DXGetImageInfoFromFile("3.PNG", &info); /*D3DXCreateTextureFromFileEx(d3ddev, "3.PNG", info.Width,info.Height, 1, D3DPOOL_DEFAULT,D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(0, 0, 0), &info, NULL, &img);*/ D3DXCreateTextureFromFile(d3ddev, "3.PNG", &img); |
12345 HRESULT D3DXCreateSprite(_In_ LPDIRECT3DDEVICE9 pDevice,_Out_ LPD3DXSPRITE *ppSprite);//这两个参数就不解释了
123456 HRESULT D3DXGetImageInfoFromFile(_In_ LPCSTR pSrcFile,_In_ D3DXIMAGE_INFO *pSrcInfo);* pSrcFile 图片的路径* pSrcInfo 一个为D3DXIMAGE_INFO的结构体
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 HRESULT D3DXCreateTextureFromFileEx(_In_ LPDIRECT3DDEVICE9 pDevice,_In_ LPCTSTR pSrcFile,_In_ UINT Width,_In_ UINT Height,_In_ UINT MipLevels,_In_ DWORD Usage,_In_ D3DFORMAT Format,_In_ D3DPOOL Pool,_In_ DWORD Filter,_In_ DWORD MipFilter,_In_ D3DCOLOR ColorKey,_Inout_ D3DXIMAGE_INFO *pSrcInfo,_Out_ PALETTEENTRY *pPalette,_Out_ LPDIRECT3DTEXTURE9 *ppTexture);pDevice [in]Type: LPDIRECTD3D设备指针pSrcFile [in]Type: LPCTSTR图片的路径Width [in]Type: UINT宽度的像素。如果设为0或D3DX_DEFAULT, 则就是文件的规模并且变为2的倍数. 如果设备支持不为2的倍数的像素纹理并且制定D3DX_DEFAULT_NONPOW2,则不会改变。Height[in]Type: UINT高度的像素,和宽度的一样。MipLevels [in]Type: UINTNumber of mip levels requested. If this value is zero or D3DX_DEFAULT, a complete mipmap chain is created. 如果是D3DX_FROM_FILE,则大小会很精确,但是在违反设备能力的时候会报错。Usage [in]Type: DWORDSetting this flag to D3DUSAGE_RENDERTARGET indicates that the surface is to be used as a render target. The resource can then be passed to the pNewRenderTarget parameter of the href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb174455(v=vs.85).aspx"><strong>SetRenderTarget</strong>method. If either <strong>D3DUSAGE_RENDERTARGET</strong> or D3DUSAGE_DYNAMIC is specified, Pool must be set to D3DPOOL_DEFAULT, and the application should check that the device supports this operation by calling CheckDeviceFormat. D3DUSAGE_DYNAMIC indicates that the surface should be handled dynamically. See Using Dynamic Textures.Format [in]Type: D3DFORMAT枚举类型,描述纹理的像素格式。Pool [in]Type: D3DPOOLD3DPOOL枚举类型,描述图片存放的内存。Filter [in]Type: DWORD组合一个或多个D3DX_FILTER指定图片应该怎么样过滤,指定为 D3DX_DEFAULT 等于指定为 D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER.MipFilter [in]Type: DWORD组合一个或多个D3DX_FILTER指定图片应该怎么样过滤,指定为D3DX_DEFAULT等于指定为D3DX_FILTER_BOX.ColorKey [in]Type: D3DCOLOR指定颜色替换成透明pSrcInfo [in, out]Type: D3DXIMAGE_INFO*图片信息的结构体pPalette [out]Type: PALETTEENTRY*指向PALETTEENTRY结构体,代表256种颜色调色板去填充,或者为NULLppTexture [out]Type: LPDIRECT3DTEXTURE9*纹理接口的指针
最后在Game_Run()中显示出来
代码如下:
1 2 3 |
//spriteobj->Draw(img, NULL, NULL, &D3DXVECTOR3(200,300,0), D3DCOLOR_XRGB(255, 255, 255)); spriteobj->Draw(img, NULL, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255)); spriteobj->End(); |
- HRESULT Begin( DWORD Flags)
Flags是必须的参数,一般为D3DXSPRITE_ALPHABLEND
表示绘制精灵支持透明
- HRESULT Draw(
LPDIRECT3DTEXTURE9 pTexture,
CONST RECT *pSrcRect,
CONST D3DXVECTOR3 *pCenter,
CONST D3DXVECTOR3 *pPosition,
D3DCOKOR Color );
pTexture 指向图片纹理的指针
pSrcRect 制定原图像的指定矩形局域,NULL为整张纹理
pCenter 旋转的中心点
pPosition 在屏幕的位置
Color 色彩变更