Monogame

Loading textures from files

var background = Content.Load<Texture2D>("stars");

Creating a Sprite Batch

var spriteBatch = new SpriteBatch(GraphicsDevice);

Creating a Render Target

var renderTarget = new RenderTarget2D(
                GraphicsDevice,
                GraphicsDevice.PresentationParameters.BackBufferWidth,
                GraphicsDevice.PresentationParameters.BackBufferHeight,
                false,
                GraphicsDevice.PresentationParameters.BackBufferFormat,
                DepthFormat.Depth24);

Setting the current Render Target

GraphicsDevice.SetRenderTarget(renderTarget);

Setting the current Render Target to be the screen

GraphicsDevice.SetRenderTarget(null);

Drawing to the current Render Target

spriteBatch.Begin();
// Draw background texture to the current Render Target
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
// Draw another Render Target to the current Render Target
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, 800, 480), Color.White);
spriteBatch.End();

Procedurally generate a texture

_texture = new Texture2D(GraphicsDevice, 100, 100);

var data = new uint[100 * 100];

for (int a = 0; a < 100 * 100; a += 4)
{
    data[a]     = 0xFF000000;
    data[a + 1] = 0xFF000000;
    data[a + 2] = 0xFFFFFFFF;
    data[a + 3] = 0xFFFFFFFF;
}

_texture.SetData(data);

NB: data is copied from the array rather than ‘pointed’ to, so changing the underlying array won’t update the texture.

For 32-bit SurfaceFormat.Color, data format = 0xAABBGGRR, examples:

Update and Draw

Update is called a fixed number of times per second. Draw may be called less frequently if the game is lagging.

The rate at which Update is called is set by TargetElapsedTime.