r/cprogramming • u/fleaspoon • Sep 26 '26
I spent 7 years building a game engine using C
I've been working on a puzzle game for the last 7 years, and over time I ended up writing almost the entire technology stack myself.
The code is written in a C style, using a C++ compiler mainly because I like operator overloading for math. Other than that, I try to keep the code close to how I would write C. I don't use the standard library, exceptions, RTTI, RAII or object-oriented abstractions.
I made my own build system, UI library, in-game editor, Vulkan renderer, asset pipeline and most of the other tools I use to make the game.
There is no SDL or Dear ImGui. Most of the code is my own, along with a few small libraries such as stb_truetype and miniaudio.
I really like having direct control over all these parts of the codebase. If the game needs something new, I can change the renderer, tools, asset pipeline, build system or engine directly instead of working around an existing framework.
The game is a rule-discovery puzzle game, and building the technology behind it has been just as interesting to me as making the game itself.
The demo is now available on Steam if you want to see what I ended up making:
https://store.steampowered.com/app/5017320/Satelital_Demo/
The full game is planned for release later this year in November.
I'd be interested to hear how other people structure larger C projects, especially around memory management, build systems, UI and tools. I'm always interested in seeing how other people approach these problems.
5
u/alkavan Sep 26 '26 edited Sep 26 '26
I've built something similar recently, I decided to implement a proper vector, matrix, linear algebra/math library first. Form there the engine was pretty simple overlay, I've got even both 2d and 3d, render and physics engines implemented. My conclusion is just splitting functionality into small libraries as the codebase growing (the engine initially was a part of the math libray). I think Vecmat could be a good example of C project structure.
2
u/scallywag_software Sep 27 '26
Hey! Cool project. I've seen your game before; did you draw inspiration from Jon Blow's Sokoban game? The first time I saw it I immediately thought it looked similar.
I have a game engine project in-flight that's written in a similar spirit to yours; no external libraries, no stdlib, very sparing use of the CRT, custom UI, custom build tools .. hell I even wrote a templating language so I don't have to deal with C++ templates.
As far as memory management goes, I have a few different strategies.
As far as the build goes, it's pretty simple and works reasonably well. I have a small handful of shell scripts.
Around tools, the most useful tool I've ever built, by far, is the templating language `poof`. The basic idea is that you write `poof` code inline in your C++ files, and the output it generates gets included immediately after that code block. In terms of functionality, it lies somewhere between the C preprocessor and C++ templates on steroids.
In terms of overlap with the preprocessor, it more-or-less operates directly on text, with the exception that it parses the datatypes in your program and lets you query your program structure. People seem to get weirded out by this idea, but it's actually great. You can emit basically any language or file format with little effort. You just tell the tool to not parse the output as C++ and it'll happily spit out markdown, html, python bindings .. anything you want.
In terms of overlap with C++ templates .. at it's core its a templating engine, but it's significantly more flexible than the C++ implementation. I use it to generate debug UI for every datatype in my engine, and ser/des code for anything I want to write to disk. I wrote a gentle introduction if you're interested in learning more.
https://scallywag.software/vim/blog/poof-i
Lastly, UI is one of the more complex systems in my engine. It started out simple, like everything, and as the amount of UI code grew, I wanted the API to be simpler and more concise. It's a lengthy explanation for the entire thing, but the general idea is that you have a bunch of functions that write "ui ops" to a command buffer, and at the end of the frame the buffer gets submitted to the renderer which does layout and drawing.
The layout system has a few discrete element types it operates on. You can draw overlays in absolute screen space, or you can draw to windows that float like those on your desktop. Most UI elements are contained in 'tables', which is just a container like an HTML <div>. Actual interactable elements (text, buttons, labels, images, etc) get wrapped in a "column", which is the main container that layout engine operates on. You can pass parameters to the renderer to overload defaults like size, element alignment, text style .. etc.
The really f'n sweet thing about my UI layer is that it plays nice with the metaprogramming tool I built, so I have debug UI for pretty much every datatype in my engine. It actually feels like magic to be able to inspect and edit basically everything at runtime in-engine. That's also a lengthy exposition, but I did do some writing about it if you'd like to know more.
https://scallywag.software/vim/blog/gui-i
I'd be interested in a similar writeup about your engine systems. Mines open-source, if you wanna check it out.
https://github.com/scallyw4g/bonsai
Anyways .. /novel
2
u/nabe9 Sep 29 '26
Awesome project!
Can you elaborate a bit more on why you decided to go with the C++ compiler? I can see why you would want operator overloading. I am working on my own game engine in pure c and at some point also felt the pain of having to write heavy vector math that I implemented a kind of pre processing script to convert math operators into the correct fn calls depending on context but for the most part I didn't find it a big miss. So I am wondering if there are other things you use a lot that would not be C compatible.
2
u/Key_River7180 Sep 29 '26
Somebody needs to invent c with classes TBH. It'd be great.
The game itself feels better than most games I played, as an FPS person
2
u/kyr0x0 Sep 30 '26
Thank you for not posting AI slop
4
u/realestLink Sep 27 '26
Dude, this is fucking sick/awesome fr
1
u/Hoxitron Sep 26 '26
Played the demo, but had no idea how much work went into it. Very impressive!
2
u/podule-games Oct 10 '26
This is really nice, good to see another developer making games in C.
1
u/OkRush4237 Oct 16 '26
I am shocked of your dedication on your project..If it might me bad, dont worry, nobody can beat you in your dedication, knowledge and the ambition you have..
I am also building a project of my own Build System just as you did and like I am just a few step away form finishing it..
But I like how you were able to do such.. Forget the people who tell that this project is bad and like I appreciate your mathematical knowledge to do the 3D Graphics Engine, GPU things and memory management and connecting UI library, in-game editor, Vulkan renderer, asset pipeline and most of the other tools I use to make the game.
Can you tell me like a advice for me that I can you when building projects like I built a Browser (Though it was terrible), a Chess Engine using C++ and a my own Bash and now building this Build system because I was facing a lot of problems from this, so I though why shouldnt I build my own and like
Still you are the greatest programmer and Finally someone that can compete with Terry (The Creator of Temple OS)
-1
u/novicefoto Sep 27 '26
You sound like a guy who would like Zig.
-3
u/RevolutionaryClub596 Sep 27 '26
I think writing C with a C++ compiler is a bad idea... You a struct has a constructor and destructor... Casting does not work as you'd expect C casts in C++ are cursed... In c I have to pass around pointers, in C++ I have references why not use them? If you're doing operator overloading, your object is NOT the same as this
struct { int int } in memory [int,int]
memcpy and memset can be funky on structs like that...
Overall, the point is if you use c++, use c++ don't write C in C++. If you want C, write C...
I do think C++ is a confusing language with lot of weird behavior but that's the key point. It's got it's own way of viewing things and casting is a different beast and you are going through a LOT of casts that you probably don't want to be going down... And in C is just not what's happening.