XNA 4.0 SpriteBatch.Draw内存异常抛出

时间:2012-12-18 04:57:04

标签: c# xna out-of-memory xna-4.0 spritebatch

嗯,首先,我的猜测是我多次调用spritebatch.draw()方法,但我需要(或者,这是我能弄清楚如何做的唯一方法)游戏窗口。我将继续并转储我的代码。

我的窗口类;

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace System.Window
{
class Window
{
    #region Variables

    public Texture2D importedTexture;
    public Texture2D WindowSkin;
    public RenderTarget2D currentWindow;
    public RenderTarget2D windowTexture;

    public Vector2 pos;

    public int prevWindowWidth;
    public int prevWindowHeight;
    public int windowWidth;
    public int windowHeight;

    public bool visible;
    public bool active;
    public bool drawNew;

    #region Rectangles

    public Rectangle clickRect;
    public Rectangle topLeftRect;
    public Rectangle topRightRect;
    public Rectangle buttonRect;
    public Rectangle botLeftRect;
    public Rectangle botRightRect;


    #endregion

    #endregion

    public Window()
    {

    }

    public void Initialize(GraphicsDevice g, Texture2D ws, Texture2D it, int w, int h, bool v, bool a)
    {
        WindowSkin = ws;
        importedTexture = it;

        windowWidth = w;
        prevWindowWidth = w;

        windowHeight = h;
        prevWindowHeight = h;

        windowTexture = new RenderTarget2D(g, windowWidth, windowHeight);
        currentWindow = windowTexture;

        visible = v;
        active = a;

        drawNew = true;

        topLeftRect = new Rectangle(0, 0, 32, 32);
        topRightRect = new Rectangle(32, 0, 32, 32);
        buttonRect = new Rectangle(64, 0, 32, 32);
        botLeftRect = new Rectangle(0, 64, 32, 32);
        botRightRect = new Rectangle(64, 64, 32, 32);

    }

    public void Update(GraphicsDevice g, Vector2 p, int width, int height)
    {
        prevWindowWidth = windowWidth;
        prevWindowHeight = windowHeight;

        pos = p;
        windowWidth = width;
        windowHeight = height;
        windowTexture = new RenderTarget2D(g, windowWidth+2, windowHeight+2);

    }

    public void Draw(SpriteBatch s, GraphicsDevice g)
    {


        s.Draw(currentWindow, pos, new Rectangle(0, 0, windowWidth, windowHeight), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);

    }

    public void DrawNewWindow(SpriteBatch s, GraphicsDevice g)
    {

            g.SetRenderTarget(windowTexture);
            g.Clear(Color.Transparent);

            s.Begin();

            #region Draw Background

            for (int w = 3; w < (windowWidth); w += 32)
            {
                for (int h = 32; h < (windowHeight); h += 32)
                {
                    s.Draw(WindowSkin, new Vector2(w, h), new Rectangle(32, 32, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                }
            }

            #endregion

            s.Draw(importedTexture, new Vector2(3, 32), new Rectangle(0, 0, importedTexture.Width, importedTexture.Height), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);


            #region Draw resizables

            for (int i = 32; i < (windowWidth - 64); i += 32)
            {
                s.Draw(WindowSkin, new Vector2(i, 0), new Rectangle(16, 0, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            for (int i = 32; i < (windowWidth - 32); i += 32)
            {
                s.Draw(WindowSkin, new Vector2(i, windowHeight - 32), new Rectangle(32, 64, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            for (int i = 64; i < (windowHeight - 32); i += 32)
            {
                s.Draw(WindowSkin, new Vector2(0, i), new Rectangle(0, 48, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            for (int i = 64; i < (windowHeight - 32); i += 32)
            {
                s.Draw(WindowSkin, new Vector2(windowWidth - 32, i), new Rectangle(64, 48, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }

            #endregion

            #region Draw Corners

            s.Draw(WindowSkin, new Vector2(0, 0), topLeftRect, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            s.Draw(WindowSkin, new Vector2(0, 32), new Rectangle(0, 32, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);

            s.Draw(WindowSkin, new Vector2(windowWidth - 64, 0), topRightRect, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            s.Draw(WindowSkin, new Vector2(windowWidth - 32, 32), new Rectangle(64, 32, 32, 32), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);

            s.Draw(WindowSkin, new Vector2(windowWidth - 32, 0), buttonRect, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            s.Draw(WindowSkin, new Vector2(0, windowHeight - 32), botLeftRect, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            s.Draw(WindowSkin, new Vector2(windowWidth - 32, windowHeight - 32), botRightRect, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);

            #endregion

            s.End();

            currentWindow = windowTexture;

    }
}
}

我的Draw()方法;

        protected override void Draw(GameTime gameTime)
    {

            window1.DrawNewWindow(spriteBatch, GraphicsDevice);
            window1.drawNew = false;


        GraphicsDevice.SetRenderTarget(null);
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteSortMode.Deferred,
                    BlendState.AlphaBlend,
                    SamplerState.PointClamp,
                    null,
                    null,
                    null);

        window1.Draw(spriteBatch, GraphicsDevice);

        spriteBatch.End();

        base.Draw(gameTime);
    }

这一切都很好,并配置了我的小窗口纹理,等等。唯一的问题是它会变得有点滞后,然后在运行它的过程中完全崩溃了一分钟。它抛出一个Out Of Memory Exception,但我不知道,也找不到与spritebatch相关的任何其他主题或帖子。有没有人对如何使这项工作有所建议并且没有占用太多记忆?我认为这是一种简单,经济有效的绘制窗口的方法。我只是不确定如何削减我的平局电话,或者恢复任何记忆。

2 个答案:

答案 0 :(得分:3)

  

windowTexture = new RenderTarget2D(g,windowWidth + 2,windowHeight + 2);

可能是罪魁祸首的一部分。尽量不要在每次更新时都实例化新内容。它每秒发生约60次并且可能导致严重的开销。而是在Initialize方法中初始化渲染目标。

答案 1 :(得分:2)

RenderTarget2D实现了IDisposable:一旦你完成它就应该总是在它上面调用Dispose(),因为它可以保存到非托管资源(比如Direct3D表面)。这是您看到的内存泄漏的根本原因。

当然,如前所述,在每个Draw()上实例化一个新的RenderTarget2D并不是一个好主意,因为它是一个昂贵的调用。

相关问题