从我在其他问题中看到的情况看来,我在我的绘图代码中实例化新内容会占用大量内存,但查看其他代码我使用作为参考,我不知道问题出在哪里
namespace Crucible
{
public class Crucible : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Drawing vars
public int renderLeft;
public int renderRight;
public int renderUp;
public int renderDown;
public Tile currentTile;
public Rectangle target;
public Rectangle source;
public Color tint;
public Texture2D img;
//
public static int baseTileWidth = 16;
public static int baseTileHeight = 16;
public static int mapWidthTile = 8400;
public static int mapHeightTile = 2400;
public static int mapWidthPixel = 8400 * baseTileWidth;
public static int mapHeightPixel = 2400 * baseTileHeight;
public static int screenWidth;
public static int screenHeight;
public static Player mainPlayer;
Tile[,] worldTilesFor; //Foreground Tiles
Tile[,] worldTilesBack; //Foreground Tiles
Texture2D coalOreSprite;
Texture2D dirtSprite;
public Crucible()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 1080;
graphics.PreferredBackBufferWidth = 1920;
}
protected override void Initialize()
{
mainPlayer = new Player("tim", 1);
worldTilesFor = new Tile[8400,2400];
worldTilesBack = new Tile[8400, 2400];
for (int i = 0; i < worldTilesFor.GetLength(0); i++)
{
for (int j = 0; j < worldTilesFor.GetLength(1); j++)
{
worldTilesFor[i, j] = new Tile(0,i,j);
if (j >= 1200)
{
worldTilesFor[i, j].tileID = 1;
}
}
}
for (int i = 0; i < worldTilesBack.GetLength(0); i++)
{
for (int j = 0; j < worldTilesBack.GetLength(1); j++)
{
worldTilesBack[i, j] = new Tile(0, i, j);
if (j < 1200)
{
worldTilesFor[i, j].tileID = 1;
}
}
}
screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
base.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
coalOreSprite = this.Content.Load<Texture2D>("Tile_coal ore");
dirtSprite = this.Content.Load<Texture2D>("Tile_Dirt");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
renderLeft = (int)Math.Floor(mainPlayer.position.X) - (screenWidth / 2);
if (renderLeft < 0)
{
renderLeft = 0;
}
renderRight = (int)Math.Ceiling(mainPlayer.position.X) + (screenWidth / 2);
if (renderRight > mapWidthPixel)
{
renderRight = mapWidthPixel;
}
renderUp = (int)Math.Floor(mainPlayer.position.Y) - (screenHeight / 2);
if (renderUp < 0)
{
renderUp = 0;
}
renderDown = (int)Math.Floor(mainPlayer.position.Y) + (screenHeight / 2);
if (renderDown > mapHeightPixel)
{
renderDown = mapHeightPixel;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for (int i = renderLeft; i < renderRight; i++)
{
for (int j = renderUp; j < renderDown; j++)
{
currentTile = worldTilesBack[i / 16, j / 16];
DrawTile(currentTile);
}
}
for (int i = renderLeft; i < renderRight; i++)
{
for (int j = renderUp; j < renderDown; j++)
{
currentTile = worldTilesFor[i/16, j/16];
DrawTile(currentTile);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
public void DrawTile(Tile tileIn)
{
switch (tileIn.tileID)
{
case 1:
img = dirtSprite;
break;
default:
img = dirtSprite;
break;
}
if (tileIn.foreground)
{
tint = Color.White;
}
else
{
tint = Color.Gray; //tint background objects grey
}
spriteBatch.Draw(dirtSprite, new Rectangle((int)(tileIn.posX * baseTileWidth - mainPlayer.position.X + screenWidth / 2), (int)(tileIn.posY * baseTileHeight - mainPlayer.position.Y + screenHeight / 2), baseTileWidth, baseTileHeight), new Rectangle?(new Rectangle(tileIn.frameX * 16, tileIn.frameY * 16, 16, 16)), tint); //error is thrown here at the source rectangle
}
}
}
非常感谢任何有关错误原因的帮助或指导。
答案 0 :(得分:0)
这是您的worldTilesFor
和worldTilesBack
数组。您没有显示Tile
类的外观,但即使我们假设每个Tile
包含100个字节的数据,这被认为是一个低猜测,那就是8400 * 2400 = 20,160,000个Tile实例* 100字节,每个阵列约2千兆字节,两者均为4字节。
我认为这就是你存储地图的方式。您将需要一个较小的地图,或者您需要一些方法来虚拟化您的地图,方法是将其存储在磁盘上,并根据需要将其加载到块中。