While researching how to draw Rectangle
s in Monogame, I came across this answer and the below code sample:
SpriteBatch spriteBatch;
Texture2D whiteRectangle;
protected override void LoadContent()
{
base.LoadContent();
spriteBatch = new SpriteBatch(GraphicsDevice);
// Create a 1px square rectangle texture that will be scaled to the
// desired size and tinted the desired color at draw time
whiteRectangle = new Texture2D(GraphicsDevice, 1, 1);
whiteRectangle.SetData(new[] { Color.White });
}
protected override void UnloadContent()
{
base.UnloadContent();
spriteBatch.Dispose();
// If you are creating your texture (instead of loading it with
// Content.Load) then you must Dispose of it
whiteRectangle.Dispose();
}
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
// Option One (if you have integer size and coordinates)
spriteBatch.Draw(whiteRectangle, new Rectangle(10, 20, 80, 30),
Color.Chocolate);
// Option Two (if you have floating-point coordinates)
spriteBatch.Draw(whiteRectangle, new Vector2(10f, 20f), null,
Color.Chocolate, 0f, Vector2.Zero, new Vector2(80f, 30f),
SpriteEffects.None, 0f);
spriteBatch.End();
}
Questions:
-
My first question concerns this section here:
protected override void UnloadContent() { base.UnloadContent(); spriteBatch.Dispose(); // If you are creating your texture (instead of loading it with // Content.Load) then you must Dispose of it whiteRectangle.Dispose(); }
When you create a new project in Monogame 3.8, you don’t have
UnloadContent
created for you.My question is, how accurate is the comment in the above section for Monogame 3.8?
Do I still need to callDispose
for every asset that I create that doesn’t useContent.Load
? -
The above code snippet works fine with Monogame, but is there a more standard/accepted way of drawing
Rectangle
s in Monogame?