// フレームを画面に描画します。 void Direct2DRenderer::Render(INT16* waveBuffer,int length,ID2D1Bitmap1* targetBitmap) { if (!targetBitmap) return; ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); context->SetTarget(targetBitmap); Windows::Foundation::Size logicalSize = { (float)targetBitmap->GetPixelSize().width, (float)targetBitmap->GetPixelSize().height};//m_deviceResources->GetLogicalSize(); context->SaveDrawingState(m_stateBlock.Get()); context->BeginDraw(); // 右下隅に配置 D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation( 0.0f, logicalSize.Height); // logicalSize.Height - m_textMetrics.layoutHeight // ); //D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation(0.0f,0.0f); screenTranslation._22 = screenTranslation._22 * -1.0f; context->SetTransform(screenTranslation * m_deviceResources->GetOrientationTransform2D()); context->DrawTextLayout( D2D1::Point2F(0.f, 0.f), m_textLayout.Get(), m_whiteBrush.Get() ); // 波形データを表示する const float delta = 1323.0f / VIDEO_WIDTH; for (float i = 0; i < VIDEO_WIDTH; i += delta){ int pos = (int) i; if (pos >= length) break; float left = ((float) waveBuffer[pos]) / 32768.0f * 150.0f + 180.0f; float right = ((float) waveBuffer[pos + 1]) / 32768.0f * 150.0f + 540.0f; context->DrawLine(D2D1::Point2F(i, 180.0f), D2D1::Point2F(i, left), m_whiteBrush.Get()); context->DrawLine(D2D1::Point2F(i, 540.0f), D2D1::Point2F(i, right), m_whiteBrush.Get()); } // D2DERR_RECREATE_TARGET をここで無視します。このエラーは、デバイスが失われたことを示します。 // これは、Present に対する次回の呼び出し中に処理されます。 HRESULT hr = context->EndDraw(); if (hr != D2DERR_RECREATE_TARGET) { DX::ThrowIfFailed(hr); } context->RestoreDrawingState(m_stateBlock.Get()); }
void Sample2DSceneRenderer::Render() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); // Dash array for dashStyle D2D1_DASH_STYLE_CUSTOM float dashes[] = { 1.0f, 2.0f, 2.0f, 3.0f, 2.0f, 2.0f }; ID2D1StrokeStyle* m_stroke; m_deviceResources->GetD2DFactory()->CreateStrokeStyle( D2D1::StrokeStyleProperties( D2D1_CAP_STYLE_FLAT, D2D1_CAP_STYLE_FLAT, D2D1_CAP_STYLE_ROUND, D2D1_LINE_JOIN_MITER, 10.0f, D2D1_DASH_STYLE_CUSTOM, 0.0f), dashes, ARRAYSIZE(dashes), &m_stroke ); context->BeginDraw(); //context->Clear(D2D1::ColorF(D2D1::ColorF::CornflowerBlue)); auto m_bitmapSize = m_pBitmap->GetSize(); context->DrawBitmap( m_pBitmap.Get(), D2D1::RectF(200.0f, padding, m_bitmapSize.width + 200.0f, m_bitmapSize.height + padding) ); context->DrawRectangle( D2D1::RectF(100.0f, 100.0f, 500.0f, 500.0f), m_pBlueBrush.Get(), 4.0f, m_stroke ); context->FillRectangle( D2D1::RectF(600.0f, 400.0f, 900.0f, 700.0f), m_pLinearGradientBrush.Get() ); context->DrawLine( D2D1::Point2F(200, 300), D2D1::Point2F(800, 500), m_pBlueBrush.Get(), 10.0f, m_stroke ); DX::ThrowIfFailed( context->EndDraw() ); }
void QWindowsDirect2DWindow::flush(QWindowsDirect2DBitmap *bitmap, const QRegion ®ion, const QPoint &offset) { QSize size; if (m_directRendering) { DXGI_SWAP_CHAIN_DESC1 desc; HRESULT hr = m_swapChain->GetDesc1(&desc); QRect geom = geometry(); if ((FAILED(hr) || (desc.Width != geom.width()) || (desc.Height != geom.height()))) { resizeSwapChain(geom.size()); m_swapChain->GetDesc1(&desc); } size.setWidth(desc.Width); size.setHeight(desc.Height); } else { size = geometry().size(); } setupBitmap(); if (!m_bitmap) return; if (bitmap != m_bitmap.data()) { m_bitmap->deviceContext()->begin(); ID2D1DeviceContext *dc = m_bitmap->deviceContext()->get(); if (!m_needsFullFlush) { QRegion clipped = region; clipped &= QRect(QPoint(), size); foreach (const QRect &rect, clipped.rects()) { QRectF rectF(rect); dc->DrawBitmap(bitmap->bitmap(), to_d2d_rect_f(rectF), 1.0, D2D1_INTERPOLATION_MODE_LINEAR, to_d2d_rect_f(rectF.translated(offset.x(), offset.y()))); } } else {
void Sample2DSceneRenderer::CreateDeviceDependentResources() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); DX::ThrowIfFailed( context->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Blue), &m_pBlueBrush) ); // Create an array of gradient stops to put in the gradient stop // collection that will be used in the gradient brush. ID2D1GradientStopCollection *pGradientStops = NULL; D2D1_GRADIENT_STOP gradientStops[2]; gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Yellow, 1); gradientStops[0].position = 0.0f; gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::ForestGreen, 1); gradientStops[1].position = 1.0f; // Create the ID2D1GradientStopCollection from a previously // declared array of D2D1_GRADIENT_STOP structs. context->CreateGradientStopCollection( gradientStops, 2, D2D1_GAMMA_2_2, D2D1_EXTEND_MODE_CLAMP, &pGradientStops ); DX::ThrowIfFailed( context->CreateLinearGradientBrush( D2D1::LinearGradientBrushProperties(D2D1::Point2F(600, 400), D2D1::Point2F(900, 700)), pGradientStops, &m_pLinearGradientBrush ) ); DX::ThrowIfFailed(LoadBitmapFromFile(m_deviceResources.get(), L"Images/drop.png", &m_pBitmap)); padding = 0; }
// Renders a frame to the screen. void SampleFpsTextRenderer::Render() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); RECT windowSize = m_deviceResources->GetWindowSize(); UINT width = windowSize.right - windowSize.left; UINT height = windowSize.bottom - windowSize.top; context->SaveDrawingState(m_stateBlock.Get()); context->BeginDraw(); // Position on the bottom right corner D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation( width - m_textMetrics.layoutWidth, height - m_textMetrics.height ); context->SetTransform(screenTranslation); DX::ThrowIfFailed( m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING) ); context->DrawTextLayout( D2D1::Point2F(0.f, 0.f), m_textLayout.Get(), m_whiteBrush.Get() ); // Ignore D2DERR_RECREATE_TARGET here. This error indicates that the device // is lost. It will be handled during the next call to Present. HRESULT hr = context->EndDraw(); if (hr != D2DERR_RECREATE_TARGET) { DX::ThrowIfFailed(hr); } context->RestoreDrawingState(m_stateBlock.Get()); }
// 将帧呈现到屏幕。 void SampleFpsTextRenderer::Render() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); Windows::Foundation::Size logicalSize = m_deviceResources->GetLogicalSize(); context->SaveDrawingState(m_stateBlock.Get()); context->BeginDraw(); // 定位在右下角 D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation( logicalSize.Width - m_textMetrics.layoutWidth, logicalSize.Height - m_textMetrics.height ); context->SetTransform(screenTranslation * m_deviceResources->GetOrientationTransform2D()); DX::ThrowIfFailed( m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING) ); context->DrawTextLayout( D2D1::Point2F(0.f, 0.f), m_textLayout.Get(), m_whiteBrush.Get() ); // 此处忽略 D2DERR_RECREATE_TARGET。此错误指示该设备 // 丢失。将在下一次调用 Present 时对其进行处理。 HRESULT hr = context->EndDraw(); if (hr != D2DERR_RECREATE_TARGET) { DX::ThrowIfFailed(hr); } context->RestoreDrawingState(m_stateBlock.Get()); }
// Renders a frame to the screen. void SampleDebugTextRenderer::Render() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); Windows::Foundation::Size logicalSize = m_deviceResources->GetLogicalSize(); context->SaveDrawingState(m_stateBlock.Get()); context->BeginDraw(); // Position the controllers in quadrants. for (unsigned int i = 0; i < XINPUT_MAX_CONTROLLERS; i++) { unsigned int playerAttached = (m_playersAttached & (1 << i)); if (!playerAttached) continue; float x = logicalSize.Width; float y = logicalSize.Height - 50.f; // size of FPS text x -= (i % 2) ? m_textMetrics[i].layoutWidth : (2 * m_textMetrics[i].layoutWidth); y -= (i > 1) ? m_inputTextHeight : (2 * m_inputTextHeight); // Translate the origin (used to position in different quadrants) D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation(x, y); context->SetTransform(screenTranslation * m_deviceResources->GetOrientationTransform2D()); DX::ThrowIfFailed( m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING) ); context->DrawTextLayout( D2D1::Point2F(0.f, 0.f), m_textLayout[i].Get(), m_whiteBrush.Get() ); } // Position FPS on the bottom right corner. D2D1::Matrix3x2F screenTranslation = D2D1::Matrix3x2F::Translation( logicalSize.Width - m_textMetricsFPS.layoutWidth, logicalSize.Height - m_textMetricsFPS.height ); context->SetTransform(screenTranslation * m_deviceResources->GetOrientationTransform2D()); DX::ThrowIfFailed( m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING) ); context->DrawTextLayout( D2D1::Point2F(0.f, 0.f), m_textLayoutFPS.Get(), m_whiteBrush.Get() ); // Ignore D2DERR_RECREATE_TARGET here. This error indicates that the device // is lost. It will be handled during the next call to Present. HRESULT hr = context->EndDraw(); if (hr != D2DERR_RECREATE_TARGET) { DX::ThrowIfFailed(hr); } context->RestoreDrawingState(m_stateBlock.Get()); }
// Renders a frame to the screen. void SampleVirtualControllerRenderer::Render() { ID2D1DeviceContext* context = m_deviceResources->GetD2DDeviceContext(); context->SaveDrawingState(m_stateBlock.Get()); context->BeginDraw(); std::unordered_map<PLAYER_ACTION_TYPES, TouchControl>::iterator iter; for (iter = m_touchControls.begin(); iter != m_touchControls.end(); ++iter) { PLAYER_ACTION_TYPES player_action = iter->first; TouchControl touchControl = iter->second; TouchControlRegion touchControlRegion = touchControl.Region; switch (touchControlRegion.RegionType) { case TOUCH_CONTROL_REGION_TYPES::TOUCH_CONTROL_REGION_ANALOG_STICK: { // Draw a virtual analog stick D2D1_ELLIPSE outerStickEllipse; if (touchControl.PointerRawX > 0) { // If there is analog stick input, center on the raw location. outerStickEllipse = D2D1::Ellipse(D2D1::Point2F(touchControl.PointerRawX, touchControl.PointerRawY), 100, 100); } else { // If there is no analog stick input, center on the last throw location. outerStickEllipse = D2D1::Ellipse(D2D1::Point2F(touchControl.PointerThrowX, touchControl.PointerThrowY), 100, 100); } D2D1_ELLIPSE innerStickEllipse = D2D1::Ellipse(D2D1::Point2F(touchControl.PointerThrowX, touchControl.PointerThrowY), 75, 75); // Get opacity based on the time since the user has used the virtual analog stick. float opacity = m_stickFadeTimer / 0.25f; // save current opacity. float previousOpacity = m_whiteBrush->GetOpacity(); m_whiteBrush->SetOpacity(opacity); context->DrawEllipse(outerStickEllipse, m_whiteBrush.Get()); context->FillEllipse(innerStickEllipse, m_whiteBrush.Get()); m_whiteBrush->SetOpacity(previousOpacity); } break; case TOUCH_CONTROL_REGION_TYPES::TOUCH_CONTROL_REGION_BUTTON: { // Draw a button. float radiusx = (touchControlRegion.LowerRightCoords.x - touchControlRegion.UpperLeftCoords.x) * 0.5f; float radiusy = (touchControlRegion.LowerRightCoords.y - touchControlRegion.UpperLeftCoords.y) * 0.5f; D2D1_POINT_2F buttonLoc = D2D1::Point2F( touchControlRegion.UpperLeftCoords.x + radiusx, touchControlRegion.UpperLeftCoords.y + radiusy ); D2D1_ELLIPSE buttonEllipse = D2D1::Ellipse(buttonLoc, radiusx, radiusy); // Get opacity based on the time since the user has used the touch screen. float opacity = m_buttonFadeTimer > 3.f ? 1.f : m_buttonFadeTimer / 3.f; // Save the opacity. float previousOpacity = m_whiteBrush->GetOpacity(); m_whiteBrush->SetOpacity(opacity); if (touchControl.ButtonPressed) { context->FillEllipse(buttonEllipse, m_whiteBrush.Get()); } else { context->DrawEllipse(buttonEllipse, m_whiteBrush.Get()); } // Set opacity to it's previous value. m_whiteBrush->SetOpacity(previousOpacity); } break; default: break; } } // Ignore D2DERR_RECREATE_TARGET here. This error indicates that the device // is lost. It will be handled during the next call to Present. HRESULT hr = context->EndDraw(); if (hr != D2DERR_RECREATE_TARGET) { DX::ThrowIfFailed(hr); } context->RestoreDrawingState(m_stateBlock.Get()); }