📄 1394surface.cpp
字号:
// 1394Surface.cpp: implementation of the C1394Surface class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "1394Surface.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
//#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
C1394Surface::C1394Surface()
{
m_pdds = NULL;
m_bColorKeyed = NULL;
}
C1394Surface::~C1394Surface()
{
SAFE_RELEASE( m_pdds );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT C1394Surface::Create( LPDIRECTDRAWSURFACE7 pdds )
{
m_pdds = pdds;
if( m_pdds )
{
m_pdds->AddRef();
// Get the DDSURFACEDESC structure for this surface
m_ddsd.dwSize = sizeof(m_ddsd);
m_pdds->GetSurfaceDesc( &m_ddsd );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT C1394Surface::Create( LPDIRECTDRAW7 pDD, DDSURFACEDESC2* pddsd )
{
HRESULT hr;
// Create the DDraw surface
if( FAILED( hr = pDD->CreateSurface( pddsd, &m_pdds, NULL ) ) )
return hr;
// Prepare the DDSURFACEDESC structure
m_ddsd.dwSize = sizeof(m_ddsd);
// Get the DDSURFACEDESC structure for this surface
m_pdds->GetSurfaceDesc( &m_ddsd );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT C1394Surface::Destroy()
{
SAFE_RELEASE( m_pdds );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: C1394Surface::DrawBitmap()
// Desc: Draws a bitmap over an entire DirectDrawSurface, stretching the
// bitmap if nessasary
//-----------------------------------------------------------------------------
HRESULT C1394Surface::DrawBitmap( HBITMAP hBMP,
DWORD dwBMPOriginX, DWORD dwBMPOriginY,
DWORD dwBMPWidth, DWORD dwBMPHeight )
{
HDC hDCImage;
HDC hDC;
BITMAP bmp;
DDSURFACEDESC2 ddsd;
HRESULT hr;
if( hBMP == NULL || m_pdds == NULL )
return E_INVALIDARG;
// Make sure this surface is restored.
if( FAILED( hr = m_pdds->Restore() ) )
return hr;
// Get the surface.description
ddsd.dwSize = sizeof(ddsd);
m_pdds->GetSurfaceDesc( &ddsd );
if( ddsd.ddpfPixelFormat.dwFlags == DDPF_FOURCC )
return E_NOTIMPL;
// Select bitmap into a memoryDC so we can use it.
hDCImage = CreateCompatibleDC( NULL );
if( NULL == hDCImage )
return E_FAIL;
SelectObject( hDCImage, hBMP );//selects an object into the specified device context (DC)
// Get size of the bitmap
GetObject( hBMP, sizeof(bmp), &bmp );
// Use the passed size, unless zero
dwBMPWidth = ( dwBMPWidth == 0 ) ? bmp.bmWidth : dwBMPWidth;
dwBMPHeight = ( dwBMPHeight == 0 ) ? bmp.bmHeight : dwBMPHeight;
// Stretch the bitmap to cover this surface
if( FAILED( hr = m_pdds->GetDC( &hDC ) ) )
return hr;
StretchBlt( hDC, 0, 0,
ddsd.dwWidth, ddsd.dwHeight,
hDCImage, dwBMPOriginX, dwBMPOriginY,
dwBMPWidth, dwBMPHeight, SRCCOPY );
if( FAILED( hr = m_pdds->ReleaseDC( hDC ) ) )
return hr;
DeleteDC( hDCImage );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: C1394Surface::DrawText()
// Desc: Draws a text string on a DirectDraw surface using hFont or the default
// GDI font if hFont is NULL.
//-----------------------------------------------------------------------------
HRESULT C1394Surface::DrawText( HFONT hFont, TCHAR* strText,
DWORD dwOriginX, DWORD dwOriginY,
COLORREF crBackground, COLORREF crForeground )
{
HDC hDC = NULL;
HRESULT hr;
if( m_pdds == NULL || strText == NULL )
return E_INVALIDARG;
// Make sure this surface is restored.
if( FAILED( hr = m_pdds->Restore() ) )
return hr;
if( FAILED( hr = m_pdds->GetDC( &hDC ) ) )
return hr;
// Set the background and foreground color
SetBkColor( hDC, crBackground );
SetTextColor( hDC, crForeground );
if( hFont )
SelectObject( hDC, hFont );
// Use GDI to draw the text on the surface
TextOut( hDC, dwOriginX, dwOriginY, strText, _tcslen(strText) );
if( FAILED( hr = m_pdds->ReleaseDC( hDC ) ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: C1394Surface::ReDrawBitmapOnSurface()
// Desc: Load a bitmap from a file or resource into a DirectDraw surface.
// normaly used to re-load a surface after a restore.
//-----------------------------------------------------------------------------
HRESULT C1394Surface::DrawBitmap( TCHAR* strBMP,
DWORD dwDesiredWidth, DWORD dwDesiredHeight )
{
HBITMAP hBMP;
HRESULT hr;
if( m_pdds == NULL || strBMP == NULL )
return E_INVALIDARG;
// Try to load the bitmap as a resource, if that fails, try it as a file
hBMP = (HBITMAP) LoadImage( GetModuleHandle(NULL), strBMP,
IMAGE_BITMAP, dwDesiredWidth, dwDesiredHeight,
LR_CREATEDIBSECTION );
if( hBMP == NULL )
{
hBMP = (HBITMAP) LoadImage( NULL, strBMP, IMAGE_BITMAP,
dwDesiredWidth, dwDesiredHeight,
LR_LOADFROMFILE | LR_CREATEDIBSECTION );
if( hBMP == NULL )
return E_FAIL;
}
// Draw the bitmap on this surface
if( FAILED( hr = DrawBitmap( hBMP, 0, 0, 0, 0 ) ) )
{
DeleteObject( hBMP );
return hr;
}
DeleteObject( hBMP );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc: 设置关键色,即透明色
//-----------------------------------------------------------------------------
HRESULT C1394Surface::SetColorKey( DWORD dwColorKey )
{
if( NULL == m_pdds )
return E_POINTER;
m_bColorKeyed = TRUE;
DDCOLORKEY ddck;
ddck.dwColorSpaceLowValue = ConvertGDIColor( dwColorKey );
ddck.dwColorSpaceHighValue = ConvertGDIColor( dwColorKey );
return m_pdds->SetColorKey( DDCKEY_SRCBLT, &ddck );
}
//-----------------------------------------------------------------------------
// Name: C1394Surface::ConvertGDIColor()
// Desc: Converts a GDI color (0x00bbggrr) into the equivalent color on a
// DirectDrawSurface using its pixel format.
//-----------------------------------------------------------------------------
DWORD C1394Surface::ConvertGDIColor( COLORREF dwGDIColor )
{
if( m_pdds == NULL )
return 0x00000000;
COLORREF rgbT;
HDC hdc;
DWORD dw = CLR_INVALID;
DDSURFACEDESC2 ddsd;
HRESULT hr;
// Use GDI SetPixel to color match for us
if( dwGDIColor != CLR_INVALID && m_pdds->GetDC(&hdc) == DD_OK)
{
rgbT = GetPixel(hdc, 0, 0); // Save current pixel value
SetPixel(hdc, 0, 0, dwGDIColor); // Set our value
m_pdds->ReleaseDC(hdc);
}
// Now lock the surface so we can read back the converted color
ddsd.dwSize = sizeof(ddsd);
hr = m_pdds->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
if( hr == DD_OK)
{
dw = *(DWORD *) ddsd.lpSurface;
if( ddsd.ddpfPixelFormat.dwRGBBitCount < 32 ) // Mask it to bpp
dw &= ( 1 << ddsd.ddpfPixelFormat.dwRGBBitCount ) - 1;
m_pdds->Unlock(NULL);
}
// Now put the color that was there back.
if( dwGDIColor != CLR_INVALID && m_pdds->GetDC(&hdc) == DD_OK )
{
SetPixel( hdc, 0, 0, rgbT );
m_pdds->ReleaseDC(hdc);
}
return dw;
}
//-----------------------------------------------------------------------------
// Name: C1394Surface::GetBitMaskInfo()
// Desc: Returns the number of bits and the shift in the bit mask
//-----------------------------------------------------------------------------
HRESULT C1394Surface::GetBitMaskInfo( DWORD dwBitMask, DWORD* pdwShift, DWORD* pdwBits )
{
DWORD dwShift = 0;
DWORD dwBits = 0;
if( pdwShift == NULL || pdwBits == NULL )
return E_INVALIDARG;
if( dwBitMask )
{
while( (dwBitMask & 1) == 0 )
{
dwShift++;
dwBitMask >>= 1;
}
}
while( (dwBitMask & 1) != 0 )
{
dwBits++;
dwBitMask >>= 1;
}
*pdwShift = dwShift;
*pdwBits = dwBits;
return S_OK;
}
//用Win32API函数来取得位图
HRESULT C1394Surface::DrawBmp( HBITMAP hBMP, DWORD dwDesiredWidth, DWORD dwDesiredHeight )
{
HDC hDCImage;//存储位图
HDC hDCMemory;//幕后内存区的DC
HRESULT hr;
if( m_pdds == NULL )
return E_INVALIDARG;
// Make sure this surface is restored.
if( FAILED( hr = m_pdds->Restore() ) )
return hr;
hDCImage = ::CreateCompatibleDC( NULL );//建立与目前显示模式兼容的DC
if( NULL == hDCImage )
return E_FAIL;
// Select bitmap into a memoryDC so we can use it.
::SelectObject( hDCImage, hBMP );//设定hDCImage中的位图为hBmp
// Stretch the bitmap to cover this surface
DDSURFACEDESC2 ddsd;
// Get the surface.description
ddsd.dwSize = sizeof(ddsd);
m_pdds->GetSurfaceDesc( &ddsd );
if( FAILED( hr = m_pdds->GetDC( &hDCMemory ) ) )//取得幕后内存区中的DC
{
// MessageBox("取得幕后内存区中的DC失败");
return hr;
}
::BitBlt( hDCMemory, 0, 0,
ddsd.dwWidth, ddsd.dwHeight,
hDCImage, 0, 0, SRCCOPY );
if( FAILED( hr = m_pdds->ReleaseDC( hDCMemory ) ) )
return hr;
::DeleteDC( hDCImage );
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -