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

📄 didevimg.cpp

📁 VC游戏编程基础
💻 CPP
📖 第 1 页 / 共 5 页
字号:

    m_dwActiveView = dwView;
    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: GetActiveView
// Desc: Returns the index of the currently active view. The active view is
//       what CDIDevImage paints when the device image is rendered.
// Args: pdwView - Pointer to a variable which will contain the current view
//         index upon successful return. May be NULL.
//       pdwNumViews - Pointer to a variable which will contain the total
//         number of views upon successful return. May be NULL.
//  Ret: DI_OK - Success.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetActiveView( LPDWORD pdwView, LPDWORD pdwNumViews )
{
    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    // For a custom UI, this method depends on the UI being calculated
    if( m_bCustomUI && m_bInvalidUI )
        BuildCustomUI();

    if( pdwView )
        *pdwView = m_dwActiveView;

    if( pdwNumViews )
        *pdwNumViews = m_dwNumViews;

    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: GetViewForObj
// Desc: Returns the index of the most appropriate view for a specific object
// Args: dwObjId - Object ID of the requested callout. This corresponds to the
//         dwType value returned by EnumDeviceObjects.
//       pdwView - Pointer to a variable which will contain an appropriate
//         view for the given object after return. May be NULL.
//  Ret: DI_OK - Success.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//       DIERR_INVALIDPARAM - Fail; Null pointer.
//       DIERR_OBJECTNOTFOUND - Fail; Given ID not found in list of objects
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::GetViewForObj( DWORD dwObjId, LPDWORD pdwView )
{
    UINT i=0; // Loop variable

    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    if( NULL == pdwView )
        return DIERR_INVALIDPARAM;

    CDIDIObject* pObject = GetObject( dwObjId );
    if( NULL == pObject )
        return DIERR_OBJECTNOTFOUND;

    // For a custom UI, this method depends on the UI being calculated
    if( m_bCustomUI && m_bInvalidUI )
        BuildCustomUI();

    // The method used to determine the best view for a particular object
    // is simple: The view which has the largest overlay rectangle probably
    // has the best view for an object. If there are no overlays for the
    // given object, use the view with the largest callout rectangle, or
    // the base view (0) if there are no callouts for the given object.
    
    DWORD dwCalloutMax = 0;
    DWORD dwCalloutIndex = 0;

    DWORD dwOverlayMax = 0;
    DWORD dwOverlayIndex = 0;

    for( i=0; i < m_dwNumViews; i++ )
    {
        DWORD dwArea = 0;

        DIDIOverlay *pOverlay = pObject->GetOverlay( i );
        DIDICallout *pCallout = pObject->GetCallout( i );

        dwArea = ( pOverlay->rcScaled.right - pOverlay->rcScaled.left ) *
                 ( pOverlay->rcScaled.bottom - pOverlay->rcScaled.top );

        if( dwArea > dwOverlayMax )
        {
            dwOverlayMax = dwArea;
            dwOverlayIndex = i;
        }      

        dwArea = ( pCallout->rcScaled.right - pCallout->rcScaled.left ) *
                 ( pCallout->rcScaled.bottom - pCallout->rcScaled.top );

        if( dwArea > dwCalloutMax )
        {
            dwCalloutMax = dwArea;
            dwCalloutIndex = i;
        }      
    }
    
    // If an overlay rectangle was found, use the overlay index; otherwise,
    // use the callout index (this will be 0 if no callouts were found ).
    *pdwView = ( dwOverlayMax > 0 ) ? dwOverlayIndex : dwCalloutIndex;
    
    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: SetOutputImageSize
// Desc: Sets the size of the image that CDIDevImage will paint and output to
//       the application
// Args: dwWidth - Preferred width.
//       dwHeight - Preferred height.
//       dwFlags - Scaling flag. Must be one of following:
//         DIDISOIS_DEFAULT - dwWidth and dwHeight values are ignored. The image
//           will not be scaled after loaded from disk; for created UIs, the
//           size is determined by the global constants. This is the default
//           value.                 
//         DIDISOIS_RESIZE - Scale the image and callouts to the given width
//           and height.
//         DIDISOIS_MAINTAINASPECTUSINGWIDTH - Scale the image and callouts to
//           the given width, but maintain the aspect ratio. The dwHeight 
//           argument is ignored
//         DIDISOIS_MAINTAINASPECTUSINGHEIGHT - Scale the image and callouts to
//           the given height, but maintain the aspect ratio. The dwWidth
//           argument is ignored.
//  Ret: DI_OK - Success.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetOutputImageSize( DWORD dwWidth, DWORD dwHeight, DWORD dwFlags )
{
    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    // Store arguments
    m_dwWidthPref   = dwWidth;
    m_dwHeightPref  = dwHeight;
    m_dwScaleMethod = dwFlags;

    // If the image size has changed, all the stored images are no longer valid
    DestroyImages();

    // This action might change the layout for the custom UI
    if( m_bCustomUI )
        m_bInvalidUI = TRUE;

    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: SetFont
// Desc: Sets the font to be used for the callout text when rendering the image
// Args: hFont - Handle to a GDI font object. The font's properties will be
//         copied and used for drawn text.
//  Ret: DI_OK - Success.
//       DIERR_INVALIDPARAM - Fail; Invalid handle.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetFont( HFONT hFont )
{
    LOGFONT logfont = {0};
    HFONT   hNewFont   = NULL;

    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;
 
    // Retrieve the logfont and create a new member font
    if( 0 == ::GetObject( hFont, sizeof(LOGFONT), &logfont ) )
        return DIERR_INVALIDPARAM;

    // Create a duplicate font
    hNewFont = CreateFontIndirect( &logfont );
    if( NULL == hNewFont )
        return E_FAIL;

    // Remove the current font
    if( m_hFont )
        DeleteObject( m_hFont );

    // Copy the font handle
    m_hFont = hNewFont;

    // This action might change the layout for the custom UI
    if( m_bCustomUI )
        m_bInvalidUI = TRUE;

    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: SetColors
// Desc: Assigns a set of colors to use when painting the various items on the
//       image
// Args: Background - Specifies the color and alpha component for the
//         image background. Device images are stored in a format which allows
//         the background to be replaced. D3D surfaces allow this background
//         to contain transparency information. Alpha values of 0 thru 254 allow
//         varying transparency effects on the output surfaces. A value
//         of 255 is treated specially during alpha blending, such that the 
//         final output image will fully opaque.
//       crColorNormal - Foreground text color for callout strings in a normal
//         state.
//       crColorHigh - Foreground text color for callout strings in a
//         highlighted state.
//  Ret: DI_OK - Success.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::SetColors( D3DCOLOR Background, COLORREF crCalloutNormal, COLORREF crCalloutHigh )
{
    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    // If the background is changing colors, the images will have to be 
    // reloaded. As an optimization, the background color is only applied
    // when the image is loaded since the background won't change as often as
    // the image is rendered.
    if( m_BkColor != Background )
        DestroyImages();
    
    m_BkColor = Background;

    for( UINT i=0; i < m_dwNumObjects; i++ )
    {
        m_apObject[i]->SetCalloutColors( crCalloutNormal, crCalloutHigh );
    }

    return DI_OK;
}




//-----------------------------------------------------------------------------
// Name: Render
// Desc: Renders an image of the device and its callouts onto a Direct3DTexture
// Args: pTexture - Pointer to a D3D Texture Object on which to paint the UI.
//  Ret: DI_OK - Success.
//       DIERR_INVALIDPARAM - Fail; Null pointer.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::Render( LPDIRECT3DTEXTURE9 pTexture )
{
    HRESULT hr = DI_OK;
    LPDIRECT3DSURFACE9 pSurface = NULL;

    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    // Check parameters
    if( NULL == pTexture )
        return DIERR_INVALIDPARAM;

    // Add a reference to the passed texture
    pTexture->AddRef();

    // Extract the surface
    hr = pTexture->GetSurfaceLevel( 0, &pSurface );
    if( FAILED(hr) )
        goto LCleanReturn;

    // Perform the render
    if( m_bCustomUI )
        hr = RenderCustomToTarget( (LPVOID) pSurface, DIDIRT_SURFACE );
    else
        hr = RenderToTarget( (LPVOID) pSurface, DIDIRT_SURFACE );


LCleanReturn:

    SAFE_RELEASE( pSurface );
    SAFE_RELEASE( pTexture );
    return hr;
}




//-----------------------------------------------------------------------------
// Name: RenderToDC
// Desc: Renders an image of the device and its callouts onto a GDI device
//       object
// Args: hDC - Handle to a device context on which to paint the UI.
//  Ret: DI_OK - Success.
//       DIERR_NOTINITIALIZED - Fail; Init() must be called first.
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::RenderToDC( HDC hDC )
{
    HRESULT hr = S_OK;

    // Verify initialization
    if( false == m_bInitialized )
        return DIERR_NOTINITIALIZED;

    if( m_bCustomUI )
        hr = RenderCustomToTarget( (LPVOID) hDC, DIDIRT_DC );
    else
        hr = RenderToTarget( (LPVOID) hDC, DIDIRT_DC );

    return hr;
}




//-----------------------------------------------------------------------------
// Name: RenderCustomToTarget
// Desc: Renders a custom UI
//-----------------------------------------------------------------------------
HRESULT CDIDevImage::RenderCustomToTarget( LPVOID pvTarget, DIDIRENDERTARGET eTarget )
{
    HRESULT    hr         = DI_OK;
    UINT       i          = 0;
    HDC        hdcRender  = NULL;
    HDC        hdcAlpha   = NULL;
    RECT       rcBitmap   = {0};
    DIBSECTION info       = {0};
    HBITMAP    hbmpRender = NULL;
    HBITMAP    hbmpAlpha  = NULL;
    SIZE       size       = {0};

    // Get the UI dimensions
    GetCustomUISize( &size );

    if( m_bInvalidUI )
        BuildCustomUI();

    // Create a background image
    BITMAPINFO bmi = {0};
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = size.cx;
    bmi.bmiHeader.biHeight = - (int) size.cy; // top-down
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    hdcRender = CreateCompatibleDC( NULL );
    if( NULL == hdcRender )
    {
        hr = DIERR_OUTOFMEMORY;
        goto LCleanReturn;
    }

    hdcAlpha = CreateCompatibleDC( NULL );
    if( NULL == hdcAlpha )
    {
        hr = DIERR_OUTOFMEMORY;
        goto LCleanReturn;
    }
    
    hbmpRender = CreateDIBSection( hdcRender, &bmi, DIB_RGB_COLORS, NULL, NULL, NULL );
    if( hbmpRender == NULL )
    {
        hr = DIERR_OUTOFMEMORY;
        goto LCleanReturn;

⌨️ 快捷键说明

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