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

📄 didcfgview.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 5 页
字号:
}




//-----------------------------------------------------------------------------
// Name: InitCustomViews()
// Desc: initializes custom views by retrieving device data, and calling
//       ConstructCustomView() to make views using bitmap images for
//       the device controls 
//-----------------------------------------------------------------------------
HRESULT DIDCfgView::InitCustomViews( LPDIRECTINPUTDEVICE8 lpDevice, BOOL bUseInternal )
{
    HRESULT hr;

    // reset the info
    if( m_lpCustomViewInfo )
        m_lpCustomViewInfo->CleanUp();
    else
    {
        m_lpCustomViewInfo = new DidcvCustomViewInfo;
        if( NULL == m_lpCustomViewInfo )
            return DIERR_OUTOFMEMORY;
    }

    // get a count of how many controls there are
    m_lpCustomViewInfo->dwType = DIDCV_EDT_COUNT;

    if( bUseInternal )
    {
        if( m_lpCalloutManager == NULL )
            return DIERR_INVALIDPARAM;

        // count internal objects that have action mapping
        m_lpCalloutManager->EnumObjects( lpDevice, DidcvEnumDeviceObjectsCallback, 
                                         m_lpCustomViewInfo, TRUE );
    }
    else
    {
        // count from device
        lpDevice->EnumObjects( DidcvEnumDeviceObjectsCallback, m_lpCustomViewInfo, DIDFT_ALL );
    }

    if( m_lpCustomViewInfo->dwCount == 0 )
    {
        m_lpCustomViewInfo->CleanUp();
        return DIERR_INVALIDPARAM;
    }

    // do a second time to get the objects
    m_lpCustomViewInfo->dwType = DIDCV_EDT_SAVE;
    m_lpCustomViewInfo->rgObjData = new LPDIDEVICEOBJECTINSTANCE[m_lpCustomViewInfo->dwCount];
    if( NULL == m_lpCustomViewInfo->rgObjData )
    {
        m_lpCustomViewInfo->CleanUp();
        return DIERR_OUTOFMEMORY;
    }

    if( bUseInternal )
    {
        if( m_lpCalloutManager == NULL )
            return DIERR_INVALIDPARAM;

        // get the internal objects that have action mapping
        m_lpCalloutManager->EnumObjects( lpDevice, DidcvEnumDeviceObjectsCallback, 
                                         m_lpCustomViewInfo, TRUE );
    }
    else
    {
        // get a list of all objects for the device
        lpDevice->EnumObjects( DidcvEnumDeviceObjectsCallback, 
                               m_lpCustomViewInfo, DIDFT_ALL );
    }

    if( m_lpCustomViewInfo->dwSize == 0 )
    {
        m_lpCustomViewInfo->CleanUp();
        return DIERR_INVALIDPARAM;
    }

    // build the custom views
    if( FAILED( hr = BuildCustomViews() ) )
    {
        m_lpCustomViewInfo->CleanUp();
        return hr;
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: BuildCustomViews()
// Desc: makes custom views
//-----------------------------------------------------------------------------
HRESULT DIDCfgView::BuildCustomViews()
{
    DidcvView* pView;
    DidcvCallout* pCallout;
    DWORD dwNumViews, dwStartIndex, dwFinishIndex;
    DWORD i, j;
    DIDEVICEIMAGEINFO devImageInfo;

    // pre-condition of this method - m_lpCustomViewInfo must be loaded
    assert( m_lpCustomViewInfo && m_lpCustomViewInfo->rgObjData );

    // calculate the number of views needed
    dwNumViews = DidcvCustomView::CalcNumViews( m_lpCustomViewInfo->dwSize );
    assert( dwNumViews );

    // reset
    m_lpViewManager->CleanUp();
    m_lpCalloutManager->CleanUp();

    // set the initial capacity
    if( FALSE == m_lpViewManager->SetCapacity( dwNumViews ) )
        return DIERR_OUTOFMEMORY;

    if( FALSE == m_lpCalloutManager->SetCapacity( m_lpCustomViewInfo->dwSize, 
                                                  m_lpCustomViewInfo->dwSize, dwNumViews ) )
        return DIERR_OUTOFMEMORY;

    dwStartIndex = 0;
    for( i = 0; i < dwNumViews; i++ )
    {
        // create views, each for a section of object array
        pView = DidcvCustomView::Create( m_lpCustomViewInfo, dwStartIndex, 
                                         &dwFinishIndex );
        if( NULL == pView )
            return DIERR_OUTOFMEMORY;

        // add the view to the manager
        if( FAILED( m_lpViewManager->AddImage( pView ) ) )
        {
            delete pView;
            continue;
        }

        for( j = dwStartIndex; j <= dwFinishIndex; j++ )
        {
            ZeroMemory( &devImageInfo, sizeof( DIDEVICEIMAGEINFO ) );

            // construct our own image info
            devImageInfo.dwFlags = DIDIFT_OVERLAY;
            // figure out the callout information for each object
            DidcvCustomView::CalcImageInfo( j, &( devImageInfo.rcOverlay ), 
                                            &( devImageInfo.dwcValidPts ),
                                            devImageInfo.rgptCalloutLine, 
                                            &( devImageInfo.rcCalloutRect ) );
            devImageInfo.dwTextAlign = DIDAL_LEFTALIGNED;
            devImageInfo.dwObjID = m_lpCustomViewInfo->rgObjData[j]->dwType;

            // create the callout and add
            pCallout = DidcvCallout::Create( &devImageInfo );
            m_lpCalloutManager->AddCallout( pCallout, pView->GetID() );
        }

        dwStartIndex = dwFinishIndex + 1;
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CopyActionMap()
// Desc: copies a DIACTIONFORMAT
//-----------------------------------------------------------------------------
BOOL DIDCfgView::CopyActionMap( LPDIACTIONFORMAT lpDiaf )
{
    if( m_lpDiaf )
    {
        // free the DIACTIONFORMAT
        if( m_lpDiaf->rgoAction )
            delete [] m_lpDiaf->rgoAction;
        delete m_lpDiaf;
        m_lpDiaf = NULL;
    }

    if( lpDiaf == NULL )
        return TRUE;

    m_lpDiaf = new DIACTIONFORMAT;
    if( m_lpDiaf == NULL )
        return FALSE;

    // copy the DIACTIONFORMAT
    memcpy( m_lpDiaf, lpDiaf, sizeof( DIACTIONFORMAT ) );
    if( lpDiaf->rgoAction )
    {
        // copy the internal array
        m_lpDiaf->rgoAction = new DIACTION[lpDiaf->dwNumActions];
        if( m_lpDiaf->rgoAction == NULL )
            return FALSE;

        memcpy( m_lpDiaf->rgoAction, lpDiaf->rgoAction, 
                sizeof( DIACTION ) * lpDiaf->dwNumActions );
    }

    return TRUE;
}




//-----------------------------------------------------------------------------
// Name: CleanUp()
// Desc: release the resource for the CustomViewInfo
//-----------------------------------------------------------------------------
void DidcvCustomViewInfo::CleanUp()
{
    // free the custom view data
    if( rgObjData )
    {
        DWORD i;
        // free each item
        for( i = 0; i < dwSize; i++)
            if( rgObjData[i] )
                delete rgObjData[i];

        // free the array
        delete [] rgObjData;
        rgObjData = NULL;
    }

    dwCount = 0;
    dwSize = 0;
}




//-----------------------------------------------------------------------------
// Name: DidcvBitmap()
// Desc: Constructor
//-----------------------------------------------------------------------------
DidcvBitmap::DidcvBitmap()
    : m_hbitmap( NULL ),
      m_lpBits( NULL )
{
    ZeroMemory( &m_size, sizeof( SIZE ) );

    // register with the alpha blend module
    DidcvAlphaBlend::AddClient();
}




//-----------------------------------------------------------------------------
// Name: ~DidcvBitmap()
// Desc: Destructor
//-----------------------------------------------------------------------------
DidcvBitmap::~DidcvBitmap()
{
    CleanUp();

    // release the alpha blending module
    DidcvAlphaBlend::ReleaseClient();
}




//-----------------------------------------------------------------------------
// Name: GetSize()
// Desc: retrieves the size of the bitmap - return FALSE if there is no bitmap
//-----------------------------------------------------------------------------
BOOL DidcvBitmap::GetSize( SIZE* lpSize ) const
{
    if( NULL == m_hbitmap )
        return FALSE;

    memcpy( lpSize, &m_size, sizeof( SIZE ) );
    return TRUE;
}




//-----------------------------------------------------------------------------
// Name: Draw()
// Desc: Draws onto a HDC, at( xStart, yStart )
//-----------------------------------------------------------------------------
BOOL DidcvBitmap::Draw( HDC hDC, INT xStart, INT yStart )
{
    if( NULL == m_hbitmap )
        return FALSE;

    BITMAP bm;
    HDC hdcMem;
    POINT ptSize, ptOrg;

    // create memory DC
    hdcMem = CreateCompatibleDC( hDC );
    SelectObject( hdcMem, m_hbitmap );
    SetMapMode( hdcMem, GetMapMode( hDC ) );

    // get bitmap info
    GetObject( m_hbitmap, sizeof( BITMAP ),( LPVOID ) &bm );

    ptSize.x = bm.bmWidth;
    ptSize.y = bm.bmHeight;
    DPtoLP( hDC, &ptSize, 1 );

    ptOrg.x = 0;
    ptOrg.y = 0;
    DPtoLP( hdcMem, &ptOrg, 1 );

    // blit
    BitBlt( hDC, xStart, yStart, ptSize.x, ptSize.y, 
            hdcMem, ptOrg.x, ptOrg.y, SRCCOPY );

    // free the memory DC
    DeleteDC( hdcMem );

    return TRUE;
}




//-----------------------------------------------------------------------------
// Name: Blend()
// Desc: draw the bitmap into HDC with alpha blending( xStart, yStart )
//-----------------------------------------------------------------------------
BOOL DidcvBitmap::Blend( HDC hDC, INT xStart, INT yStart )
{
    return DidcvAlphaBlend::Blend( hDC, xStart, yStart, 
                                   m_size.cx, m_size.cy, m_hbitmap, &m_size );
}




//-----------------------------------------------------------------------------
// Name: Blend()
// Desc: blend this bitmap unto the image
//-----------------------------------------------------------------------------
BOOL DidcvBitmap::Blend( VOID* lpBits, INT xStart, INT yStart, INT width, INT height )
{
    return DidcvAlphaBlend::Blend( lpBits, xStart, yStart, width, height, 
                                   m_lpBits, m_size.cx, m_size.cy );
}




//-----------------------------------------------------------------------------
// Name: GetHandle()
// Desc: return the GDI HBITMAP handle
//-----------------------------------------------------------------------------
HBITMAP DidcvBitmap::GetHandle()
{
    return m_hbitmap;
}




//-----------------------------------------------------------------------------
// Name: GetBits()
// Desc: returns a pointer to the bits
//-----------------------------------------------------------------------------
LPVOID DidcvBitmap::GetBits()
{
    return m_lpBits;
}




//-----------------------------------------------------------------------------
// Name: Create()
// Desc: static function to instantiate a bitmap object from a pathname
//-----------------------------------------------------------------------------
DidcvBitmap* DidcvBitmap::Create( LPCTSTR tszFilename )
{
    return CreateViaD3dx( tszFilename );
}




//-----------------------------------------------------------------------------
// Name: Create()
// Desc: static function to instantiate a bitmap object from dimensions
//-----------------------------------------------------------------------------
DidcvBitmap* DidcvBitmap::Create( INT width, INT height )
{
    BITMAPINFO bmi;
    DidcvBitmap* bm;
    HBITMAP hbm;
    VOID* lpBits;

    // sanity check
    if( width <= 0 || height <= 0 )
        return NULL;

    // make the header
    ZeroMemory( &bmi, sizeof( BITMAPINFO ) );
    bmi.bmiHeader.biSize = sizeof( bmi.bmiHeader );
    bmi.bmiHeader.biWidth = width;
    bmi.bmiHeader.biHeight = height;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = 0;
    bmi.bmiHeader.biXPelsPerMeter = 0;
    bmi.bmiHeader.biYPelsPerMeter = 0;
    bmi.bmiHeader.biClrUsed = 0;
    bmi.bmiHeader.biClrImportant = 0;

    // create a DIBSection
    hbm = CreateDIBSection( NULL, &bmi, DIB_RGB_COLORS, &lpBits, NULL, 0 );
    if( hbm == NULL )
        return NULL;

    // allocate an object
    bm = new DidcvBitmap;
    if( NULL == bm )
        return NULL;

⌨️ 快捷键说明

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