gpui-cc Course handbook
CHAPTER 02 / STAGE 07COURSE

Paint a recorded scene in order

Separate paint commands from pixel writes.

scene-order
On this page

Learn: paint intent is data, not a write

Two opaque draws at one destination cannot be reordered freely. Store what to paint — rectangle, color, effective clip — without writing pixels yet. A plain vector in declaration order is enough. Clip is inherited down a branch and must be restored when you return to its sibling.

Run it: see painter’s order in one cell

Terminal
./build/examples/core-lab write-order
C++ · core-lab.cpp
void write_order() {
    for (const auto writes : {std::array{'A', 'B'}, std::array{'B', 'A'}}) {
        char cell = '.';
        for (char paint : writes) cell = paint;
        std::cout << cell << '\n';
    }
}

Expect B then A. Try it: append a third write. The final cell is determined by order, not color. The example has no rectangle, clipping, or scene traversal.

Build it: record then replay

Your challenge: Build a scene containing ordered opaque rectangle commands, and replay it using Stage 6’s fill. Snapshot the effective clip at recording time. You can carry a clip by value during traversal or use push/pop with a guaranteed restoration; do not keep a pointer to a mutable clip that changes after recording.

Check it

Use a 4 × 2 fixture. Record A over [0,3) × [0,2), then B over [1,4) × [0,2) clipped to [1,3) × [0,1). Expect rows ABB. and AAA., where . is untouched. Reverse the two commands: both rows are AAA.. Add a separate sibling paint at (3,1) and verify it is not clipped by B’s branch.

Terminal
python3 course/check_core.py scene-order

Hints and troubleshooting

Hint: the sibling disappears, but the overlap is correct

Trace the inherited clip entering and leaving one branch. Check that a command carries its own clip and that replay does not mutate the recorded sequence.