這樣我們就得到了需要計算的區(qū)域的信息??梢酝ㄟ^查詢對象的HasMoved屬性來判斷它是否發(fā)生了移動。這些對象移入的區(qū)域可以通過計算每個對象當前的繪制矩形來得到;對象通過GetRenderRectangle函數(shù)來返回該矩形。如果我們使用CombineRectangles函數(shù),那么可以將所有這些矩形合并為一個大的繪制矩形。然后就形成了所有移動對象的移入?yún)^(qū)域。
要得到對象移出區(qū)域也很容易。我們只需要將每個對象的PreviousRenderRect對象進行合并,就如同掃描它們一樣。得到這些信息后,將對象的當前繪制矩形復(fù)制到先前的繪制矩形中,為下一次獲取該信息做好準備。
所有這些操作都由FindCurrentRenderRectangle函數(shù)處理,如程序清單4-17所示。
程序清單4-17 查找需要在其中進行繪制的矩形
/// <summary>
/// Calculate the bounds of the rectangle inside which all of the moving
/// objects reside.
/// </summary>
/// <returns></returns>
private Rectangle FindCurrentRenderRectangle()
{
Rectangle renderRect = new Rectangle();
Rectangle objectRenderRect;
// Loop through all items, combining the positions of those that have
// moved or created into the render rectangle
foreach (CGameObjectGDIBase gameObj in GameObjects)
{
// Has this object been moved (or created) since the last update?
gameObj.CheckIfMoved();
if (gameObj.HasMoved)
{
// The object has moved so we need to add the its rectangle to the
// render rectangle. Retrieve its current rectangle
objectRenderRect = gameObj.GetRenderRectangle();
// Add to the overall rectangle
renderRect = CGameFunctions.CombineRectangles(renderRect, objectRenderRect);
// Include the object's previous rectangle too.
// (We can't rely on its LastX/Y position as it may have been updated
// multiple times since the last render)
renderRect = CGameFunctions.CombineRectangles(renderRect,
gameObj.PreviousRenderRect);
// Store the current render rectangle into the object as its
// previous rectangle for the next call.
gameObj.PreviousRenderRect = objectRenderRect;
// Clear the HasMoved flag now that we have observed this object's
// movement
gameObj.HasMoved = false;
}
// This object has now been processed so it is no longer new
gameObj.IsNew = false;
}
return renderRect;
}