AmmoStatus.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   AmmoStatus.cpp
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the ammunition status object displayed in 
  11. *                       the main dialog.
  12. *                       
  13. *                                                                             
  14. *   Authors: Eran Yariv - 28484475                                           
  15. *            Moshe Zur  - 24070856                                           
  16. *                                                                            
  17. *                                                                            
  18. *   Date: 23/09/98                                                           
  19. *                                                                            
  20. ******************************************************************************/
  21. #include "stdafx.h"
  22. #include "Tanks.h"
  23. #include "AmmoStatus.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. BEGIN_MESSAGE_MAP(CAmmoStatus, CButton)
  30. //{{AFX_MSG_MAP(CAmmoStatus)
  31. ON_WM_QUERYNEWPALETTE()
  32. ON_WM_PALETTECHANGED()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. CAmmoStatus::CAmmoStatus():
  36.          m_uPrevShellsCount (0xffff),
  37.          m_uCurShellsCount (0),
  38.          m_uPrevBulletsCount (0xffff),
  39.          m_uCurBulletsCount (0),
  40.          m_uPrevMinesCount (0xffff),
  41.          m_uCurMinesCount (0)
  42. {
  43.     m_DIB.CreateEmpty (AMMO_STATUS_WIDTH, AMMO_STATUS_HEIGHT);
  44.     m_DIB.GetPaletteFromResourceBitmap (IDB_BULLET);
  45. }
  46. void CAmmoStatus::Create ()
  47. {
  48.     CDIB DIBMineIcon,
  49.          DIBBulletIcon,
  50.          DIBShellIcon;
  51.     int iTextWidth = DIGIT_HEIGHT * 3;
  52.     m_DIB.FillSolidColor (0,0,0);
  53.     DIBMineIcon.ReadFromResource (IDB_MINE_ICON);
  54.     DIBBulletIcon.ReadFromResource (IDB_BULLET_ICON);
  55.     DIBShellIcon.ReadFromResource (IDB_SHELL_ICON);
  56.     m_DIBDigits.ReadFromResource (IDB_DIGITS);
  57.     m_uShellIconYPos = AMMO_STATUS_HEIGHT - DIBShellIcon.Height();
  58.     m_uBulletIconYPos = m_uShellIconYPos - (DIBBulletIcon.Height() + iTextWidth);
  59.     m_uMineIconYPos = m_uBulletIconYPos - (DIBMineIcon.Height() + iTextWidth);
  60.     m_DIB.CopyRectFrom (&DIBShellIcon, 
  61.                         0, 
  62.                         0,
  63.                         DIBShellIcon.Width(),
  64.                         DIBShellIcon.Height(), 
  65.                         (AMMO_STATUS_WIDTH / 2) - (DIBShellIcon.Width() / 2),
  66.                         m_uShellIconYPos);
  67.     m_DIB.CopyRectFrom (&DIBBulletIcon, 
  68.                         0, 
  69.                         0,
  70.                         DIBBulletIcon.Width(),
  71.                         DIBBulletIcon.Height(), 
  72.                         (AMMO_STATUS_WIDTH / 2) - (DIBBulletIcon.Width() / 2),
  73.                         m_uBulletIconYPos);
  74.     m_DIB.CopyRectFrom (&DIBMineIcon, 
  75.                         0, 
  76.                         0,
  77.                         DIBMineIcon.Width(),
  78.                         DIBMineIcon.Height(), 
  79.                         (AMMO_STATUS_WIDTH / 2) - (DIBMineIcon.Width() / 2),
  80.                         m_uMineIconYPos);
  81. }
  82. void CAmmoStatus::WriteNumber (UINT u, UINT x, UINT y)
  83. {
  84.     ASSERT (u < 1000);  // Max of 3 digits on display
  85.     char sz[4];
  86.     wsprintf (sz,"%3d",u);
  87.     for (int i=0; i<3; i++)
  88.     { 
  89.         m_DIB.CopyRectFrom (&m_DIBDigits,
  90.                             (sz[i] == ' ') ? (DIGIT_WIDTH * 10) : (DIGIT_WIDTH*(sz[i] - '0')),
  91.                             0,
  92.                             DIGIT_WIDTH,
  93.                             DIGIT_HEIGHT,
  94.                             x,y);
  95.         x += (DIGIT_WIDTH + 2);
  96.     }
  97. }
  98. void CAmmoStatus::SetShellsCount (UINT uCount)
  99. {
  100.     m_uCurShellsCount = uCount;
  101.     if (m_uCurShellsCount != m_uPrevShellsCount)
  102.         InvalidateRect (NULL, FALSE);
  103. }
  104. void CAmmoStatus::SetBulletsCount (UINT uCount)
  105. {
  106.     m_uCurBulletsCount = uCount;
  107.     if (m_uCurBulletsCount != m_uPrevBulletsCount)
  108.         InvalidateRect (NULL, FALSE);
  109. }
  110. void CAmmoStatus::SetMinesCount (UINT uCount)
  111. {
  112.     m_uCurMinesCount = uCount;
  113.     if (m_uCurMinesCount != m_uPrevMinesCount)
  114.         InvalidateRect (NULL, FALSE);
  115. }
  116. enum {  BLOCK_SIZE = 7,
  117.         GAP_SIZE   = 2
  118.      };
  119. CDIB *CAmmoStatus::GetImage ()
  120. {
  121.     if (m_uCurShellsCount != m_uPrevShellsCount ||
  122.         m_uCurBulletsCount != m_uPrevBulletsCount ||
  123.         m_uCurMinesCount != m_uPrevMinesCount)
  124.     {   // Something has changed - recreate image
  125.         const int iNumberXPos = ((AMMO_STATUS_WIDTH / 2) - ((DIGIT_WIDTH * 3 + 4) / 2));
  126.         WriteNumber (m_uCurShellsCount, iNumberXPos, m_uShellIconYPos - DIGIT_HEIGHT - 4);
  127.         WriteNumber (m_uCurBulletsCount, iNumberXPos, m_uBulletIconYPos - DIGIT_HEIGHT - 4);
  128.         WriteNumber (m_uCurMinesCount, iNumberXPos, m_uMineIconYPos - DIGIT_HEIGHT - 4);
  129.         m_uPrevShellsCount = m_uCurShellsCount;
  130.         m_uPrevBulletsCount = m_uCurBulletsCount;
  131.         m_uPrevMinesCount = m_uCurMinesCount;
  132.     }
  133.     return &m_DIB;
  134. }