Mattias Gustavsson 💖 Profile picture
Jun 13, 2022 33 tweets 21 min read Read on X
I’m going to do a 🧵 about making retro-style games in C/C++, and also about getting them to run in a browser. ImageImageImageImage
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 ImageImageImageImage
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 Image
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… ImageImage
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 ImageImageImageImage
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 🙂 ImageImageImage
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 Image
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 Image
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 ImageImage
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 Image
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 ImageImageImageImage
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 Image
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 ImageImageImageImage
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 Image
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 ImageImageImageImage
Maybe I’ll even finish this embryo of a fighting game

mattiasgustavsson.itch.io/gladiator Image
I definitely want a web version of Extrication, which I consider to be my best game to date

mattiasgustavsson.itch.io/extrication ImageImageImageImage
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 :) ImageImageImageImage
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

ImageImageImageImage
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…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mattias Gustavsson 💖

Mattias Gustavsson 💖 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Mattias_G

Apr 26
"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. Image
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
Read 20 tweets
Mar 7, 2024
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 Image
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
Image
Read 11 tweets
Nov 1, 2023
A quick and easy way to start making something fun in C.

1. Clone this repo:

2. Download tcc and unzip it

3. Execute `tcc\tcc source\main.c`

You now have window that you can plot pixels in - all you need to get started :) github.com/mattiasgustavs…
github.com/mattiasgustavs…
Image
Want to run it in a browser?

4. Download and unzip the wasm build tools:

5. Execute `wasm\node wasm\wajicup.js source/main.c main.html`

You know have the same code built into a stand-alone html file, that runs in the browser github.com/mattiasgustavs…
Image
That's it, no additional installation or setup necessary. Just clone the repo, download/unzip build tools, and you are good to go.

And the code to initialize the window and start drawing? It looks like this. Image
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(