1

Well, first of all, my guess is that I'm calling the spritebatch.draw() method to many times, but I need to (Or, it's the only way I can figure out how to) Draw my in-game windows. I'll just go ahead and dump my code.

My Window Class;

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;

    }
}
}

My Draw() method;

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

It's all nice and configured for my little windowskin texture, and such. the only problem is that it will get a little laggy, and then completely crash on me about a minute into running it. It throws an Out Of Memory Exception, but I don't know and can't find any other topic or post on this relating to spritebatch. Does anybody have any suggestions on how I can get this working and not take up much memory? I would think this as an easy, cost effective way of drawing a window. I'm just not sure how cut down on my draw calls, or get any of that memory back.

2 Answers2

3

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

may be part of the culprit. Try not to instantiate something new every update. It occurs about 60 times a second and can cause serious overhead. Initialize the render targets in the Initialize method instead.

SchautDollar
  • 348
  • 3
  • 13
  • But how would I compensate for the changed rendertarget size? I coded it this way so it was easily resizable. Also, It's '+2' on both axes because it would cut off a little on both sides when it was moving faster then the update could keep up with. Adding a little to it fixed that. Wait, that means I can just update it's width/height, instead of re-initializing... lemme check. EDIT: Nope, it's read only... I think that's why I re-initialized in the first place.. Xd – kikigreydragon Dec 18 '12 at 05:08
  • I've also tried to only draw the new window size if it has changed sizes since the last update, but trying to tell it not to redraw(So it doesn't have to loop through all those draws) doesn't work for some reason, it just disappears after the first draw. It's like the rendertarget deletes itself once it's been drawn. – kikigreydragon Dec 18 '12 at 05:14
  • Have a couple rendertargets if you need them(really, you should only need one) afterwards, move the texture onto a texture so you can then draw it. For dealing with the screen size changing, depending whether it matters if images can be scaled, then just draw(on a rendertarget) everything on a huge texture and scale that down with the screen dimensions for the final draw. – SchautDollar Dec 18 '12 at 05:34
  • It's not the screen size I'm changing, it's the in-game windows size. I've tried moving the rendertarget to a texture afterwards, but it still disappears after the first draw. Why, I've got no idea. EDIT: WAIT, WAIT, I think I fixed it. I took out the re-initialization line, and changed the initialization line to the maximum size the window can be. I never would have thought of that. I think it's fixed, but let me test on it a bit to make sure. :] – kikigreydragon Dec 18 '12 at 05:38
  • explain, so you are drawing a building with windows? So a sidescroller? and the faster you go, the wider the windows? – SchautDollar Dec 18 '12 at 05:39
  • Where did you get the building idea from?... Oh, sorry, I didn't explain thoroughly. It's a window window, like an in-game windows form type of window. I'm making an editor, so it's used for the tiles. Also, see my previous edit. – kikigreydragon Dec 18 '12 at 05:44
  • 1
    Yup I think I fixed it. Thanks for the trouble, +1. :] Or, at least I would if it would let me... – kikigreydragon Dec 18 '12 at 05:47
  • Ah, ok. Now I get ya. And I hope it works out for ya. If my answer helped solve, could ya accept it. Thanks – SchautDollar Dec 18 '12 at 05:48
  • 1
    Eheh... I can't find a way to accept it. Could you tell me where it's located? EDIT: Pfft, nevermind. The checkmark. Done, and thanks. – kikigreydragon Dec 18 '12 at 05:58
  • OH, and one more thing, consider exploring http://gamedev.stackexchange.com/ They have tons of stuff related to xna and more! – SchautDollar Dec 18 '12 at 06:16
2

RenderTarget2D implements IDisposable: you should always call Dispose() on it once you're done with it, as it may hold unto unmanaged resources (like a Direct3D surface). This is the root cause of the memory leak you're seeing.

Of course, as noted already, instantiating a new RenderTarget2D on every Draw() is not a good idea to begin with, as it is an expensive call to make.

Asik
  • 21,506
  • 6
  • 72
  • 131