Reputation: 35
I'm trying to get a bitmap to tile something like this:
---- ---- ----
| || || |
---- ---- ----
doing the following as an example:
CD2DBitmapBrush* m_pBitBrush;
m_pBitBrush = new CD2DBitmapBrush(GetRenderTarget(), _T("grass.bmp"));
POINT pt;
pt.x = 0;
pt.y = 0;
CRect rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());
pRenderTarget->FillRectangle(rect, m_pBitBrush);
for(int i = 0; i < 5; i++)
{
pt.x += 40;
rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());
pRenderTarget->FillRectangle(rect, m_pBitBrush);
}
When I do that, the bitmap displays correctly once, but every instance after that is "stretched" (i.e. the last column of pixels is repeated, but not the rest of the image).
If I change x, the right-most column is repeated. If I change y, the bottom row is repeated. And if i change both x and y (going diagonally), the corner pixel fills the rectangle.
x:
---- -----------
| |||||||||||||
---- -----------
y: x and y:
---- *----*
| | | |
---- *----*
---- ******
---- ******
---- ******
Also, I have tried changing the image that the brush uses after it is called the first time, and nothing displays after the original.
CRect rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());
pRenderTarget->FillRectangle(rect, m_pBitBrush);
CD2DBitmap* bit = new CD2DBitmap(GetRenderTarget()/*pRenderTarget*/, _T("stone.bmp"));
m_pBitBrush->SetBitmap(bit);
for(int i = 0; i < 5; i++)
{
pt.x += 40;
rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());
pRenderTarget->FillRectangle(rect, m_pBitBrush);
}
Surely, I am doing something wrong here since I am new, but I cannot seem to get this to work. Can anyone point me in the right direction?
Upvotes: 3
Views: 655
Reputation: 4244
You want to use SetExtendModeX(D2D1_EXTEND_MODE mode)
and/or SetExtendModeY(D2D1_EXTEND_MODE mode)
on your CD2DBitmapBrush
. The default seems to be D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_CLAMP
, which causes the stretching. You want D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_WRAP
.
Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368100(v=vs.85).aspx
Upvotes: 2