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

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   ImageManager.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the image manager object. Handle the game
  11. *                       objects' images - load from the resource, calculate
  12. *                       image bitmask, animate, and release the images at the 
  13. *                       end.
  14. *                       
  15. *                                                                             
  16. *   Authors: Eran Yariv - 28484475                                           
  17. *            Moshe Zur  - 24070856                                           
  18. *                                                                            
  19. *                                                                            
  20. *   Date: 23/09/98                                                           
  21. *                                                                            
  22. ******************************************************************************/
  23. #include <stdafx.h>
  24. #include <ImageManager.h>
  25. #include <GameConsts.h>
  26. #include <BitmapTables.h>
  27. void CImageManager::LoadImages ()
  28. {
  29.     ASSERT (!m_bCreated); // Called only once
  30.     for (int uImgID=0; uImgID < IMG_LAST_INDEX; uImgID++)
  31.         if (IMG_GAMEOVER != uImgID) // Conserve memory
  32.             LoadSingleImage (ImageType(uImgID));
  33.     m_bCreated=TRUE;
  34. }            
  35. void CImageManager::LoadSingleImage (ImageType uImgID)
  36. {
  37.     ASSERT (uImgID >=0 && uImgID < IMG_LAST_INDEX);
  38.     // Traverse all images
  39.     for (UINT uSumImgID = 0; 
  40.          uSumImgID < m_ImagesInfo[uImgID].uNumSubImages; 
  41.          uSumImgID++) 
  42.         // Traverse all sub-images
  43.         for (UINT uFrameID = 0;
  44.              uFrameID < m_ImagesInfo[uImgID].SubImages[uSumImgID].uNumBitmaps;
  45.              uFrameID++) 
  46.         {
  47.             // Traverse all bitmaps
  48.             CDIB *pDIB = new CDIB();
  49.             //TRACE("new dib %08xn", pDIB);
  50.             m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pDIB = pDIB;
  51.             if (!pDIB->ReadFromResource(m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].uBitmapID))
  52.                 AfxThrowResourceException();    // Can't load resource bitmap
  53.             // Create bit mask if needed (pointer is initialized to a non-null value):
  54.             if (m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pBitMask) {
  55.                 m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pBitMask = 
  56.                     new CBitMask(*pDIB);
  57.                 //TRACE("new bitmask %08xn", m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pBitMask);
  58.             }
  59.         }
  60. }
  61. void CImageManager::ReleaseSingleImage (ImageType uImgID)
  62. {
  63.     ASSERT (uImgID >=0 && uImgID < IMG_LAST_INDEX);
  64.     // Traverse all images
  65.     for (UINT uSumImgID = 0; 
  66.          uSumImgID < m_ImagesInfo[uImgID].uNumSubImages; 
  67.          uSumImgID++) 
  68.         // Traverse all sub-images
  69.         for (UINT uFrameID = 0;
  70.              uFrameID < m_ImagesInfo[uImgID].SubImages[uSumImgID].uNumBitmaps;
  71.              uFrameID++) 
  72.         {
  73.             // Traverse all bitmaps
  74.             CDIB *&pDIB = 
  75. m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pDIB;
  76.             if (NULL != pDIB)
  77.             {
  78.                 delete pDIB;
  79.                 pDIB = NULL;
  80.             }
  81.             CBitMask *&pBitMask = 
  82. m_ImagesInfo[uImgID].SubImages[uSumImgID].Bitmaps[uFrameID].pBitMask;
  83.             if (NULL != pBitMask)
  84.             {
  85.                 delete pBitMask;
  86.                 pBitMask = NULL;
  87.             }
  88.         }
  89. }
  90. void CImageManager::ReloadMap ()
  91. {
  92.     ASSERT (m_bCreated); // Called only once
  93.     CDIB *pDIB = m_ImagesInfo[IMG_BOARD].SubImages[0].Bitmaps[0].pDIB;
  94.     ASSERT (NULL != pDIB);
  95.     CDIB tmpDIB;
  96.     BOOL bSuccess = tmpDIB.ReadFromResource (IDB_BOARD);
  97.     ASSERT (bSuccess);
  98.     if (!bSuccess)
  99.         return;
  100.     bSuccess = pDIB->CopyFrom (&tmpDIB);
  101.     ASSERT (bSuccess);
  102. }
  103. CImageManager::~CImageManager ()
  104. {
  105.     if (!m_bCreated)
  106.         return; // Never created
  107.     for (int uImgID=IMG_TANK0; uImgID < IMG_LAST_INDEX; uImgID++)
  108.         ReleaseSingleImage (ImageType(uImgID));
  109.     m_bCreated=FALSE;
  110. }
  111. void CImageManager::DisplayImage (HIMAGE &himg, CDIB *pDstDIB, CPoint &pos)
  112. {
  113.     ASSERT (m_bCreated);
  114.     ASSERT_VALID_HIMAGE (himg); // Costly call
  115.     BitmapType *pCurBitmap = &m_ImagesInfo[himg.bImageType].SubImages[himg.bSubImgID].Bitmaps[himg.bFrame];
  116.     pDstDIB->PasteCKRect (pCurBitmap->pDIB, 
  117.                           pos.x + pCurBitmap->iXOffset, 
  118.                           pos.y + pCurBitmap->iYOffset, 
  119.                           TRANSP_COLOR);
  120.     if (himg.pOverlay != NULL)
  121.         DisplayImage (*himg.pOverlay, pDstDIB, pos);
  122. }
  123. void
  124. CImageManager::UpdateImage (HIMAGE &himg, BOOL &bImageChanged)
  125. {
  126.     HIMAGE himgOldCopy = himg;
  127.     ASSERT (m_bCreated);
  128.     ASSERT_VALID_HIMAGE (himg); // Costly call
  129.     DWORD dwFrameDelay = m_ImagesInfo[himg.bImageType].SubImages[himg.bSubImgID].dwFrameDelay;
  130.     if (dwFrameDelay != 0) { 
  131.         // Not a static image 
  132.         DWORD dwNow = GetTickCount();
  133.         if (0 == himg.dwStartFrameTime)  // Animation isn't started yet
  134.         {
  135.             himg.dwStartFrameTime = dwNow;   // Animation just started
  136.         }
  137.         else
  138.         {   // Animation is on the way, calc new frame
  139.             ASSERT (himg.dwStartFrameTime <= dwNow);
  140.             int iNewFrame = int ((dwNow - himg.dwStartFrameTime) / dwFrameDelay);
  141.             UINT uNumBitmaps = m_ImagesInfo[himg.bImageType].SubImages[himg.bSubImgID].uNumBitmaps;
  142.             if (UINT(iNewFrame) >= uNumBitmaps)
  143.             {   // Overlap occured
  144.                 if (m_ImagesInfo[himg.bImageType].SubImages[himg.bSubImgID].bCyclicAnim)
  145.                 {
  146.                     // Animation is cyclic => simply restart
  147.                     iNewFrame %= uNumBitmaps;
  148.                     himg.bFrame = BYTE(iNewFrame);
  149.                 }
  150.                 else 
  151.                 {  // Animation is not cyclic => Display last frame and set end flag
  152.                     himg.bFrame = BYTE(uNumBitmaps - 1);
  153.                     himg.bAnimEnded = TRUE;
  154.                 }
  155.             } // end of overlap
  156.             else
  157.                 himg.bFrame = BYTE(iNewFrame);
  158.         } // End of animation on the way case
  159.         //TRACE ("Animation: timegap = %d, delay = %d, frame = %dn",dwNow - himg.dwStartFrameTime, dwFrameDelay, himg.bFrame);
  160.     } // End of animation image case
  161.     if (FALSE == bImageChanged &&   // The calling object thinks its image hasn't changed
  162.         0 != memcmp (&himgOldCopy, &himg, sizeof (HIMAGE))) // but the image handle has changed
  163.         bImageChanged = TRUE;
  164. }