cSurface.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
- // cSurface.cpp: implementation of the cSurface class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "cSurface.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- cSurface::cSurface()
- {
- m_pSurface = NULL;
- m_ColorKey = -1;
- }
- cSurface::cSurface(HINSTANCE hInst, UINT nResource, int nWidth, int nHeight, COLORREF dwColorKey)
- {
- m_pSurface = NULL;
- m_ColorKey = -1;
-
- Create(nWidth, nHeight, dwColorKey);
- this->LoadBitmap(hInst, nResource);
- }
- cSurface::~cSurface()
- {
- if(m_pSurface != NULL)
- {
- OutputDebugString("Surface Destroyedn");
- m_pSurface->Release();
- m_pSurface = NULL;
- }
- }
- BOOL cSurface::LoadBitmap(HINSTANCE hInst, UINT nRes, int nX, int nY, int nWidth, int nHeight)
- {
- HDC hdcImage;
- HDC hdc;
- BITMAP bm;
- DDSURFACEDESC2 ddsd;
- HRESULT hr;
- HBITMAP hbm;
- hbm = (HBITMAP) LoadImage(hInst, MAKEINTRESOURCE(nRes), IMAGE_BITMAP, nWidth, nHeight, 0L);
- if (hbm == NULL || m_pSurface == NULL)
- return FALSE;
- //确保要重置曲面
- m_pSurface->Restore();
- //要把位图载入到memoryDC中
- hdcImage = CreateCompatibleDC(NULL);
- if (!hdcImage)
- return FALSE;
- //将位图载入到MemoryDC
- SelectObject(hdcImage, hbm);
- //从位图句柄得到位图结构
- GetObject(hbm, sizeof(bm), &bm);
- //获得位图宽度和高度
- if(nWidth == 0)
- nWidth = bm.bmWidth;
-
- if(nHeight == 0)
- nHeight = bm.bmHeight;
-
- //得到曲面大小信息
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
- m_pSurface->GetSurfaceDesc(&ddsd);
- if ((hr = m_pSurface->GetDC(&hdc)) == DD_OK)
- {
- StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, nX, nY,
- nWidth, nHeight, SRCCOPY);
- m_pSurface->ReleaseDC(hdc);
- }
- DeleteDC(hdcImage);
- return TRUE;
- }
- BOOL cSurface::Create(int nWidth, int nHeight, COLORREF dwColorKey)
- {
- DDSURFACEDESC2 ddsd;
- HRESULT hRet;
- DDCOLORKEY ddck;
- m_ColorKey = dwColorKey;
- ZeroMemory( &ddsd, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( ddsd );
- ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |
- DDSCAPS_SYSTEMMEMORY;
-
- ddsd.dwWidth = nWidth;
- ddsd.dwHeight = nHeight;
- //根据ddsd对曲面描述,创建一个曲面m_pSurface。
- hRet = GetDirectDraw()->CreateSurface(&ddsd, &m_pSurface, NULL );
- if( hRet != DD_OK )
- {
- DXTRACE_MSG("DirectDraw Surface Creation Failed");
- return FALSE;
- }
- if((int)dwColorKey != -1)
- {
- ddck.dwColorSpaceLowValue = dwColorKey;
- ddck.dwColorSpaceHighValue = 0;
- //设置m_pSurface底色
- m_pSurface->SetColorKey(DDCKEY_SRCBLT, &ddck);
- }
- //设置曲面高度和宽度
- m_Width = nWidth;
- m_Height = nHeight;
- return TRUE;
- }
- BOOL cSurface::Draw(LPDIRECTDRAWSURFACE7 lpDest, int iDestX, int iDestY, int iSrcX, int iSrcY, int nWidth, int nHeight)
- {
- RECT rcRect;
- if(nWidth == 0 && nHeight == 0)
- {
- nWidth = m_Width;
- nHeight = m_Height;
- }
- rcRect.left = iSrcX;
- rcRect.top = iSrcY;
- rcRect.right = nWidth + iSrcX;
- rcRect.bottom = nHeight + iSrcY;
- if((int)m_ColorKey < 0)
- {
- //非透明的彩色拷贝
- lpDest->BltFast(iDestX, iDestY, m_pSurface, &rcRect, DDBLTFAST_NOCOLORKEY);
- }
- else
- {
- //透明的彩色拷贝
- lpDest->BltFast(iDestX, iDestY, m_pSurface, &rcRect, DDBLTFAST_SRCCOLORKEY);
- }
- return TRUE;
- }
- void cSurface::Destroy()
- {
- if(m_pSurface != NULL)
- {
- m_pSurface->Release();
- m_pSurface = NULL;
- }
- }
- UINT cSurface::Height()
- {
- return m_Height;
- }
- UINT cSurface::Width()
- {
- return m_Width;
- }
- LPDIRECTDRAWSURFACE7 cSurface::GetSurface()
- {
- return m_pSurface;
- }