DirectX游戏编程入门——第二部分(游戏编程工具箱) ——精灵变换

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

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


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


这一章我们学习使用Direct3D矩阵及一个极为方便D3DX助手函数来旋转、缩放及平移精灵。

2D变换

ID3DXSprite可以处理变换矩阵是它的迷人之处。

介绍完成这些功能的变换函数:

D3DXMATRIX  *D3DXMatrixTransformation2D(
        D3DXMATRIX  *pOut,
        CONST  D3DXVECTOR2 *pScalingCenter,
        FLOAT pScalingRotation,
        CONST D3DXVECTOR2 *pScaling,
        CONST D3DXVECTOR2 *pRotationCenter,
        FLOAT  Rotation,
        CONST D3DXVECTOR2 *pTranslation
);
这个函数生成一个实际上通过引用来传递的矩阵(第一个参数),然后填满矩阵值返回给用户。

在得到这个矩阵之后,我们就可以告诉精灵对象(ID3DXSprite)依据它来作为当前的变换:

spriteobj->SetTransform(&mat); //假设得到的矩阵是mat。

Rotate_Scale_Demo程序(绘制简单精灵):

void Sprite_Transform_Draw(LPDIRECT3DTEXTURE9 image, int x, int y, int width, int height, 
    int frame, int columns, float rotation, float scaling, D3DCOLOR color)
{
    //create a scale vector
    D3DXVECTOR2 scale( scaling, scaling );

    //create a translate vector
    D3DXVECTOR2 trans( x, y );

    //set center by dividing width and height by two
    D3DXVECTOR2 center( (float)( width * scaling )/2, (float)( height * scaling )/2);

    //create 2D transformation matrix
    D3DXMATRIX mat;
    D3DXMatrixTransformation2D( &mat, NULL, 0, &scale, ¢er, rotation, &trans );
    
    //tell sprite object to use the transform
    spriteobj->SetTransform( &mat );

    //calculate frame location in source image
    int fx = (frame % columns) * width;
    int fy = (frame / columns) * height;
    RECT srcRect = {fx, fy, fx + width, fy + height};

    //draw the sprite frame
    spriteobj->Draw( image, &srcRect, NULL, NULL, color );
}



Rotate_Animate_Demo(绘制动画精灵,使用Sprite_Animate()):



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值