Мне кажется, я в корне не понимаю, как работают цели. В моем понимании RenderTargets - это просто текстуры, на которые рисует вызовы spritebatch.
Итак, я попробовал этот код для рендеринга окон графического интерфейса, чтобы убедиться, что они могут рисовать только в своей клиентской области, и она обрезается за пределами этого.
for (int i = Controls.Count - 1; i >= 0; i--)
{
RenderTarget2D oldTarget;
if (graphics.GetRenderTargets().Count() == 0) oldTarget = null;
else oldTarget = (RenderTarget2D)graphics.GetRenderTargets()[0].RenderTarget; // Get the old target being used.
graphics.SetRenderTarget(canvas); //set the target to a temporary RT
graphics.Clear(Color.Black); // Clear it
Control c = Controls[i]; // Get the current control (a form in this case)
c.Draw(spriteBatch, gameTime); // Draw it to the temp RT
graphics.SetRenderTarget(oldTarget); // Set the RT back to the main RT
Vector2 dest = c.DrawCoOrds(); // Gets the draw coordinates of the control
spriteBatch.Begin();
spriteBatch.Draw(canvas, new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), Color.White);
// take the rect from the temp RT and draw it to the main RT.
spriteBatch.End();
}
Однако этот код рисует только последнюю форму в списке, что означает, что он должен каким-то образом очищать основной RT, но я не понимаю, почему. Я называю clear только тогда, когда RT настроен на временную канву.