// フレームを画面に描画します。 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()); }
// 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()); }