繁体   English   中英

使用尝试绘制时C#XNA内存不足异常

[英]C# XNA Out of Memory Exception when using trying to Draw

从我在其他问题中看到的内容来看,似乎正在实例化绘图代码中的新内容,这将占用大量内存,但是通过将其他代码用作参考,我不确定问题出在哪里。

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
        }
    }
}

对于导致错误的原因的任何帮助或指导,将不胜感激。

这是您的worldTilesForworldTilesBack数组。 您没有显示Tile类的样子,但是即使我们假设每个Tile容纳100字节的数据(认为这是一个很小的猜测),也就是8400 * 2400 = 20,160,000个Tile实例* 100字节,大约2 GB的RAM PER ARRAY,两个都为4。

我认为这就是您存储地图的方式。 您可能需要一个较小的地图,或者需要某种方式通过将地图存储在磁盘上并根据需要将其分块加载来虚拟化地图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM