Mattias Gustavsson's Avatar

Mattias Gustavsson

@mattiasg.bsky.social

Hobby game dev, making retro style games: mattiasgustavsson.itch.io and public domain C libs: github.com/mattiasgustavsson I write about things at mattiasgustavsson.com Feel free to DM me about anything

1,063 Followers  |  653 Following  |  313 Posts  |  Joined: 25.08.2023  |  2.3244

Latest posts by mattiasg.bsky.social on Bluesky

Preview
pow2_ceil.h GitHub Gist: instantly share code, notes, and snippets.

Round a uint32 up to the nearest power-of-two value:
gist.github.com/mattiasgusta...

01.08.2025 13:45 β€” πŸ‘ 9    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

I found that on the latest compiler versions, my previous intrinsics-based implementations tripped some undefined behavior optimizations. fixed it now

03.08.2025 00:57 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Preview
pow2_ceil.h GitHub Gist: instantly share code, notes, and snippets.

Round a uint32 up to the nearest power-of-two value:
gist.github.com/mattiasgusta...

01.08.2025 13:45 β€” πŸ‘ 9    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0
Post image

I am excited to share this new single-header C library I have been working on for a while now: vecmath.h, a comprehensive vector/matrix math library for graphics/games/3d.
It allows you to write vector math code in C that looks like this:

21.07.2025 15:09 β€” πŸ‘ 96    πŸ” 20    πŸ’¬ 4    πŸ“Œ 0

that'd be cool :) the current vecmath.h is already very large, but I tried to limit its features to those found in HLSL and D3DX, to have as a baseline. There's many convenience functions and higher level functionality that could be added, so I'm considering a "vecmath_util.h" for all of those

25.07.2025 07:36 β€” πŸ‘ 1    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

If you're looking for a vector math lib for C (or C++) check it out

github.com/mattiasgusta...

If you do take a look, please let me know what you think about it - getting feedback is a big part of the reward releasing libraries like this, and I spent a lot of time and effort on it

21.07.2025 15:09 β€” πŸ‘ 10    πŸ” 1    πŸ’¬ 0    πŸ“Œ 0

It's been tested on windows, mac, linux and web-assembly, using msvc, clang, gcc and tcc (tiny c compiler).

21.07.2025 15:09 β€” πŸ‘ 6    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

It is not trying to be the most optimized library, not using any SIMD intrinsics or assembly code, just straightforward, fully portable, simple implementations of a large number of math functions.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

It has a large set of functions, as it attempts to implement most of what you get in shader languages like HLSL and GLSL, and also most of the utility functions from libraries like D3DX in DirectX.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

Even though it is primarily designed as a C library, I have also tried to make it fairly nice to use from C++, conditionally adding operator overloads and constructors, but it is not trying to be idiomatic "modern C++" in any way.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

Here is the library: github.com/mattiasgusta...

Now, you get the best ergonomics if you use it with clang compiler in C11 or later, as it makes (optional) use of C11 _Generics and clang vector type extension, but it doesn't require either to work.

21.07.2025 15:09 β€” πŸ‘ 11    πŸ” 3    πŸ’¬ 1    πŸ“Œ 0

I see those kind of operations as very much outside the scope of this library, which focus more on basic operations matching the features of HLSL and D3DX. I think for any user that needs special case functions like that, it's easy for them to build out of the primitives provided in the lib

22.07.2025 07:14 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0

No, I don't use any optimizations like that, it's just pretty much the simplest implementation of textbook Cramer’s rule, for generic inversion

21.07.2025 17:39 β€” πŸ‘ 2    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

yeah, definitely give clang a go! i find that for my use cases, it compiles faster AND generates more performant code than both msvc and gcc. and, it got that taste vector extension :)

21.07.2025 17:33 β€” πŸ‘ 2    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0

Oh, it’s got full support for matrices and quaternions too, the lib is very comprehensive - they are just not in the small example :)

21.07.2025 16:34 β€” πŸ‘ 5    πŸ” 0    πŸ’¬ 2    πŸ“Œ 0

it is similar, but also quite different - it does not allow you to access the elements using .x, .y, .z etc, only using [0],[1],[2].. so it didn't work well with the rest of my library design, so I opted to only support the clang one

21.07.2025 15:39 β€” πŸ‘ 4    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

Clang has a custom compiler extension for "vector types", so the operators part only works if you're building with clang - but clang is actually great on all platforms I use, so that works out ok :)

21.07.2025 15:24 β€” πŸ‘ 6    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

If you're looking for a vector math lib for C (or C++) check it out

github.com/mattiasgusta...

If you do take a look, please let me know what you think about it - getting feedback is a big part of the reward releasing libraries like this, and I spent a lot of time and effort on it

21.07.2025 15:09 β€” πŸ‘ 10    πŸ” 1    πŸ’¬ 0    πŸ“Œ 0

It's been tested on windows, mac, linux and web-assembly, using msvc, clang, gcc and tcc (tiny c compiler).

21.07.2025 15:09 β€” πŸ‘ 6    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

It is not trying to be the most optimized library, not using any SIMD intrinsics or assembly code, just straightforward, fully portable, simple implementations of a large number of math functions.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

It has a large set of functions, as it attempts to implement most of what you get in shader languages like HLSL and GLSL, and also most of the utility functions from libraries like D3DX in DirectX.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

Even though it is primarily designed as a C library, I have also tried to make it fairly nice to use from C++, conditionally adding operator overloads and constructors, but it is not trying to be idiomatic "modern C++" in any way.

21.07.2025 15:09 β€” πŸ‘ 7    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

Here is the library: github.com/mattiasgusta...

Now, you get the best ergonomics if you use it with clang compiler in C11 or later, as it makes (optional) use of C11 _Generics and clang vector type extension, but it doesn't require either to work.

21.07.2025 15:09 β€” πŸ‘ 11    πŸ” 3    πŸ’¬ 1    πŸ“Œ 0
Post image

I am excited to share this new single-header C library I have been working on for a while now: vecmath.h, a comprehensive vector/matrix math library for graphics/games/3d.
It allows you to write vector math code in C that looks like this:

21.07.2025 15:09 β€” πŸ‘ 96    πŸ” 20    πŸ’¬ 4    πŸ“Œ 0
Preview
The adventure games that inspired Yarnspin Yarnspin is my story-telling game engine, designed to be very easy to use, and it can run as a local build or in your browser.Β  It is completely free (public domain) and you can find it here: Yarns...

And I wrote about the inspiration for the Yarnspin engine here: mattiasgustavsson.com/the-adventur...

29.06.2025 15:03 β€” πŸ‘ 3    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0
Post image

The engine also includes an editor for adjusting images, implemented using a custom basic imgui system. I've written a bit about it here: mattiasgustavsson.itch.io/yarnspin/dev...

29.06.2025 15:03 β€” πŸ‘ 6    πŸ” 2    πŸ’¬ 1    πŸ“Œ 0
Post image Post image

This is what my dev environment looks like when I'm making a game in Yarnspin. I use notepad++, with a custom color scheme, with the "folder as workspace" to get a tree view for both scripts and image files. And with keyboard shortcuts for run/compile and go to error.

29.06.2025 15:03 β€” πŸ‘ 9    πŸ” 2    πŸ’¬ 2    πŸ“Œ 0
Post image Post image

The custom script language for Yarnspin is made to be simple, and without a lot of notation or complex syntax. Mostly it's just specifying images and text to display, listing options to choose from and where they lead to

29.06.2025 15:03 β€” πŸ‘ 8    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0
Preview
GitHub - mattiasgustavsson/yarnspin: A simple story-telling game engine A simple story-telling game engine. Contribute to mattiasgustavsson/yarnspin development by creating an account on GitHub.

It was made in about a week, using my Yarnspin game engine, which is written in plain C using only stb-style single header libraries github.com/mattiasgusta...

29.06.2025 15:03 β€” πŸ‘ 6    πŸ” 1    πŸ’¬ 1    πŸ“Œ 0

yeah, it's a custom pixelshader I use for a lot of my little games, I have a few variants, source code is here github.com/mattiasgusta...

09.07.2025 17:33 β€” πŸ‘ 1    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

@mattiasg is following 20 prominent accounts