Recursive AI

Draw2D: Draw 2D shapes in C#

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

Use Draw2D.FillCircle in your entity’s Draw method. No image file needed.

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.Core;
using OneGame.Host;
using OneGame.UI;
 
[Entity]
partial class Marker : Entity, IView2D
{
    [View]
    public void Draw(Draw2D draw) =>
        draw.FillCircle(Vector2.Zero, 24,
            Color.Rgb(0xf2c14e));
}

Add the entity to your world with world.Add(new Marker()), as in the full program. Its view draws a gold circle with a radius of 24 world units.

Unzip the example, then dotnet run.

The full program

The download needs a supported graphics device and driver, but it doesn’t open a window or read any assets. Its project file pins <PackageReference Include="RecursiveAI.OneGame.Host" Version="2.5.3" />. The package brings in the entity source generator, which creates the EntityTypes registration helper under the assembly name (Example here).

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 = 320, height = 180;
var registry = new EntityRegistry();
OneGame.Generated.Example.EntityTypes.Register(registry);
var world = new World(1, registry);
world.Add(new Marker());
 
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()), "circle.png");
Console.WriteLine($"Wrote circle.png ({width} x {height})");
 
[Entity]
partial class Marker : Entity, IView2D
{
    [View]
    public void Draw(Draw2D draw) =>
        draw.FillCircle(Vector2.Zero, 24,
            Color.Rgb(0xf2c14e));
}
Text
Wrote circle.png (320 x 180)

Open circle.png: the circle is centered on the frame. If the graphics backend can’t create a device, the program stops before it writes the image.

Variations

Draw a ring instead. Replace the drawing call inside Draw(Draw2D draw). The fourth argument is the outline thickness.

C#
draw.StrokeCircle(Vector2.Zero, 24,
    Color.Rgb(0xf2c14e), 3);

Draw a filled rectangle instead. Pass its top-left X and Y, width, and height.

C#
draw.FillRect(new Rect(-30, -12, 60, 24), Color.White);

Draw a line instead. Pass both endpoints and the thickness.

C#
draw.Line(new Vector2(-30, 0), new Vector2(30, 0), Color.White, 3);

Draw a sprite with Draw2D · Draw a 2D physics collider overlay


The short answer

Use Draw2D.FillCircle for a solid disk and Draw2D.StrokeCircle for a ring. Put the call in the [View] method on an IView2D entity. Add that entity to the world so the engine calls its Draw method each frame.

How do I draw a circle in C# with Draw2D?

Give FillCircle a center, a radius, and a color. Unlike a rectangle, the circle’s position is its center, not its top-left corner. Its radius is half its diameter: the example’s radius of 24 makes it 48 world units across. At camera zoom 1, that’s 48 pixels across. X increases to the right and Y increases down. Vector2.Zero lands in the middle of this image because the default camera is centered on the world origin.

A Draw2D call draws into the current frame. It doesn’t add a persistent object, a physics collider, or a clickable button. To move the circle, draw it at the entity’s current position each frame. Read synced position in the view, but update that position in the simulation. In a running game the engine provides the Draw2D context; you don’t need the offscreen render setup from the download.

How do I draw a circle outline in C#?

Use StrokeCircle with the same center and radius, plus a thickness. Use that for a selection ring when the player needs to see what’s inside it. To outline a solid shape, draw the fill and then draw the stroke. On the same drawing layer, higher z values draw later; calls with equal z draw in call order. That also lets an outline draw over a sprite on the same layer without changing either asset. The 2D drawing guide shows the equivalent rectangle and line calls.

All onegame recipes