in cpp/VirtualSurfaces/VirtualSurfaces/DirectXTileRenderer.cpp [51:102]
bool DirectXTileRenderer::DrawTileRange(Rect rect, std::list<Tile> const& tiles)
{
SIZE updateSize = { static_cast<LONG>(rect.Width - 5), static_cast<LONG>(rect.Height - 5) };
//making sure the update rect doesnt go past the maximum size of the surface.
RECT updateRect = { static_cast<LONG>(rect.X), static_cast<LONG>(rect.Y), static_cast<LONG>(min((rect.X + rect.Width),m_surfaceSize)), static_cast<LONG>(min((rect.Y + rect.Height),m_surfaceSize)) };
//Cannot update a surface larger than the max texture size of the hardware. 2048X2048 is the lowest max texture size for relevant hardware.
int MAXTEXTURESIZE = D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION;
//3 is the buffer here.
SIZE constrainedUpdateSize = { min(updateSize.cx, MAXTEXTURESIZE-3), min(updateSize.cy, MAXTEXTURESIZE-3) };
float savedColorCounter = m_colorCounter;
//Breaking the BeginDraw/EndDraw calls to update rects that dont exceed the max texture size.
for (LONG y = updateRect.top; y < updateRect.bottom; y += constrainedUpdateSize.cy)
{
for (LONG x = updateRect.left; x < updateRect.right; x += constrainedUpdateSize.cx)
{
m_colorCounter = savedColorCounter;
POINT offset{};
RECT constrainedUpdateRect = RECT{ x, y, min(x + constrainedUpdateSize.cx, updateRect.right), min(y + constrainedUpdateSize.cy, updateRect.bottom) };
com_ptr<ID2D1DeviceContext> d2dDeviceContext;
com_ptr<ID2D1SolidColorBrush> textBrush;
com_ptr<ID2D1SolidColorBrush> tileBrush;
// Begin our update of the surface pixels. Passing nullptr to this call will update the entire surface. We only update the rect area that needs to be rendered.
if (!CheckForDeviceRemoved(m_surfaceInterop->BeginDraw(&constrainedUpdateRect, __uuidof(ID2D1DeviceContext), (void **)d2dDeviceContext.put(), &offset)))
{
return false;
}
d2dDeviceContext->Clear(D2D1::ColorF(D2D1::ColorF::Red, 0.f));
// Create a solid color brush for the text. Half alpha to make it more visually pleasing as it blends with the background color.
check_hresult(d2dDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::DimGray, 0.5f), textBrush.put()));
//Create a solid color brush for the tiles and which will be set to a different color before rendering.
check_hresult(d2dDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Green, 1.0f), tileBrush.put()));
//Get the offset difference that can be applied to every tile before drawing.
POINT differenceOffset{ (LONG)(offset.x - x), (LONG)(offset.y - y) };
//Iterate through the tiles and do DrawRectangle and DrawText calls on those.
for (Tile tile : tiles) {
DrawTile(d2dDeviceContext.get(), textBrush.get(), tileBrush.get(), tile, differenceOffset);
}
m_surfaceInterop->EndDraw();
}
}
return true;
}