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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   ObjectsArray.inl
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Contents: Inline functions implementations.
  11. *                                                                             
  12. *   Authors: Eran Yariv - 28484475                                           
  13. *            Moshe Zur  - 24070856                                           
  14. *                                                                            
  15. *                                                                            
  16. *   Date: 23/09/98                                                           
  17. *                                                                            
  18. ******************************************************************************/
  19. inline CObjectsArray::CObjectsArray () :
  20. m_uSize(0),
  21. m_Array(NULL),
  22. m_lplpTop(NULL)
  23. {
  24. }
  25. inline void
  26. CObjectsArray::Init (UINT uSize)
  27. {
  28.     m_uSize = uSize;
  29.     // Ensure array size is bigger than 0, and allocation succeeded:
  30.     ASSERT(m_uSize);
  31.     m_Array = new CGameObject*[m_uSize];
  32.     m_lplpTop = m_Array;
  33.     m_lplpLast = &m_Array[m_uSize-1];
  34. }
  35. inline CObjectsArray::~CObjectsArray()
  36. {
  37.     delete [] m_Array;
  38. }
  39. inline ARR_POS
  40. CObjectsArray::GetHeadPosition()
  41. {
  42.     return m_Array;     // Initialized to NULL on empty array
  43. }
  44. inline CGameObject* 
  45. CObjectsArray::GetNext(ARR_POS& pos)
  46. {
  47.     CGameObject *pObj = NULL;
  48.     if (pos < m_lplpTop && pos >= m_Array)   // If legal and we can still advance
  49.         pObj = *pos++;
  50.     return pObj;
  51. }
  52. inline BOOL 
  53. CObjectsArray::Add(CGameObject *pGameObj)
  54. {
  55.     if (m_lplpTop <= m_lplpLast) {  // Is there a place ?
  56.         ASSERT(pGameObj);
  57.         pGameObj->SetArrayPosition(m_lplpTop);   // Store position for fast removal
  58.         *m_lplpTop++ = pGameObj;    // Store pointer and advance
  59.         return TRUE;
  60.     }
  61.     return FALSE;
  62. }
  63. inline BOOL 
  64. CObjectsArray::Remove(CGameObject *pGameObj)
  65. {
  66.     // Object stores his position in list:
  67.     ASSERT(pGameObj);
  68.     CGameObject **lplpDelObj = pGameObj->GetArrayPosition();
  69.     ASSERT(lplpDelObj && lplpDelObj>=m_Array && lplpDelObj<m_lplpTop);
  70.     
  71.     // Replace last entry with the one just evacuated:
  72.     *lplpDelObj = *(--m_lplpTop);   // This will also move top one entry down
  73.     
  74.     // Update the newly shuffled object's position:
  75.     (*lplpDelObj)->SetArrayPosition(lplpDelObj);
  76.     // And ofcourse - delete object
  77.     delete pGameObj;
  78.     return TRUE;
  79. }
  80. inline BOOL 
  81. CObjectsArray::IsEmpty()
  82. {
  83.     return (m_lplpTop == m_Array);
  84. }
  85. inline int 
  86. CObjectsArray::GetObjectsCount() const
  87. {
  88.     return (DWORD(m_lplpTop) - DWORD(m_Array)) / sizeof(CGameObject*);
  89. }
  90. inline void
  91. CObjectsArray::Empty()
  92. {
  93.     for (CGameObject** p = m_Array; p < m_lplpTop; p++)
  94.         delete (*p);
  95.     m_lplpTop = m_Array;
  96. }