BitmapWrapperD2D::BitmapWrapperD2D(bitmap bmp) { IWICBitmap* wic = 0; Graphics::locator.getfactory()->CreateBitmapFromMemory( bmp.width(), bmp.height(), GUID_WICPixelFormat32bppBGRA, 4 * bmp.width(), bmp.length(), (BYTE*)bmp.data(), &wic); if (wic != 0) { IWICFormatConverter* converter = nullptr; IWICBitmap* temp = nullptr; int result = Graphics::locator.getfactory()->CreateFormatConverter(&converter); if (result == 0) { converter->Initialize(wic, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, 0, 0.f, WICBitmapPaletteTypeMedianCut); Graphics::locator.getfactory()->CreateBitmapFromSource(converter, WICBitmapNoCache, &temp); converter->Release(); } wic->Release(); ID2D1BitmapRenderTarget* target = Graphics::locator.gettarget(); if (target) { target->CreateBitmapFromWicBitmap(temp, &source); temp->Release(); temp = nullptr; } else { source = nullptr; } } else { source = nullptr; } }
void BitmapWrapperD2D::draw(rectangle2d<int32_t> rect, float_t xscale, float_t yscale, vector2d<int32_t> center, float_t alpha) const { ID2D1BitmapRenderTarget* target = Graphics::locator.gettarget(); if (target) { bool transform = (xscale != 1.0f) || (yscale != 1.0f); if (transform) { target->SetTransform( D2D1::Matrix3x2F::Scale( D2D1::Size(xscale, yscale), D2D1::Point2F( static_cast<FLOAT>(center.x()), static_cast<FLOAT>(center.y()) ))); } if (source) { D2D_RECT_F r = D2D1::RectF((FLOAT)rect.l(), (FLOAT)rect.t(), (FLOAT)rect.r(), (FLOAT)rect.b()); target->DrawBitmap(source, r, alpha); } if (transform) { target->SetTransform( D2D1::Matrix3x2F::Scale( D2D1::Size(1.0f, 1.0f), D2D1::Point2F( static_cast<FLOAT>(center.x()), static_cast<FLOAT>(center.y()) ))); } } }
// Create a pattern brush HRESULT DemoApp::CreateGridPatternBrush( ID2D1RenderTarget *pRenderTarget, ID2D1BitmapBrush **ppBitmapBrush) { HRESULT hr = S_OK; // Create a compaitble render target ID2D1BitmapRenderTarget *pCompatibleRenderTarget = NULL; hr = pRenderTarget->CreateCompatibleRenderTarget(D2D1::SizeF(10.f, 10.f), &pCompatibleRenderTarget); if (SUCCEEDED(hr)) { // Draw a pattern ID2D1SolidColorBrush *pGridBrush = NULL; hr = pCompatibleRenderTarget->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF(0.93f, 0.94f, 0.96f, 1.0f)), &pGridBrush); if (SUCCEEDED(hr)) { pCompatibleRenderTarget->BeginDraw(); pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.0f, 10.0f, 1.0f), pGridBrush); pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.1f, 1.0f, 10.0f), pGridBrush); pCompatibleRenderTarget->EndDraw(); // Retrieve the bitmap from the render target ID2D1Bitmap *pGridBitmap = NULL; hr = pCompatibleRenderTarget->GetBitmap(&pGridBitmap); if (SUCCEEDED(hr)) { // Choose the filling mode for the bitmap brush D2D1_BITMAP_BRUSH_PROPERTIES brushProperties = D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP); // Create the bitmap brush hr = m_pRenderTarget->CreateBitmapBrush(pGridBitmap, brushProperties, ppBitmapBrush); pGridBitmap->Release(); } pGridBrush->Release(); } pCompatibleRenderTarget->Release(); } return hr; }
// this imitates http://blogs.msdn.com/b/wpfsdk/archive/2006/10/26/uncommon-dialogs--font-chooser-and-color-picker-dialogs.aspx static void drawGrid(ID2D1RenderTarget *rt, D2D1_RECT_F *fillRect) { D2D1_SIZE_F size; D2D1_PIXEL_FORMAT pformat; ID2D1BitmapRenderTarget *brt; D2D1_COLOR_F color; D2D1_BRUSH_PROPERTIES bprop; ID2D1SolidColorBrush *brush; D2D1_RECT_F rect; ID2D1Bitmap *bitmap; D2D1_BITMAP_BRUSH_PROPERTIES bbp; ID2D1BitmapBrush *bb; HRESULT hr; // mind the divisions; they represent the fact the original uses a viewport size.width = 100 / 10; size.height = 100 / 10; // yay more ABI bugs pformat = rt->GetPixelFormat(); hr = rt->CreateCompatibleRenderTarget(&size, NULL, &pformat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, &brt); if (hr != S_OK) logHRESULT(L"error creating render target for grid", hr); brt->BeginDraw(); color.r = 1.0; color.g = 1.0; color.b = 1.0; color.a = 1.0; brt->Clear(&color); color = D2D1::ColorF(D2D1::ColorF::LightGray, 1.0); ZeroMemory(&bprop, sizeof (D2D1_BRUSH_PROPERTIES)); bprop.opacity = 1.0; bprop.transform._11 = 1; bprop.transform._22 = 1; hr = brt->CreateSolidColorBrush(&color, &bprop, &brush); if (hr != S_OK) logHRESULT(L"error creating brush for grid", hr); rect.left = 0; rect.top = 0; rect.right = 50 / 10; rect.bottom = 50 / 10; brt->FillRectangle(&rect, brush); rect.left = 50 / 10; rect.top = 50 / 10; rect.right = 100 / 10; rect.bottom = 100 / 10; brt->FillRectangle(&rect, brush); brush->Release(); hr = brt->EndDraw(NULL, NULL); if (hr != S_OK) logHRESULT(L"error finalizing render target for grid", hr); hr = brt->GetBitmap(&bitmap); if (hr != S_OK) logHRESULT(L"error getting bitmap for grid", hr); brt->Release(); ZeroMemory(&bbp, sizeof (D2D1_BITMAP_BRUSH_PROPERTIES)); bbp.extendModeX = D2D1_EXTEND_MODE_WRAP; bbp.extendModeY = D2D1_EXTEND_MODE_WRAP; bbp.interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR; hr = rt->CreateBitmapBrush(bitmap, &bbp, &bprop, &bb); if (hr != S_OK) logHRESULT(L"error creating bitmap brush for grid", hr); rt->FillRectangle(fillRect, bb); bb->Release(); bitmap->Release(); }