深度探索DxFramework 4-1

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

第4章 初始锋芒 剖析DemoA

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

4.1.1 实现的功能点

4.1.2 Demo的整体架构

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

 1class C_DemoA : public C_GameState
 2{
 3public:
 4    C_DemoA();
 5    virtual ~C_DemoA();
 6    virtual void    Input();
 7    virtual void    Update();       
 8    virtual void    Render2D(); 
 9private:
10    C_Sprite*    p_Background;    //背景图片
11    C_Sprite*    p_Box;            //用来演示的小方盒
12
13    // 各种dxfrramework支持的图片格式
14    C_Sprite*    p_Bmp8;            //8位bmp格式
15    C_Sprite*    p_Bmp24;        //24位bmp格式
16    C_Sprite*    p_Png8;            //8位png格式
17    C_Sprite*    p_Png24;        //24位png格式
18    C_Sprite*    p_Png32;        //32位png格式
19    DVECTOR2    translation;            // box的屏幕坐标
20    int            alpha;                    // box的透明度
21    static const int VELOCITY;            // box的移动速度,为常量
22    static const int ANIMATION_SPEED;    // box的动画变化速度,为常量
23};
24
25C_DemoA::C_DemoA() 
26{
27    alpha = 255;
28    p_Background =  new C_Sprite(_T("demoa_background.png"));     
29    p_Box = new C_Sprite(_T("box-3-2-2.png"));
30    translation = DVECTOR2(400,300) - p_Box->GetCenter();
31
32    //载入DxFramework所能支持的不同格式的图片,创建精灵
33    p_Bmp8    =  new C_Sprite(_T("bitmap8.bmp"));
34    p_Bmp24 =  new C_Sprite(_T("bitmap24.bmp"));
35    p_Png8    =  new C_Sprite(_T("png8.png"));
36    p_Png24 =  new C_Sprite(_T("png24.png"));
37    p_Png32 =  new C_Sprite(_T("png32.png"));
38    gp_Timer->Reset();    //把计时器清零,准备用来对小方盒产生动画效果
39
40    //因为使用了背景图片,所以不必每帧都清空颜色缓冲区和z缓冲区
41    gp_View->SetClear(false);
42}

4.3 对外设输入的响应

 1void C_DemoA::Input() 
 2{
 3    // 按ESC键退出demo,返回主菜单
 4    if (gp_Controller->GetKeyState(DIK_ESCAPE)== BUTTON_PRESSED){
 5        gp_Model->ChangeState(MENU);
 6    } 
 7
 8    // 按F2,F3键切换到demo B,demo C
 9    if (gp_Controller->GetKeyState(DIK_F2)== BUTTON_PRESSED){
10        gp_Model->ChangeState(DEMOB);
11    }   
12    if (gp_Controller->GetKeyState(DIK_F3)== BUTTON_PRESSED){
13        gp_Model->ChangeState(DEMOC);
14    }   
15
16    //按上下左右箭头键,向上,向下,向左,向右移动小方盒
17    double increment = VELOCITY * gp_Timer->GetElapsed();
18    if (gp_Controller->GetKeyState(DIK_UP) ==  BUTTON_DOWN) 
19        translation.y -= increment;
20    if (gp_Controller->GetKeyState(DIK_DOWN) ==  BUTTON_DOWN)
21        translation.y += increment;
22    if (gp_Controller->GetKeyState(DIK_LEFT) ==  BUTTON_DOWN)
23        translation.x -= increment;
24    if (gp_Controller->GetKeyState(DIK_RIGHT) ==  BUTTON_DOWN)
25        translation.x += increment;
26}

4.4 更新各属性状态

 1void C_DemoA::Update() 
 2{
 3    //把当前时刻除以动画显示速度,得到余数
 4    double tempTime = gp_Timer->TimeMod(ANIMATION_SPEED);
 5
 6    // 如果等于0,表示到了切换帧的时间了。把当前显示的动画帧切换到其他动画帧去
 7    if(tempTime == 0){
 8        p_Box->Animate();
 9        alpha = 0;
10    }else{
11        // 如果不等于0的话,就用余数除以动画显示速度,乘以圆周率,做正弦操作
12        // 使得alpha值产生周期性重复变化的效果
13        alpha = (int)(255 * sin(D3DX_PI * tempTime/ANIMATION_SPEED));
14    }
15
16    // 对小方盒的坐标做检查,放置小方盒超出屏幕范围
17    if (translation.x < 0) 
18        translation.x = 0;
19    if (translation.x > (C_View::SCREEN_WIDTH-p_Box->GetWidth())) 
20        translation.x = C_View::SCREEN_WIDTH-p_Box->GetWidth();
21    if (translation.y < 0) 
22        translation.y = 0;
23    if (translation.y > (C_View::SCREEN_HEIGHT-p_Box->GetHeight())
24        translation.y = C_View::SCREEN_HEIGHT-p_Box->GetHeight();
25}

4.5 渲染

 1void C_DemoA::Render2D() {
 2
 3    //渲染背景
 4    p_Background->Render2D();     
 5
 6    p_Box->Render2D( translation, DVECTOR2(0,0), 0, DVECTOR2(1,1),
 7                     D3DCOLOR_ARGB(alpha,255,255,255));
 8    //渲染不同格式的图形
 9    p_Bmp8->Render2D(DVECTOR2(459,461));
10    p_Bmp24->Render2D(DVECTOR2(523,461));
11    p_Png8->Render2D(DVECTOR2(587,461));
12    p_Png24->Render2D(DVECTOR2(651,461));
13    p_Png32->Render2D(DVECTOR2(715,461));
14    // 显示当前的alpha值
15    gp_Model->GetFont()->Render2D(_T("Alpha Value = ")+ 
16                       toTString(alpha),DVECTOR2(5,350), BLACK);
17}

4.6 本Demo的技术点总结