sdl3-gl-cpp-starter
Background
I’ve been trying to learn graphics programming for a while, and I find myself remaking the same starting point every time I come back to it. It’s quite a time consumer doing this rereading and reimplementing, so I decided to make a template repository I can fork in seconds.
I’m writing to explain my technology choices both to readers of my blog (all 2 of you) and my future self who’s forgotten what I was thinking.
Here’s the repo if you wanna poke around yourself: https://codeberg.org/ajnt25/sdl3-gl-cpp-starter
Technologies
OpenGL
For ergonomics and attempt to learn the “modern” way, I’ve usually gone for wgpu, an implementation of WebGPU. WebGPU is cool, I can use it with Rust and I’m sure its good to learn in the longrun, but there are so many awesome resources for beginners with OpenGL. WebGPU does have a plethora of tutorials for JavaScript/TypeScript, but I honestly would rather eat a leather boot than deal with those languages/ecosystems for more than light website scripts.
I’m currently reading through learnopengl, a pretty thorough resource for OpenGL using C++. OpenGL familiarity should provide a jumping point for more modern API’s (Vulkan, WebGPU, etc.) and their tutorials.
C++
I’m a mechanical engineering student who loves robotics, and C++ is a prominent language in that field. I’ve made small projects here and there with an esp32 and C or Arduino code, but I feel like I should familiarize myself with the real nitty gritty of C++, best practices and footguns included. It’s really easy to just assume my Java experience is enough because the syntax feels so familiar, but the semantics explode if I assume that 😅.
CMake
One annoying factor with C++ over Rust or Python is the lack of a defacto build
system like cargo or uv. The closest thing is CMake, which as I understand is
the forbidden fruit described in the bible. It’s quite a freeform and powerful
language, but I’m accustomed to the opinionated bliss of Cargo.toml,
build.rs, and pyproject.toml. Despite this, it’s almost a defacto tool for
existing C++ codebases, and if I’m trying to work in robotics, I may as well
learn it.
I have to admit that CMake has some handy features. FetchContent allows the build process to pull a dependency’s source code from its git repo directly, avoiding the pain of installing systemwide dependencies on various Unix/Linux distros or Microslop Windows. As I understand, each dependency has to have its own CMake build code in place, but that isn’t a problem for any technologies here.
SDL3 (with callbacks)
SDL is a monumentally popular library for cross-platform application and game development. It abstracts away features like window opening, OpenGL/Vulkan context creation, event handling, input methods, etc. If you do any C/C++ development for games, I’m sure you’ve heard of it.
SDL3 is the most recent version, and it introduced a new paradigm for structuring programs. Typically, an SDL application is in control of its main loop, and the operating system sort of has to comply. However, modern operating systems don’t like this, and for good reason. Mobile devices like laptops and smartphones need to employ all sorts of techniques in order to conserve battery power. Part of this is controlling when an application gets to run in the background, and apparently a single main loop is at odds with that. The SDL team’s solution was a callback based API which let’s the operating system choose when the app can initialize, when it can run an iteration (like a single execution of the typical main loop), when it needs to handle an event (such as a keypress, touch input, or termination), and when the program needs to cleanup its resources for a graceful exit. That’s my understanding anyway, you should read the official explanation for a more accurate summary.
I decided the new paradigm would help keep my codebases clean. Additionally, my career goal is to work on CAD software, and being used to mobile friendly application development would help with future porting ventures.
glad
OpenGL is pretty much everywhere, but hardware specific drivers still need to be loaded/referenced. glad helps do all that nasty stuff for me, and it was pretty easy to add to the build system.
Concluding Thoughts
While this template is primarily for my purposes, I am more than open to anyone using it for themselves. The license is currently MIT, as its so simple I don’t think any big evil company would be profiting off of it. Please let me know if you have any thoughts or issues with it! I’d love to talk to others interested in these technologies. Check my about page for contact info or check out the git repo.