static void PositionSprite(List<ArrangedSprite> p_sprites, int p_index, int p_outputWidth)
{
int l_x = 0;
int l_y = 0;
while (true) {
// Is this position free for us to use?
int intersects = FindIntersectingSprite(p_sprites, p_index, l_x, l_y);
if (intersects < 0) {
p_sprites[p_index].X = l_x;
p_sprites[p_index].Y = l_y;
return;
}
// Skip past the existing sprite that we collided with.
l_x = p_sprites[intersects].X + p_sprites[intersects].Width;
// If we ran out of room to move to the right,
// try the next line down instead.
if (l_x + p_sprites[p_index].Width > p_outputWidth) {
l_x = 0;
l_y++;
}
}
}