⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddutil.cpp

📁 对游戏编程感兴趣的 朋友可以 下载下来看看 对DX讲解的很很详细
💻 CPP
📖 第 1 页 / 共 2 页
字号:
HRESULT CDisplay::Blt( DWORD x, DWORD y, LPDIRECTDRAWSURFACE7 pdds, RECT* prc,
                       DWORD dwFlags )
{
    if( NULL == m_pddsBackBuffer )
        return E_POINTER;

	//将平面位图中的prc矩形区域画到后平面的x,y位置上
    return m_pddsBackBuffer->BltFast( x, y, pdds, prc, dwFlags );
}


//-----------------------------------------------------------------------------
// Name: 将平面类对应的位图画到后平面上
// Desc: 后平面上的x坐标,后平面上的y坐标,平面类对象指针,位图(文字)平面上的一个矩形区域
//-----------------------------------------------------------------------------
HRESULT CDisplay::Blt( DWORD x, DWORD y, CSurface* pSurface, RECT* prc )
{
    if( NULL == pSurface )
        return E_INVALIDARG;

    if( pSurface->IsColorKeyed() )	//将平面实例上的位图画到后平面上
        return Blt( x, y, pSurface->GetDDrawSurface(), prc, DDBLTFAST_SRCCOLORKEY );//清除透明色
    else
        return Blt( x, y, pSurface->GetDDrawSurface(), prc, 0L );//不清除透明色
}


//-----------------------------------------------------------------------------
// Name: 使用指定的颜色填充屏幕
// 参数: 颜色值 
//-----------------------------------------------------------------------------
HRESULT CDisplay::Clear( DWORD dwColor )
{
    if( NULL == m_pddsBackBuffer )
        return E_POINTER;

    // Erase the background
    DDBLTFX ddbltfx;
    ZeroMemory( &ddbltfx, sizeof(ddbltfx) );
    ddbltfx.dwSize      = sizeof(ddbltfx);
    ddbltfx.dwFillColor = dwColor;	//定义颜色
	//使用颜色填充平面
    return m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
CSurface::CSurface()
{
    m_pdds = NULL;
    m_bColorKeyed = NULL;	//只能为NULL,不能为FALSE
	dwWidth = 0;
	dwHeight = 0;
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
CSurface::~CSurface()
{
    SAFE_RELEASE( m_pdds );
}



//-----------------------------------------------------------------------------
// Name: 创建平面类的实例
// 参数:DirectX对象,平面的结构指针 
//-----------------------------------------------------------------------------
HRESULT CSurface::Create( LPDIRECTDRAW7 pDD, DDSURFACEDESC2* pddsd )
{
    HRESULT hr;

    // Create the DDraw surface创建一个平面
    if( FAILED( hr = pDD->CreateSurface( pddsd, &m_pdds, NULL ) ) )
        return hr;

    // Use the passed size, unless zero
    dwWidth  = pddsd->dwWidth;		//保留平面的尺寸
    dwHeight = pddsd->dwHeight;

    // 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: CSurface::DrawBitmap()向平面画一个位图
// 参数:位图的句柄
// Desc: Draws a bitmap over an entire DirectDrawSurface, stretching the 
//       bitmap if nessasary
// 注意:位图尺寸大于表面的尺寸时位图尺寸变小
//-----------------------------------------------------------------------------
HRESULT CSurface::DrawBitmap( HBITMAP hBMP )
{
    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 );	//设置为位图设备

    // Get size of the bitmap
    GetObject( hBMP, sizeof(bmp), &bmp );//得到位图的信息

    // Stretch the bitmap to cover this surface
    if( FAILED( hr = m_pdds->GetDC( &hDC ) ) )	//得到平面的DC
        return hr;

    StretchBlt( hDC, 0, 0, ddsd.dwWidth, ddsd.dwHeight,	// 画到DC上
                hDCImage, 0, 0, bmp.bmWidth, bmp.bmHeight, 
				SRCCOPY );

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

    DeleteDC( hDCImage );//释放

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CSurface::DrawText()
// Desc: Draws a text string on a DirectDraw surface using hFont or the default
//       GDI font if hFont is NULL.  
// 说明:将文字画到平面上
// 参数:字体,文字,背景色,字体色
//-----------------------------------------------------------------------------
HRESULT CSurface::DrawText( HFONT hFont, TCHAR* strText,
                            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 ) ) )	//得到DC
        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, 0, 0, strText, _tcslen(strText) );	//输出文字

	SIZE sizeText;
    GetTextExtentPoint32( hDC, strText, _tcslen(strText), &sizeText );	//得到显示文字的尺寸
	dwHeight = sizeText.cx ;	//保留字体的大小
	dwHeight = sizeText.cy;

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

    return S_OK;
}


//-------------------------------------------------
// Name: 向平面画一个位图
// 参数:位图的字符描述
//-------------------------------------------------
HRESULT CSurface::DrawBitmap( TCHAR* strBMP )
{
    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, 0, 0, 
                                LR_CREATEDIBSECTION );
    if( hBMP == NULL )
    {
        hBMP = (HBITMAP) LoadImage( NULL, strBMP, IMAGE_BITMAP, 
                                    0, 0, 
                                    LR_LOADFROMFILE | LR_CREATEDIBSECTION );
        if( hBMP == NULL )
            return E_FAIL;
    }

    // Draw the bitmap on this surface  在平面上画一个位图
    if( FAILED( hr = DrawBitmap( hBMP ) ) )
    {
        DeleteObject( hBMP );
        return hr;
    }

    DeleteObject( hBMP );

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: 设置平面的透明颜色
// 参数: 透明颜色值 
//-----------------------------------------------------------------------------
HRESULT CSurface::SetColorKey( COLORREF crColorKey )
{
    if( NULL == m_pdds )
        return E_POINTER;

    m_bColorKeyed = TRUE;	//使用透明颜色

    DDCOLORKEY ddck;

    ddck.dwColorSpaceLowValue  = crColorKey;
    ddck.dwColorSpaceHighValue = crColorKey;
    
    return m_pdds->SetColorKey( DDCKEY_SRCBLT, &ddck );//设置透明颜色
}





⌨️ 快捷键说明

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