This is the source repo for the book Game Programming Patterns

Overview

Note: Now that the book is done, I'm not actively working on it.

There are only so many hours in the day, and I have other projects that need my love, including a new book. This means I'm not responding to issues and pull requests here. However, I do intend to circle back and do a second edition at some point, so please do continue to file bugs.

Even though I'm not responding, I do deeply appreciate every issue filed. Thank you!

– bob


This is the source repo for the book Game Programming Patterns.

Building the Book

The book is written in Markdown (in book/). A little Python script (script/format.py) converts that along with a SASS file (asset/style.scss) and HTML template (asset/template.html) to the final HTML (in html/). To run the format script locally, you'll need to have Python 2.7-ish, and install Python Markdown, Pygments, and SmartyPants:

$ pip install markdown
$ pip install pygments
$ pip install smartypants

You may need sudo for those. Once that's done, you can run:

$ python script/format.py

Make sure to run this from the root directory of the repo. That will regenerate all of the chapter and section intro HTML files. If you're editing stuff, the script can also be run in watch mode:

$ python script/format.py --watch

That will monitor the file system for changes to the markdown files, SASS file, or HTML template, and reprocess them as needed.

Comments
  • The electrons have a shorter distance to travel.

    The electrons have a shorter distance to travel.

    This phrase in the "A pallet for your CPU" section is not technically right. The main reason for the access speed improvement is the different memory type used for caches. Even every cache may have a different architecture that lower the cost but increase the access time. I apologize about my english.

    opened by ghost 9
  • License unclear.

    License unclear.

    Hello. It's clear that the book's source-code is made available gratis here on GitHub, but what is its license, exactly? What am I allowed to do with this source-code, besides read it? If I make modifications to it, am I allowed to publish on my own website? Am I allowed to publish a PDF of your book I prepared myself, or of modified copies?

    Please specify a license on the project root. Free documentation licenses are contracts upon which communities can build, deprecating trust. Have you considered the GFDL or something else?

    I could help you choose a license, if you want.

    opened by guiprav 8
  • Difference between Command and Observer.

    Difference between Command and Observer.

    Hello, I apologize if it's a stupid question. I learned to code on my own, for years I have coded on my own and I never read about common solutions or patterns. Because of that, my brain works in strange ways to solve problems and usually I can't understand what to other people it's common knowledge or darn obvious.

    For what I read, Command and Observer patterns serve the same propose: something happens -> inform/react Both act as a way of decoupling code that isn't directly related.

    I don't understand when to use one or when to use the other. The best that I came up with, is that one is about what gets communicated and the other about how to communicate it.

    To me the Command pattern looks like an Observer pattern where the subject has only one observer and a complex data.

    Some light over the topic will be appreciated.

    opened by ghost 6
  • no easy way to give you money

    no easy way to give you money

    I don't pay for movies because I can't get them for free online, I pay for them because it's the right thing to do. Similarly, I very much appreciate you posting the complete text of your book online but feel like you should still have a "buy now" button or "become a registered supporter" button. And don't leave an empty box with a "donate what you feel like" option. Have some balls and pick a number of how much you think people should be paying you for being able to read your book. (I'm thinking $10 or $20) Still worth keeping it online but for those of us who can afford it we need to be giving you something.

    Oh, and your book is awesome, but you (should) already know that.

    opened by darinmcgill 5
  • Type Object / Code style inconsistency

    Type Object / Code style inconsistency

    In this chapter, methods that override virtual method don't use the virtual keyword. However, if I recall correctly, the code snippets from other chapters do.

    Edit: Referring specifically to the Dragon and Troll classes.

    opened by colms 5
  • Has this book been finished?

    Has this book been finished?

    Hi, Bob, everyone :] This book is very useful to me, and I wana say: Thank you! I have a plan now: I am going to translate this book into Chinese. By the way, has this book been finished? From the commit history, I see there are almost small fixes such as typo, spelling etc recently. Looking forward to your reply :+1:

    opened by dabing1022 4
  • Error in Buffered Slaps Section of Double Buffer Chapter

    Error in Buffered Slaps Section of Double Buffer Chapter

    In this section and the previous we have three examples:

    For all of these examples we have a series of Comedians who are stored in an array and then updated in sequence. Additionally, each Comedian's behavior is dependent on what other Comedians have done within a single update.

    With the first example we have no incorrect behavior because the scene is set up so that the Comedians are dependent on other Comedians whose update methods have already executed. You provide a demonstration of the propagation of the slaps as follows:

    Harry -> Baldy -> Chump -> Harry

    In the second example you reverse the dependencies of the Comedians. Comedians are now dependent on the Comedian's whose update methods execute after their own. As you state in the section, this causes an error. Your demonstration in this example is as follows:

    Harry -- Baldy <- Chump -- Harry

    Clearly the slap did not fully propagate and only Baldy was able to receive a slap.

    In the next section, Buggered Slaps, you provide a third example which I believe does not work. All of the Comedians get two slapped_ which are swapped after states and, as you state in the section, "The end result of this is that an actor will only see a slap in the frame after it was actually slapped." I agree with your statement but the behavior is still incorrect.

    Your statment leads the reader to believe that we would now see this:

    Harry <- Baldy <- Chump <- Harry

    But I believe that the execution of the code would actually cause this:

    Harry -- Baldy <- Chump -- Harry

    Within a single frame, the slap still won't propagate to Harry. This is because Baldy still has no knowledge that he was slapped until his Swap() is executed.

    I can elaborate a little bit further if you wish.

    opened by tyleo 4
  • Type Object and quotation mark edits

    Type Object and quotation mark edits

    This pull request includes:

    • Proofreading edits to the Type Object chapter
    • A few minor fixes to the Introduction and Double Buffer chapters to move punctuation that appeared inside quotation marks outside of the quotation marks (unless it was part of the quoted matter) in other chapters
    • An update to the style guide to define the quote/punctuation convention
    opened by Lauren-Bee 4
  • Singleton alternative: dependency injection

    Singleton alternative: dependency injection

    I see this in the discussion of singletons so often that I was surprised not to find the mention of it in the chapter. Unlike other proposed alternatives, it even makes writing tests and mocking up environments for them almost trivial.

    opened by golergka 4
  • phrase could be better

    phrase could be better

    I think this book is great, I'm enjoying reading it, congratulations both on writing it and all all the work you've done to get to the point where you could write it, it's good.

    I've got no reason to think you're sexist, in fact quite the opposite, you come across in your writing as a thoughtful humble person - but the phrase 'giddy as a schoolgirl' is sexist. I guess it is one of those patterns, familiar, right there ready to be used like - singleton - but if I had somehow managed to write this great book, and I think I'd edit this phrase out.

    opened by absurdist 4
  • Possible new chapter: race conditions

    Possible new chapter: race conditions

    I'm not certain if you would create a "mutex / lock / monitor" chapter (as that is more in vein with your current naming schema - one chapter per pattern) or if you would consider a chapter of common issues encountered by programmers, however I feel like race conditions are a common enough issue in normal code (and even more-so in video gaming) that it would be helpful to cover common solutions.

    Examples:

    • User enters a new location during auto-save
    • User and enemy attempt to access the audio driver simultaneously
    • User levels up while a spell is in flight

    There's a number of different conditions where ensuring one bit of code executes before another can make a huge difference - and more importantly there are plenty of examples where one bit of code executing before another can completely crash or deadlock your program.

    Consider the dining philosopher's problem

    opened by stevendesu 4
  • GoF links broken

    GoF links broken

    I just noticed some of the links to GoF patterns are now broken (the curse of the internet), e.g., the Interpreter pattern link here.

    Not a big deal since one can now easily Google them, but probably quite easy to fix too.

    opened by zxul767 0
  • [WIP] Dark theme

    [WIP] Dark theme

    Hi. This PR:

    • adds a small button on top of every page. This small button is responsible for toggling between dark and light themes.
    • persists the user's current choice in the local storage, so the latest selection is persistent across page refreshes and navigations.
    • implements a (n almost) well-looking dark theme.
    • fixes two minor bugs in script.js.
    • reads changes from the user's preferences to enable dark or light themes based on the user's system preferences.

    TODO:

    • Improve the structure of the asset/dark-styles.scss file to use meaningful variables and better order of styles.
    • Maybe better icons for light and dark themes.

    ONLINE DEMO

    https://aghardeshir.github.io/game-programming-patterns/html/command.html

    VIDEO DEMO

    dark-theme-for-game-programming-patterns.webm

    VIDEO DEMO FOR READING THEME FROM SYSTEM SETTINGS

    dark-theme-from-system.webm

    opened by aghArdeshir 1
  • sleep() missing in loop example

    sleep() missing in loop example "play catch up"?

    I try to wrap my head around the final code example on the page on game loops:

    double previous = getCurrentTime();
    double lag = 0.0;
    while (true)
    {
      double current = getCurrentTime();
      double elapsed = current - previous;
      previous = current;
      lag += elapsed;
    
      processInput();
    
      while (lag >= MS_PER_UPDATE)
      {
        update();
        lag -= MS_PER_UPDATE;
      }
    
      render();
    }
    

    In the text it says:

    We then have an inner loop to update the game, one fixed step at a time, until it’s caught up.

    I fail to see any fixed time step here. How often update() is called is only limited by how fast the underlying machine is. So a faster machine will give more updates than a slower machine. This throws us back to non-deterministic behavior as described a chapter before.

    It also doesn't match with the diagram, where there's a clock above the update() box: game-loop-fixed

    According to this image I would expect a call to sleep() in the inner loop. Could it be missing?

    opened by mikehaertl 4
  • Saving data with component pattern instead of inheritance.

    Saving data with component pattern instead of inheritance.

    This may be a newbie question but it is unclear to me how to save data when using a component pattern. For example say I have a Room class with max 5 random monsters that all inherit from the Monster class. If I don't use inheritance how can I save the data of these monsters without knowing the info at runtime? My room might spawn 5 Tree monsters, or 2 snakes and 3 blobs or something else.. What kind of pointer do I use (I can't use Monster * monster[ 5 ] in the Room class anymore... I also asked this on stack overflow https://stackoverflow.com/questions/71280409/how-to-save-c-class-data-from-a-template-when-using-component-pattern

    opened by conqueran 0
Owner
Bob Nystrom
Programming language developer, ex-game developer, UI nerd, author of "Game Programming Patterns" and "Crafting Interpreters".
Bob Nystrom
Improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World.

OpenXRay OpenXRay is an improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World. S

null 2.2k Jan 1, 2023
Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve external game process read/write with minimum footprint.

Launcher Abuser Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve ex

Ricardo Nacif 80 Nov 25, 2022
Game Boy, Game Boy Color, and Game Boy Advanced Emulator

SkyEmu SkyEmu is low level cycle accurate GameBoy, GameBoy Color and Game Boy Advance emulator that I have been developing in my spare time. Its prima

Sky 321 Jan 4, 2023
Minetest is an open source voxel game engine with easy modding and game creation

Minetest is an open source voxel game engine with easy modding and game creation

Minetest 8.3k Dec 29, 2022
Project 1945 is a top down scroller game made with SDL2, CMake and programmed in C as a project of study for the Italian Videogames Academy for the 3rd year course of Videogames programming.

Project 1945 is a top down scroller game made with SDL2, CMake and programmed in C as a project of study for the Italian Videogames Academy for the 3rd year course of Videogames programming. The game is based on the old videogame 1945: The Final Front of the 2002.

null 19 Dec 30, 2022
A programming game, in which your goal is to help a group of dwarves establish a small outpost in the middle of a dangerous forest.

"Since they were to come in the days of the power of Melkor, Aulë made the dwarves strong to endure. Therefore they are stone-hard, stubborn, fast in

Alexey Nikolaev 6 Sep 29, 2022
dos-like is a programming library/framework, kind of like a tiny game engin

dos-like is a programming library/framework, kind of like a tiny game engine, for writing games and programs with a similar feel to MS-DOS productions from the early 90s. But rather than writing code that would run on a real DOS machine, dos-like is about making programs which runs on modern platforms like Windows, Mac and Linux, but which attempts to recreate the look, feel, and sound of old DOS programs.

Mattias Gustavsson 818 Jan 7, 2023
Recreation of the MineStorm game for a student programming project.

MineStorm General information This project is a student project done for ISART Digital by Rémi SERRA and Alexandre PERCHÉ. The goal was to recreate th

Rémi Serra 1 Dec 7, 2022
A cycle-accurate Game Boy and Game Boy Color Emulator, with rewind feature.

Azayaka is a free and open-source Game Boy and Game Boy Color emulator written in C++. Features Cycle-Accurate emulation. Console based Debugg

Zach Collins 23 Dec 3, 2022
The Game Boy ROM of the Game Boy bitcoin miner!

game-boy-bitcoin-miner The Game Boy ROM of the Game Boy bitcoin miner! To build this, currently this patch needs to be applied to GBDK: https://gist.g

Ghidra Ninja 80 Dec 11, 2022
CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine

CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine. This engine has been designed with a cross-platform design in mind. Thanks to Vulkan API it delivers a next-gen experience with ray tracing to both Linux and Windows platforms

Jakub Biliński 48 Dec 29, 2022
Ground Engine is an easy to use Game Engine for 3D Game Development written in C++

Ground Engine is an easy to use Game Engine Framework for 3D Game Development written in C++. It's currently under development and its creation will b

 PardCode 61 Dec 14, 2022
A game made for the Game (Engineless) Jam using Raylib

Fastest Pizza Delivery A fun little 3D game made for the Game (Engineless) Jam. It is still is development but the basic gameplay is something l

Ryuzaki 2 Apr 3, 2022
SameBoy DX is a Qt-based interface of SameBoy, a free, highly accurate Game Boy and Game Boy Color emulator.

SameBoy DX SameBoy DX is a Qt-based interface of SameBoy, a free, highly accurate Game Boy and Game Boy Color emulator. Build requirements: CMake Pyth

Snowy 7 Oct 2, 2022
To recreate the board game Scotland yard and enable a single player to play the game by letting one of the roles being played by the computer based on written algorithm

Scotland Yard GAME OF SCOTLAND YARD This is a custom version of the classic board game, Scotland Yard .The game uses the London map used in the origin

Brshank 2 Nov 11, 2021
Ncurses based omok game, execute omok game in your terminal

omok_game execute omok game in your terminal Omok game played by two people. 한국어 버전(korean version)

SunjungAn 1 Dec 3, 2022
Slender Man Returns is a game inspired by the original indie game Slender

Slender Man Returns is a game inspired by the original indie game Slender: The Eight Pages, but redesigned to run on the popular Unity game engine. Ported to PSVita. Original project by Rosario Terranova.

null 8 Jul 5, 2022
A Game Boy game that rewards you for playing it on several console models!

GB Corp. A Game Boy game for the Game Boy Competition 2021 by Dr. Ludos (2021) This is the source code, you can get a precompiled rom from here: https

Dr. Ludos 10 Sep 25, 2022
GB Studio is a quick and easy to use retro adventure game creator for Game Boy available for Mac, Linux and Windows

GB Studio is a quick and easy to use retro adventure game creator for Game Boy available for Mac, Linux and Windows

Chris Maltby 6.8k Jan 8, 2023