Welcome to Using C++ to Build a Traditional Roguelike. Over twenty-seven chapters, this book builds one game, from an empty window to a finished roguelike that you can win. The game is Amulets of SDL: nine floors of dungeon, different every time you play, three Amulets waiting in its vaults, and one life to bring them out with. Every chapter ends with a game you can build, run and play, and nothing is thrown away on the way: the glyph cache you write in Chapter 1 is still drawing the victory screen in Chapter 26.
A traditional roguelike is turn-based and grid-based, with a dungeon made fresh for every run, and a death that's permanent. It's a wonderful thing to build, because it's made almost entirely of systems, and systems are where real programming lives. There's no engine here, and no framework doing the thinking for you. It's C++, SDL 3 for the window, the keyboard, the mouse and the sound, and you.
What You'll Build
By the end of the book, the game has everything a traditional roguelike is known for, and a few things that make it a pleasure to play:
- Dungeons carved by binary space partitioning, and caves grown by cellular automata, with doors, secret doors, traps, and vaults drawn by hand
- Field of view by shadowcasting, and a fog of war that remembers what you've seen
- Monsters that hear you, hunt you around corners with Dijkstra maps, remember where they last saw you, and flee when they're hurt
- Items with the genre's identification gamble, equipment, and status effects such as poison and confusion
- Bows, thrown weapons, and scrolls of lightning and fireball, aimed along a line of fire
- Three classes, the Fighter, the Rogue and the Mage, each with gifts of its own
- Travel to any cell by A*, and auto-explore, which stop when danger appears
- Two views of the same game, in characters or in tiles, played with the keyboard or the mouse
- Hit flashes, flying arrows, screen shake, and sound
- Its content in data files, and a save that checks everything it reads
- A morgue file that tells each run's story, and a table of high scores
- A bot that plays the game by itself, and balances it by experiment
The finished game is about 14,000 lines of C++, in 65 files, and every line of it is on these pages.
Who This Book Is For
You should know C++ up to classes: functions, references and const, classes with their constructors and destructors, and the standard containers, such as std::vector and std::map. There's no need to be an expert. Anything past that, from a lambda to std::optional, is explained where it's first used.
Everything else is explained in full. That means the patterns the game is built on, such as RAII, a cache, and a dirty flag; the shape of the program, and why it changes when it does; and everything that makes a roguelike a roguelike. If you've never played one, the book explains each idea as it arrives, and why the genre does it that way.
If you're newer to C++ than this, my book Learning C++ by Building Games teaches everything this one assumes, from the first line. Its last project, Rogue SDL, is a taste of the genre, and this book starts where Rogue SDL stops.
The book builds the game on Windows, with Visual Studio. The code itself is standard C++ and SDL, and Appendix B shows how to build it on a Mac or on Linux.
How the Book Is Organized
The twenty-seven chapters come in five parts:
- Part I, The Engine (Chapters 1 to 7). A window of glyphs, a dungeon carved by binary space partitioning, the player, field of view and fog of war, monsters and the dice, and turn-based fighting. Chapter 7 then gathers it all into a
Gameclass, without changing how the game plays. - Part II, Two Views (Chapters 8 and 9). A second way to draw the same game, in tiles, with a camera that scrolls, and the mouse, to look at anything on the screen.
- Part III, Systems (Chapters 10 to 17). Dijkstra maps, monster minds, items and the identification gamble, equipment, status effects, deeper floors, content in data files, and saving and loading done right.
- Part IV, A Deeper Dungeon (Chapters 18 to 22). Caves, doors, secrets and traps, vaults, ranged combat, and character classes.
- Part V, The Finished Game (Chapters 23 to 27). Commands that take many turns, juice, sound, victory, and a bot that plays the game.
Three appendices follow. Appendix A sets up Visual Studio and SDL from scratch, Appendix B is for Mac and Linux users, and Appendix C is a glossary of every term the book teaches.
How Each Chapter Works
Each chapter opens by showing what it builds, with a picture of the game at its end. Then it plans the work: which files are new and which change, and a diagram of how they fit together.
Then you type the code in. That's the whole method. Every line of the game is on these pages, introduced before you see it and explained after. When a chapter changes code you've already typed, it says exactly where: find these lines, and change them to these. Where a function grows in several places at once, a map shows where each change goes.
Checkpoints along the way tell you to build and run the game, and exactly what you should see when you do. Most of them use a seed, a number that makes the same dungeon every time, so what's on your screen matches what's on the page, cell for cell.
Each chapter ends the same way. Playing the Game says what's new to try, and Understanding the Code sums up how it all works. Experimenting suggests changes to make and undo, and what each one shows. Common Errors and Fixes gives the exact message Visual Studio prints for each likely mistake, and how to fix it, and the FAQ answers the questions the code tends to raise.
After the FAQ, an optional AI exercise suggests a way to take the game further with an AI's help, with a prompt to start from, and what to check in the answer that comes back. And all through the chapters, Design notes show how other roguelikes handled the same problem, and what this game does differently. They range from Rogue and NetHack to Angband, Dungeon Crawl Stone Soup and Brogue.
Getting the Code
Every chapter's finished project is on GitHub, at https://github.com/EliteIntegrity/Using-C-to-Build-a-Traditional-Roguelike-2nd-Edition. Choose Code > Download ZIP there, and you'll have a folder for each chapter, from Chapter 01 to Chapter 27. Each holds a complete Visual Studio project, as it stands at the end of that chapter, with its data files, its assets, and the SDL files it needs.
The code on these pages matches the repository's character for character, at every checkpoint. So when something won't build, you can compare your files with mine, and when a change's place isn't clear, the chapter's folder is the final word. You can start the book at any chapter, too, from the folder of the chapter before it.
The game's tiles and sounds are mine, from two games of my own, Ormeq and Ammerow: Lands Beyond. They come with the code, and you're welcome to use them in games of your own, free or commercial, with credit. The repository's ASSETS-LICENSE.md explains how.
If you have the first edition, its code is still where it always was, at https://github.com/EliteIntegrity/Roguelike-tutorial-series.
What You'll Need
You'll need Visual Studio 2026, whose Community edition is free, with its Desktop development with C++ workload. The game also needs SDL 3, the library that opens the window, reads the keyboard and the mouse, and plays the sound. It needs SDL3_ttf too, SDL's companion for drawing text. The book was built with SDL 3.4.4 and SDL3_ttf 3.2.2, and any later 3.x releases will do.
If you've worked through Learning C++ by Building Games, you have all three already, in the folders this book uses. Otherwise, Appendix A sets up Visual Studio and SDL from scratch, and Chapter 1 adds SDL3_ttf when the code first needs it.
New in This Edition
If you've read the first edition, you'll find a much bigger book: twenty-seven chapters where there were seventeen, with a game that's been rebuilt from its first window. It draws in tiles as well as characters, and answers the mouse. Its monsters hear, remember and search, and its content lives in data files, which a save checks as carefully as it checks itself. It has doors, secrets, traps and vaults, magic that ignores armor, travel and auto-explore, effects and sound, a morgue file and high scores, and a bot that plays it.
The way the book teaches has changed too. Every change is shown where it goes. Each chapter has checkpoints, exact error messages and an AI exercise, and every chapter's folder in the repository is a complete project, ready to open and run.
Before We Begin
Roguelikes are a genre built by hobbyists, shared freely, and argued about lovingly for more than forty years. By the end of this book, you won't just have played one: you'll have built one, and you'll know it down to the last tile.
Let's dig.
