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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   BitMask.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Each game object has one or more bitmaps. In order to 
  11. *                       calculate collision detection between 2 objects, we 
  12. *                       need to find which part of the object's bitmap is 
  13. *                       overlapping the other object's bitmap. 
  14. *                       If we find at least one pixel overlapping,
  15. *                       that is not a background colored pixel,
  16. *                       than we have a collision. To save the effort of 
  17. *                       comparing bitmaps of 24 bpp, we make a mask for each
  18. *                       bitmap, of only 1 byte per pixel - a pixel w/ value
  19. *                       0 is a background (transparent) one, and w/ value 1 is
  20. *                       part of the game object.
  21. *                       
  22. *                                                                             
  23. *   Authors: Eran Yariv - 28484475                                           
  24. *            Moshe Zur  - 24070856                                           
  25. *                                                                            
  26. *                                                                            
  27. *   Date: 23/09/98                                                           
  28. *                                                                            
  29. ******************************************************************************/
  30. #include "stdafx.h"
  31. #include "BitMask.h"
  32. #include "GameConsts.h"
  33. /*------------------------------------------------------------------------------
  34.   Function: Constructor.
  35.   Purpose:  Constructs a bit mask for the associated bitmap.
  36.   Input:    dib: A game object bitmap.
  37.   Output:   None.
  38.   Remarks:  Along with the bit mask, we calculate the borders of the object 
  39.             actuall image inside the bitmap.
  40. ------------------------------------------------------------------------------*/
  41. CBitMask::CBitMask(CDIB& dib) : m_Size(dib.Size()), m_BitMask(new BYTE[m_Size.cy * m_Size.cx])
  42. {
  43.     ASSERT(m_BitMask);
  44.     PPIXEL Bits = (PPIXEL)dib.m_pBits;
  45.     BYTE *Masks = m_BitMask;
  46.     m_ActualRect.top = m_ActualRect.left = LONG_MAX; 
  47.     m_ActualRect.right = m_ActualRect.bottom = LONG_MIN; 
  48.     for (int x = 0; x < m_Size.cx; x++)
  49.         for (int y = 0; y < m_Size.cy; y++)
  50.         {
  51.             *Masks++ = (*Bits++ != TRANSP_COLOR);
  52.             if (TRANSP_COLOR != dib.ColorAt (x,y))
  53.             {   // Real pixel
  54.                 m_ActualRect.top = min (m_ActualRect.top, y);
  55.                 m_ActualRect.left = min (m_ActualRect.left, x);
  56.                 m_ActualRect.right = max (m_ActualRect.right, x);
  57.                 m_ActualRect.bottom = max (m_ActualRect.bottom, y);
  58.             }
  59.         }
  60. }