A pure Zig 2D graphics library - z2d

Hey folks!

Version 0.10.0 has been released. This is a small release before the new year, and includes hairline support, in addition to some very small API changes.

You can check out the changelog at: https://github.com/vancluever/z2d/blob/v0.10.0/CHANGELOG.md

Thanks!

3 Likes

Does anyone have advice on using z2d with SDL3? Getting a bit lost in the details since I’m new to Zig, z2d and SDL.

I’m trying using the 7Games/zig-sdl3 wrapper but an example using another wrapper, calls to the SDL C functions or even just a description would be helpful.

It seems like the general gist is:

  1. Build up a z2d surface using the painter commands
  2. Create an SDL3 surface or texture
  3. Copy the raw pixel buffer from the z2d surface to the SDL3 surface or texture
  4. Render the SDL3 surface or texture to the window

Am I on the right track?

@g4b3 you have the right approach! If you know you’re always going to be working with said surface/texture memory exclusively, you might want to look into how you can just map the memory directly, and then use Surface.initBuffer so you can just render directly to it, and save yourself some copying.

Happy to answer any other questions!

1 Like

Hey folks, 0.11.0 was released last week.

Release notes can be found at https://github.com/vancluever/z2d/blob/v0.11.0/CHANGELOG.md

This was mainly released to support Zig 0.16.0. 0.12.0 will probably follow soon for Zig 0.17.0, after it’s released.

Thanks!

7 Likes

Hey folks, 0.12.1 was released yesterday!

This was another small release, but with a novel feature, path effects!

About path effects

Path effects is the informal term I’m giving to ungrouped functions in in z2d.Path that all have one thing in common: they all mutate the supplied path, returning a new one with a particular effect applied.

The first two functions of this type are complementary of each other: Path.simplify and Path.offset.

Simplify

Path.simplify takes a path and returns non-intersecting outlines. An easy way to illustrate this would be a 5-point self-intersecting star, and simplifying that to just be the outline:

This will also split self-sealing paths up into separate closed paths (think something along the lines of creating two triangles with a single closed path).

Offset

Path.offset allows one to create a new path with an inset (negative offset) or outset (positive offset). This allows for easy effects off of a single path:

This can also be used to apply offsets to an open path, which is used in Ghostty to ensure certain stroked glyphs can fit in a cell while still being aligned to the cell bounds.

The only requirement for offset is that the path is not self-intersecting (for which you can use Path.simplify to get a simplified path).

Context path effects

Like most other functionality in z2d, both path effect functions have corresponding functions in Context, those functions being Context.simplifyPath and Context.offsetPath. These functions replace the current context-managed path with the result of the operation.

Examples

You can get more details on how to apply Path.simplify and Path.offset in the acceptance test from which the above images were taken! This acceptance test uses the Context variants, demonstrating the higher-level, managed versions of these functions.

Release notes

Full release notes are here, including a couple of additional bug fixes: https://github.com/vancluever/z2d/blob/v0.12.1/CHANGELOG.md

11 Likes

Just wanted to say that I’m a happy user of z2d. I use it extensively for the CPU renderer of a linux desktop app framework I work on in my spare time. I really like your build.zig.zon’s .dependencies field, which is… nothing. :slight_smile:

I made some local modifications (as I always vendor my dependencies) to text rendering, mostly consisting of introducing a glyph cache to improve performance on text heavy workloads. The well structured code really helped with that.

Just for your information, I can’t wait for you to tackle Surface patterns! This will unlock a lot of features, especially if you implement a nice set of image scaling algorithms. :slight_smile:

Thank you for maintaining this library and for keeping it pure zig and dependency free.

7 Likes