Recursive AI

Draw a 2D minimap with a second camera in C#

onegame is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required, Vulkan on Linux for this example.

Give the minimap its own camera so it can show a wider area than the main view.

Terminal
dotnet add package RecursiveAI.OneGame.Host

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.Numerics;
using OneGame.Host;
using OneGame.Render2D;
using OneGame.UI;
 
static void DrawMinimap(
    Draw2D d, Vector2 player)
{
    var mini = d.GetLayer("minimap");
    mini.Camera = new Camera2D { Zoom = 0.25f };
    mini.Viewport = new Rect(348, 12, 120, 84);
    var world = d.Layer;
    d.Layer = mini;
    var gold = Color.Rgb(0xf2c14e);
    d.FillCircle(player, 16, gold);
    d.Layer = world;
}

Put DrawMinimap inside your entity’s [View] method as a local function. After drawing the world, call it with the view’s Draw2D context and the player’s world position. The gold marker is clipped to a 120 × 84 pixel rectangle. The full example adds terrain and writes minimap.png.

Rect(348, 12, 120, 84) fits a 480-pixel-wide frame: 348 = 480 − 120 − 12, leaving a 12-pixel right margin. See Variations for other frame widths.

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The full program

There are no image inputs. The program draws a river, a road and a player at (64, 24) on a 480 × 270 frame. The main camera follows the player; the minimap camera looks at the world origin. The project pins <PackageReference Include="RecursiveAI.OneGame.Host" Version="2.8.0" />; its source generator creates the EntityTypes helper in OneGame.Generated.Example. The Example suffix comes from the assembly name, which defaults to the project name.

To run the same [View] in a game window, add the entity to the world in the first game guide and run the game as shown there.

C#
using System.Numerics;
using OneGame.Atlas;
using OneGame.Core;
using OneGame.Host;
using OneGame.Render2D;
using OneGame.Text;
using OneGame.UI;
using RecursiveAI.OneGpu;
 
const int width = 480, height = 270;
var registry = new EntityRegistry();
OneGame.Generated.Example.EntityTypes.Register(registry);
var world = new World(1, registry);
world.Add(new MapView());
 
var fonts = new FontLibrary();
using var device = GpuBackends.CreateDevice();
using var stage = new Render2DStage(new HostResources(fonts,
    new FontTextMeasurer(fonts), new ArtAtlas(), GeneratedShaders.Instance));
using var target = device.CreateTexture(new TextureDesc(width, height,
    GpuFormat.Rgba8UnormSrgb, TextureUsage.RenderTarget | TextureUsage.CopySource));
world.Step();
stage.Render(device, target, new FrameInfo(width, height, 1, world.Tick,
    new DisplayList(), world, Fixed.Zero));
var pixels = new byte[width * height * 4];
device.ReadTexture(target, 0, 0, pixels);
Png.Save(new CapturedFrame(width, height, pixels, device.Adapter.ToString()), "minimap.png");
Console.WriteLine($"Adapter: {device.Adapter.Name}");
Console.WriteLine($"Wrote minimap.png ({width} x {height})");
 
[Entity]
partial class MapView : Entity, IView2D
{
    [View]
    public void Draw(Draw2D d)
    {
        var player = new Vector2(64, 24);
        var gold = Color.Rgb(0xf2c14e);
        var water = Color.Rgb(0x236b83);
        var grass = Color.Rgb(0x3d7467);
        d.Camera.Zoom = 1.5f;
        d.Follow(player);
        d.FillRect(new Rect(-1000, -1000, 2000, 2000), grass);
        d.FillRect(new Rect(-160, -400, 70, 800), water);
        d.FillRect(new Rect(-90, -16, 420, 32), Color.Rgb(0xb3a081));
        d.FillCircle(player, 8, gold);
 
        var mini = d.GetLayer("minimap");
        mini.Camera = new Camera2D { Zoom = 0.25f };
        mini.Viewport = new Rect(348, 12, 120, 84);
        d.Layer = mini;
        d.FillRect(new Rect(-1000, -1000, 2000, 2000), Color.Rgb(0x23463f));
        d.FillRect(new Rect(-160, -400, 70, 800), water);
        d.FillRect(new Rect(-90, -16, 420, 32), Color.Rgb(0xb3a081));
        d.FillCircle(player, 16, gold);
    }
}
Text
Adapter: NVIDIA GeForce RTX 4070
Wrote minimap.png (480 x 270)

A large gold player marker in the main view, with a smaller marker and a wider view of the river and road in the upper-right minimap.

If the player dot disappears from a stationary minimap, the player may have walked outside that camera’s viewport. Follow the player or zoom out farther.

Variations

Keep the dot centered. Replace the minimap camera assignment, not the main camera’s. player is the same world position you pass to FillCircle:

C#
mini.Camera = new Camera2D
{
    Position = player, Zoom = 0.25f
};

Keep the map in the upper-right corner after a resize. Replace the fixed viewport with one calculated from the current frame width:

C#
mini.Viewport = new Rect(d.Width - 132, 12, 120, 84);

This leaves a 12-pixel margin on a frame at least 132 pixels wide. On a narrower frame, reduce the map width as well.

Draw 2D shapes · Draw 2D sprites


The short answer

Get a named layer with d.GetLayer("minimap"). Set its Camera and Viewport, switch d.Layer to it, and draw map geometry and markers in world units. The main view still uses d.Camera. You don’t need a second window or a render texture for a rectangular corner map.

How do you create a 2D minimap with a second camera?

Draw the main view on the default layer and put the minimap on a separate layer. The Draw2D layer rules put later-created layers over earlier ones, then sort by z within each layer. Use one layer name each frame and restore the previous d.Layer if there are more main-view draw calls to make.

A second camera doesn’t automatically copy draw calls from the main layer. Draw what belongs on the map there explicitly. That can be as little as a rectangle for each landmark and a circle for each player. Reuse their world positions, but choose icon sizes for the map. A radius of 16 world units at zoom 0.25 makes a 4-pixel radius on the map, regardless of the main camera’s zoom.

How do you fit or clip a minimap to a corner?

Viewport is a rectangle in frame pixels: left, top, width and height. The layer’s camera places its world position at the center of that rectangle and clips drawing at the edges. In the example the rectangle runs from (348, 12) to (468, 96), so world origin maps to (408, 54). The player’s map point is (424, 60) after scaling the world offset by 0.25. Draw the dot at (64, 24) in world coordinates; don’t add the viewport offset again.

The viewport controls where the map goes, while the camera controls how much of the world it shows. At zoom 0.25, the example’s 120 × 84 pixel viewport covers 480 × 336 world units. For a fixed map of known size, center the camera on the map bounds and choose the smaller of the viewport-width-to-world-width and viewport-height-to-world-height ratios. That fits the whole map without changing the shape of its features. For a scrolling map, keep the zoom fixed and follow the player instead.

All onegame recipes