dxsurface.cpp

来自「网络泡泡被.net管理」· C++ 代码 · 共 1,815 行 · 第 1/4 页

CPP
1,815
字号
	{
		SetBkColor(hDC,crBackground);
		SetBkMode(hDC,OPAQUE);
	}
    // Set the background and foreground color
    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;
}


HRESULT CDxSurface::DrawText( HFONT hFont, LPCTSTR lpString, GRECT* lpRect, LONG lWidth, LONG lHeight, UINT uFormat,
		              D3DCOLOR crForeground, D3DCOLOR crBackground )
{
    if( m_pdds == NULL || lpString == NULL )
        return E_INVALIDARG;
	if(lpRect!=NULL)
	{
		if(lpRect->left>lWidth || lpRect->top>lHeight 
			|| lpRect->right<0 || lpRect->bottom<0)
			return S_OK;
	}
    HDC     hDC = NULL;
    HRESULT hr;
	CGsSurface surface(m_pEngine);
	surface.CreateEx(lWidth, lHeight, false);

	
    if( FAILED( hr = surface.m_pdds->GetDC( &hDC ) ) )
        return hr;
	
	SetBkMode(hDC, TRANSPARENT);
    // Set the background and foreground color
    SetTextColor( hDC, RGB(255,255,255) );

    if( hFont )
        SelectObject( hDC, hFont );

		SIZE	sz;
		GetTextExtentPoint(hDC, "8", _tcslen("8"),&sz);
		lpRect->right	-= sz.cx/2;

    // Use GDI to draw the text on the surface
    ::DrawText( hDC, lpString, _tcslen(lpString), lpRect, uFormat | DT_NOCLIP );

    if( FAILED( hr = surface.m_pdds->ReleaseDC( hDC ) ) )
        return hr;
    // Make sure this surface is restored.
    if( FAILED( hr = m_pdds->Restore() ) )
        return hr;

//	Clear(0);

 	DDSURFACEDESC2 ddsd,ddsd_src;
	ddsd.dwSize = sizeof(ddsd);
    while( hr = m_pdds->Lock( NULL, &ddsd, 0, 0 ) == DDERR_WASSTILLDRAWING );
	if(FAILED(hr))
		return hr;
 	ddsd_src.dwSize = sizeof(ddsd_src);
	while( hr = surface.m_pdds->Lock( NULL, &ddsd_src, 0, 0 ) == DDERR_WASSTILLDRAWING );
	if(FAILED(hr))
		return hr;


	DWORD lPitch = ddsd.lPitch;
    BYTE* pBytes = (BYTE*)ddsd.lpSurface;


	DWORD inc_src = ddsd_src.ddpfPixelFormat.dwRGBBitCount/8;
    BYTE* pBytes_src = (BYTE*)ddsd_src.lpSurface;
	

	DWORD crPix		= ConvertColor(crForeground);
	DWORD crBk		= ConvertColor(crBackground);


    for( DWORD h=0; h<lHeight && h<ddsd.dwHeight; h++ )
    {
        DWORD* pDstData32 = ((DWORD*)pBytes);
        WORD*  pDstData16 = ((WORD*)pBytes);
		BYTE*  p_src	= pBytes_src;

        for( DWORD w=0; w<lWidth && w<ddsd.dwWidth; w++ )
        {
			//byte	= surface.GetPixelInfo(w,h,NULL,&byte);
			if(*p_src==0)
			{
				if(crBackground!=0)
				{
					if( 32 == ddsd.ddpfPixelFormat.dwRGBBitCount )
						pDstData32[w] = crBk;
					else
						pDstData16[w] = crBk;
				}
			}
			else
			{
				if( 32 == ddsd.ddpfPixelFormat.dwRGBBitCount )
					pDstData32[w] = crPix;
				else
					pDstData16[w] = crPix;
 			}
			p_src += inc_src;
       }
		
        pBytes += ddsd.lPitch;
		pBytes_src += ddsd_src.lPitch;
    }
	surface.m_pdds->Unlock(0);
	m_pdds->Unlock(0);


    return S_OK;

}

HRESULT CDxSurface::DrawNumber( HFONT hFont, LPCTSTR lpString, GRECT* lpRect, LONG lWidth, LONG lHeight, UINT uFormat,
		              D3DCOLOR crForeground, D3DCOLOR crBackground )
{
    if( m_pdds == NULL || lpString == NULL )
        return E_INVALIDARG;
	if(lpRect!=NULL)
	{
		if(lpRect->left>lWidth || lpRect->top>lHeight 
			|| lpRect->right<0 || lpRect->bottom<0)
			return S_OK;
	}
    HDC     hDC = NULL;
    HRESULT hr;
	CGsSurface surface(m_pEngine);
	surface.CreateEx(lWidth, lHeight, false);

	
    if( FAILED( hr = surface.m_pdds->GetDC( &hDC ) ) )
        return hr;
	
	SetBkMode(hDC, TRANSPARENT);
    // Set the background and foreground color
    SetTextColor( hDC, RGB(255,255,255) );

    if( hFont )
        SelectObject( hDC, hFont );

		SIZE	sz;
		GetTextExtentPoint(hDC, "8", _tcslen("8"),&sz);
		lpRect->right	-= sz.cx/2;

	if(uFormat & DT_RIGHT)
	{

		GRECT rc = *lpRect;
//		rc.right = lpRect->right - sz.cx;
		for(int i=0; i<_tcslen(lpString); i++)
		{
			rc.right = lpRect->right - sz.cx * i - sz.cx/2;
			::DrawText(hDC, lpString+_tcslen(lpString)-1-i, 1, &rc, uFormat | DT_NOCLIP );
		}
	}
	else if(uFormat & DT_LEFT)
	{
		SIZE	sz;
		GetTextExtentPoint(hDC, "8", _tcslen("8"),&sz);

		GRECT rc = *lpRect;
//		rc.right = lpRect->right - sz.cx;
		for(int i=0; i<_tcslen(lpString); i++)
		{
			rc.left = lpRect->left + sz.cx * i;
			::DrawText(hDC, lpString+_tcslen(lpString)-1-i, 1, &rc, uFormat | DT_NOCLIP );
		}
	}
	else
	{

		// Use GDI to draw the text on the surface
		::DrawText( hDC, lpString, _tcslen(lpString), lpRect, uFormat );
	}

    if( FAILED( hr = surface.m_pdds->ReleaseDC( hDC ) ) )
        return hr;
    // Make sure this surface is restored.
    if( FAILED( hr = m_pdds->Restore() ) )
        return hr;

//	Clear(0);

 	DDSURFACEDESC2 ddsd,ddsd_src;
	ddsd.dwSize = sizeof(ddsd);
    while( hr = m_pdds->Lock( NULL, &ddsd, 0, 0 ) == DDERR_WASSTILLDRAWING );
	if(FAILED(hr))
		return hr;
 	ddsd_src.dwSize = sizeof(ddsd_src);
	while( hr = surface.m_pdds->Lock( NULL, &ddsd_src, 0, 0 ) == DDERR_WASSTILLDRAWING );
	if(FAILED(hr))
		return hr;


	DWORD lPitch = ddsd.lPitch;
    BYTE* pBytes = (BYTE*)ddsd.lpSurface;


	DWORD inc_src = ddsd_src.ddpfPixelFormat.dwRGBBitCount/8;
    BYTE* pBytes_src = (BYTE*)ddsd_src.lpSurface;
	

	DWORD crPix		= ConvertColor(crForeground);
	DWORD crBk		= ConvertColor(crBackground);


    for( DWORD h=0; h<lHeight && h<ddsd.dwHeight; h++ )
    {
        DWORD* pDstData32 = ((DWORD*)pBytes);
        WORD*  pDstData16 = ((WORD*)pBytes);
		BYTE*  p_src	= pBytes_src;

        for( DWORD w=0; w<lWidth && w<ddsd.dwWidth; w++ )
        {
			//byte	= surface.GetPixelInfo(w,h,NULL,&byte);
			if(*p_src==0)
			{
				if(crBackground!=0)
				{
					if( 32 == ddsd.ddpfPixelFormat.dwRGBBitCount )
						pDstData32[w] = crBk;
					else
						pDstData16[w] = crBk;
				}
			}
			else
			{
				if( 32 == ddsd.ddpfPixelFormat.dwRGBBitCount )
					pDstData32[w] = crPix;
				else
					pDstData16[w] = crPix;
 			}
			p_src += inc_src;
       }
		
        pBytes += ddsd.lPitch;
		pBytes_src += ddsd_src.lPitch;
    }
	surface.m_pdds->Unlock(0);
	m_pdds->Unlock(0);


    return S_OK;

}

//-----------------------------------------------------------------------------
// Name: CDxSurface::DrawTextCenter()
// Desc: Draws a text string on a DirectDraw surface using hFont or the default
//       GDI font if hFont is NULL.  
//-----------------------------------------------------------------------------
HRESULT CDxSurface::DrawTextCenter( HFONT hFont, const TCHAR* strText, 
									DWORD dwOriginX, DWORD dwOriginY, DWORD dwLineLength,
									COLORREF crForeground, COLORREF crBackground )
{
    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;


	if(crBackground==RGB(0,0,0))
		SetBkMode(hDC, TRANSPARENT);
	else
	{
		SetBkColor(hDC,crBackground);
		SetBkMode(hDC,OPAQUE);
	}
    // Set the background and foreground color
    SetTextColor( hDC, crForeground );

    if( hFont )
        SelectObject( hDC, hFont );

    // Use GDI to draw the text on the surface
	SIZE	sz;
	GetTextExtentPoint(hDC, strText, _tcslen(strText),&sz);
	TextOut(hDC, dwOriginX+(dwLineLength-sz.cx)/2 , dwOriginY, strText, _tcslen(strText));

    if( FAILED( hr = m_pdds->ReleaseDC( hDC ) ) )
        return hr;

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: CDxSurface::DrawTextInRect()
// Desc: Draws a text string on a DirectDraw surface using hFont or the default
//       GDI font if hFont is NULL.  
//-----------------------------------------------------------------------------
HRESULT CDxSurface::DrawTextInRect( HFONT hFont, const TCHAR* strText, 
									DWORD dwOriginX, DWORD dwOriginY,
									DWORD dwLineLength, DWORD dwHeight,
									COLORREF crForeground, COLORREF crBackground )
{
    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;

	if(crBackground==RGB(0,0,0))
		SetBkMode(hDC, TRANSPARENT);
	else
	{
		SetBkColor(hDC,crBackground);
		SetBkMode(hDC,OPAQUE);
	}
    // Set the background and foreground color
    SetTextColor( hDC, crForeground );

    if( hFont )
        SelectObject( hDC, hFont );

    // Use GDI to draw the text on the surface
	SIZE	sz;
	int j=0,k=0,l;
	int r;
	//SetTextCharacterExtra(hDC, col_space);
	for(int i=0; i<(int)_tcslen(strText); )
	{
		if ((BYTE)(*( strText+i)) >= 0xA1/* && (BYTE)(* (strText+i + 1) ) >= 0xA1*/)
		{//中文字
			l=2;
		}
		else
			l=1;//英文字母
		GetTextExtentPoint(hDC,strText+k,j+l,&sz);
		r=sz.cx-dwLineLength;
		if(r>0)
		{
			GetTextExtentPoint(hDC,strText+i,l,&sz);
			if(sz.cx/3<r)
			{
				if(j>0)
					TextOut(hDC, dwOriginX, dwOriginY, strText+k, j);
				k=i;
				dwOriginY+=sz.cy ;//+ row_space);
				j=0;
			}
		}
		j+=l;
		i+=l;
	}
	if(j>0)
	{
		TextOut(hDC, dwOriginX, dwOriginY, strText+k, j);
		dwOriginY+=sz.cy ;//+ row_space);
	}
    
	
    if( FAILED( hr = m_pdds->ReleaseDC( hDC ) ) )
        return hr;

    return S_OK;
}


//-----------------------------------------------------------------------------
// Name: CDxSurface::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 CDxSurface::DrawBitmap( const 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: CDxSurface::ConvertGDIColor()
// Desc: Converts a GDI color (0x00bbggrr) into the equivalent color on a 
//       DirectDrawSurface using its pixel format.  
//-----------------------------------------------------------------------------
DWORD CDxSurface::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);
//    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?