我正在使用 .NET Framework 4 在 C# 中进行编程,并打算使用 XNA 制作基于图块的游戏。我有一个大纹理(256 像素 x 4096 像素)。请记住,这是一个基于图块的游戏,所以这个纹理之所以如此庞大只是因为它包含许多图块,每个图块都是 32 像素 x 32 像素。我想专家们肯定会知道基于图块的游戏是什么样的。方向是正交的(如棋盘),而不是等距的。
在 Game.Draw() 方法中,我有两个选择,其中一个会比另一个更有效率。
选择/方法#1:
半伪代码:
public void Draw()
{
// map tiles are drawn left-to-right, top-to-bottom
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
SpriteBatch.Draw(
MyLargeTexture, // One large 256 x 4096 texture
new Rectangle(x, y, 32, 32), // Destination rectangle - ignore this, its ok
new Rectangle(x, y, 32, 32), // Notice the source rectangle 'cuts out' 32 by 32 squares from the texture corresponding to the loop
Color.White); // No tint - ignore this, its ok
}
}
}
说明: 因此,有效地,第一种方法是多次引用一个大纹理,每次使用这个大纹理的一个小矩形来绘制适当的平铺图像。
选择/方法#2:
半伪代码:
public void Draw()
{
// map tiles are drawn left-to-right, top-to-bottom
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
Texture2D tileTexture = map.GetTileTexture(x, y); // Getting a small 32 by 32 texture (different each iteration of the loop)
SpriteBatch.Draw(
tileTexture,
new Rectangle(x, y, 32, 32), // Destination rectangle - ignore this, its ok
new Rectangle(0, 0, tileTexture.Width, tileTexture.Height), // Notice the source rectangle uses the entire texture, because the entire texture IS 32 by 32
Color.White); // No tint - ignore this, its ok
}
}
}
说明: 所以,实际上,第二种方法是多次绘制许多小纹理。
问题:使用哪种方法,为什么?就个人而言,我认为使用第一种方法会更加有效。如果您考虑一下这对地图中的瓦片数组意味着什么(例如,考虑具有 2000 x 2000 个瓦片的大地图),每个瓦片对象只需包含 2 个整数,用于源的 X 和 Y 位置一个大纹理中的矩形 - 8 个字节。但是,如果您使用方法 #2,则地图瓦片数组中的每个瓦片对象都必须存储一个 32×32 纹理 - 一个图像 - 必须为 RGBA 像素分配 32 x 32 倍的内存 - 每个瓦片 4096 字节然后?那么,哪种方法以及为什么?第一要务是速度,然后是内存负载,然后是效率或专家认为的任何东西。