DirectX游戏编程入门——第二部分(游戏编程工具箱) ——精灵编程之简介

本系列文章由 net_assassin 整理编写,转载请注明出处。

http://blog.csdn.net/net_assassin/article/category/1100363


作者:net_assassin 邮箱: net_assassin@hotmail.com 期待着与志同道合的朋友们相互交流


精灵是一个小的绘制在屏幕上的位图图像,代表游戏中的一个角色或对象。

精灵通常将一系列图片单元(tile)储存于一个位图文件中,每个图片单元表示该精灵动画序列中的一个帧。动画的效果更像方向的改变,而不是移动。


使用Direct3D绘制精灵有两种方法。这两种方法都要求我们保存精灵的位置、尺寸和速度,还要保存我们自己所需的其他属性,所以其逻辑是独立的。

  • 将精灵图像转载到D3D表面中,然后使用StretchRect 绘制精灵。(Bomb Catcher游戏中用过)
  • 用D3DXSprite 对象来处理Direct3D中的精灵。D3DXSprite使用纹理而不是表面来保存精灵图像。(可以实现一些特殊效果,如透明、旋转和缩放)
这一章我们主要介绍第二种方法。

Bomb Catcher 游戏中的问题:

使用Direct3D表面绘制精灵。而表面最大的问题就是除了绘图速度很慢以外,还缺乏对任何透明类型的支持(这是我们的和篮子精灵有黑色轮廓的原因)。

Direct3D中两种渲染2D对象的方法:

  1. 创建一个由两个三角形组成的四边形(quad),这两个三角形带有纹理(纹理映射,一种对三角形赋予图像数据的技术),表示想要绘制的2D图形。这种技术不仅有效,而且甚至支持透明、响应光照并且可再Z方向上移动。
  2. 使用精灵,这是我们现在讨论的重点。

装载精灵图像

  1. 创建游戏精灵首先要做的是创建一个用于装载精灵位图图像的纹理对象 LPDIRECT3DTEXTURE9 texture = NULL;
  2. 然后需要做的是使用D3DXGetImageInfoFromFile函数从位图文件中取出分辨率数据 D3DXIMAGE_INFO info; result = D3DXGetImageInfoFromFile(“image.bmp”,&info);
  3. 接下来,使用D3DXCreateTextureFromFileEx函数一步直接从文图文件中将精灵的图像装载到纹理中。 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
我们完全可以把所有这些信息放在一个函数中,LoadTexture(string filename,D3DCOLOR transcolor)
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值