283. Console 背景帧缓冲

从 Console 暴露当前的背景帧缓冲数据。

283 / 356 · tests/chapter_05_rendering_pipeline/test_283_console_background_framebuffer.py

待更新文件

emulator/console.py

为什么需要这一步

渲染器现在可以从当前的 PPU 内存中提取背景帧缓冲:

ppu_background_to_framebuffer(ppu)

Console 是拥有 CPU/PPU 关系的顶层机器协调者。前端代码最终应该能够向 Console 请求当前的背景帧缓冲,而不需要了解 PPU 内存提取的细节。

这一步添加一个小方法

console.render_background_framebuffer() -> Framebuffer

重要的职责分离

Console.step()
    advances emulation time

Console.render_background_framebuffer()
    observes current PPU memory and returns pure framebuffer data

不要在 Console.step() 内部进行渲染。一帧真实画面包含许多条 CPU 指令;在每条 CPU 指令之后都渲染一次会很浪费,也会把时序和观察混在一起。

建议的实现示例

from emulator.rendering.framebuffer import Framebuffer
from emulator.rendering.ppu_background_renderer import ppu_background_to_framebuffer


class Console:
    ...

    def render_background_framebuffer(self) -> Framebuffer:
        return ppu_background_to_framebuffer(self.ppu)

架构规则

这仍然返回纯帧缓冲数据。不要在此处导入 pygame。

范围之外

  • pygame 显示
  • step_until_frame/run_frame
  • 精灵
  • OAMDMA
  • 手柄输入

运行本课

uv run pytest tests/chapter_05_rendering_pipeline/test_283_console_background_framebuffer.py -v