2D游戏引擎(八)——添加动画子画面支持

动画子画面是通过循环播放一系列帧图像来获得动画效果。为了向引擎添加动画子画面,修改代码如下。

向Bitmap.cpp添加DrawPart()方法,该方法允许只绘制子画面位图图像的一部分,从而支持帧动画。

  1. void Bitmap::DrawPart(HDC hDC, int x, int y, int xPart, int yPart,
  2.   int wPart, int hPart, BOOL bTrans, COLORREF crTransColor)
  3. {
  4.   if (m_hBitmap != NULL)
  5.   {
  6.     // Create a memory device context for the bitmap
  7.     HDC hMemDC = CreateCompatibleDC(hDC);
  8.     // Select the bitmap into the device context
  9.     HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
  10.     // Draw the bitmap to the destination device context
  11.     if (bTrans)
  12.       TransparentBlt(hDC, x, y, wPart, hPart, hMemDC, xPart, yPart,
  13.         wPart, hPart, crTransColor);
  14.     else
  15.       BitBlt(hDC, x, y, wPart, hPart, hMemDC, xPart, yPart, SRCCOPY);
  16.     // Restore and delete the memory device context
  17.     SelectObject(hMemDC, hOldBitmap);
  18.     DeleteDC(hMemDC);
  19.   }
  20. }

须保持原来程序的可用性,修改Draw()方法。

  1. void Bitmap::Draw(HDC hDC, int x, int y, BOOL bTrans, COLORREF crTransColor)
  2. {
  3.   DrawPart(hDC, x, y, 0, 0, GetWidth(), GetHeight(), bTrans, crTransColor);
  4. }

增加Sprite.h的成员变量。

  1.  
  2.  int       m_iNumFrames,m_iCurFrame;       //帧数,当前帧
  3.  int       m_iFrameDelay,m_iFrameTrigger;  //帧延迟,帧触发器

Sprite()构造函数初始化这些成员变量。

  1.   m_iNumFrames=1;     //默认帧数为1
  2.   m_iCurFrame=m_iFrameDelay=m_iFrameTrigger=0;

Sprite.h中增加一些成员函数。

  1. //设置子画面的帧数
  2. inline void Sprite::SetNumFrames(int iNumFrames)
  3. {
  4.   //设置帧数
  5.   m_iNumFrames = iNumFrames;
  6.   //重新计算位置
  7.   RECT rect = GetPosition();
  8.   rect.bottom = rect.top + ((rect.bottom - rect.top)/iNumFrames);
  9.   SetPosition(rect);
  10. }
  11. //更新子画面的当前动画帧
  12. inline void Sprite::UpdateFrame()
  13. {
  14.   if((m_iFrameDelay>=0)&&(--m_iFrameTrigger<=0))
  15.   {
  16.     //重置帧触发器
  17.     m_iFrameTrigger = m_iFrameDelay;
  18.         //将帧加1
  19.     if(++m_iCurFrame>=m_iNumFrames)
  20.       m_iCurFrame=0;
  21.   }
  22. }
  23. //设置帧延迟
  24.   void    SetFrameDelay(int iFrameDelay) { m_iFrameDelay = iFrameDelay; };
  25. //新的GetHeight()方法
  26.   int     GetHeight()  { return ( m_pBitmap->GetHeight()/m_iNumFrames );}

自然同时要修改Sprite.cpp

  1. SPRITEACTION Sprite::Update()
  2. {
  3.   //更新帧
  4.   UpdateFrame();
  5.   ...
  6. }
  7. void Sprite::Draw(HDC hDC)
  8. {
  9.   // Draw the sprite if it isn't hidden
  10.   if (m_pBitmap != NULL && !m_bHidden)
  11.   {
  12.     //如有必要,绘制适当的帧
  13.     if(m_iNumFrames==1)
  14.      m_pBitmap->Draw(hDC, m_rcPosition.left, m_rcPosition.top, TRUE);
  15.     else
  16.       m_pBitmap->DrawPart(hDC,m_rcPosition.left,m_rcPosition.top,
  17.         0,m_iCurFrame*GetHeight(),GetWidth(),GetHeight(),TRUE);
  18.   }
  19. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值