Yo Odin ยท Preface

Yo Odin

Building a WebAssembly arcade game from scratch in Odin: the renderer, the server, and the game, with nothing but Odin, HTML, JavaScript, and CSS.


Preface

This is a book about building a real game. That's it.

By the last page, you will have written Destroids, a complete, playable Asteroids-style arcade game, starting from an empty directory. It's a game with a vector ship that thrusts and drifts with inertia, asteroids that split into smaller lumps and bounce off each other, bullets, particle explosions, spinning debris, screen shake, a twinkling starfield, a heads-up display, a pause screen, a game-over screen, synthesised sound, a risky hyperspace jump, and a high score that survives a browser reload. Sometimes. Hey, I'm not a front-end dev!

You will build it in Odin, a lean, modern systems programming language, and compile it to WebAssembly so it runs in any desktop browser. And you will build almost everything it stands on, too:

  • Your own batched-Canvas2D rendering library: a miniature of the kind of engine that lets a game draw a thousand shapes a frame without drowning in overhead.
  • Your own web server, written in Odin, to serve the game over HTTP.

No game engine. No graphics library. No image files. No audio files. No npm, no bundler, and no build framework. Every pixel is drawn with a canvas primitive; every sound is generated live from an oscillator; every tool is either the Odin compiler or a browser you already have.

Who this is for

Technically, me. I wrote this for me as a way of putting my learnings over the past couple of months on paper so that I have something to reference. But you're welcome to it if you like.

You should be comfortable programming in some language: C, Go, Rust, TypeScript, Python, it doesn't matter which. You should be at home in a terminal and a text editor. That's the bar.

You do not need to know Odin (I'll teach the slice of it the game needs), or WebAssembly, or the Canvas API, or Web Audio, or how a web server works. Each of those is introduced from first principles at the moment you first need it.

If you've never programmed at all, this isn't the right first book, but it might be a good second one.

What you'll get out of it

Three things, I'd say.

The obvious one is a finished game you built yourself and hopefully understand top to bottom.

The subtler and more valuable one is a transferable blueprint. The patterns in this book: crossing the WebAssembly boundary cleanly, batching draw calls, driving a fixed-timestep game loop, laying out entities without a garbage collector breathing down your neck, synthesising sound, and persisting state, are exactly what you'd reach for to build any small canvas game in Odin. When you finish Destroids and want to build your own thing, you'll already own the machinery.

And lastly, you'd have made me super famous and fabulously rich. I mean, you did pay an exorbitant amount of money for this, right? Mate, if you didn't pay, you got scammed! Or is it the other way around?

The ground rules

A few deliberate constraints shape the whole book. It's worth knowing them up front, because they explain choices that might otherwise look eccentric.

  • Odin and the browser, nothing else. We use the Odin compiler and its standard library, plus the HTML, JavaScript, and CSS every browser already understands. No third-party libraries: not for the game, not for the renderer, and not for the server. When we need a capability, we build it or the browser provides it.
  • No minifier, no bundler. The production version of this game runs its JavaScript and HTML without a minifier here. Our output stays plain and readable, so you can open any file and see exactly what runs. It costs a few kilobytes; it buys clarity, and clarity is the whole point of a book.
  • British English. Spelling and grammar throughout are British: colour, centre, initialise, grey, licence. Where our code touches a browser API that insists on the American spelling (fillStyle, CSS color), the code keeps it; that's the API's name, not our prose.
  • Zero binary assets. No .png, no .wav, no fonts to download. It's a discipline that keeps the project tiny and self-contained, and it's a surprisingly fun way to work: you'll be amazed what a few line segments and a sine wave can do.
  • Everything here is real. Every line of code in this book was compiled and run by me. If you type it in as you read, it works. The finished project matches the listings in Appendix B.

How to read it

Build along. Reading this book without typing the code is like reading a book about swimming while sitting on the sofa. Open a terminal, make the project directory, and add to it chapter by chapter. Every chapter ends at a point where something compiles and runs, and tells you exactly what you should see.

The chapters are cumulative: each assumes the project as the previous one left it, so read them in order the first time through. Afterwards the book works fine as a reference: the table of contents and Appendix E (links) will get you back to any specific technique.

Throughout, you'll meet three kinds of aside:

Why. The reasoning behind a design decision: why the code is shaped this way and not some other, equally plausible way.

Gotcha. A trap that has genuinely bitten people (sometimes me). Read these; they save hours.

Odin note. A language detail worth knowing, usually with a pointer to the official docs so you can dig deeper.

A word on Odin, and a recommendation

This book teaches only as much Odin as Destroids requires. That's a real language tour: packages, structs, slices, array programming, and the calling-convention machinery you need to talk to JavaScript, but it is not the whole language. In fact, I don't know a fraction of the language so I really can't teach you much.

If you catch the Odin bug (many do), the book to read next is Karl Zylinski's _Understanding the Odin Programming Language_, available at odinbook.com. It's the thorough, standalone treatment of the language that Yo Odin deliberately isn't, and I recommend it without reservation to anyone who wants to go deeper. Buy it; support people who write good books about good languages. I have referenced the book repeatedly over the last year, so I know it's worth buying.

Acknowledgements

Odin is the work of Ginger Bill and a generous community; this book would not exist without the language or the people around it. Thank you for building along: the more people who are interested in Odin, the better for the community.

Right. Let's go.