本系列文章由 net_assassin 整理编写,转载请注明出处。
http://blog.csdn.net/net_assassin/article/category/1100363
作者:net_assassin 邮箱: net_assassin@hotmail.com 期待着与志同道合的朋友们相互交流
ID3DXSprite 对象对于计划使用Direct3D编写2D游戏的程序员来说,实在是个极大的惊喜。
它的好处是,在使用和以前的实现(DirectDraw)一样快得2D函数的同时有完整的3D渲染器随时为你服务。将精灵处理成纹理并且以矩形(两个三角形组成)来渲染,我们就可以对精灵进行变换
初始化精灵渲染器
ID3DXSprite 对象是个包含从纹理绘制精灵的函数的精灵处理器。
LPD3DXSPRITE spriteobj = NULL;
//将精灵处理器附着在Direct3D设备上
HRESULT WINAPI D3DXCreateSprite(
LPDIRECT3DDEVICE9 pDevice,
LPD3DXSPRITE *ppSprite
);
- 启动精灵渲染器 在主D3D设备中调用了BeginScene之后,就可以开始绘制精灵。 spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
- 绘制精灵
HRESULT Draw(
LPDIRECT3DTEXTURE9 pTexture, //指定精灵所用的源图像的纹理
CONST RECT *pSrcRect, //可以从源图像中抓出“图片单元”
CONST D3DXVECTOR3 *pCenter,//指定旋转发生的中心点
CONST D3DXVECTOR3 *pPosition,//指定精灵的位置
D3DCOLOR Color //指定在绘制精灵图像时对其进行的色彩变更(不影响透明度)。颜色键
)
- 停止精灵渲染器
spriteobj->End();
透明的精灵程序
在之前我们的模板中添加改动:
- 在MyDirectX.h文件中添加两个函数的声明,并且在MyDirectX.cpp文件中添加相应的函数实现。
D3DXVECTOR2 GetBitmapSize(string filename);
LPDIRECT3DTEXTURE9 LoadTexture(string filename, D3DCOLOR transcolor = D3DCOLOR_XRGB(0,0,0));
D3DXVECTOR2 GetBitmapSize(string filename)
{
D3DXIMAGE_INFO info;
D3DXVECTOR2 size = D3DXVECTOR2(0.0f,0.0f);
HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
if (result == D3D_OK)
size = D3DXVECTOR2( (float)info.Width, (float)info.Height);
else
size = D3DXVECTOR2( (float)info.Width, (float)info.Height);
return size;
}
LPDIRECT3DTEXTURE9 LoadTexture(std::string filename, D3DCOLOR transcolor)
{
LPDIRECT3DTEXTURE9 texture = NULL;
//get width and height from bitmap file
D3DXIMAGE_INFO info;
HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
if (result != D3D_OK) return NULL;
//create the new texture by loading a bitmap image file
D3DXCreateTextureFromFileEx(
d3ddev, //Direct3D device object
filename.c_str(), //bitmap filename
info.Width, //bitmap image width
info.Height, //bitmap image height
1, //mip-map levels (1 for no chain)
D3DPOOL_DEFAULT, //the type of surface (standard)
D3DFMT_UNKNOWN, //surface format (default)
D3DPOOL_DEFAULT, //memory class for the texture
D3DX_DEFAULT, //image filter
D3DX_DEFAULT, //mip filter
transcolor, //color key for transparency
&info, //bitmap file info (from loaded file)
NULL, //color palette
&texture ); //destination texture
//make sure the bitmap textre was loaded correctly
if (result != D3D_OK) return NULL;
return texture;
}
- 修改Direct3D_init 和 Direct3D_Shutdown 以自动创建和销毁精灵对象。
- 修改MyGame.cpp 在Game_Init 函数中添加:
//load non-transparent image
image_notrans = LoadTexture("shuttle_notrans.bmp");
if (!image_notrans) return false;
//load color-keyed transparent image
image_colorkey = LoadTexture("shuttle_colorkey.bmp", D3DCOLOR_XRGB(255,0,255));
if (!image_colorkey) return false;
//load alpha transparent image
image_alpha = LoadTexture("shuttle_alpha.tga");
if (!image_alpha) return false;
//You can use this function to get the size of the image
D3DXVECTOR2 size = GetBitmapSize("shuttle_alpha.tga");
在Game_Run 函数中添加://start drawing
spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
//draw the sprite
D3DXVECTOR3 pos1( 10, 10, 0);
spriteobj->Draw( image_notrans, NULL, NULL, &pos1, D3DCOLOR_XRGB(255,255,255));
D3DXVECTOR3 pos2( 350, 10, 0);
spriteobj->Draw( image_colorkey, NULL, NULL, &pos2, D3DCOLOR_XRGB(255,255,255));
D3DXVECTOR3 pos3( 700, 10, 0);
spriteobj->Draw( image_alpha, NULL, NULL, &pos3, D3DCOLOR_XRGB(255,255,255));
//stop drawing
spriteobj->End();
Game_End中释放资源://free memory and shut down
image_notrans->Release();
image_colorkey->Release();
image_alpha->Release();
说明 :从上图我们可以看出,第一张图部分是镂空的,不是原来的黑色,原因是ID3DXSprite::Draw 默认使用黑色作为颜色键。
如果在装载纹理时指定了透明颜色键,则ID3DXSprite::Draw 将使用这一颜色键,即使在图像中还有alpha通道.
分享到:
相关推荐
这资源是与博文中讲解内容配套的源代码。3D模型渲染部分
directx11 游戏编程入门 PDF
DirectX 10 3D游戏编程入门.pdf
DirectX+10+3D游戏编程入门
很好的directx游戏编程入门教程,特别适用于初学者。下下来看看吧!
DirectX游戏编程,PDF文档,方便大家学习
游戏编程入门 directx C++ 课件 代码 第二2课游戏编程入门 directx C++ 课件 代码 第二2课游戏编程入门 directx C++ 课件 代码 第二2课
最新英文版DirectX11,与DirectX9差别较大,C++语言描述, 适合新手入门及已入门查阅。
DirectX.11游戏编程入门
DirectX 10 3D游戏编程入门.pdf
DirectX11游戏编程入门(中文版)(word+pdf)
DirectX3d 游戏编程入门 3D红龙 DirectX3d 游戏编程入门 3D红龙
《游戏编程入门(原书第3版)》涵盖了使用DirectX编写代码创建2D和3D游戏所需的所有技能,而读者只需基本掌握C++语言即可学会游戏的每个元素都按部就班地在书中讲授——从学习如何创建简单的Windows程序,到使用关键的...
这是Jim Adams的《Programming Role Playing Games with DirectX》第二版的中文版的第二部分。一共两部分。
游戏编程入门 directx C++ 课件 代码 第二2课游戏编程入门 directx C++ 课件 代码 第二2课
DirectX游戏开发入门.pdf
本书首先介绍Windows和DirectX编程,然后快速介绍游戏编程的工具箱,包括使用C++和DirectX开发游戏所需的所有基础知识。读者将学习到把思想转化为现实所需的技术,比如2D、3D图形的绘制、背景卷动、处理游戏输入、...