CPI_Image.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:11k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "globals.h"
  3. #include "CompositeFile.h"
  4. //
  5. CPs_Image* CPIG_CreateImage_FromFile(const char* pcFilename)
  6. {
  7.     HBITMAP hbmLoad;
  8.     BITMAP bmLoad;
  9.     CPs_Image* pNewImage;
  10.     char cFilename[MAX_PATH];
  11.     strcpy(cFilename, "P:\Skin\");
  12.     strcat(cFilename, pcFilename);
  13.     hbmLoad = LoadImage(NULL, cFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  14.     if(!hbmLoad)
  15.         return NULL;
  16.     GetObject(hbmLoad, sizeof(bmLoad), &bmLoad);
  17.     pNewImage = (CPs_Image*)malloc(sizeof(CPs_Image));
  18.     pNewImage->m_hbmImage = hbmLoad;
  19.     pNewImage->m_szSize.cx = bmLoad.bmWidth;
  20.     pNewImage->m_szSize.cy = bmLoad.bmHeight;
  21.     return pNewImage;
  22. }
  23. //
  24. //
  25. //
  26. CPs_Image* CPIG_CreateImage_FromSubFile(CP_COMPOSITEFILE hmComposite, const char* pcSubFilename)
  27. {
  28.     void* pFileData;
  29.     unsigned int iLength;
  30.     BOOL bSucceeded;
  31.     HBITMAP hbmLoad;
  32.     SIZE szBitmap;
  33.     CPs_Image* pNewImage;
  34.     bSucceeded = CF_GetSubFile(hmComposite, pcSubFilename, &pFileData, &iLength);
  35.     if(!bSucceeded)
  36.         return NULL;
  37.     {
  38.         BITMAPFILEHEADER* pFileHeader = (BITMAPFILEHEADER*)pFileData;
  39.         BITMAPINFO* pBitmapInfo;
  40.         void* pBitmapBytes;
  41.         HDC dcDisplay, dcDraw;
  42.         if(pFileHeader->bfType != 0x4D42)
  43.         {
  44.             free(pFileData);
  45.             return NULL;
  46.         }
  47.         pBitmapInfo = (BITMAPINFO*)(((BYTE*)pFileData) + sizeof(BITMAPFILEHEADER));
  48.         pBitmapBytes = ((BYTE*)pFileData) + pFileHeader->bfOffBits;
  49.         dcDisplay = GetDC(NULL);
  50.         szBitmap.cx = pBitmapInfo->bmiHeader.biWidth;
  51.         szBitmap.cy = pBitmapInfo->bmiHeader.biHeight;
  52.         hbmLoad = CreateCompatibleBitmap(dcDisplay, szBitmap.cx, szBitmap.cy);
  53.         dcDraw = CreateCompatibleDC(dcDisplay);
  54.         ReleaseDC(NULL, dcDisplay);
  55.         SetDIBits(dcDraw, hbmLoad, 0, pBitmapInfo->bmiHeader.biHeight,
  56.                   pBitmapBytes, pBitmapInfo, DIB_RGB_COLORS);
  57.         DeleteDC(dcDraw);
  58.     }
  59.     free(pFileData);
  60.     pNewImage = (CPs_Image*)malloc(sizeof(CPs_Image));
  61.     pNewImage->m_hbmImage = hbmLoad;
  62.     pNewImage->m_szSize = szBitmap;
  63.     return pNewImage;
  64. }
  65. //
  66. CPs_Image* CPIG_CreateImage_FromResource(const UINT uiResourceID)
  67. {
  68.     HBITMAP hbmLoad;
  69.     BITMAP bmLoad;
  70.     CPs_Image* pNewImage;
  71.     hbmLoad = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(uiResourceID));
  72.     if(!hbmLoad)
  73.         return NULL;
  74.     GetObject(hbmLoad, sizeof(bmLoad), &bmLoad);
  75.     pNewImage = (CPs_Image*)malloc(sizeof(CPs_Image));
  76.     pNewImage->m_hbmImage = hbmLoad;
  77.     pNewImage->m_szSize.cx = bmLoad.bmWidth;
  78.     pNewImage->m_szSize.cy = bmLoad.bmHeight;
  79.     return pNewImage;
  80. }
  81. //
  82. //
  83. //
  84. CPs_Image_WithState* CPIG_CreateStateImage(CPs_Image* pSource, const int iNumStates)
  85. {
  86.     CPs_Image_WithState* pNewIS;
  87.     int iStateIDX;
  88.     CP_ASSERT( (iNumStates-1) <= igsLast);
  89.     pNewIS = (CPs_Image_WithState*)malloc(sizeof(*pNewIS));
  90.     pNewIS->m_pImage = pSource;
  91.     pNewIS->m_iStateHeight = pSource->m_szSize.cy / iNumStates;
  92.     memset(pNewIS->m_ptSource, 0, sizeof(pNewIS->m_ptSource));
  93.     for(iStateIDX = 0; iStateIDX < iNumStates; iStateIDX++)
  94.     {
  95.         pNewIS->m_ptSource[iStateIDX].x = 0;
  96.         pNewIS->m_ptSource[iStateIDX].y = iStateIDX * pNewIS->m_iStateHeight;
  97.     }
  98.     return pNewIS;
  99. }
  100. //
  101. //
  102. //
  103. void CPIG_DestroyImage(CPs_Image* pImage)
  104. {
  105.     if(!pImage)
  106.         return;
  107.     CP_ASSERT(pImage->m_hbmImage);
  108.     DeleteObject(pImage->m_hbmImage);
  109.     free(pImage);
  110. }
  111. //
  112. //
  113. //
  114. void CPIG_DestroyImage_WithState(CPs_Image_WithState* pImageWithState)
  115. {
  116.     if(!pImageWithState)
  117.         return;
  118.     CP_ASSERT(pImageWithState->m_pImage);
  119.     CPIG_DestroyImage(pImageWithState->m_pImage);
  120.     free(pImageWithState);
  121. }
  122. //
  123. //
  124. //
  125. void CPIG_DrawStateImage(CPs_DrawContext* pDC, const int iX, const int iY, CPs_Image_WithState* pSource, const CPe_ImageState enState)
  126. {
  127.     HDC dcCompat;
  128.     HBITMAP bmOld;
  129.     CP_CHECKOBJECT(pSource);
  130.     dcCompat = CreateCompatibleDC(pDC->m_dcDraw);
  131.     bmOld = (HBITMAP)SelectObject(dcCompat, pSource->m_pImage->m_hbmImage);
  132.     SetBkColor(dcCompat, glb_pSkin->m_clrTransparent);
  133.     BitBlt(pDC->m_dcDraw, iX + pDC->m_ptOffset.x, iY + pDC->m_ptOffset.y, pSource->m_pImage->m_szSize.cx, pSource->m_iStateHeight,
  134.            dcCompat, pSource->m_ptSource[enState].x, pSource->m_ptSource[enState].y, SRCCOPY);
  135.     SelectObject(dcCompat, bmOld);
  136.     DeleteDC(dcCompat);
  137. }
  138. //
  139. //
  140. //
  141. void CPIG_DrawImage(CPs_DrawContext* pDC, const int iX, const int iY, CPs_Image* pSource)
  142. {
  143.     HDC dcCompat;
  144.     HBITMAP bmOld;
  145.     CP_CHECKOBJECT(pSource);
  146.     dcCompat = CreateCompatibleDC(pDC->m_dcDraw);
  147.     bmOld = (HBITMAP)SelectObject(dcCompat, pSource->m_hbmImage);
  148.     SetBkColor(dcCompat, glb_pSkin->m_clrTransparent);
  149.     BitBlt(pDC->m_dcDraw, iX + pDC->m_ptOffset.x, iY + pDC->m_ptOffset.y, pSource->m_szSize.cx, pSource->m_szSize.cy, dcCompat, 0, 0, SRCCOPY);
  150.     SelectObject(dcCompat, bmOld);
  151.     DeleteDC(dcCompat);
  152. }
  153. //
  154. //
  155. //
  156. void CPIG_TiledFill(CPs_DrawContext* pDC, const RECT* _prTarget, const RECT* prSourceRect, CPs_Image* pSourceImage, const DWORD dwOptions)
  157. {
  158.     HDC dcCompat;
  159.     HBITMAP bmOld;
  160.     RECT rSourceBorders;
  161.     RECT rTarget;
  162.     RECT rLocalClip;
  163.     CP_CHECKOBJECT(pSourceImage);
  164.     if(_prTarget->right < pDC->m_rClip.left
  165.             || _prTarget->bottom < pDC->m_rClip.top
  166.             || _prTarget->left > pDC->m_rClip.right
  167.             || _prTarget->top > pDC->m_rClip.bottom)
  168.     {
  169.         return;
  170.     }
  171.     rTarget = *_prTarget;
  172.     OffsetRect(&rTarget, pDC->m_ptOffset.x, pDC->m_ptOffset.y);
  173.     rLocalClip = pDC->m_rClip;
  174.     OffsetRect(&rLocalClip, pDC->m_ptOffset.x, pDC->m_ptOffset.y);
  175.     dcCompat = CreateCompatibleDC(pDC->m_dcDraw);
  176.     bmOld = (HBITMAP)SelectObject(dcCompat, pSourceImage->m_hbmImage);
  177.     SetBkColor(dcCompat, glb_pSkin->m_clrTransparent);
  178.     rSourceBorders.left = prSourceRect->left;
  179.     rSourceBorders.right = pSourceImage->m_szSize.cx - prSourceRect->right;
  180.     rSourceBorders.top = prSourceRect->top;
  181.     rSourceBorders.bottom = pSourceImage->m_szSize.cy - prSourceRect->bottom;
  182.     if(rSourceBorders.top > 0
  183.             && ((rTarget.top+rSourceBorders.top) >= rLocalClip.top && rTarget.top <= rLocalClip.bottom))
  184.     {
  185.         BitBlt(pDC->m_dcDraw, rTarget.left, rTarget.top, rSourceBorders.left, rSourceBorders.top, dcCompat, 0, 0, SRCCOPY);
  186.         BitBlt(pDC->m_dcDraw, rTarget.right - rSourceBorders.right, rTarget.top,
  187.                rSourceBorders.right, rSourceBorders.top,
  188.                dcCompat, prSourceRect->right, 0, SRCCOPY);
  189.         if(prSourceRect->left < prSourceRect->right)
  190.         {
  191.             int iSourceCursorPos;
  192.             int iTileWidth;
  193.             int iEndCol;
  194.             iSourceCursorPos = rSourceBorders.left + rTarget.left;
  195.             iEndCol = rTarget.right-rSourceBorders.right;
  196.             while(iSourceCursorPos < iEndCol)
  197.             {
  198.                 iTileWidth = (prSourceRect->right - prSourceRect->left);
  199.                 if( (iSourceCursorPos + iTileWidth) > iEndCol)
  200.                     iTileWidth = iEndCol - iSourceCursorPos;
  201.                 BitBlt(pDC->m_dcDraw, iSourceCursorPos, rTarget.top,
  202.                        iTileWidth, rSourceBorders.top,
  203.                        dcCompat, prSourceRect->left, 0, SRCCOPY);
  204.                 iSourceCursorPos += iTileWidth;
  205.             }
  206.         }
  207.     }
  208.     if(rSourceBorders.bottom > 0
  209.             && (rTarget.bottom >= rLocalClip.top && (rTarget.bottom-rSourceBorders.bottom) <= rLocalClip.bottom) )
  210.     {
  211.         BitBlt(pDC->m_dcDraw, rTarget.left, rTarget.bottom - rSourceBorders.bottom,
  212.                rSourceBorders.left, rSourceBorders.bottom, dcCompat, 0, prSourceRect->bottom, SRCCOPY);
  213.         BitBlt(pDC->m_dcDraw, rTarget.right - rSourceBorders.right, rTarget.bottom - rSourceBorders.bottom,
  214.                rSourceBorders.right, rSourceBorders.bottom,
  215.                dcCompat, prSourceRect->right, prSourceRect->bottom, SRCCOPY);
  216.         if(prSourceRect->left < prSourceRect->right)
  217.         {
  218.             int iSourceCursorPos;
  219.             int iTileWidth;
  220.             int iEndCol;
  221.             iSourceCursorPos = rSourceBorders.left + rTarget.left;
  222.             iEndCol = rTarget.right-rSourceBorders.right;
  223.             while(iSourceCursorPos < iEndCol)
  224.             {
  225.                 iTileWidth = (prSourceRect->right - prSourceRect->left);
  226.                 if( (iSourceCursorPos + iTileWidth) > iEndCol)
  227.                     iTileWidth = iEndCol - iSourceCursorPos;
  228.                 BitBlt(pDC->m_dcDraw, iSourceCursorPos, rTarget.bottom - rSourceBorders.bottom,
  229.                        iTileWidth, rSourceBorders.bottom,
  230.                        dcCompat, prSourceRect->left, prSourceRect->bottom, SRCCOPY);
  231.                 iSourceCursorPos += iTileWidth;
  232.             }
  233.         }
  234.     }
  235.     if(prSourceRect->top < prSourceRect->bottom
  236.             && prSourceRect->bottom > prSourceRect->top)
  237.     {
  238.         int iSourceCursorPos_Y;
  239.         int iTileHeight;
  240.         int iEndRow;
  241.         iSourceCursorPos_Y = rSourceBorders.top + rTarget.top;
  242.         iEndRow = rTarget.bottom-rSourceBorders.bottom;
  243.         while(iSourceCursorPos_Y < iEndRow)
  244.         {
  245.             iTileHeight = (prSourceRect->bottom - prSourceRect->top);
  246.             if( (iSourceCursorPos_Y + iTileHeight) > iEndRow)
  247.                 iTileHeight = iEndRow - iSourceCursorPos_Y;
  248.             if((iSourceCursorPos_Y+iTileHeight) >= rLocalClip.top && iSourceCursorPos_Y <= rLocalClip.bottom)
  249.             {
  250.                 BitBlt(pDC->m_dcDraw, rTarget.left, iSourceCursorPos_Y, rSourceBorders.left, iTileHeight, dcCompat, 0, rSourceBorders.top, SRCCOPY);
  251.                 BitBlt(pDC->m_dcDraw, rTarget.right - rSourceBorders.right, iSourceCursorPos_Y,
  252.                        rSourceBorders.right, iTileHeight,
  253.                        dcCompat, prSourceRect->right, rSourceBorders.top, SRCCOPY);
  254.                 if(prSourceRect->left < prSourceRect->right
  255.                         && (dwOptions & CIC_TILEDFILOPTIONS_NOCENTRE) == 0)
  256.                 {
  257.                     int iSourceCursorPos;
  258.                     int iTileWidth;
  259.                     int iEndCol;
  260.                     iSourceCursorPos = rSourceBorders.left + rTarget.left;
  261.                     iEndCol = rTarget.right-rSourceBorders.right;
  262.                     while(iSourceCursorPos < iEndCol)
  263.                     {
  264.                         iTileWidth = (prSourceRect->right - prSourceRect->left);
  265.                         if( (iSourceCursorPos + iTileWidth) > iEndCol)
  266.                             iTileWidth = iEndCol - iSourceCursorPos;
  267.                         BitBlt(pDC->m_dcDraw, iSourceCursorPos, iSourceCursorPos_Y,
  268.                                iTileWidth, iTileHeight,
  269.                                dcCompat, prSourceRect->left, rSourceBorders.top, SRCCOPY);
  270.                         iSourceCursorPos += iTileWidth;
  271.                     }
  272.                 }
  273.             }
  274.             iSourceCursorPos_Y += iTileHeight;
  275.         }
  276.     }
  277.     SelectObject(dcCompat, bmOld);
  278.     DeleteDC(dcCompat);
  279. }
  280. //