Question

So I understand that the title may be a bit vague, so I'll try to explain what I mean. Basically what I'm trying to accomplish is to draw lots of different objects and images, but in a sort of buffer, so that I can then later on use the drawing function to only draw a part of this buffer.

I am trying to make a Scrolling functionality in MonoGame, much like the Horizontal/Vertical slider control in WinForms, and I got a slider working. What I want is for the slider to accurately cut off other controls inside this slider window.

Now I understand that what I'm trying to accomplish is what RenderTarget2D is there for, and while I've tried working with it, I find it extremely difficult to make it do what i want. My main problem right now is that when I draw something in the RenderTarget, it clears everything else once I set the SetRenderTarget(null) (so that I draw on the game window once more).

Long story short, the following code draws everything AFTER I've performed the RenderTarget2D code, but clears everything prior to it.

protected override void Draw(GameTime gameTime)
{
    _spriteBatch.Begin();
    _spriteBatch.DrawString(font, "Prior", Vector2.Zero, Color.White);
    _spriteBatch.End();

    RenderTarget2D target = new RenderTarget2D(GraphicsDevice, 320, 110, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
    GraphicsDevice.SetRenderTarget(target);

    _spriteBatch.Begin();
    _spriteBatch.DrawString(font, "Middle", new Vector2(0, 120), Color.White);
    _spriteBatch.End();

    GraphicsDevice.SetRenderTarget(null);

    _spriteBatch.Begin();
    _spriteBatch.Draw(target, new Vector2(0, 120), Color.White);
    _spriteBatch.DrawString(font, "Past", new Vector2(0, 240), Color.White);
    _spriteBatch.End();

    base.Draw(gameTime);
}

Also, I have these:

public Game1()
    : base()
{
    _graphics = new GraphicsDeviceManager(this);
    _graphics.PreparingDeviceSettings += OnPreparingDeviceSettings;

    Content.RootDirectory = "Content";
}

private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
    e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;
}

I can't imagine how the last bit matters, but I figured I might as well add a little too much rather a little too little.

Right, so if anyone has any idea of what might be wrong, I'd really like to know, cause now I've been looking at this code and trying to make head and tail of it for about 5 hours or so, and I seem to have made no progress at all.

Thanks

Was it helpful?

Solution

So as it turns out, I figured out a replacement for the buffer, which works in my particular case.

I use the GraphicsDevice.ScissorRectangle to make it draw on particular rectangles, which suited my need, seeing as I wanted to accomplish a sort of Slider container control.

More info: MSDN - GraphicsDevice.ScissorRectangle Property

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top