Mattias Gustavsson 💖 Profile picture
Hobby game dev, making retro style games: https://t.co/Mb7qXNtOju and public domain C libs: https://t.co/tGsN1QbZiI Feel free to DM me about anything

Jun 13, 2022, 33 tweets

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)

Using C and only single-file header-only libs, I get impressively fast compile times (especially using Tiny C Compile, tcc), pretty much instant.

It also eliminates the need for any build systems. Building is just a matter of invoking the compiler for the single C file

Tiny C Compiler builds very fast, but is non-optimizing. For final version, I build with msvc, clang or gcc. But for iterating, it’s very useful

To make sure I don’t accidentally break compatibility with tcc, I verify the build on each commit, using this

github.com/mattiasgustavs…

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 🙂

github.com/mattiasgustavs…

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.

github.com/schellingb/waj…

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!

All the dos-like sample can be run here:

mattiasgustavsson.com/wasm/

Try it out! It even works on your phone 🙂

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

github.com/mattiasgustavs…

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:

mattiasgustavsson.com/wasm/doom

If I may say so myself, this is a pretty damn good port. Smooth, responsive, and has sound+music too

The source code is here

github.com/mattiasgustavs…

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 made a github template project for it here:

github.com/mattiasgustavs…

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

mattiasgustavsson.itch.io/yarnspin

Just this weekend, I got Yarnsp into a state where it runs in the browser:

mattiasgustavsson.com/wasm/yarnspin

It also works well on a phone, as it is all point-and-click

I also decided to get rid of some lingering C++, and generally clean up this 6 year old code. It’ll be on github soon

I have made one game in Yarnspin, for global game jam a few years ago, so that one can now be played in a browser too:

mattiasgustavsson.com/wasm/no_sunshi…

CW: depression and loss

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

mattiasgustavsson.itch.io/newyork2018

Pixie us a few years old now - the very first version was a heavily OOP C++ engine I started working on in 2006.

The more recent version is still C++, but less of the OOP
github.com/mattiasgustavs…

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)

mattiasgustavsson.itch.io/repair-rearm

Maybe I’ll even finish this embryo of a fighting game

mattiasgustavsson.itch.io/gladiator

I definitely want a web version of Extrication, which I consider to be my best game to date

mattiasgustavsson.itch.io/extrication

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

There’s more information about it in this thread, for those who are interested

It can be downloaded here mattiasgustavsson.itch.io/i-love-music

And the source code is on github github.com/mattiasgustavs…

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling