bool DirectXTileRenderer::DrawTile()

in cpp/AdvancedColorImages/AdvancedColorImages/DirectXTileRenderer.cpp [57:108]


bool DirectXTileRenderer::DrawTile(Rect rect)
{
	//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)) };
	SIZE updateSize = { updateRect.right - updateRect.left, updateRect.bottom - updateRect.top };

	//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> 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 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()));

			// Set a transform to draw into this section of the virtual surface using the input coordate space
			d2dDeviceContext->SetTransform(D2D1::Matrix3x2F::Translation((FLOAT)(offset.x - x), (FLOAT)(offset.y - y)));

			D2D1_RECT_F d2dRect = { constrainedUpdateRect.left, constrainedUpdateRect.top, constrainedUpdateRect.right, constrainedUpdateRect.bottom };

			d2dDeviceContext->PushAxisAlignedClip(d2dRect, D2D1_ANTIALIAS_MODE_ALIASED);
			d2dDeviceContext->DrawImage(m_finalOutput.get());
			d2dDeviceContext->PopAxisAlignedClip();

			d2dDeviceContext->DrawRectangle(d2dRect, tileBrush.get(), 3.0f);

			m_surfaceInterop->EndDraw();
		}
	}

	return true;
}