2026-03-23 游戏开发 Game Programming Patterns 设计模式 1.3k 4 分钟

Game Programming Patterns 阅读笔记 1

Introduction - Game Programming Patterns

本书主要介绍作者从自身的游戏开发经验中发现的最适用于游戏的一些设计模式。

I. Introduction - 序

What's in Store - 与市面上已有书籍的对比

  • 本书不是 Domain-Specific Book

  • 本书不是 Whole-Engine Book

  • 本书中每一章节都传达独立的、可用于编码中的思想方法。这意味着你可以快速找到你感兴趣的章节优先阅读,以此快速解决你当前在编码中遇到的难题。

How it Relates to Design Patterns - 本书和设计模式有何关系

本书与 GoF 的经典著作 "Design Patterns: Elements of Reusable Object-Oriented Software" 有关。

本书介绍的设计模式致力于:

  • 让游戏中的事情以正确的顺序在合适的时间发生
  • 减少耦合,帮助你快速迭代
  • 游戏中,各个系统的良好相互作用
  • 优化游戏性能(用更少的CPU周期干更多的事情)

How to Read the Book - 如何阅读本书

本书分为三大部分

  • Introduction
  • Design Patterns Revisited
  • ⭐Core Part: Patterns for Game Development
    • Sequencing Patterns - 序列模式
      • Intent: for quickly hunting
      • Motivation: an example problem
      • Pattern: distilling the essence
      • When to use & Keep in Mind
      • Sample Code
      • Design Decisions: how to implement it
      • See Also
    • Behavioural Patterns - 行为模式
      • (As above)
    • Decoupling Patterns - 解耦模式
      • (As above)
    • Optimization Patterns - 优化模式
      • (As above)

About the Sample Code - 关于示例代码

  • C++
    • not written in "modern"

Where to Go From Here - 接下来怎么做

1. Architecture, Performance, and Game - 架构,性能和游戏

本节介绍游戏开发中的软件架构

What is Software Architecture? - 什么是软件架构

  • 软件架构不是算法(3D算法、搜索树、空间音频),软件架构是如何组织这些算法、这些系统。
  • 我们期望学习如何良好地组织代码

What is good software architecture? - 什么是好的架构

  • 打破幻想,我们不能期望一个每次迭代的时候都丝滑无比、代码库毫无波澜的架构存在

Architecture is about change.

  • 软件架构越好,在修改代码时候遇到的阻力就越小

How do you make a change? - 如何修改代码

How do decoupling (解耦) help? - 为什么解耦很重要

  • 耦合(Coupling)的一种理解:如果两段代码是耦合的,那么意味着你不能做到只理解其中一个而不理解另一个
  • 良好的软件架构的 关键目的尽量减少你每次更改前需要学习的相关代码,减小这个上下文的长度
  • 解耦(Decoupling)的一种理解:对一段代码的更改并不意味着必须对另一段代码进行更改。

At What Cost? - 那么代价呢?

  • 你必须遵守规范,每次变更时都不能懈怠,将新内容完美的融入旧代码中,让代码库在无数细小的变化中保持条理清晰

But this is where it starts to get tricky. Whenever you add a layer of abstraction or a place where extensibility is supported, you’re speculating that you will need that flexibility later. You’re adding code and complexity to your game that takes time to develop, debug, and maintain.

  • 但是...每一层抽象,每一个可拓展点,都会给未来的自己增加维护上的成本
  • 个人理解:⭐心智负担 = 抽象层 + 实际功能层
    • 平衡两者是很重要的
    • 最终目的是降低维护时的心智负担

Performance and Speed - 性能与速度

  • 性能和灵活性之间是负相关的
    • 反例:C++模板,没有运行时(Runtime)开销,启发:编译时语法糖是好的选择
  • 灵活性和迭代速度是正相关的

My experience, though, is that it’s easier to make a fun game fast than it is to make a fast game fun. One compromise is to keep the code flexible until the design settles down and then tear out some of the abstraction later to improve your performance.

  • 快速迭代,直到核心玩法稳定后,再考虑优化

The Good in Bad Code - 坏代码也有优点

  • 在原型开发中,用”坏代码“快速迭代是很好的做法

  • 但是...要确保你最终能够丢弃它,不在正式项目代码中使用

  • 否则就需要完全 重写

Striking a Balance - 保持平衡

  • 不可能三角:优雅的架构、极高的运行时性能、极短的编码时间和低心智负担

Simplicity - 简洁

  • 保持你的代码简单直接有助于达到以上提到的平衡

However, note that I’m not saying simple code takes less time to write. You’d think it would since you end up with less total code, but a good solution isn’t an accretion of code, it’s a distillation of it.

  • 好的代码能够一骑当千,解决一群问题,而不仅仅是一个问题

Go On With It, Already - 马上结束

  • 不要做 无用的抽象和解耦,除非你 真的需要
  • 性能问题 要抓大放小,小问题要留到最后解决
  • 快速迭代,但 不要过快
  • 如果你在将来打算放弃代码,那么就别浪费时间让他们变得优雅
  • 享受创作过程才能做出有趣的游戏!

References - 引用