深度探索DxFramework 4-1

Table of Contents

请尊重原作者的工作,转载时请务必注明转载自:www.xionggf.com

第4章 初始锋芒 剖析DemoA

4.1 Demo实现的功能点和整体架构

4.1.1 实现的功能点

4.1.2 Demo的整体架构

4.2 Demo实例类的定义和初始化

class C_DemoA : public C_GameState
{
public:
    C_DemoA();
    virtual ~C_DemoA();
    virtual void    Input();
    virtual void    Update();       
    virtual void    Render2D(); 
private:
    C_Sprite*    p_Background;    //背景图片
    C_Sprite*    p_Box;            //用来演示的小方盒

    // 各种dxfrramework支持的图片格式
    C_Sprite*    p_Bmp8;            //8位bmp格式
    C_Sprite*    p_Bmp24;        //24位bmp格式
    C_Sprite*    p_Png8;            //8位png格式
    C_Sprite*    p_Png24;        //24位png格式
    C_Sprite*    p_Png32;        //32位png格式
    DVECTOR2    translation;            // box的屏幕坐标
    int            alpha;                    // box的透明度
    static const int VELOCITY;            // box的移动速度,为常量
    static const int ANIMATION_SPEED;    // box的动画变化速度,为常量
};

C_DemoA::C_DemoA() 
{
    alpha = 255;
    p_Background =  new C_Sprite(_T("demoa_background.png"));     
    p_Box = new C_Sprite(_T("box-3-2-2.png"));
    translation = DVECTOR2(400,300) - p_Box->GetCenter();

    //载入DxFramework所能支持的不同格式的图片,创建精灵
    p_Bmp8    =  new C_Sprite(_T("bitmap8.bmp"));
    p_Bmp24 =  new C_Sprite(_T("bitmap24.bmp"));
    p_Png8    =  new C_Sprite(_T("png8.png"));
    p_Png24 =  new C_Sprite(_T("png24.png"));
    p_Png32 =  new C_Sprite(_T("png32.png"));
    gp_Timer->Reset();    //把计时器清零,准备用来对小方盒产生动画效果

    //因为使用了背景图片,所以不必每帧都清空颜色缓冲区和z缓冲区
    gp_View->SetClear(false);
}

4.3 对外设输入的响应

void C_DemoA::Input() 
{
    // 按ESC键退出demo,返回主菜单
    if (gp_Controller->GetKeyState(DIK_ESCAPE)== BUTTON_PRESSED){
        gp_Model->ChangeState(MENU);
    } 

    // 按F2,F3键切换到demo B,demo C
    if (gp_Controller->GetKeyState(DIK_F2)== BUTTON_PRESSED){
        gp_Model->ChangeState(DEMOB);
    }   
    if (gp_Controller->GetKeyState(DIK_F3)== BUTTON_PRESSED){
        gp_Model->ChangeState(DEMOC);
    }   

    //按上下左右箭头键,向上,向下,向左,向右移动小方盒
    double increment = VELOCITY * gp_Timer->GetElapsed();
    if (gp_Controller->GetKeyState(DIK_UP) ==  BUTTON_DOWN) 
        translation.y -= increment;
    if (gp_Controller->GetKeyState(DIK_DOWN) ==  BUTTON_DOWN)
        translation.y += increment;
    if (gp_Controller->GetKeyState(DIK_LEFT) ==  BUTTON_DOWN)
        translation.x -= increment;
    if (gp_Controller->GetKeyState(DIK_RIGHT) ==  BUTTON_DOWN)
        translation.x += increment;
}

4.4 更新各属性状态

void C_DemoA::Update() 
{
    //把当前时刻除以动画显示速度,得到余数
    double tempTime = gp_Timer->TimeMod(ANIMATION_SPEED);

    // 如果等于0,表示到了切换帧的时间了。把当前显示的动画帧切换到其他动画帧去
    if(tempTime == 0){
        p_Box->Animate();
        alpha = 0;
    }else{
        // 如果不等于0的话,就用余数除以动画显示速度,乘以圆周率,做正弦操作
        // 使得alpha值产生周期性重复变化的效果
        alpha = (int)(255 * sin(D3DX_PI * tempTime/ANIMATION_SPEED));
    }

    // 对小方盒的坐标做检查,放置小方盒超出屏幕范围
    if (translation.x < 0) 
        translation.x = 0;
    if (translation.x > (C_View::SCREEN_WIDTH-p_Box->GetWidth())) 
        translation.x = C_View::SCREEN_WIDTH-p_Box->GetWidth();
    if (translation.y < 0) 
        translation.y = 0;
    if (translation.y > (C_View::SCREEN_HEIGHT-p_Box->GetHeight())
        translation.y = C_View::SCREEN_HEIGHT-p_Box->GetHeight();
}

4.5 渲染

void C_DemoA::Render2D() {

    //渲染背景
    p_Background->Render2D();     

    p_Box->Render2D( translation, DVECTOR2(0,0), 0, DVECTOR2(1,1),
                     D3DCOLOR_ARGB(alpha,255,255,255));
    //渲染不同格式的图形
    p_Bmp8->Render2D(DVECTOR2(459,461));
    p_Bmp24->Render2D(DVECTOR2(523,461));
    p_Png8->Render2D(DVECTOR2(587,461));
    p_Png24->Render2D(DVECTOR2(651,461));
    p_Png32->Render2D(DVECTOR2(715,461));
    // 显示当前的alpha值
    gp_Model->GetFont()->Render2D(_T("Alpha Value = ")+ 
                       toTString(alpha),DVECTOR2(5,350), BLACK);
}

4.6 本Demo的技术点总结

kumakoko avatar
kumakoko
pure coder
comments powered by Disqus