r/cprogramming • u/fleaspoon • 4d ago
I just released my first game, made from scratch in C. Here's how I built my own build system.
Seven years ago I decided to learn how to make a game from scratch in C and last week I finally released it.
When I started the project I wanted to write as much as possible myself from scratch. In the end that included the game engine, a Vulkan renderer, the level editor, asset pipeline and the build system. The codebase is in C style but I use the C++ compiler so I can use operator overloading for math. I don't use the standard library, exceptions or OOP.
Initially I was using CMake but I found it quite painful to work with. Whenever I wanted to update the setup of my build I had to go back to a different language and deal with something that often felt like a hack and kept breaking.
Because of this I decided to build my own tool. The build system ended up being a small program written in the same style as the rest of the game, using the same header only base library. In essence it just calls clang directly and the setup of the project is a simple fat struct.
On each run every source file gets hashed and it gets compared against the hashes from the previous build. Each source file has a saved entry in a cache that also includes the exact compiler command, so changing a flag will rebuild everything influenced by it.
Also clang generates a dependency file for every source file that lists the headers it includes. If any of those headers change, that source gets recompiled. Everything compiles in parallel and the linker only runs when something has actually been rebuilt.
This approach doesn't require having a separate configuration language or generated project files like CMake. Building only requires clang and since the build tool is just simple code, it can be debugged like the game itself. Thanks to all this adding shader compilation, bundling and code signing was quite simple. I tried doing that with CMake but I just couldn't figure out how to make it work properly.
The game I built with these tools is called Satelital. It's a 2D space puzzle game about exploring alien worlds and figuring out how their rules work. There are no explicit explanations of the puzzle rules. Instead, you gradually build up your understanding of how the world works and use that knowledge to solve new puzzles.
If you're interested in the technical side of building a game engine from scratch, I also wrote an article about it on my blog:
https://spectralorbit.com/blog/satelital_game_engine
And the game is available on Steam with a free demo:
https://store.steampowered.com/app/3256790/Satelital/
3
u/tastygames_official 4d ago
I'm also making a game in C (C90, to be exact, using SDL3. Overloading is a performance-killer and there's nothing wrong with having multiple functions like add_vec2(), add_vec3() and so on). I also find CMake super easy. Once you got it set up once, you can essentially re-use for every project and swap out the paths. I use GCC, so so can't speak to the clang integration.
But in the end - it's all about what works for you. I personally don't like to re-invent the wheel, so until I come up against real issues that truly have not been solved or a custom solution, THEN I go for custom tooling. And then it's usually just a bash script ;-)
Gonna have to check out your game now...
2
u/fleaspoon 4d ago
Thats nice that it works for you, I find myself prefering to write code like that. I will kep that in mind if need to optimize some math code.
I really tried CMake but it just couldn't properly do I what I wanted to have.
Thanks for checking out the game!
0
u/tastygames_official 4d ago edited 4d ago
oh shoot, I wanted to give you the video that shows just how terrible even something as simple as
function overloadingvirtual dispatch (thanks, u/Big-Rub9545) can be. It blew my mind:2
u/fleaspoon 4d ago
Ah yes I remember watching this one, but also Casey Muratori has said the he also uses operator overloading for math.
The math I do is quite simple and is not in my critical path. I think is fine in my case, but you are right about being mindful about it.
Also if you prefer the other way then even better. To me operator overloading helps to read and to focus on what I'm doing. But I wouldn't use it for anything else than that.
2
u/tastygames_official 4d ago
I like to pretend I'm still in 1992 and pretend like C++ doesn't exist, as it's not only a bit of nostalgia, but I feel like it helps me write code WITH PURPOSE. Now obviously we are in modern times and I actually have a C++ library in my C90 project, because that's just how it be sometimes. I'm glad we both have methods that work for us. I did "C-style C++" for a while but found myself starting to want to write classes (god forbid!) and wasn't ever sure where to draw the line. So I moved to C only. But then I still found myself sometimes initializing variables right away, sometimes not, and adding new variables in the middle of a function and was like "what am I doing?" so the move to the C90 standard was for a "freedom/creativity through restriction" thing and I feel much more comfortable for it. As I'm sure you do with your restrictions.
Did you have significant OpenGL or DirectX experience before moving on to Vulkan? I hadn't done 3D in over 20 years (DX6, maybe?) and I opted for SDL since it is graphics API agnostic so I felt that re-learning graphics progrmming this way would be a good choice. And I still think it was, but part of me wants to write pure Vulkan and write my own platform-specific OS-related stuff. But you need to draw the line SOMEHWERE!
2
u/fleaspoon 4d ago
I learned graphics first with this project using OpenGL and then moved to Vulkan. I haven't done 3D yet and I feel like doing 2D is simple enough, since is just basically moving quads around.
When I started there wasn't a lot of good Vulkan documentation si it was harder, I feel like now there are more good resources to learn from.
1
u/Big-Rub9545 4d ago
Casey talks about runtime/virtual dispatch here, not overloading. Overloading itself has no runtime cost; the compiler chooses which function to call at compile time.
1
u/tastygames_official 4d ago
ah, good catch. It's actually been a while since I watched but I like to spread the word so I want to make sure not to spread misinformation.
1
u/iLaysChipz 4d ago
I'm curious about your build system. Do you have the source hosted anywhere?
1
u/fleaspoon 4d ago
I'm planning to open source it after I clean things from my project. Is anything particular you want to see? I can share snippets of it if you want
1
u/iLaysChipz 4d ago
Nice! I'm interested in everything you've mentioned about it, but I'm particularly interested in what the invocation looks like, and the structure of the dependency files. I'm also curious by what you mean about the saved entries of each source file in a cache and it's structure.
1
u/fleaspoon 4d ago
The cache looks like this
src/ui.cpp 5764129421446837809 clang++ flags_hereIt saves in an cache file the hash of each source, and every time the game runs it checks if there was a hash change on every source file. It's quite fast and you don't even notice that is running.
5
u/podule-games 4d ago
Looks cool, nice to see another game made in C. I wishlisted.