这样我们就得到了需要计算的区域的信息。可以通过查询对象的HasMoved属性来判断它是否发生了移动。这些对象移入的区域可以通过计算每个对象当前的绘制矩形来得到;对象通过GetRenderRectangle函数来返回该矩形。如果我们使用CombineRectangles函数,那么可以将所有这些矩形合并为一个大的绘制矩形。然后就形成了所有移动对象的移入区域。
要得到对象移出区域也很容易。我们只需要将每个对象的PreviousRenderRect对象进行合并,就如同扫描它们一样。得到这些信息后,将对象的当前绘制矩形复制到先前的绘制矩形中,为下一次获取该信息做好准备。
所有这些操作都由FindCurrentRenderRectangle函数处理,如程序清单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;
}