I’m going to do a 🧵 about making retro-style games in C/C++, and also about getting them to run in a browser.
So, for a good few years now, I’ve been working on various game projects and jam entries, all in a retro style, using C/C++. All the while, I’ve been looking to simplify things - fewer dependencies, simpler code, simpler build process, etc
For me, this simplification process led me to move away from C++ and object oriented programming, in favor of C and procedural programming.
It also led me to exclusively use stb-style single-file header-only libraries, like stb_image (which is not the same as C++ template libs)
Last year, I made a mini-engine called “dos-like”, which helps you make games/apps in a DOS style - both regarding the end result and how you write the code - but running on modern platforms.
I wanted the code to feel as simple as in the old days 🙂
To demo the dos-like engine I made a bunch of example programs, most of them ports of various tutorial projects found around the web, which also means there’s good write-ups on how they work
Shortly after I released dos-like, @B_Schelling added support to build it for WebAssembly, to run in the browser, using his own WAjic - WebAssembly JavaScript Interface Creator.
This is really awesome! It means this very simple code, written in C and originally only made for producing native executables, can now run in a browser!
I had looked at Emscripten earlier, but it all just seemed so cumbersome to me… like, sure, it worked, but it was not a joy to work with. This new way totally changed that.
@B_Schelling even put together a nice distribution o the tools to build wasm
This really was such a game changer for me. A single 60mb zip you can just download and unpack, and you have a full build system for making C programs run in the browser. Nothing to install, no setup, no config, no additional dependencies. How cool is that? 😎
This all made me really happy. Considering how much malware is out there, it’s not really reasonable to expect people to download native apps just to try out small game jam entries and the like. Being able to build the C code for the browser would totally fix that issue
As a more comprehensive test of the “dos-like” engine, I made a port of the original DOOM to run on it. And you can indeed run it in the browser:
And just like I always do these days, I made building it super easy - no build system necessary
An interesting thing about packaging the web version into a single self-contained html file, as I did for DOOM here, is that it’s possible to just save the html file from the browser, and you get a local html that you can copy around as you like, and share how you want.
Pretty much all my projects these days are built on a lib called app.h. It creates a window, an opengl context, reads inputs and renders a single sound buffer, and allows you to show a frame buffer.
I think this makes for an excellent starting point for new projects. There’s a single library in there, app.h, and the sample code in main is just a simple program plotting pixels randomly. From there on, just add whatever you need 🙂 and of course, building it is simple as ever
Note that in the example above, passing NULL instead of `canvas` to app_present, makes it work like glSwapBuffers, if you want to do OpenGL rendering yourself.
Template project is set up with github actions to build on a bunch of compilers at commit - including tcc and wasm
So, using this as a starting point, I thought I’d look at getting some of my other projects running in the browser as well, starting with a mini-engine for making choose-your-own-adventure style retro-looking games, called Yarnspin
So what’s next after this? Well, I have other small games or jam things I’d like to port as well. Like this one for a movie themed game jam, built on my old C++ game engine Pixie
Not that there’s anything wrong with OOP - it’s just not where my preference is.
A while ago I started on a new version of pixie: github.com/mattiasgustavs…
where I am simplifying a lot of things, and making it pure C (again, as that’s just what I prefer these days - but C++ is fine too if that’s your thing)
And, dos-like was built on a subset of this code.
I’ve only ported one of my games to new pixie - a global game jam entry called ”Stranded”. But that one, I’ve also ported to dos-like, so it can already be played in the browser mattiasgustavsson.com/wasm/?stranded
But I still want to get new pixie more finished and wasm-enabled
Getting new pixie in order, would allow me to make web versions of my other games, which are currently built on old pixie.
Like this vertically scrolling shoot-em-up called ”Repair/Rearm” (with sound effects by @Vdelding)
And once I get to that point, I’d be very tempted to pick up #AfterworldRPG again (with music by @itsphilipbak), port it to new pixie and have it running in a browser too :)
A small addendum: even though my personal preference is plain C, I make sure that all my code - libraries, engines, games - can be compiled with a C++ compiler as well. This means I avoid C only features. I think it is worth it to make things more useful for those who use C++
Also, even though this thread has been about retro style games, I use the same approach and libraries for other things as well. Like this MP3 player/organizer I made a while back. It is also written in plain C, single-file header-only libs and using app.h
"dos-like" is a mini-engine/framework I made a couple of years ago. It makes it easy to make games and other things with a 90s MS-DOS look and feel, but using a modern C compiler and running on Windows, Linux, macOS and in the browser using WebAssembler.
It is very easy to start using. If you are on Windows, all you need to do is download the zip file from my itch page, unpack the contents, and you have everything ready to make standalone executables - there's no setup, nothing to install, no dependencies. It is all ready to go🙂
For Mac and Linux, there are a few dependencies to install, but instructions are included. Also, you need to get clang or gcc installed and set up (on Windows, the package includes Tiny C Compiler). You can download it here: mattiasgustavsson.itch.io/dos-like
Have you ever thought about playing around with the original DOOM source code? I'd recommend it, it is really nice.
But if you grab the original source code release, it is not very easy to get it to build.
So I wanted to share this quick fix-up for it, making it easy to build
The repo is here:
and to build it, you just go:
cl doom.c
or
tcc doom.c
no need to install any dependencies or use any build systems.
This version is for windows and wasm, but might not be too difficult to port to mac/linuxgithub.com/mattiasgustavs…
I also made it so you can build it for WebAssembly, so you can run it in a browser.
Try it here:
Full instructions for building for WASM is in the README in the repo (it is just another one-liner, and a build tool you just download and unzip). mattiasgustavsson.com/wasm/doom-crt