Rounded rectangles in MonoGame?

So basically I’m trying to write a window manager inside MonoGame. I’m going for a Linux-y kind of look, so slightly rounded corners on most things. Well, I guess that’s more of a GTK look than Linux in general, you get the point though, I hope.

The goal is to hopefully have rounded title bars, like this:

As you can see, the corner where the close/minimize/maximize buttons are is rounded. I’d like to achieve this effect in MonoGame, without having to bake it into the texture.

Not baking it into the texture allows me to easily adjust the amount of rounding to apply to each corner - oh yeah, I also want to choose which corners I want to round - whether it’s only one corner, two, three, four, etc, and which ones do and don’t get rounded.

If there’s any way to achieve this, please let me know :smiley:

You can’t do it with SpriteBatch. You have to batch triangles to build up the geometry and use the low-level DrawPrimitives functions to render them.

I don’t really have a sample lying around, but I did write a class for batching 2D primitives some time ago. I uploaded it to a gist: https://gist.github.com/Jjagg/bd0540ded0d399e716f25e00641488e1
It’s not documented, but from the IPrimitiveBatcher interface the API should be pretty clear.

To render a rounded rectangle with different radii in the corners, use the DrawRoundedRect overload that takes radii and segment count for all corners. You can extend the batcher to support filled rounded rectangles with different radii in the corners if you’d like. UV mapping is also not implemented for rounded rectangles, so if you want to apply textures you may want to add that as well.
When you finish all your draw calls for the frame you can call Flush to render it. This code definitely lacks polish, so if you decide to use it and run into any issues feel free to let me know and I’ll update the gist :slight_smile:

1 Like

Neat! I’ll have a look when I’m at a development environment tonight.

How exactly would I go about UV mapping though? I’m like extremely new to the low level stuff (no experience at all actually. I don’t even know HLSL yet). Other than that, this may be exactly what I’m looking for.