/// <summary> /// Ensure necessary Direct2d resources are created /// </summary> /// <returns>indicates success or failure</returns> HRESULT AudioPanel::EnsureResources() { HRESULT hr = S_OK; if (NULL == m_pRenderTarget) { // Get the panel size RECT panelRect = {0}; GetWindowRect(m_hWnd, &panelRect); UINT panelWidth = panelRect.right - panelRect.left; UINT panelHeight = panelRect.bottom - panelRect.top; D2D1_SIZE_U size = D2D1::SizeU(panelWidth, panelHeight); m_RenderTargetTransform = D2D1::Matrix3x2F::Scale(D2D1::SizeF((FLOAT)panelWidth, (FLOAT)panelWidth)); D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties(); rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE); rtProps.usage = D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE; // Create a hWnd render target, in order to render to the window set in initialize hr = m_pD2DFactory->CreateHwndRenderTarget( rtProps, D2D1::HwndRenderTargetProperties(m_hWnd, size), &m_pRenderTarget ); if (SUCCEEDED(hr)) { hr = CreateEnergyDisplay(); if (SUCCEEDED(hr)) { hr = CreateBeamGauge(); if (SUCCEEDED(hr)) { hr = CreateSourceGauge(); if (SUCCEEDED(hr)) { hr = CreatePanelOutline(); } } } } } if ( FAILED(hr) ) { DiscardResources(); } return hr; }
HRESULT ImageRenderer::EndDraw() { HRESULT hr = m_pRenderTarget->EndDraw(); // Device lost, need to recreate the render target // We'll dispose it now and retry drawing if (hr == D2DERR_RECREATE_TARGET) { hr = S_OK; DiscardResources(); } return hr; }
/// <summary> /// Draws a 32 bit per pixel image of previously specified width, height, and stride to the associated hwnd /// </summary> /// <param name="pImage">image data in RGBX format</param> /// <param name="cbImage">size of image data in bytes</param> /// <returns>indicates success or failure</returns> HRESULT ImageRenderer::Draw(BYTE* pImage, unsigned long cbImage) { // incorrectly sized image data passed in if (cbImage < ((m_sourceHeight - 1) * m_sourceStride) + (m_sourceWidth * 4)) { return E_INVALIDARG; } // create the resources for this draw device // they will be recreated if previously lost HRESULT hr = EnsureResources(); if (FAILED(hr)) { return hr; } // Copy the image that was passed in into the direct2d bitmap hr = m_pBitmap->CopyFromMemory(NULL, pImage, m_sourceStride); if (FAILED(hr)) { return hr; } m_pRenderTarget->BeginDraw(); /* bitmapSize = sizeof(&m_pBitmap); wss << bitmapSize; wss >> wstr; result = wstr.c_str(); OutputDebugString(result); OutputDebugString(L"\n"); */ // Draw the bitmap stretched to the size of the window m_pRenderTarget->DrawBitmap(m_pBitmap); hr = m_pRenderTarget->EndDraw(); // Device lost, need to recreate the render target // We'll dispose it now and retry drawing if (hr == D2DERR_RECREATE_TARGET) { hr = S_OK; DiscardResources(); } return hr; }
/// <summary> /// Draws a 32 bit per pixel image of previously specified width, height, and stride to the associated hwnd /// </summary> /// <param name="pImage">image data in RGBX format</param> /// <param name="cbImage">size of image data in bytes</param> /// <param name="vBodies">vector of bodies to draw</param> /// <returns>indicates success or failure</returns> HRESULT ImageRenderer::Draw(BYTE* pImage, unsigned long cbImage, std::vector<Body> &vBodies) { // incorrectly sized image data passed in if (cbImage < ((m_sourceHeight - 1) * m_sourceStride) + (m_sourceWidth * 4)) { return E_INVALIDARG; } // create the resources for this draw device // they will be recreated if previously lost HRESULT hr = EnsureResources(); if (FAILED(hr)) { return hr; } // Copy the image that was passed in into the direct2d bitmap hr = m_pBitmap->CopyFromMemory(NULL, pImage, m_sourceStride); if (FAILED(hr)) { return hr; } m_pRenderTarget->BeginDraw(); // Draw the bitmap stretched to the size of the window m_pRenderTarget->DrawBitmap(m_pBitmap); for (unsigned int i = 0; i < vBodies.size(); i++) { if (vBodies[i].bTracked) { DrawBody(vBodies[i]); } } hr = m_pRenderTarget->EndDraw(); // Device lost, need to recreate the render target // We'll dispose it now and retry drawing if (hr == D2DERR_RECREATE_TARGET) { hr = S_OK; DiscardResources(); } return hr; }
/// <summary> /// Draws a 32 bit per pixel image of previously specified width, height, and stride to the associated hwnd /// </summary> /// <param name="pImage">image data in RGBX format</param> /// <param name="cbImage">size of image data in bytes</param> /// <returns>true if successful, false otherwise</returns> bool PhotoViewer::Draw( BYTE * pImage, unsigned long cbImage ) { // incorrectly sized image data passed in if ( cbImage < ((m_sourceHeight - 1) * m_sourceStride) + (m_sourceWidth * 4) ) { return false; } // create the resources for this draw device // they will be recreated if previously lost HRESULT hr = EnsureResources( ); if ( FAILED( hr ) ) { return false; } // Copy the image that was passed in into the direct2d bitmap hr = m_pBitmap->CopyFromMemory( NULL, pImage, m_sourceStride ); if ( FAILED( hr ) ) { return false; } m_pRenderTarget->BeginDraw(); // Draw the bitmap stretched to the size of the window m_pRenderTarget->DrawBitmap( m_pBitmap ); hr = m_pRenderTarget->EndDraw(); // Device lost, need to recreate the render target // We'll dispose it now and retry drawing if ( hr == D2DERR_RECREATE_TARGET ) { hr = S_OK; DiscardResources(); } return SUCCEEDED( hr ); }
/// <summary> /// Draws audio panel. /// </summary> /// <returns>S_OK on success, otherwise failure code.</returns> HRESULT AudioPanel::Draw() { // create the resources for this draw device. They will be recreated if previously lost. HRESULT hr = EnsureResources(); if ( FAILED(hr) ) { return hr; } m_pRenderTarget->BeginDraw(); m_pRenderTarget->SetTransform(m_RenderTargetTransform); m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); // Draw energy display m_pRenderTarget->DrawBitmap(m_pEnergyDisplay, m_EnergyDisplayPosition); // Draw audio beam gauge m_pRenderTarget->FillGeometry(m_pBeamGauge, m_pBeamGaugeFill, NULL); m_pRenderTarget->SetTransform(m_BeamNeedleTransform * m_RenderTargetTransform); m_pRenderTarget->FillGeometry(m_pBeamNeedle, m_pBeamNeedleFill, NULL); m_pRenderTarget->SetTransform(m_RenderTargetTransform); // Draw sound source gauge m_pSourceGaugeFill->SetTransform(m_SourceGaugeTransform); m_pRenderTarget->FillGeometry(m_pSourceGauge, m_pSourceGaugeFill, NULL); // Draw panel outline m_pRenderTarget->DrawGeometry(m_pPanelOutline, m_pPanelOutlineStroke, 0.001f); hr = m_pRenderTarget->EndDraw(); // Device lost, need to recreate the render target. We'll dispose it now and retry drawing. if (hr == D2DERR_RECREATE_TARGET) { hr = S_OK; DiscardResources(); } return hr; }
/// <summary> /// Destructor /// </summary> PhotoViewer::~PhotoViewer() { DiscardResources(); SafeRelease(m_pD2DFactory); }
/// <summary> /// Destructor /// </summary> ImageRenderer::~ImageRenderer() { DiscardResources(); SafeRelease(m_pD2DFactory); }
/// <summary> /// Destructor /// </summary> AudioPanel::~AudioPanel() { DiscardResources(); SafeRelease(m_pD2DFactory); }
//------------------------------------------------------------------- // Destructor //------------------------------------------------------------------- DrawDevice::~DrawDevice() { DiscardResources(); SafeRelease(m_pD2DFactory); }